No Description

main.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os, os.path
  4. import glob
  5. import pprint
  6. try:
  7. from myterm.parser import OptionParser
  8. except:
  9. from optparse import OptionParser
  10. from piwigotools import Piwigo, __version__
  11. from piwigo.ws import Ws
  12. from piwigotools.interface import *
  13. DESCRIPTION = "tools for piwigo gallery"
  14. USAGE = """piwigo verb --param1=value1 --param2=value2
  15. verb list
  16. - upload
  17. - download
  18. - sync
  19. - ws
  20. to get help: piwigo verb --help
  21. """
  22. AUTHOR = "Frederic Aoustin"
  23. PROG = "piwigo"
  24. VERSION = __version__
  25. VERBS = {
  26. "upload":
  27. {
  28. "usage" : "usage for verb upload",
  29. "description" : "upload file in piwigo gallery",
  30. "arg" :
  31. {
  32. "category" : {"type":"string", "default":"/", "help":"destination category of piwigo gallery"},
  33. "source" : {"type":"string", "default":"*.jpg", "help":"path of upload picture"},
  34. "url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
  35. "user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
  36. "password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
  37. "thread" : {"type":"int", "default":"1", "help":"number of thread"},
  38. },
  39. },
  40. "download":
  41. {
  42. "usage" : "usage for verb download",
  43. "description" : "download image from piwigo gallery",
  44. "arg" :
  45. {
  46. "category" : {"type":"string", "default":"/", "help":"source category of piwigo gallery"},
  47. "dest" : {"type":"string", "default":".", "help":"path of destination"},
  48. "url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
  49. "user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
  50. "password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
  51. "thread" : {"type":"int", "default":"1", "help":"number of thread"},
  52. },
  53. },
  54. "sync":
  55. {
  56. "usage" : "usage for verb sync",
  57. "description" : "synchronization between path and piwigo gallery",
  58. "arg" :
  59. {
  60. "category" : {"type":"string", "default":"/", "help":"category of piwigo gallery"},
  61. "source" : {"type":"string", "default":".", "help":"path of picture"},
  62. "url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
  63. "user" : {"type":"string", "default":"", "help":"user of piwigo gallery"},
  64. "password" : {"type":"string", "default":"", "help":"password of piwigo gallery"},
  65. "thread" : {"type":"int", "default":"1", "help":"number of thread"},
  66. },
  67. },
  68. "ws":
  69. {
  70. "usage" : "usage for verb ws",
  71. "description" : "use web service of piwigo gallery",
  72. "arg" :
  73. {
  74. "method" : {"type":"string", "default":".", "help":"name of web service"},
  75. "url" : {"type":"string", "default":"", "help":"url of piwigo gallery"},
  76. },
  77. },
  78. }
  79. def add_dynamic_option(parser):
  80. # add arg for verb
  81. if not len(sys.argv) > 1:
  82. parser.print_help()
  83. sys.exit(1)
  84. if sys.argv[1] in ("--help", "-h"):
  85. parser.print_help()
  86. parser.print_version()
  87. sys.exit(0)
  88. if sys.argv[1] in ("--version"):
  89. parser.print_version()
  90. sys.exit(0)
  91. verb = sys.argv[1]
  92. arg_know = ['--help']
  93. for arg in VERBS.get(verb, {'arg':{}})['arg']:
  94. kw = VERBS[sys.argv[1]]['arg'][arg]
  95. kw['dest'] = arg
  96. parser.add_option("--%s" % arg, **kw)
  97. arg_know.append("--%s" % arg)
  98. # add arg in argv
  99. for arg in sys.argv[2:]:
  100. if arg[:2] == '--' and arg.split('=')[0] not in arg_know:
  101. arg = arg[2:].split('=')[0]
  102. parser.add_option("--%s" % arg , dest=arg, type="string")
  103. arg_know.append("--%s" % arg)
  104. #check verb
  105. if verb not in VERBS:
  106. parser.print_help()
  107. parser.exit(status=2, msg='verb "%s" unknow\n' % verb)
  108. sys.exit(0)
  109. parser.set_usage(VERBS[verb]["usage"])
  110. parser.description = VERBS[verb]["description"]
  111. if '--help' in sys.argv[1:]:
  112. parser.print_help()
  113. sys.exit(0)
  114. def main():
  115. usage = USAGE
  116. parser = OptionParser(version="%s %s" % (PROG,VERSION), usage=usage)
  117. parser.description= DESCRIPTION
  118. parser.epilog = AUTHOR
  119. try:
  120. add_dynamic_option(parser)
  121. (options, args) = parser.parse_args()
  122. verb = args[0]
  123. if verb == 'ws':
  124. piwigo = Piwigo(url=options.url)
  125. if 'user' and 'password' in options.__dict__:
  126. piwigo.login(options.user, options.password)
  127. kw = purge_kw(options.__dict__,('user','password','url'))
  128. pp = pprint.PrettyPrinter(indent=4)
  129. pp.pprint(Ws(piwigo, options.method)(**kw))
  130. if piwigo.islogged:
  131. piwigo.logout()
  132. if verb == "download":
  133. ana = Analyse('Analyze')
  134. ana.start()
  135. try:
  136. piwigo = Piwigo(url=options.url)
  137. piwigo.login(options.user, options.password)
  138. # check
  139. if not os.path.isdir(options.dest):
  140. os.makedirs(options.dest)
  141. options.dest = os.path.abspath(options.dest)
  142. piwigo.iscategory(options.category)
  143. if options.category[-2:] == ' /' : options.category = options.category[:-2]
  144. # treatment
  145. run = Run(verb, options.thread)
  146. kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
  147. for img in piwigo.images(options.category, **kw):
  148. run.add(piwigo.download,
  149. ["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))],
  150. kw)
  151. except Exception as e:
  152. ana.stop()
  153. raise e
  154. ana.stop()
  155. run.start()
  156. piwigo.logout()
  157. if run.error:
  158. parser.error(run.strerror)
  159. if verb == "upload":
  160. ana = Analyse('Analyze')
  161. ana.start()
  162. try:
  163. piwigo = Piwigo(url=options.url)
  164. piwigo.login(options.user, options.password)
  165. # check
  166. piwigo.makedirs(options.category)
  167. # treatment
  168. run = Run(verb, options.thread)
  169. kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
  170. for img in glob.glob(options.source):
  171. run.add(piwigo.upload,
  172. [os.path.abspath(img), options.category],
  173. kw)
  174. ana.stop()
  175. except Exception as e:
  176. ana.stop()
  177. raise e
  178. run.start()
  179. piwigo.logout()
  180. if run.error:
  181. parser.error(run.strerror)
  182. except Exception as e:
  183. parser.error(e)
  184. sys.exit(1)
  185. if __name__ == "__main__":
  186. main()