示例#1
0
文件: importer.py 项目: NaPs/Kolekto
    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))
示例#2
0
文件: edit.py 项目: 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
示例#3
0
文件: config.py 项目: NaPs/Kolekto
 def run(self, args, config):
     config_fullname = os.path.join(args.tree, '.kolekto', 'config')
     with open(config_fullname, 'r+') as fconfig:
         config = printer.edit(fconfig.read())
         fconfig.seek(0)
         fconfig.truncate()
         fconfig.write(config)
     printer.p('Saved.')
示例#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)