Exemplo n.º 1
0
    def run(self, args, config):
        # Check the args:
        if args.symlink and args.delete:
            raise KolektoRuntimeError('--delete can\'t be used with --symlink')
        elif args.symlink and args.hardlink:
            raise KolektoRuntimeError('--symlink and --hardlink are mutually exclusive')

        # Load the metadata database:
        mdb = self.get_metadata_db(args.tree)

        # Load informations from db:
        mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class)

        attachment_store = AttachmentStore(os.path.join(args.tree, '.kolekto', 'attachments'))

        for filename in args.file:
            filename = filename.decode('utf8')
            movie = self._import(mdb, mds, args, config, filename)

            # Refresh the full data for the choosen movie:
            movie = mds.refresh(movie)

            # Append the import date
            movie['import_date'] = datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')

            if args.show:
                show(movie)
                printer.p('')

            # Edit available data:
            if not args.auto and printer.ask('Do you want to edit the movie metadata', default=False):
                movie = self.profile.object_class(json.loads(printer.edit(json.dumps(movie, indent=True))))

            # Hardlink or copy the movie in the tree
            if args.hardlink or args.symlink:
                printer.p('\nComputing movie sha1sum...')
                movie_hash = link(args.tree, filename, args.symlink)
            else:
                printer.p('\nCopying movie in kolekto tree...')
                movie_hash = copy(args.tree, filename)
            printer.p('')

            mdb.save(movie_hash, movie)
            printer.debug('Movie {hash} saved to the database', hash=movie_hash)

            if args.delete:
                os.unlink(filename)
                printer.debug('Deleted original file {filename}', filename=filename)

            # Import the attachments
            if movie_hash is not None and args.import_attachments:
                attachments = list_attachments(filename)
                if attachments:
                    printer.p('Found {nb} attachment(s) for this movie:', nb=len(attachments))
                    for attach in attachments:
                        printer.p(' - {filename}', filename=attach)
                    if not args.auto and printer.ask('Import them?', default=True):
                        for attach in attachments:
                            _, ext = os.path.splitext(attach)
                            attachment_store.store(movie_hash, ext.lstrip('.'), open(attach))
Exemplo n.º 2
0
    def run(self, args, config):
        mdb = self.get_metadata_db(args.tree)
        mds = MovieDatasource(config.subsections('datasource'), args.tree, self.profile.object_class)

        if args.input is None: # Refresh all movies
            if printer.ask('Would you like to refresh all movies?', default=True):
                with printer.progress(mdb.count(), task=True) as update:
                    for movie_hash, movie in list(mdb.itermovies()):
                        movie = mds.refresh(movie)
                        mdb.save(movie_hash, movie)
                        printer.verbose('Saved {hash}', hash=movie_hash)
                        update(1)
        else:
            movie_hash = get_hash(args.input)

            try:
                movie = mdb.get(movie_hash)
            except KeyError:
                printer.p('Unknown movie hash.')
                return
            else:
                movie = mds.refresh(movie)
                show(movie)
                if printer.ask('Would you like to save the movie?', default=True):
                    mdb.save(movie_hash, movie)
                    printer.p('Saved.')
Exemplo n.º 3
0
Arquivo: edit.py Projeto: NaPs/Kolekto
    def run(self, args, config):
        mdb = self.get_metadata_db(args.tree)

        movie_hash = get_hash(args.input)

        try:
            movie = mdb.get(movie_hash)
        except KeyError:
            printer.p('Unknown movie hash.')
            return

        movie_json = json.dumps(movie, indent=4)

        while True:
            movie_json = printer.edit(movie_json)

            try:
                mdb.save(movie_hash, json.loads(movie_json))
            except ValueError:
                if printer.ask('Bad json data, would you like to try again?', default=True):
                    continue
                else:
                    break
            else:
                printer.p('Saved.')
                break
Exemplo n.º 4
0
    def _import(self, mdb, mds, args, config, filename):
        printer.debug('Importing file {filename}', filename=filename)
        short_filename = os.path.basename(filename)
        title, ext = os.path.splitext(short_filename)

        year, title = clean_title(title)

        # Disable the year filter if auto mode is disabled:
        if not args.auto:
            year = None

        while True:
            # Disable the title input if auto mode is enabled:
            if not args.auto:
                title = printer.input(u'Title to search', default=title)
            datasource, movie = self._search(mds, title, short_filename, year, auto=args.auto)
            if datasource == 'manual':
                movie = self.profile.object_class
            elif datasource == 'abort':
                printer.p('Aborted import of {filename}', filename=filename)
                return
            break

        if datasource is None:
            return

        # Refresh the full data for the choosen movie:
        movie = mds.refresh(movie)

        if args.show:
            show(movie)
            printer.p('')

        # Edit available data:
        if not args.auto and printer.ask('Do you want to edit the movie metadata', default=False):
            movie = self.profile.object_class(json.loads(printer.edit(json.dumps(movie, indent=True))))

        # Hardlink or copy the movie in the tree
        if args.hardlink or args.symlink:
            printer.p('\nComputing movie sha1sum...')
            movie_hash = link(args.tree, filename, args.symlink)
        else:
            printer.p('\nCopying movie in kolekto tree...')
            movie_hash = copy(args.tree, filename)
        printer.p('')

        mdb.save(movie_hash, movie)
        printer.debug('Movie {hash} saved to the database', hash=movie_hash)

        if args.delete:
            os.unlink(filename)
            printer.debug('Deleted original file {filename}', filename=filename)
Exemplo n.º 5
0
Arquivo: rm.py Projeto: NaPs/Kolekto
    def run(self, args, config):
        mdb = self.get_metadata_db(args.tree)

        movie_hash = get_hash(args.input)

        try:
            mdb.get(movie_hash)
        except KeyError:
            printer.p('Unknown movie hash.')
            return

        if printer.ask('Are you sure?', default=False):
            mdb.remove(movie_hash)
            printer.p('Removed. You need to launch gc to free space.')
Exemplo n.º 6
0
Arquivo: gc.py Projeto: NaPs/Kolekto
 def run(self, args, config):
     mdb = self.get_metadata_db(args.tree)
     db_files = set()
     for movie_hash, movie in mdb.itermovies():
         db_files.add(movie_hash)
         db_files.update(movie.get("_externals", []))
     printer.verbose("Found {nb} files in database", nb=len(db_files))
     fs_files = set(os.listdir(os.path.join(args.tree, ".kolekto", "movies")))
     printer.verbose("Found {nb} files in filesystem", nb=len(fs_files))
     orphan_files = fs_files - db_files
     printer.p("Found {nb} orphan files to delete", nb=len(orphan_files))
     if orphan_files:
         printer.verbose("Files to delete: {files}", files=", ".join(orphan_files))
         if printer.ask("Would you like to delete orphans?"):
             for orphan_file in orphan_files:
                 try:
                     os.remove(os.path.join(args.tree, ".kolekto", "movies", orphan_file))
                 except OSError as err:
                     printer.p("Unable to delete {file}: {err}", file=orphan_file, err=err)
                 else:
                     printer.verbose("Deleted {file}", file=orphan_file)