Example #1
0
    def sync(self):
        self.dao = LycheeDAO(self.conf)

        path = self.conf["srcdir"]
        event_handler = GalleryHandler(self)
        observer = Observer()
        observer.schedule(event_handler, path, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
Example #2
0
    def sync(self):
        """
        Program main loop
        Scans files to add in the sourcedirectory and add them to Lychee
        according to the conf file and given parameters
        Returns nothing
        """

        # Connect db
        # and drop it if dropdb activated
        self.dao = LycheeDAO(self.conf)

        if self.conf['dropdb']:
            self.deleteAllFiles()

        # Load db

        createdalbums = 0
        discoveredphotos = 0
        importedphotos = 0
        album = {}
        albums = []

        ignored = [];

        album_name_max_width = self.dao.getAlbumNameDBWidth()

        # walkthroug each file / dir of the srcdir
        for root, dirs, files in os.walk(self.conf['srcdir']):

            # Init album data
            album['id'] = None
            album['name'] = None
            album['path'] = None
            album['relpath'] = None  # path relative to srcdir
            album['photos'] = []  # path relative to srcdir

            # if a there is at least one photo in the files
            if any([self.isAPhoto(f) for f in files]):
                album['path'] = root

                if "ignore" in self.conf and root.endswith(tuple(self.conf["ignore"])):
                    msg = ("WARN: directory is ignored"), root
                    print msg
                    ignored.append(root)
                    continue

                if ".lycheeignore" in files:
                    msg = ("WARN: directory ignored by .lycheeignore"), root
                    print msg
                    ignored.append(root)
                    continue

                if root.startswith(tuple(ignored)):
                    msg = ("WARN: directory is a child of an ignored directory"), root
                    print msg
                    continue

                # don't know what to do with theses photo
                # and don't wan't to create a default album
                if album['path'] == self.conf['srcdir']:
                    msg = ("WARN: file at srcdir root won't be added to lychee, " +
                           "please move them in a subfolder"), os.path.join(root, f)
                    print msg
                    continue

                # Fill in other album properties
                # albumnames start at srcdir (to avoid absolute path albumname)
                album['relpath'] = os.path.relpath(album['path'], self.conf['srcdir'])
                album['name'] = self.getAlbumNameFromPath(album)

                if len(album['name']) > album_name_max_width:
                    print "WARN: album name too long, will be truncated " + album['name']
                    album['name'] = album['name'][0:album_name_max_width]
                    if self.conf['verbose']:
                        print "WARN: album name is now " + album['name']

                album['id'] = self.dao.albumExists(album)

                if not(album['id']):
                    # create album
                    album['id'] = self.createAlbum(album)
                    # TODO go to next album if it fails
                    if not(album['id']):
                        print "ERROR didn't manage to create album for: " + album['relpath']
                        continue

                    createdalbums += 1
                elif self.conf['replace']:
                    # drop album photos
                    filelist = self.dao.eraseAlbum(album)
                    self.deleteFiles(filelist)

                # Albums are created or emptied, now take care of photos
                for f in files:
                    if self.isAPhoto(f):

                        try:
                            discoveredphotos += 1
                            photo = LycheePhoto(self.conf, f, album)

                            if not(self.dao.photoExists(photo)):
                                if self.conf['verbose']:
                                    print "INFO: adding to lychee", os.path.join(root, f)
                                self.makeThumbnail(photo)
                                res = self.addFileToAlbum(photo)
                                self.adjustRotation(photo)
                                # increment counter
                                if res:
                                    importedphotos += 1
                                # report
                                if self.conf['verbose']:
                                    if res:
                                        album['photos'].append(photo)
                                    else:
                                        print "ERROR: while adding to lychee", os.path.join(root, f)
                            else:
                                if self.conf['verbose']:
                                    print "WARN: photo already exists in lychee with same name or same checksum: ", photo.srcfullpath
                        except Exception:
                            print "ERROR could not add " + str(f) + " to album " + album['name']
                            traceback.print_exc()

                a = album.copy()
                albums.append(a)

        self.updateAlbumsDate(albums)
        if self.conf['sort']:
            self.reorderalbumids(albums)
            self.dao.reinitAlbumAutoIncrement()
        self.dao.close()

        # Final report
        print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        print "Directory scanned:", self.conf['srcdir']
        print "Created albums: ", str(createdalbums)
        if (importedphotos == discoveredphotos):
            print str(importedphotos), "photos imported on", str(discoveredphotos), "discovered"
        else:
            print 'ERROR: ' + str(importedphotos), "photos imported on", str(discoveredphotos), "discovered"
        print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"