mirror of
				https://github.com/fraoustin/piwigotools.git
				synced 2025-10-30 18:07:16 +00:00 
			
		
		
		
	check ws, upload and download
This commit is contained in:
		
							parent
							
								
									0e95c6af1f
								
							
						
					
					
						commit
						1f1ba6a9a7
					
				| @ -60,8 +60,7 @@ class Piwigo(piwigo.Piwigo): | |||||||
| 
 | 
 | ||||||
|     @property |     @property | ||||||
|     def plan(self): |     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 { i["name"] : 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'] } |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     def _checkarg(fn): |     def _checkarg(fn): | ||||||
| @ -70,7 +69,7 @@ class Piwigo(piwigo.Piwigo): | |||||||
|             # manage path |             # manage path | ||||||
|             if inspect.getargspec(fn).args.count('path'):  |             if inspect.getargspec(fn).args.count('path'):  | ||||||
|                 pos = inspect.getargspec(fn).args.index('path') -1 |                 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) |             args = tuple(args) | ||||||
|             return fn(self, *args, **kw) |             return fn(self, *args, **kw) | ||||||
|         return checking     |         return checking     | ||||||
| @ -172,8 +171,8 @@ class Piwigo(piwigo.Piwigo): | |||||||
|         """ |         """ | ||||||
|             recursive category create function |             recursive category create function | ||||||
|         """ |         """ | ||||||
|         pp = '/' |         pp = '' | ||||||
|         for p in path.split('/')[1:]: |         for p in path.split(' / '): | ||||||
|             pp = '%s%s' % (pp, p) |             pp = '%s%s' % (pp, p) | ||||||
|             if not self.iscategory(pp): |             if not self.iscategory(pp): | ||||||
|                 self.mkdir(pp, **kw) |                 self.mkdir(pp, **kw) | ||||||
|  | |||||||
| @ -26,7 +26,6 @@ class Step(threading.Thread): | |||||||
|                 try: |                 try: | ||||||
|                     call(*arg, **kw) |                     call(*arg, **kw) | ||||||
|                 except Exception as e: |                 except Exception as e: | ||||||
|                    print(e) |  | ||||||
|                    self.qerr.put([call, arg, kw, e])  |                    self.qerr.put([call, arg, kw, e])  | ||||||
|                 self.qout.put([call, arg, kw]) |                 self.qout.put([call, arg, kw]) | ||||||
|             except queue.Empty: |             except queue.Empty: | ||||||
| @ -41,6 +40,24 @@ class Run: | |||||||
|         self._qerr = queue.Queue() |         self._qerr = queue.Queue() | ||||||
|         self._threads = [ Step(self._qin, self._qout, self._qerr) for i in range(cnt)]  |         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): |     def add(self, call, arg, kw): | ||||||
|         self._qin.put([call, arg, kw]) |         self._qin.put([call, arg, kw]) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -152,6 +152,7 @@ def main(): | |||||||
|         if verb == "download": |         if verb == "download": | ||||||
|             ana = Analyse('Analyze') |             ana = Analyse('Analyze') | ||||||
|             ana.start() |             ana.start() | ||||||
|  |             try: | ||||||
|                 piwigo = Piwigo(url=options.url) |                 piwigo = Piwigo(url=options.url) | ||||||
|                 piwigo.login(options.user, options.password) |                 piwigo.login(options.user, options.password) | ||||||
|                 # check |                 # check | ||||||
| @ -159,7 +160,7 @@ def main(): | |||||||
|                     os.makedirs(options.dest)         |                     os.makedirs(options.dest)         | ||||||
|                 options.dest = os.path.abspath(options.dest) |                 options.dest = os.path.abspath(options.dest) | ||||||
|                 piwigo.iscategory(options.category) |                 piwigo.iscategory(options.category) | ||||||
|             if options.category[-1] == '/' : options.category = options.category[:-1] |                 if options.category[-2:] == ' /' : options.category = options.category[:-2] | ||||||
|                 # treatment |                 # treatment | ||||||
|                 run = Run(verb, options.thread) |                 run = Run(verb, options.thread) | ||||||
|                 kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread')) |                 kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread')) | ||||||
| @ -167,12 +168,18 @@ def main(): | |||||||
|                     run.add(piwigo.download,  |                     run.add(piwigo.download,  | ||||||
|                             ["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))], |                             ["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))], | ||||||
|                             kw) |                             kw) | ||||||
|  |             except Exception as e: | ||||||
|  |                 ana.stop() | ||||||
|  |                 raise e | ||||||
|             ana.stop() |             ana.stop() | ||||||
|             run.start() |             run.start() | ||||||
|             piwigo.logout() |             piwigo.logout() | ||||||
|  |             if run.error: | ||||||
|  |                parser.error(run.strerror)  | ||||||
|         if verb == "upload": |         if verb == "upload": | ||||||
|             ana = Analyse('Analyze') |             ana = Analyse('Analyze') | ||||||
|             ana.start() |             ana.start() | ||||||
|  |             try: | ||||||
|                 piwigo = Piwigo(url=options.url) |                 piwigo = Piwigo(url=options.url) | ||||||
|                 piwigo.login(options.user, options.password) |                 piwigo.login(options.user, options.password) | ||||||
|                 # check |                 # check | ||||||
| @ -185,8 +192,13 @@ def main(): | |||||||
|                             [os.path.abspath(img), options.category],  |                             [os.path.abspath(img), options.category],  | ||||||
|                             kw) |                             kw) | ||||||
|                 ana.stop() |                 ana.stop() | ||||||
|  |             except Exception as e: | ||||||
|  |                 ana.stop() | ||||||
|  |                 raise e | ||||||
|             run.start() |             run.start() | ||||||
|             piwigo.logout() |             piwigo.logout() | ||||||
|  |             if run.error: | ||||||
|  |                 parser.error(run.strerror) | ||||||
|     except Exception as e: |     except Exception as e: | ||||||
|         parser.error(e) |         parser.error(e) | ||||||
|         sys.exit(1) |         sys.exit(1) | ||||||
| @ -194,6 +206,3 @@ def main(): | |||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     main() |     main() | ||||||
| 
 | 
 | ||||||
| # TODO |  | ||||||
| # verb sync |  | ||||||
| # test python3: problem request return bytes and not str ... only str python2 or 3 and encoding? |  | ||||||
|  | |||||||
| @ -33,66 +33,66 @@ class BasicTestCase(unittest.TestCase): | |||||||
| 
 | 
 | ||||||
|     def test_createCategory(self): |     def test_createCategory(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         self.piwigo.login(self.usertest, self.passwordtest) | ||||||
|         self.assertTrue(self.piwigo.mkdir('/level')) |         self.assertTrue(self.piwigo.mkdir('level')) | ||||||
|         self.assertTrue(self.piwigo.mkdir('/level/sublevel')) |         self.assertTrue(self.piwigo.mkdir('level / sublevel')) | ||||||
|         self.assertTrue(self.piwigo.makedirs('/level2/sublevel2')) |         self.assertTrue(self.piwigo.makedirs('level2 / sublevel2')) | ||||||
|         self.piwigo.removedirs('/level2') |         self.piwigo.removedirs('level2') | ||||||
|         self.piwigo.removedirs('/level') |         self.piwigo.removedirs('level') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_checkpath(self): |     def test_checkpath(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         self.piwigo.login(self.usertest, self.passwordtest) | ||||||
|         self.piwigo.mkdir('/level') |         self.piwigo.mkdir('level') | ||||||
|         self.assertTrue(self.piwigo.iscategory('/level')) |         self.assertTrue(self.piwigo.iscategory('level')) | ||||||
|         self.assertTrue(self.piwigo.iscategory('/level/')) |         self.assertTrue(self.piwigo.iscategory('level /')) | ||||||
|         self.piwigo.removedirs('/level') |         self.piwigo.removedirs('level') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_removeCategory(self): |     def test_removeCategory(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         self.piwigo.login(self.usertest, self.passwordtest) | ||||||
|         self.piwigo.makedirs('/level2/sublevel2') |         self.piwigo.makedirs('level2 / sublevel2') | ||||||
|         self.assertTrue(self.piwigo.removedirs('/level2')) |         self.assertTrue(self.piwigo.removedirs('level2')) | ||||||
|         self.assertFalse(self.piwigo.iscategory('/level2')) |         self.assertFalse(self.piwigo.iscategory('level2')) | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_uploadImage(self): |     def test_uploadImage(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         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') |         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(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.pwg.images.delete(image_id=id, pwg_token=self.piwigo.token) | ||||||
|         self.piwigo.removedirs('/level') |         self.piwigo.removedirs('level') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_removeImage(self): |     def test_removeImage(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         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') |         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(self.piwigo.remove('/level/samplepiwigotools.jpg')) |         self.assertTrue(self.piwigo.remove('level / samplepiwigotools.jpg')) | ||||||
|         self.assertFalse(self.piwigo.isimage('/level/samplepiwigotools.jpg')) |         self.assertFalse(self.piwigo.isimage('level / samplepiwigotools.jpg')) | ||||||
|         self.piwigo.removedirs('/level') |         self.piwigo.removedirs('level') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_sublevel(self): |     def test_sublevel(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         self.piwigo.login(self.usertest, self.passwordtest) | ||||||
|         self.piwigo.makedirs('/level2/sublevel2') |         self.piwigo.makedirs('level2 / sublevel2') | ||||||
|         self.assertTrue(len(self.piwigo.sublevels('/level2'))) |         self.assertTrue(len(self.piwigo.sublevels('level2'))) | ||||||
|         self.piwigo.removedirs('/level2') |         self.piwigo.removedirs('level2') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
|     def test_downloadImage(self): |     def test_downloadImage(self): | ||||||
|         self.piwigo.login(self.usertest, self.passwordtest) |         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') |         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') |         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) |         os.remove(imgdst) | ||||||
|         self.piwigo.remove('/level/samplepiwigotools.jpg') |         self.piwigo.remove('level / samplepiwigotools.jpg') | ||||||
|         self.piwigo.removedirs('/level') |         self.piwigo.removedirs('level') | ||||||
|         self.piwigo.logout() |         self.piwigo.logout() | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user