Esempio n. 1
0
def run_commands(commands, callback):
    '''
    For example::

        run_commands(['a.yaml', 'b.yaml', '--x=1'], method)

    will do the following:

    - Load a.yaml into config
        - Set config['a'] = 1
        - Change to directory where a.yaml is
        - Call method(config)
    - Load b.yaml into config
        - Set config['a'] = 1
        - Change to directory where b.yaml is
        - Call method(config)

    Command line arguments are passed as ``commands``.
    Callback is a function that is called for each config file.
    '''
    args = parse_command_line(commands)
    original_path = os.getcwd()
    for config_file in args.pop('_'):
        config = gramex.cache.open(config_file, 'config')
        config = merge(old=config, new=args, mode='overwrite')
        os.chdir(os.path.dirname(os.path.abspath(config_file)))
        try:
            callback(**config)
        finally:
            os.chdir(original_path)
Esempio n. 2
0
def commandline(args=None):
    '''
    usage: slidesense [config.yaml] [url-name] [--source=...] [--target=...] [--data=...]

    Generates target PPTX from a source PPTX, applying rules in config file and opens it.
    If no config file is specified, uses `gramex.yaml` in the current directory.

    The config file can have a pptgen configuration like {source: ..., target: ..., rules: ...}
    or be a `gramex.yaml` with `url: {url-name: {handler: PPTXHandler, kwargs: {source: ...}}}`
    Rules are picked up from the first PPTXHandler URL that matches `url-name`,
    or the first PPTXHandler in `gramex.yaml`.

    --source=... overrides source PPTX path in config file
    --target=... overrides target PPTX path in config file (defaults to output.pptx)
    --data=...   overrides data file in config path
    --no-open    don't open target PPTX after generating it
    '''
    args = gramex.parse_command_line(sys.argv[1:] if args is None else args)

    if 'help' in args or (not args['_'] and not os.path.exists('gramex.yaml')):
        return gramex.console(dedent(commandline.__doc__).strip())

    config_file, *urls = args.pop('_') or ['gramex.yaml']
    conf = gramex.cache.open(config_file, 'config')

    if 'url' in conf:
        for key, spec in conf.url.items():
            if spec.handler == 'PPTXHandler':
                if not urls or any(url in key for url in urls):
                    rules = spec.kwargs
                    break
        else:
            return app_log.error(
                f'No PPTXHandler matched in file: {config_file}')
    elif any(key in conf for key in ('source', 'target', 'data', 'rules')):
        rules = conf
    else:
        return app_log.error(f'No rules found in file: {config_file}')

    gramex.config.merge(rules, args)
    rules.setdefault('target', 'output.pptx')
    rules.setdefault('mode', 'expr')
    # Allow importing python files in current directory
    sys.path.append('.')
    # Generate output
    gramex.pptgen2.pptgen(**rules)
    # If --no-open is specified, or the OS doesn't have startfile (e.g. Linux), stop here.
    # Otherwise, open the output PPTX created
    if not rules.get('no-open', False) and hasattr(os, 'startfile'):
        # os.startfile() is safe since the target is an explicit file we've created
        os.startfile(rules['target'])  # nosec
 def eq(cmd, value):
     value.setdefault('_', [])
     return self.assertEqual(parse_command_line(cmd), value)