class FolderSource(object): image_types = ['.' + x for x in image_types_lower() + video_types_lower()] def __init__(self, root): self.root = root def get_file_data(self): if not os.path.isdir(self.root): return None file_list = [] for root, dirs, files in os.walk(self.root): # ignore special directories such as .thumbs dirs[:] = [x for x in dirs if x[0] != '.'] for name in files: base, ext = os.path.splitext(name) if ext.lower() in self.image_types: file_list.append(os.path.join(root, name)) file_data = {} for path in file_list: metadata = Metadata(path) timestamp = metadata.date_taken if not timestamp: timestamp = metadata.date_digitised if not timestamp: timestamp = metadata.date_modified if not timestamp: # use file date as last resort timestamp = datetime.fromtimestamp(os.path.getmtime(path)) else: timestamp = timestamp['datetime'] sc_path = metadata.find_sidecar() name = os.path.basename(path) camera = metadata.camera_model if camera: camera = camera['model'] file_data[name] = { 'camera': camera, 'path': path, 'sc_path': sc_path, 'name': name, 'timestamp': timestamp, } return file_data def copy_files(self, info_list, move): for info in info_list: dest_path = info['dest_path'] dest_dir = os.path.dirname(dest_path) if not os.path.isdir(dest_dir): os.makedirs(dest_dir) sc_file = info['sc_path'] if move: shutil.move(info['path'], dest_path) if sc_file: shutil.move(sc_file, dest_path + '.xmp') else: shutil.copy2(info['path'], dest_path) if sc_file: shutil.copy2(sc_file, dest_path + '.xmp') yield info
class FolderSource(object): image_types = ['.' + x for x in image_types_lower() + video_types_lower()] def __init__(self, root): self.root = root def get_file_data(self): if not os.path.isdir(self.root): return None file_list = [] for root, dirs, files in os.walk(self.root): for name in files: base, ext = os.path.splitext(name) if ext.lower() in self.image_types: file_list.append(os.path.join(root, name)) file_data = {} for path in file_list: metadata = Metadata(path) timestamp = metadata.date_taken if not timestamp: timestamp = metadata.date_digitised if not timestamp: timestamp = metadata.date_modified if not timestamp: # use file date as last resort timestamp = datetime.fromtimestamp(os.path.getmtime(path)) else: timestamp = timestamp.datetime name = os.path.basename(path) file_data[name] = { 'camera' : metadata.camera_model, 'path' : path, 'name' : name, 'timestamp' : timestamp, } return file_data def read_files(self, info_list): for info in info_list: if not os.path.isfile(info['path']): yield info, None, 'not a file' break dest_path = info['dest_path'] dest_dir = os.path.dirname(dest_path) if not os.path.isdir(dest_dir): os.makedirs(dest_dir) shutil.copy2(info['path'], dest_path) yield info, None, 'ok'
class CameraSource(object): image_types = ['.' + x for x in image_types_lower() + video_types_lower()] def __init__(self, model, port_name): self.model = model self.port_name = port_name @contextmanager def session(self): # initialise camera camera = gp.Camera() # search ports for camera port name port_info_list = gp.PortInfoList() port_info_list.load() idx = port_info_list.lookup_path(self.port_name) camera.set_port_info(port_info_list[idx]) camera.init() # check camera is the right model if camera.get_abilities().model != self.model: raise RuntimeError('Camera model mismatch') try: yield camera finally: camera.exit() def _list_files(self, camera, path='/'): result = [] # get files for name, value in camera.folder_list_files(path): base, ext = os.path.splitext(name) if ext.lower() in self.image_types: result.append(os.path.join(path, name)) # get folders folders = [] for name, value in camera.folder_list_folders(path): folders.append(name) # recurse over subfolders for name in folders: result.extend(self._list_files(camera, os.path.join(path, name))) return result def get_file_data(self): with self.session() as camera: try: file_list = self._list_files(camera) except gp.GPhoto2Error: # camera is no longer visible return None file_data = {} for path in file_list: folder, name = os.path.split(path) try: info = camera.file_get_info(str(folder), str(name)) except gp.GPhoto2Error: return None timestamp = datetime.utcfromtimestamp(info.file.mtime) file_data[name] = { 'camera': self.model, 'folder': folder, 'name': name, 'timestamp': timestamp, } return file_data def copy_files(self, info_list, move): with self.session() as camera: for info in info_list: dest_dir = os.path.dirname(info['dest_path']) if not os.path.isdir(dest_dir): os.makedirs(dest_dir) camera_file = camera.file_get(info['folder'], info['name'], gp.GP_FILE_TYPE_NORMAL) camera_file.save(info['dest_path']) if move: camera.file_delete(info['folder'], info['name']) yield info