Пример #1
0
    def can_handle(fname):
        """Return a score between 0 and 100.

        100 is for the best match (specific extension), 0 is for no match at all.
        """
        if helper.is_video_file(fname):
            return 80
        return 0
Пример #2
0
 def can_handle(fname):
     ext = os.path.splitext(fname)[1].lower()
     if ext in ('.txt', '.log'):
         return 100
     elif ext == '.gz':
         return 50
     elif helper.is_video_file(fname):
         return 0
     else:
         # It may handle any type of file ?
         return 1
Пример #3
0
def main():
    logging.basicConfig(level=logging.INFO)
    if os.environ.get('ADVENE_DEBUG'):
        LOGGING = { 'version': 1,
                    'disable_existing_loggers': False,
                    'loggers': { } }
        # Handle ADVENE_DEBUG variable.
        for m in os.environ.get('ADVENE_DEBUG', '').split(':'):
            LOGGING['loggers'][m] = { 'level': 'DEBUG' }
            LOGGING['loggers'][m.replace('.', '_')] = { 'level': 'DEBUG' }
            # Plugin package name can be mangled
            if '.plugins' in m:
                LOGGING['loggers'][m.replace('.', '_').replace('_plugins_', '_app_plugins_')] = { 'level': 'DEBUG' }
        logging.config.dictConfig(LOGGING)

    USAGE = f"{sys.argv[0]} [-o filter_options] filter_name input_file [output_file]"

    import advene.core.controller as controller
    # The controller will import (as a module) advene.util.importer,
    # which will be different from __main__. So local IMPORTERS
    # variable will be different from advene.util.importer.IMPORTERS
    # Fix this situation by restoring appropriate IMPORTERS reference:
    IMPORTERS = controller.advene.util.importer.IMPORTERS

    # Basic controller initialization - load plugins
    c = controller.AdveneController()
    c.init_plugins()

    if len(config.data.args) < 2:
        logger.info("""\n\nSyntax: %s

filter_name can be "auto" for autodetection (first valid importer),
"list" for a list of valid importers for the file

Use "-o help" as filter option for getting parameter help. Filter
parameters displayed as --long-option-name in the help message should
be entered as "-o long-option-name=value" (strip leading -- from
option and use one "-o" option per parameter).

A base template package can be specified using the "-o
template_package=path.azp" option. The given package will be loaded
before the import takes place.

If no output file is specified, then data will be dumped to stdout in a JSON format.

Available filters:
  * %s
        """ % (USAGE,
               "\n  * ".join(sorted(i.name.replace(' ', '_')
                                    for i in controller.advene.util.importer.IMPORTERS))))
        sys.exit(0)

    # Handle input parameters/options
    filtername = config.data.args[0].replace('_', ' ')
    inputfile = config.data.args[1]
    if config.data.args[2:]:
        outputfile = config.data.args[2]
    else:
        outputfile = ''

    # Filter options are passed using the -o option, but there is a
    # specific template_package option to specify initial template
    # packages
    template_package = config.data.options.options.get('template_package')
    # Rebuild filter option string from config.data.options.options dict
    option_list = [ (f"--{k}={v}" if v else f"--{k}")
                    for (k, v) in config.data.options.options.items()
                    if k != 'template_package' ]

    # If template_package is None, then the controller will use the
    # standard template package
    c.load_package(template_package)

    # If we are importing a video file, then set it as default media
    # for the package. This will also initialize the imagecache.
    if helper.is_video_file(inputfile):
        c.set_default_media(inputfile)

    def progress(value, label=""):
        sys.stderr.write('\rProgress %02d%% - %s' % (int(100 * value), label))
        return True

    if filtername == 'auto':
        i = get_importer(inputfile, package=c.package, controller=c, callback=progress)
    elif filtername == 'list':
        # List valid importer names for the given file
        valid, invalid = get_valid_importers(inputfile)
        validlist = '\n  * '.join(sorted(i.name for i in valid))
        logger.info(f"Valid importers for {inputfile}:\n  * {validlist}")
        sys.exit(1)
    else:
        i = None
        cl = [ f for f in controller.advene.util.importer.IMPORTERS if f.name.startswith(filtername) ]
        if len(cl) == 1:
            i = cl[0](package=c.package, controller=c, callback=progress)
        elif len(cl) > 1:
            logger.error("Too many possibilities:\n%s", "\n".join(f.name for f in cl))
            sys.exit(1)

    if i is None:
        logger.error("No matching importer starting with %s", filtername)
        sys.exit(1)

    i.optionparser.set_usage(USAGE)
    if option_list:
        args = i.process_options(option_list)

    # (for .sub conversion for instance, --fps, --offset)
    logger.info("Converting %s to %s using %s", inputfile, outputfile, i.name)

    # Serialize data as JSON to stdout
    def json_serialize(p, filename="-"):
        from advene.util.exporter import FlatJsonExporter
        e = FlatJsonExporter(controller=c)
        e.set_source(p)
        e.export(filename or '-')

    reqs = i.check_requirements()
    if reqs:
        # Not all requirements are met. Display some information.
        logger.error("The filter is not ready.\n%s" % "\n".join(reqs))
        sys.exit(1)

    if hasattr(i, 'async_process_file'):
        # async mode

        from gi.repository import GLib
        mainloop = GLib.MainLoop()
        def end_callback():
            if outputfile == '' or outputfile.endswith('.json'):
                json_serialize(i.package, outputfile)
            else:
                i.package.save(outputfile)
            mainloop.quit()
            return True
        i.async_process_file(inputfile, end_callback)
        mainloop.run()
    else:
        i.process_file(inputfile)
        if outputfile == '' or outputfile.endswith('.json'):
            json_serialize(i.package, outputfile)
        else:
            i.package.save(outputfile)

        logger.info(i.statistics_formatted())
    sys.exit(0)