Browse Source

check ws, upload and download

fraoustin 5 years ago
parent
commit
1f1ba6a9a7
4 changed files with 99 additions and 74 deletions
  1. 13
    14
      piwigotools/__init__.py
  2. 18
    1
      piwigotools/interface.py
  3. 39
    30
      piwigotools/main.py
  4. 29
    29
      tests/test_basic.py

+ 13
- 14
piwigotools/__init__.py View File

@@ -60,8 +60,7 @@ class Piwigo(piwigo.Piwigo):
60 60
 
61 61
     @property
62 62
     def plan(self):
63
-        #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'] }
64
-        return { "/%s" % i["name"].replace(" / ","/") : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
63
+        return { i["name"] : i["id"] for i in self.pwg.categories.getList(recursive=True, fullname=True)['categories'] }
65 64
 
66 65
 
67 66
     def _checkarg(fn):
@@ -70,7 +69,7 @@ class Piwigo(piwigo.Piwigo):
70 69
             # manage path
71 70
             if inspect.getargspec(fn).args.count('path'): 
72 71
                 pos = inspect.getargspec(fn).args.index('path') -1
73
-                if args[pos][-1] == '/' : args[pos] = args[pos][:-1]
72
+                if args[pos][-2:] == ' /' : args[pos] = args[pos][:-2]
74 73
             args = tuple(args)
75 74
             return fn(self, *args, **kw)
76 75
         return checking    
@@ -138,8 +137,8 @@ class Piwigo(piwigo.Piwigo):
138 137
 
139 138
     @_checkarg
140 139
     def isimage(self, path):
141
-        img = path.split('/')[-1]
142
-        path =  '/'.join(path.split('/')[:-1])
140
+        img = path.split(' / ')[-1]
141
+        path =  ' / '.join(path.split(' / ')[:-1])
143 142
         if img in self.images(path):
144 143
             return True
145 144
         return False
@@ -148,8 +147,8 @@ class Piwigo(piwigo.Piwigo):
148 147
     def idimage(self, path):
149 148
         if not self.isimage(path):
150 149
             raise PiwigoExistException("image %s not exist" % path)
151
-        img = path.split('/')[-1]
152
-        path =  '/'.join(path.split('/')[:-1])
150
+        img = path.split(' / ')[-1]
151
+        path =  ' / '.join(path.split(' / ')[:-1])
153 152
         return self.images(path)[img]["id"]
154 153
 
155 154
     @_checkarg
@@ -158,8 +157,8 @@ class Piwigo(piwigo.Piwigo):
158 157
         """
159 158
             create a category named path
160 159
         """
161
-        kw['name'] = path.split('/')[-1]
162
-        parent = '/'.join(path.split('/')[:-1])
160
+        kw['name'] = path.split(' / ')[-1]
161
+        parent = ' / '.join(path.split(' / ')[:-1])
163 162
         if parent and not self.iscategory(parent):
164 163
             raise PiwigoExistException("category %s not exist" % parent)
165 164
         if parent : kw['parent'] = self.plan[parent]
@@ -172,12 +171,12 @@ class Piwigo(piwigo.Piwigo):
172 171
         """
173 172
             recursive category create function
174 173
         """
175
-        pp = '/'
176
-        for p in path.split('/')[1:]:
174
+        pp = ''
175
+        for p in path.split(' / '):
177 176
             pp = '%s%s' % (pp, p)
178 177
             if not self.iscategory(pp):
179 178
                 self.mkdir(pp, **kw)
180
-            pp = '%s/' % pp
179
+            pp = '%s / ' % pp
181 180
         return self.idcategory(path)
182 181
 
183 182
     @_checkarg
@@ -210,8 +209,8 @@ class Piwigo(piwigo.Piwigo):
210 209
         """
211 210
         if not self.isimage(path):
212 211
             raise PiwigoException("image %s not exist" % path)
213
-        img = path.split('/')[-1]
214
-        path =  '/'.join(path.split('/')[:-1])
212
+        img = path.split(' / ')[-1]
213
+        path =  ' / '.join(path.split(' / ')[:-1])
215 214
         url = self.images(path)[img]['element_url']
216 215
         with open(dst, 'wb') as img:
217 216
             r = requests.get(url)

+ 18
- 1
piwigotools/interface.py View File

@@ -26,7 +26,6 @@ class Step(threading.Thread):
26 26
                 try:
27 27
                     call(*arg, **kw)
28 28
                 except Exception as e:
29
-                   print(e)
30 29
                    self.qerr.put([call, arg, kw, e]) 
31 30
                 self.qout.put([call, arg, kw])
32 31
             except queue.Empty:
@@ -41,6 +40,24 @@ class Run:
41 40
         self._qerr = queue.Queue()
42 41
         self._threads = [ Step(self._qin, self._qout, self._qerr) for i in range(cnt)] 
43 42
 
43
+    @property
44
+    def error(self):
45
+        """
46
+            return true if _qerr.qsize() > 0
47
+        """
48
+        if self._qerr.qsize() > 0:
49
+            return True
50
+        return False    
51
+
52
+    @property
53
+    def strerror(self):
54
+        ret = ""
55
+        while not self._qerr.empty():
56
+            call, arg, kw, e = self._qerr.get_nowait()
57
+            ret = "%s%s\n" % (ret, e) 
58
+        return ret    
59
+
60
+
44 61
     def add(self, call, arg, kw):
45 62
         self._qin.put([call, arg, kw])
46 63
 

+ 39
- 30
piwigotools/main.py View File

@@ -152,41 +152,53 @@ def main():
152 152
         if verb == "download":
153 153
             ana = Analyse('Analyze')
154 154
             ana.start()
155
-            piwigo = Piwigo(url=options.url)
156
-            piwigo.login(options.user, options.password)
157
-            # check
158
-            if not os.path.isdir(options.dest):
159
-                os.makedirs(options.dest)        
160
-            options.dest = os.path.abspath(options.dest)
161
-            piwigo.iscategory(options.category)
162
-            if options.category[-1] == '/' : options.category = options.category[:-1]
163
-            # treatment
164
-            run = Run(verb, options.thread)
165
-            kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
166
-            for img in piwigo.images(options.category, **kw):
167
-                run.add(piwigo.download, 
168
-                        ["%s/%s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))],
169
-                        kw)
155
+            try:
156
+                piwigo = Piwigo(url=options.url)
157
+                piwigo.login(options.user, options.password)
158
+                # check
159
+                if not os.path.isdir(options.dest):
160
+                    os.makedirs(options.dest)        
161
+                options.dest = os.path.abspath(options.dest)
162
+                piwigo.iscategory(options.category)
163
+                if options.category[-2:] == ' /' : options.category = options.category[:-2]
164
+                # treatment
165
+                run = Run(verb, options.thread)
166
+                kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
167
+                for img in piwigo.images(options.category, **kw):
168
+                    run.add(piwigo.download, 
169
+                            ["%s / %s" % (options.category, str(img)), "%s/%s" % (options.dest, str(img))],
170
+                            kw)
171
+            except Exception as e:
172
+                ana.stop()
173
+                raise e
170 174
             ana.stop()
171 175
             run.start()
172 176
             piwigo.logout()
177
+            if run.error:
178
+               parser.error(run.strerror) 
173 179
         if verb == "upload":
174 180
             ana = Analyse('Analyze')
175 181
             ana.start()
176
-            piwigo = Piwigo(url=options.url)
177
-            piwigo.login(options.user, options.password)
178
-            # check
179
-            piwigo.makedirs(options.category)
180
-            # treatment
181
-            run = Run(verb, options.thread)
182
-            kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
183
-            for img in glob.glob(options.source):
184
-                run.add(piwigo.upload,
185
-                        [os.path.abspath(img), options.category], 
186
-                        kw)
187
-            ana.stop()
182
+            try:
183
+                piwigo = Piwigo(url=options.url)
184
+                piwigo.login(options.user, options.password)
185
+                # check
186
+                piwigo.makedirs(options.category)
187
+                # treatment
188
+                run = Run(verb, options.thread)
189
+                kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
190
+                for img in glob.glob(options.source):
191
+                    run.add(piwigo.upload,
192
+                            [os.path.abspath(img), options.category], 
193
+                            kw)
194
+                ana.stop()
195
+            except Exception as e:
196
+                ana.stop()
197
+                raise e
188 198
             run.start()
189 199
             piwigo.logout()
200
+            if run.error:
201
+                parser.error(run.strerror)
190 202
     except Exception as e:
191 203
         parser.error(e)
192 204
         sys.exit(1)
@@ -194,6 +206,3 @@ def main():
194 206
 if __name__ == "__main__":
195 207
     main()
196 208
 
197
-# TODO
198
-# verb sync
199
-# test python3: problem request return bytes and not str ... only str python2 or 3 and encoding?

+ 29
- 29
tests/test_basic.py View File

@@ -33,66 +33,66 @@ class BasicTestCase(unittest.TestCase):
33 33
 
34 34
     def test_createCategory(self):
35 35
         self.piwigo.login(self.usertest, self.passwordtest)
36
-        self.assertTrue(self.piwigo.mkdir('/level'))
37
-        self.assertTrue(self.piwigo.mkdir('/level/sublevel'))
38
-        self.assertTrue(self.piwigo.makedirs('/level2/sublevel2'))
39
-        self.piwigo.removedirs('/level2')
40
-        self.piwigo.removedirs('/level')
36
+        self.assertTrue(self.piwigo.mkdir('level'))
37
+        self.assertTrue(self.piwigo.mkdir('level / sublevel'))
38
+        self.assertTrue(self.piwigo.makedirs('level2 / sublevel2'))
39
+        self.piwigo.removedirs('level2')
40
+        self.piwigo.removedirs('level')
41 41
         self.piwigo.logout()
42 42
 
43 43
     def test_checkpath(self):
44 44
         self.piwigo.login(self.usertest, self.passwordtest)
45
-        self.piwigo.mkdir('/level')
46
-        self.assertTrue(self.piwigo.iscategory('/level'))
47
-        self.assertTrue(self.piwigo.iscategory('/level/'))
48
-        self.piwigo.removedirs('/level')
45
+        self.piwigo.mkdir('level')
46
+        self.assertTrue(self.piwigo.iscategory('level'))
47
+        self.assertTrue(self.piwigo.iscategory('level /'))
48
+        self.piwigo.removedirs('level')
49 49
         self.piwigo.logout()
50 50
 
51 51
     def test_removeCategory(self):
52 52
         self.piwigo.login(self.usertest, self.passwordtest)
53
-        self.piwigo.makedirs('/level2/sublevel2')
54
-        self.assertTrue(self.piwigo.removedirs('/level2'))
55
-        self.assertFalse(self.piwigo.iscategory('/level2'))
53
+        self.piwigo.makedirs('level2 / sublevel2')
54
+        self.assertTrue(self.piwigo.removedirs('level2'))
55
+        self.assertFalse(self.piwigo.iscategory('level2'))
56 56
         self.piwigo.logout()
57 57
 
58 58
     def test_uploadImage(self):
59 59
         self.piwigo.login(self.usertest, self.passwordtest)
60
-        self.piwigo.mkdir('/level')
60
+        self.piwigo.mkdir('level')
61 61
         img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg')
62
-        id = self.piwigo.upload(image=img, path="/level")
62
+        id = self.piwigo.upload(image=img, path="level")
63 63
         self.assertTrue(id)
64
-        self.assertTrue(self.piwigo.isimage('/level/samplepiwigotools.jpg'))
64
+        self.assertTrue(self.piwigo.isimage('level / samplepiwigotools.jpg'))
65 65
         self.piwigo.pwg.images.delete(image_id=id, pwg_token=self.piwigo.token)
66
-        self.piwigo.removedirs('/level')
66
+        self.piwigo.removedirs('level')
67 67
         self.piwigo.logout()
68 68
 
69 69
     def test_removeImage(self):
70 70
         self.piwigo.login(self.usertest, self.passwordtest)
71
-        self.piwigo.mkdir('/level')
71
+        self.piwigo.mkdir('level')
72 72
         img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg')
73
-        id = self.piwigo.upload(image=img, path="/level")
74
-        self.assertTrue(self.piwigo.remove('/level/samplepiwigotools.jpg'))
75
-        self.assertFalse(self.piwigo.isimage('/level/samplepiwigotools.jpg'))
76
-        self.piwigo.removedirs('/level')
73
+        id = self.piwigo.upload(image=img, path="level")
74
+        self.assertTrue(self.piwigo.remove('level / samplepiwigotools.jpg'))
75
+        self.assertFalse(self.piwigo.isimage('level / samplepiwigotools.jpg'))
76
+        self.piwigo.removedirs('level')
77 77
         self.piwigo.logout()
78 78
 
79 79
     def test_sublevel(self):
80 80
         self.piwigo.login(self.usertest, self.passwordtest)
81
-        self.piwigo.makedirs('/level2/sublevel2')
82
-        self.assertTrue(len(self.piwigo.sublevels('/level2')))
83
-        self.piwigo.removedirs('/level2')
81
+        self.piwigo.makedirs('level2 / sublevel2')
82
+        self.assertTrue(len(self.piwigo.sublevels('level2')))
83
+        self.piwigo.removedirs('level2')
84 84
         self.piwigo.logout()
85 85
 
86 86
     def test_downloadImage(self):
87 87
         self.piwigo.login(self.usertest, self.passwordtest)
88
-        self.piwigo.mkdir('/level')
88
+        self.piwigo.mkdir('level')
89 89
         img = os.path.join(os.path.dirname(os.path.abspath(__file__)),'samplepiwigotools.jpg')
90
-        id = self.piwigo.upload(image=img, path="/level")
90
+        id = self.piwigo.upload(image=img, path="level")
91 91
         imgdst = os.path.join(os.path.dirname(os.path.abspath(__file__)),'download.jpg')
92
-        self.assertTrue(self.piwigo.download("/level/samplepiwigotools.jpg",imgdst))
92
+        self.assertTrue(self.piwigo.download("level / samplepiwigotools.jpg",imgdst))
93 93
         os.remove(imgdst)
94
-        self.piwigo.remove('/level/samplepiwigotools.jpg')
95
-        self.piwigo.removedirs('/level')
94
+        self.piwigo.remove('level / samplepiwigotools.jpg')
95
+        self.piwigo.removedirs('level')
96 96
         self.piwigo.logout()
97 97
 
98 98
 if __name__ == '__main__':