Exemplo n.º 1
0
def script_init(cli_args):
    """
    - will initialize a ConfBorg object containing cli arguments, configutation file elements
    - will initialize loggers
    """

    root_level = ".."

    # compute log file absolute path
    pathname = os.path.dirname(sys.argv[0])
    full_path = os.path.abspath(pathname)

    # root level is different if main.py or sync.py is used to launch script
    log_conf_path = os.path.join(full_path, root_level, "ressources",
                                 'logging.json')
    log_conf_path2 = os.path.join(full_path, "ressources", 'logging.json')

    # append path to configuration
    cli_args['full_path'] = full_path

    # read log configuration
    if os.path.exists(log_conf_path):
        init_loggers(log_conf_path, cli_args['verbose'])
    elif os.path.exists(log_conf_path2):
        init_loggers(log_conf_path2, cli_args['verbose'])
    else:
        # default value
        logging.basicConfig(level=logging.DEBUG)
        logging.warning("**** logging conf -> default conf")

    # read application configuration
    if os.path.exists(cli_args['confpath']):
        with open(cli_args['confpath'], 'rt') as f:
            conf = json.load(f)
    else:
        logger.warn("**** Loading default conf in ressources/conf.json")
        conf_path = os.path.join(full_path, root_level, "ressources",
                                 'conf.json')
        if os.path.exists(conf_path):
            with open(conf_path, 'rt') as f:
                conf = json.load(f)

    # initialize conf with items loaded from conf file AND command lines arguments
    # cli args have priority over configuration file
    z = conf.copy()
    z.update(cli_args)
    borg = ConfBorg(z)
    logger.debug("**** loaded configuration: ")
    logger.debug("**** " + borg.pretty)
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
 def __init__(self):
     """
     Takes a dictionnary of conf as input
     """
     borg = ConfBorg()
     self.conf = borg.conf
Exemplo n.º 6
0
 def __init__(self):
     self.db = None
     self.cb = TestBorg()
     self._lychee_sync_conf_borg = ConfBorg(force_init=True)