class MyEventHandler(FileSystemEventHandler): def catch_all_handler(self, event): return def on_moved(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: albSrc = getAlbum(self, event.src_path) albDest = getAlbum(self, event.dest_path) logger.info("%s Album moved to %s. ", event.src_path, event.dest_path) self.dao.setAlbumParentAndTitle(albDest['name'], albDest['parent'], albSrc['id']) return else: if match_path( event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) dirs2 = event.dest_path.split(os.sep) albDir2 = os.sep.join(dirs2[:-1]) album = getAlbum(self, albDir) if album['id'] == None: album = getAlbum(self, albDir2) dbPhoto = self.dao.get_photo_light(album['id'], os.sep.join(dirs[-1:]), "") album2 = getAlbum(self, albDir2) logger.info("%s Photo moved to %s. ", event.src_path, event.dest_path) self.dao.setPhotoAlbumAndTitle(os.sep.join(dirs2[-1:]), album2['id'], dbPhoto['id']) return def on_created(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: album = getAlbum(self, event.src_path) logger.info("Created album: %s.", album['name']) self.dao.createAlbum(album) return else: if match_path( event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) if not (self.dao.photoExists(photo)): res = copyFileToLychee(self, photo) adjustRotation(self, photo) makeThumbnail(self, photo) res = self.dao.addFileToAlbum(photo) logger.info("Created Photo: %s.", photo.srcfullpath) # increment counter if not res: logger.error("while adding to album: %s photo: %s", album['name'], photo.srcfullpath) else: logger.error( "photo already exists in this album with same name or same checksum: %s it won't be added to lychee", photo.srcfullpath) return def on_deleted(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: album = getAlbum(self, event.src_path) if album['id'] is not None: filelist = self.dao.eraseAlbum(album['id']) deleteFiles(self, filelist) logger.info("Deleted album: %s.", album['name']) assert self.dao.dropAlbum(album['id']) else: logger.error( "Tried to delete album: %s, but it wasn't present in the DB", album['name']) return else: if match_path( event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir dbPhoto = self.dao.get_photo_light(album['id'], os.sep.join(dirs[-1:]), "") if dbPhoto is not None: delete = [dbPhoto] deletePhotos(self, delete) logger.info("Deleted Photo: %s.", os.sep.join(dirs[-1:])) else: logger.info( "Tried to delete Photo: %s, but it wasn't in the database.", os.sep.join(dirs[-1:])) return def on_modified(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: return else: if match_path( event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) dbPhoto = self.dao.get_photo(photo) if dbPhoto is not None: delete = [dbPhoto] deletePhotos(self, delete) dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) if not (self.dao.photoExists(photo)): res = copyFileToLychee(self, photo) adjustRotation(self, photo) makeThumbnail(self, photo) res = self.dao.addFileToAlbum(photo) logger.info("Modified Photo: %s.", photo.srcfullpath) # increment counter if not res: logger.error("while adding to album: %s photo: %s", album['name'], photo.srcfullpath) else: logger.error( "photo already exists in this album with same name or same checksum: %s it won't be added to lychee", photo.srcfullpath) return
class MyEventHandler(FileSystemEventHandler): def catch_all_handler(self, event): return def on_moved(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: albSrc = getAlbum(self, event.src_path) albDest = getAlbum(self, event.dest_path) logger.info("%s Album moved to %s. ", event.src_path, event.dest_path) self.dao.setAlbumParentAndTitle(albDest['name'], albDest['parent'], albSrc['id']) return else: if match_path(event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) dirs2 = event.dest_path.split(os.sep) albDir2 = os.sep.join(dirs2[:-1]) album = getAlbum(self, albDir) if album['id'] == None: album = getAlbum(self, albDir2) dbPhoto = self.dao.get_photo_light(album['id'], os.sep.join(dirs[-1:]), "") album2 = getAlbum(self, albDir2) logger.info("%s Photo moved to %s. ", event.src_path, event.dest_path) self.dao.setPhotoAlbumAndTitle(os.sep.join(dirs2[-1:]), album2['id'], dbPhoto['id']) return def on_created(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: album = getAlbum(self, event.src_path) logger.info("Created album: %s.", album['name']) self.dao.createAlbum(album) return else: if match_path(event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) if not (self.dao.photoExists(photo)): res = copyFileToLychee(self, photo) adjustRotation(self, photo) makeThumbnail(self, photo) res = self.dao.addFileToAlbum(photo) logger.info("Created Photo: %s.", photo.srcfullpath) # increment counter if not res: logger.error( "while adding to album: %s photo: %s", album['name'], photo.srcfullpath) else: logger.error( "photo already exists in this album with same name or same checksum: %s it won't be added to lychee", photo.srcfullpath) return def on_deleted(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: album = getAlbum(self, event.src_path) if album['id'] is not None: filelist = self.dao.eraseAlbum(album['id']) deleteFiles(self, filelist) logger.info("Deleted album: %s.", album['name']) assert self.dao.dropAlbum(album['id']) else: logger.error("Tried to delete album: %s, but it wasn't present in the DB", album['name']) return else: if match_path(event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir dbPhoto = self.dao.get_photo_light(album['id'], os.sep.join(dirs[-1:]), "") if dbPhoto is not None: delete = [dbPhoto] deletePhotos(self, delete) logger.info("Deleted Photo: %s.", os.sep.join(dirs[-1:])) else: logger.info("Tried to delete Photo: %s, but it wasn't in the database.", os.sep.join(dirs[-1:])) return def on_modified(self, event): borg = ConfBorg() self.conf = borg.conf self.dao = LycheeDAO(self.conf) if event.is_directory: return else: if match_path(event.src_path, included_patterns=['*.jpg', '*.jpeg', '*.gif', '*.png'], excluded_patterns=None, case_sensitive=False): dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) dbPhoto = self.dao.get_photo(photo) if dbPhoto is not None: delete = [dbPhoto] deletePhotos(self, delete) dirs = event.src_path.split(os.sep) albDir = os.sep.join(dirs[:-1]) album = getAlbum(self, albDir) album['path'] = albDir photo = LycheePhoto(self.conf, os.sep.join(dirs[-1:]), album) if not (self.dao.photoExists(photo)): res = copyFileToLychee(self, photo) adjustRotation(self, photo) makeThumbnail(self, photo) res = self.dao.addFileToAlbum(photo) logger.info("Modified Photo: %s.", photo.srcfullpath) # increment counter if not res: logger.error( "while adding to album: %s photo: %s", album['name'], photo.srcfullpath) else: logger.error( "photo already exists in this album with same name or same checksum: %s it won't be added to lychee", photo.srcfullpath) return