-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpimgdb.py
458 lines (353 loc) · 16.9 KB
/
rpimgdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# -*- coding: utf-8 -*-
"""
Time-lapse with Rasberry Pi controlled camera
Copyright (C) 2016-2017 Istvan Z. Kovacs
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Implements the rpiImageDbx class to manage images in a remote directory (dropbox).
Use Dropbox SDK, API V2, Python 3.4
https://www.dropbox.com/developers/documentation/python
https://github.com/dropbox/dropbox-sdk-python
"""
import os
import sys
import time
import datetime
import posixpath
import atexit
try:
import json
except ImportError:
import simplejson as json
try:
from dropbox import Dropbox
from dropbox.files import WriteMode, SearchMode, FileMetadata, FolderMetadata
from dropbox.exceptions import ApiError, AuthError, DropboxException, InternalServerError
from requests import exceptions
DBXUSE = True
except ImportError:
DBXUSE = False
pass
### The rpi(cam)py modules
from rpilogger import rpiLogger
if not DBXUSE:
rpiLogger.error("Dropbox module not found or not available!")
os._exit()
import rpififo
from rpibase import rpiBaseClass, rpiBaseClassError
from rpibase import ERRCRIT, ERRLEV2, ERRLEV1, ERRLEV0, ERRNONE
class rpiImageDbxClass(rpiBaseClass):
"""
Implements the rpiImageDb class to manage images in a remote directory (dropbox).
"""
def __init__(self, name, rpi_apscheduler, rpi_events, rpi_config, cam_rpififo=None):
### Get the Dbx error event
#self._eventDbErr = rpi_events.eventErrList["DBXJob"]
### Get the custom config parameters
self._config = rpi_config
### Get FIFO buffer for images from the camera (deque)
self._imageFIFO = cam_rpififo
### The FIFO buffer for the uploaded images (deque)
self.imageUpldFIFO = rpififo.rpiFIFOClass([], 576)
self.imageUpldFIFO.crtSubDir = ''
### Init base class
super().__init__(name, rpi_apscheduler, rpi_events)
def __repr__(self):
return "<%s (name=%s, rpi_apscheduler=%s, rpi_events=dict(), rpi_config=%s, dbuff_rpififo=%s)>" % (self.__class__.__name__, self.name, self._sched, self._config, self._imageFIFO)
def __str__(self):
msg = super().__str__()
return "%s::: dbinfo: %s, config: %s\nimageUpldFIFO: %s\n%s" % \
(self.name, self.dbinfo, self._config, self.imageUpldFIFO, msg)
def __del__(self):
### Clean base class
super().__del__()
#
# Main interface methods
#
def jobRun(self):
try:
# Lock the buffer
self._imageFIFO.acquireSemaphore()
# Get the current images in the FIFO
# Refresh the last remote image when available
if len(self._imageFIFO):
# Update remote cam image with the current (last) image
if not (self._imageFIFO[-1] == self.crt_image_snap):
self._putImage(self._imageFIFO[-1], self._config['image_snap'], True)
self.crt_image_snap = self._imageFIFO[-1]
self.numImgUpdDb += 1
rpiLogger.info("Updated remote %s with %s" % (self._config['image_snap'], self._imageFIFO[-1]) )
# Lock the upload buffer
self.imageUpldFIFO.acquireSemaphore()
# Check if a new upload sub-folder has to be used
if not (self.imageUpldFIFO.crtSubDir == self._imageFIFO.crtSubDir):
self.imageUpldFIFO.crtSubDir = self._imageFIFO.crtSubDir
self.upldir = os.path.normpath(os.path.join(self._config['image_dir'], self.imageUpldFIFO.crtSubDir))
self._mkdirImage(self.upldir)
# Upload only images in the FIFO which have not been uploaded yet
for img in self._imageFIFO:
if not img in self.imageUpldFIFO:
self._putImage(img, os.path.join(self.upldir, os.path.basename(img)))
rpiLogger.info("Uploaded %s" % img )
# Release the upload buffer
self.imageUpldFIFO.releaseSemaphore()
# Update status
self.statusUpdate = (self.name, self.numImgUpdDb)
else:
# Update status
self.statusUpdate = (self.name, ERRNONE)
rpiLogger.info('Nothing to upload')
# Handle exceptions, mostly HTTP/SSL related!
except exceptions.Timeout as e:
# Catching this error will catch both ReadTimeout and ConnectTimeout.
raise rpiBaseClassError("%s::: jobRun(): Connect/ReadTimeoutError:\n%s" % (self.name, str(e)), ERRLEV2)
except exceptions.ConnectionError as e:
# A Connection error occurred.
raise rpiBaseClassError("%s::: jobRun(): ConnectionError:\n%s" % (self.name, str(e)), ERRLEV2)
except exceptions.HTTPError as e:
# An HTTP error occurred.
raise rpiBaseClassError("%s::: jobRun(): HTTPError:\n%s" % (self.name, str(e)), ERRLEV2)
except exceptions.RequestException as e:
# There was an ambiguous exception that occurred while handling your request.
raise rpiBaseClassError("%s::: jobRun(): RequestException:\n%s" % (self.name, str(e)), ERRLEV2)
# except BadStatusLine as e:
# self.eventErr_set('run()')
# rpiLogger.debug("BadStatusLine:\n%s" % str(e))
# pass
except rpiBaseClassError as e:
if e.errval == ERRCRIT:
self.endDayOAM()
raise rpiBaseClassError("%s::: jobRun(): %s" % (self.name, e.errmsg), e.errval)
except RuntimeError as e:
self.endDayOAM()
raise rpiBaseClassError("%s::: jobRun(): RuntimeError:\n%s" % (self.name, str(e)), ERRCRIT)
except:
self.endDayOAM()
raise rpiBaseClassError("%s::: jobRun(): Unhandled Exception:\n%s" % (self.name, str(sys.exc_info())), ERRCRIT)
finally:
# Release the buffer
self._imageFIFO.releaseSemaphore()
def initClass(self):
""""
(re)Initialize the class.
"""
#self.imageDbHash = None
self._imageDbCursor = None
self.imageDbList = []
self.numImgUpdDb = 0
self.crt_image_snap = None
self.imgid = self._imageFIFO.camID + '.jpg'
self.upldir = os.path.normpath(os.path.join(self._config['image_dir'], self.imageUpldFIFO.crtSubDir))
self.logfile = './upldlog.json'
### When there are already images listed in the upload log file, then
# make sure we don't upload them to the remote folder again
# Else, create the file with an empty list; to be updated in endDayOAM()
try:
self.imageUpldFIFO.acquireSemaphore()
self.imageUpldFIFO.clear()
if os.path.isfile(self.logfile):
with open(self.logfile,'r') as logf:
upldimg = json.load(logf)
for img in upldimg:
self.imageUpldFIFO.append(img)
del upldimg
rpiLogger.info("%s::: Local log file %s found and loaded." % (self.name, self.logfile))
else:
with open(self.logfile,'w') as logf:
json.dump([], logf)
rpiLogger.info("%s::: Local log file %s initialized." % (self.name, self.logfile))
except IOError:
raise rpiBaseClassError("%s::: initClass(): Local log file %s was not found or could not be created." % (self.name, self.logfile), ERRCRIT)
finally:
# Release the upload buffer
self.imageUpldFIFO.releaseSemaphore()
### Init Dropbox API client
self._token_file = self._config['token_file']
self._dbx = None
self.dbinfo = None
try:
with open(self._token_file, 'r') as token:
self._dbx = Dropbox(token.read())
info = self._dbx.users_get_current_account()
# info._all_field_names_ =
# {'account_id', 'is_paired', 'locale', 'email', 'name', 'team', 'country', 'account_type', 'referral_link'}
self.dbinfo ={'email': info.email, 'referral_link': info.referral_link}
rpiLogger.info("%s::: Loaded access token from ''%s''" % (self.name, self._token_file) )
### Create remote root folder (relative to app root) if it does not exist yet
self._mkdirImage(os.path.normpath(self._config['image_dir']))
except rpiBaseClassError as e:
if e.errval == ERRCRIT:
self.endDayOAM()
raise rpiBaseClassError("initClass(): %s" % e.errmsg, e.errval)
except IOError:
self.endDayOAM()
raise rpiBaseClassError("initClass(): Token file ''%s'' could not be read." % (self.name, self._token_file), ERRCRIT)
except AuthError as e:
self.endDayOAM()
raise rpiBaseClassError("initClass(): AuthError:\n%s" % e.error, ERRCRIT)
except DropboxException as e:
self.endDayOAM()
raise rpiBaseClassError("initClass(): DropboxException:\n%s" % str(e), ERRCRIT)
except InternalServerError as e:
self.endDayOAM()
raise rpiBaseClassError("initClass(): InternalServerError:\n%s" % str(e.status_code), ERRCRIT)
def endDayOAM(self):
"""
End-of-Day Operation and Maintenance sequence.
"""
self._lsImage(self.upldir)
rpiLogger.info("%s::: %d images in the remote folder %s" % (self.name, len(self.imageDbList), self.upldir))
# Lock the uplaod buffer
self.imageUpldFIFO.acquireSemaphore()
try:
upldimg=[]
for img in self.imageUpldFIFO:
upldimg.append(img)
with open(self.logfile,'w') as logf:
json.dump(upldimg, logf)
del upldimg
rpiLogger.info("%s::: Local log file %s updated." % (self.name, self.logfile))
except IOError:
raise rpiBaseClassError("endDayOAM(): Local log file %s was not found." % self.logfile, ERRCRIT)
finally:
# Release the upload buffer
self.imageUpldFIFO.releaseSemaphore()
# def endOAM(self):
# """
# End OAM procedure.
# """
# @atexit.register
# def atexitend():
# self.endDayOAM()
def _lsImage(self,from_path):
"""
List the image/video files in the remote directory.
Stores the found file names in self.imageDbList.
"""
try:
if self._imageDbCursor is None:
self.ls_ref = self._dbx.files_list_folder('/' + os.path.normpath(from_path), recursive=False, include_media_info=True )
else:
new_ls = self._dbx.files_list_folder_continue(self._imageDbCursor)
if new_ls.entries == []:
rpiLogger.debug("%s::: _lsImage():: No changes on the server." % self.name)
else:
self.ls_ref = new_ls
# Select only images and only the ones for the current imgid (camid)
foundImg = False
for f in self.ls_ref.entries:
if 'media_info' in f._all_field_names_ and \
f.media_info is not None:
if self.imgid.lower() in f.path_lower:
img = '.%s' % f.path_lower
foundImg = True
if not img in self.imageDbList:
self.imageDbList.append(img)
if not foundImg:
self.imageDbList = []
### Store the hash of the folder
self._imageDbCursor = self.ls_ref.cursor
if len(self.imageDbList) > 0:
rpiLogger.debug("%s::: _lsImage():: imageDbList[0..%d]: %s .. %s" % (self.name, len(self.imageDbList)-1, self.imageDbList[0], self.imageDbList[-1]) )
else:
rpiLogger.debug("%s::: _lsImage():: imageDbList[]: empty" % self.name)
except ApiError as e:
raise rpiBaseClassError("_lsImage(): %s" % e.error, ERRLEV2)
def _putImage(self, from_path, to_path, overwrite=False):
"""
Copy local file to remote file.
Stores the uploaded files names in self.imageUpldFIFO.
Examples:
_putImage('./path/test.jpg', '/path/dropbox-upload-test.jpg')
"""
mode = (WriteMode.overwrite if overwrite else WriteMode.add)
with open(from_path, "rb") as from_file:
try:
self._dbx.files_upload( from_file.read(), '/' + os.path.normpath(to_path), mode)
if not overwrite:
self.imageUpldFIFO.append(from_path)
rpiLogger.debug("%s::: _putImage(): Uploaded file from %s to remote %s" % (self.name, from_path, to_path))
except ApiError as err:
# This checks for the specific error where a user doesn't have
# enough Dropbox space quota to upload this file
if (err.error.is_path() and err.error.get_path().error.is_insufficient_space()):
raise rpiBaseClassError("_putImage(): ERROR: Cannot back up; insufficient space.", ERRCRIT)
elif err.user_message_text:
raise rpiBaseClassError("_putImage(): %s" % err.user_message_text, ERRLEV2)
else:
raise rpiBaseClassError("_putImage(): %s" % err.error, ERRLEV2)
#except IOError:
#raise rpiBaseClassError("_putImage(): Local img file %s could not be opened." % from_path, ERRCRIT)
def _mkdirImage(self, path):
"""
Create a new remote directory.
Examples:
_mkdirImage('/dropbox_dir_test')
"""
try:
self._dbx.files_create_folder('/' + os.path.normpath(path))
rpiLogger.debug("%s::: Remote output folder /%s created." % (self.name, path))
except ApiError as e:
noerr = False
# dropbox.files.CreateFolderError
if e.error.is_path():
# dropbox.files.WriteError
we = e.error.get_path()
if we.is_conflict():
# dropbox.files.WriteConflictError
wce = we.get_conflict()
# union tag is 'folder'
if wce.is_folder():
rpiLogger.info("%s::: Remote output folder /%s already exist!" % (self.name, path))
noerr = True
if not noerr:
raise rpiBaseClassError("_mkdirImage(): Remote output folder /%s was not created! %s" % (path, e.error), ERRCRIT)
else:
pass
def _mvImage(self, from_path, to_path):
"""
Move/rename a remote file or directory.
Examples:
_mvImage('./path1/dropbox-move-test.jpg', '/path2/dropbox-move-test.jpg')
"""
try:
self._dbx.files_move( '/' + os.path.normpath(from_path), '/' + os.path.normpath(to_path) )
rpiLogger.debug("%s::: _mvImage(): Moved file from %s to %s" % (self.name, from_path, to_path))
except ApiError as e:
raise rpiBaseClassError("_mvImage(): Image %s could not be moved to %s! %s" % (from_path, to_path, e.error), ERRLEV2)
# def _getImage(self, from_file, to_path):
# """
# Copy file from remote directory to local file.
#
# Examples:
# _getImage('./path/dropbox-download-test.jpg', './file.jpg',)
# """
# try:
# metadata, response = self._dbx.files_download_to_file( to_path, '/' + os.path.normpath(from_file) )
# rpiLogger.debug("%s::: _getImage(): Downloaded file from remote %s to %s. Metadata: %s" % (self.name, from_file, to_path, metadata) )
#
# except ApiError as e:
# raise rpiBaseClassError("_getImage(): %s" % e.error, ERRLEV2)
# def _searchImage(self, string):
# """
# Search remote directory for image/video filenames containing the given string.
# """
# try:
# results = self._dbx.files_search( '', string, start=0, max_results=100, mode=SearchMode('filename', None) )
#
# except ApiError as e: #rest.ErrorResponse as e:
# raise rpiBaseClassError("_searchImage(): %s" % e.error, ERRLEV2)
# def _rmImage(self, path):
# """
# Delete a remote image/video file
# """
#