diff --git a/piwigotools/__init__.py b/piwigotools/__init__.py index 6374622..c74e6e7 100644 --- a/piwigotools/__init__.py +++ b/piwigotools/__init__.py @@ -60,8 +60,7 @@ class Piwigo(piwigo.Piwigo): @property def plan(self): - #return { (("/%s" % i["name"].replace(" / ","/")).encode('utf-8')).decode('utf-8') : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] } - return { "/%s" % i["name"].replace(" / ","/") : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] } + return { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] } def _checkarg(fn): @@ -70,7 +69,7 @@ class Piwigo(piwigo.Piwigo): # manage path if inspect.getargspec(fn).args.count('path'): pos = inspect.getargspec(fn).args.index('path') -1 - if args[pos][-1] == '/' : args[pos] = args[pos][:-1] + if args[pos][-2:] == ' /' : args[pos] = args[pos][:-2] args = tuple(args) return fn(self, *args, **kw) return checking @@ -138,8 +137,8 @@ class Piwigo(piwigo.Piwigo): @_checkarg def isimage(self, path): - img = path.split('/')[-1] - path = '/'.join(path.split('/')[:-1]) + img = path.split(' / ')[-1] + path = ' / '.join(path.split(' / ')[:-1]) if img in self.images(path): return True return False @@ -148,8 +147,8 @@ class Piwigo(piwigo.Piwigo): def idimage(self, path): if not self.isimage(path): raise PiwigoExistException("image %s not exist" % path) - img = path.split('/')[-1] - path = '/'.join(path.split('/')[:-1]) + img = path.split(' / ')[-1] + path = ' / '.join(path.split(' / ')[:-1]) return self.images(path)[img]["id"] @_checkarg @@ -158,8 +157,8 @@ class Piwigo(piwigo.Piwigo): """ create a category named path """ - kw['name'] = path.split('/')[-1] - parent = '/'.join(path.split('/')[:-1]) + kw['name'] = path.split(' / ')[-1] + parent = ' / '.join(path.split(' / ')[:-1]) if parent and not self.iscategory(parent): raise PiwigoExistException("category %s not exist" % parent) if parent : kw['parent'] = self.plan[parent] @@ -172,12 +171,12 @@ class Piwigo(piwigo.Piwigo): """ recursive category create function """ - pp = '/' - for p in path.split('/')[1:]: + pp = '' + for p in path.split(' / '): pp = '%s%s' % (pp, p) if not self.iscategory(pp): self.mkdir(pp, **kw) - pp = '%s/' % pp + pp = '%s / ' % pp return self.idcategory(path) @_checkarg @@ -210,8 +209,8 @@ class Piwigo(piwigo.Piwigo): """ if not self.isimage(path): raise PiwigoException("image %s not exist" % path) - img = path.split('/')[-1] - path = '/'.join(path.split('/')[:-1]) + img = path.split(' / ')[-1] + path = ' / '.join(path.split(' / ')[:-1]) url = self.images(path)[img]['element_url'] with open(dst, 'wb') as img: r = requests.get(url) diff --git a/piwigotools/interface.py b/piwigotools/interface.py index 67694a9..dd377f2 100644 --- a/piwigotools/interface.py +++ b/piwigotools/interface.py @@ -26,7 +26,6 @@ class Step(threading.Thread): try: call(*arg, **kw) except Exception as e: - print(e) self.qerr.put([call, arg, kw, e]) self.qout.put([call, arg, kw]) except queue.Empty: @@ -41,6 +40,24 @@ class Run: self._qerr = queue.Queue() self._threads = [ Step(self._qin, self._qout, self._qerr) for i in range(cnt)] + @property + def error(self): + """ + return true if _qerr.qsize() > 0 + """ + if self._qerr.qsize() > 0: + return True + return False + + @property + def strerror(self): + ret = "" + while not self._qerr.empty(): + call, arg, kw, e = self._qerr.get_nowait() + ret = "%s%s\n" % (ret, e) + return ret + + def add(self, call, arg, kw): self._qin.put([call, arg, kw]) diff --git a/piwigotools/main.py b/piwigotools/main.py index 2fe42f1..44d4eba 100644 --- a/piwigotools/main.py +++ b/piwigotools/main.py @@ -152,41 +152,53 @@ def main(): if verb == "download": ana = Analyse('Analyze') ana.start() - piwigo = Piwigo(url=options.url) - piwigo.login(options.user, options.password) - # check - if not os.path.isdir(options.dest): - os.makedirs(options.dest) - options.dest = os.path.abspath(options.dest) - piwigo.iscategory(options.category) - if options.category[-1] == '/' : options.category = options.category[:-1] - # treatment - run = Run(verb, options.thread) - kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread')) - for img in piwigo.images(options.category, **kw): - run.add(piwigo.download, - ["%s/%s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))], - kw) + try: + piwigo = Piwigo(url=options.url) + piwigo.login(options.user, options.password) + # check + if not os.path.isdir(options.dest): + os.makedirs(options.dest) + options.dest = os.path.abspath(options.dest) + piwigo.iscategory(options.category) + if options.category[-2:] == ' /' : options.category = options.category[:-2] + # treatment + run = Run(verb, options.thread) + kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread')) + for img in piwigo.images(options.category, **kw): + run.add(piwigo.download, + ["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))], + kw) + except Exception as e: + ana.stop() + raise e ana.stop() run.start() piwigo.logout() + if run.error: + parser.error(run.strerror) if verb == "upload": ana = Analyse('Analyze') ana.start() - piwigo = Piwigo(url=options.url) - piwigo.login(options.user, options.password) - # check - piwigo.makedirs(options.category) - # treatment - run = Run(verb, options.thread) - kw = purge_kw(options.__dict__,('user','password','url','source','category','thread')) - for img in glob.glob(options.source): - run.add(piwigo.upload, - [os.path.abspath(img), options.category], - kw) - ana.stop() + try: + piwigo = Piwigo(url=options.url) + piwigo.login(options.user, options.password) + # check + piwigo.makedirs(options.category) + # treatment + run = Run(verb, options.thread) + kw = purge_kw(options.__dict__,('user','password','url','source','category','thread')) + for img in glob.glob(options.source): + run.add(piwigo.upload, + [os.path.abspath(img), options.category], + kw) + ana.stop() + except Exception as e: + ana.stop() + raise e run.start() piwigo.logout() + if run.error: + parser.error(run.strerror) except Exception as e: parser.error(e) sys.exit(1) @@ -194,6 +206,3 @@ def main(): if __name__ == "__main__": main() -# TODO -# verb sync -# test python3: problem request return bytes and not str ... only str python2 or 3 and encoding? diff --git a/tests/test_basic.py b/tests/test_basic.py index 9309a86..bc782fc 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -33,66 +33,66 @@ class BasicTestCase(unittest.TestCase): def test_createCategory(self): self.piwigo.login(self.usertest, self.passwordtest) - self.assertTrue(self.piwigo.mkdir('/level')) - self.assertTrue(self.piwigo.mkdir('/level/sublevel')) - self.assertTrue(self.piwigo.makedirs('/level2/sublevel2')) - self.piwigo.removedirs('/level2') - self.piwigo.removedirs('/level') + self.assertTrue(self.piwigo.mkdir('level')) + self.assertTrue(self.piwigo.mkdir('level / sublevel')) + self.assertTrue(self.piwigo.makedirs('level2 / sublevel2')) + self.piwigo.removedirs('level2') + self.piwigo.removedirs('level') self.piwigo.logout() def test_checkpath(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.mkdir('/level') - self.assertTrue(self.piwigo.iscategory('/level')) - self.assertTrue(self.piwigo.iscategory('/level/')) - self.piwigo.removedirs('/level') + self.piwigo.mkdir('level') + self.assertTrue(self.piwigo.iscategory('level')) + self.assertTrue(self.piwigo.iscategory('level /')) + self.piwigo.removedirs('level') self.piwigo.logout() def test_removeCategory(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.makedirs('/level2/sublevel2') - self.assertTrue(self.piwigo.removedirs('/level2')) - self.assertFalse(self.piwigo.iscategory('/level2')) + self.piwigo.makedirs('level2 / sublevel2') + self.assertTrue(self.piwigo.removedirs('level2')) + self.assertFalse(self.piwigo.iscategory('level2')) self.piwigo.logout() def test_uploadImage(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.mkdir('/level') + self.piwigo.mkdir('level') img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg') - id = self.piwigo.upload(image=img, path="/level") + id = self.piwigo.upload(image=img, path="level") self.assertTrue(id) - self.assertTrue(self.piwigo.isimage('/level/samplepiwigotools.jpg')) + self.assertTrue(self.piwigo.isimage('level / samplepiwigotools.jpg')) self.piwigo.pwg.images.delete(image_id=id, pwg_token=self.piwigo.token) - self.piwigo.removedirs('/level') + self.piwigo.removedirs('level') self.piwigo.logout() def test_removeImage(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.mkdir('/level') + self.piwigo.mkdir('level') img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg') - id = self.piwigo.upload(image=img, path="/level") - self.assertTrue(self.piwigo.remove('/level/samplepiwigotools.jpg')) - self.assertFalse(self.piwigo.isimage('/level/samplepiwigotools.jpg')) - self.piwigo.removedirs('/level') + id = self.piwigo.upload(image=img, path="level") + self.assertTrue(self.piwigo.remove('level / samplepiwigotools.jpg')) + self.assertFalse(self.piwigo.isimage('level / samplepiwigotools.jpg')) + self.piwigo.removedirs('level') self.piwigo.logout() def test_sublevel(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.makedirs('/level2/sublevel2') - self.assertTrue(len(self.piwigo.sublevels('/level2'))) - self.piwigo.removedirs('/level2') + self.piwigo.makedirs('level2 / sublevel2') + self.assertTrue(len(self.piwigo.sublevels('level2'))) + self.piwigo.removedirs('level2') self.piwigo.logout() def test_downloadImage(self): self.piwigo.login(self.usertest, self.passwordtest) - self.piwigo.mkdir('/level') + self.piwigo.mkdir('level') img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg') - id = self.piwigo.upload(image=img, path="/level") + id = self.piwigo.upload(image=img, path="level") imgdst = os.path.join(os.path.dirname(os.path.abspath(__file__)),'download.jpg') - self.assertTrue(self.piwigo.download("/level/samplepiwigotools.jpg",imgdst)) + self.assertTrue(self.piwigo.download("level / samplepiwigotools.jpg",imgdst)) os.remove(imgdst) - self.piwigo.remove('/level/samplepiwigotools.jpg') - self.piwigo.removedirs('/level') + self.piwigo.remove('level / samplepiwigotools.jpg') + self.piwigo.removedirs('level') self.piwigo.logout() if __name__ == '__main__':