-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncCamera.py
More file actions
executable file
·102 lines (78 loc) · 3.49 KB
/
SyncCamera.py
File metadata and controls
executable file
·102 lines (78 loc) · 3.49 KB
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
#!/usr/bin/python2
from datetime import datetime
import logging
import argparse
import os
import sys
import time
from modules import *
import gphoto_if
class SyncCamera():
GPhotoIf_debug = False
SyncCamera_debug = logging.DEBUG
def __init__(self):
logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s', level=self.SyncCamera_debug)
self._log = logging.getLogger('SyncCamera')
self._arg = getParsedArguments()
self._notification = NotificationInterface()
#check arguments
create_datafile(self._arg.datafile, self._arg.create_datafile)
is_valid_directory(self._arg.incomming_folder, 'Inncomming Directory is not existing!')
is_valid_file(self._arg.datafile, 'Datafile is not existing!')
self._sig = CheckSignals()
self._cam_finder = CameraFinder(self._arg.ip)
def run(self):
while not self._sig.terminationSignalReceived():
if self._cam_finder.isReachable():
self._log.debug('Camera found! Start synchronization...')
if self.syncCamera() is True:
self._notification.pushMessage('All files successfully synchronized!')
time.sleep(20)
else:
self._notification.pushMessage('An error ocured. Synchronization abborded!')
time.sleep(3)
def syncCamera(self):
try:
gp = gphoto_if.GPhotoIf(self._arg.model, self._arg.ip, self.GPhotoIf_debug)
db = DataBase(self._arg.datafile)
sync = CameraSynchronizer(gp,db)
while not self._sig.terminationSignalReceived():
if sync.processFile(self._arg.incomming_folder) is False:
self._log.debug('All files synchronized!')
return True
except UserWarning as e:
self._log.error('Error while syncing data! {}'.format(e.message))
return False
def getParsedArguments():
parser = argparse.ArgumentParser(
description='Waits till defined camera is available and copy all new images to defined foler.', \
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--incomming_folder', default='incomming_folder', \
help='directory where received images are placed')
parser.add_argument('--datafile', default='copied_images.txt', \
help='file containing all allready copied images')
parser.add_argument('-c', '--create_datafile', default=False, action='store_true',
help='Creates a new empty datafile')
parser_cam = parser.add_argument_group('CameraConfiguration', 'Arguments affecting camera.')
parser_cam.add_argument('--ip', default='192.168.1.120', \
help='Cameras IP address')
parser_cam.add_argument('--model', default='Sony PTP', \
help='Camera model')
return parser.parse_args()
def is_valid_directory(dir, text, create=False):
if create and not os.path.exists(dir):
os.mkdir(dir)
if not os.path.isdir(dir):
print('Error: {} ({})'.format(text, dir))
sys.exit(1)
def is_valid_file(file, text):
if not os.path.isfile(file):
print('Error: {} ({})'.format(text, file))
sys.exit(1)
def create_datafile(file, create):
if create:
with open(file, 'w') as f:
f.write('SyncCamera/Datafile\n')
if __name__ == "__main__":
s = SyncCamera()
sys.exit(s.run())