How to use the gramex.config.merge function in gramex

To help you get started, we’ve selected a few gramex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gramener / gramex / tests / test_formhandler.py View on Github external
self.check('/formhandler/edits-multidata', method='post', data={
                'csv:देश': ['भारत'],
                'csv:city': ['X'],
                'csv:product': ['Q'],
                'csv:sales': ['10'],
                'sql:देश': ['भारत'],
                'sql:city': ['X'],
                'sql:product': ['Q'],
                'sql:sales': ['20'],
            }, headers={
                'count-csv': '1',
                'count-sql': '1',
            })
            data = self.check('/formhandler/edits-multidata').json()
            eq_(data['csv'][-1], merge(row, {'sales': 10}))
            eq_(data['sql'][-1], merge(row, {'sales': 20}))
            eq_(len(data['csv']), len(self.sales) + 1)
            eq_(len(data['sql']), len(self.sales) + 1)

            self.check('/formhandler/edits-multidata', method='put', data={
                'csv:city': ['X'],
                'csv:product': ['Q'],
                'csv:sales': ['30'],
                'sql:city': ['X'],
                'sql:product': ['Q'],
                'sql:sales': ['40'],
            }, headers={
                'count-csv': '1',
                'count-sql': '1',
            })
            data = self.check('/formhandler/edits-multidata').json()
            eq_(data['csv'][-1], merge(row, {'sales': 30}))
github gramener / gramex / testlib / test_config / __init__.py View on Github external
def check(a, b, c, mode='overwrite'):
            '''Check if merge(a, b) is c. Parameters are in YAML'''
            old = yaml.load(a, Loader=ConfigYAMLLoader)
            new = yaml.load(b, Loader=ConfigYAMLLoader)
            # merging a + b gives c
            eq_(
                yaml.load(c, Loader=ConfigYAMLLoader),
                merge(old, new, mode))
            # new is unchanged
            # eq_(old, yaml.load(a, Loader=ConfigYAMLLoader))
            eq_(new, yaml.load(b, Loader=ConfigYAMLLoader))
github gramener / gramex / testlib / test_config / __init__.py View on Github external
merge(old, new, mode))
            # new is unchanged
            # eq_(old, yaml.load(a, Loader=ConfigYAMLLoader))
            eq_(new, yaml.load(b, Loader=ConfigYAMLLoader))

        check('x: 1', 'y: 2', 'x: 1\ny: 2')
        check('x: {a: 1}', 'x: {a: 2}', 'x: {a: 2}')
        check('x: {a: 1}', 'x: null', 'x: null')
        check('x: {a: 1}', 'x: {b: 2}', 'x: {a: 1, b: 2}')
        check('x: {a: {p: 1}}', 'x: {a: {q: 1}, b: 2}', 'x: {a: {p: 1, q: 1}, b: 2}')
        check('x: {a: {p: 1}}', 'x: {a: null, b: null}', 'x: {a: null, b: null}')
        check('x: 1', 'x: 2', 'x: 1', mode='underwrite')
        check('x: {a: 1, c: 3}', 'x: {a: 2, b: 2}', 'x: {a: 1, c: 3, b: 2}', mode='underwrite')

        # Check basic behaviour
        eq_(merge({'a': 1}, {'a': 2}), {'a': 2})
        eq_(merge({'a': 1}, {'a': 2}, mode='setdefault'), {'a': 1})
        eq_(merge({'a': {'b': 1}}, {'a': {'b': 2}}), {'a': {'b': 2}})
        eq_(merge({'a': {'b': 1}}, {'a': {'b': 2}}, mode='setdefault'), {'a': {'b': 1}})

        # Ensure int keys will work
        eq_(merge({1: {1: 1}}, {1: {1: 2}}), {1: {1: 2}})
        eq_(merge({1: {1: 1}}, {1: {1: 2}}, mode='setdefault'), {1: {1: 1}})
github gramener / gramex / tests / test_formhandler.py View on Github external
'csv:product': ['Q'],
                'csv:sales': ['10'],
                'sql:देश': ['भारत'],
                'sql:city': ['X'],
                'sql:product': ['Q'],
                'sql:sales': ['20'],
            }, headers={
                'count-csv': '1',
                'count-sql': '1',
            }).json()
            eq_(result['csv']['modify'], 8)
            eq_(result['modify'], 8)

            data = self.check('/formhandler/edits-multidata').json()
            eq_(data['csv'][-1], merge(row, {'sales': 10}))
            eq_(data['sql'][-1], merge(row, {'sales': 20}))
            eq_(len(data['csv']), len(self.sales) + 1)
            eq_(len(data['sql']), len(self.sales) + 1)

        finally:
            dbutils.mysql_drop_db(variables.MYSQL_SERVER, 'test_formhandler')
github gramener / gramex / gramex / handlers / formhandler.py View on Github external
args.update(json.loads(self.request.body))
        filter_kwargs = AttrDict(dataset)
        filter_kwargs.pop('modify', None)
        prepare = filter_kwargs.pop('prepare', None)
        queryfunction = filter_kwargs.pop('queryfunction', None)
        filter_kwargs['transform_kwargs'] = {'handler': self}
        # Use default arguments
        defaults = {
            k: v if isinstance(v, list) else [v]
            for k, v in filter_kwargs.pop('default', {}).items()
        }
        # /(.*)/(.*) become 2 path arguments _0 and _1
        defaults.update({'_%d' % k: [v] for k, v in enumerate(path_args)})
        # /(?P\d+)/(?P\d+) become 2 keyword arguments x and y
        defaults.update({k: [v] for k, v in path_kwargs.items()})
        args = merge(namespaced_args(args, key), defaults, mode='setdefault')
        if callable(prepare):
            result = prepare(args=args, key=key, handler=self)
            if result is not None:
                args = result
        if callable(queryfunction):
            filter_kwargs['query'] = queryfunction(args=args, key=key, handler=self)
        return AttrDict(
            fmt=args.pop('_format', ['json'])[0],
            download=args.pop('_download', [''])[0],
            args=args,
            meta_header=args.pop('_meta', [''])[0],
            filter_kwargs=filter_kwargs,
        )
github gramener / gramex / gramex / apps / ui / __init__.py View on Github external
def sass(handler, template=uicomponents_path):
    '''
    Return a bootstrap theme based on the custom SASS variables provided.
    '''
    args = dict(variables.get('ui-bootstrap', {}))
    args.update({key: handler.get_arg(key) for key in handler.args})
    args = {key: val for key, val in args.items() if val}

    # Set default args
    config = gramex.cache.open(config_file)
    merge(args, config.get('defaults'), mode='setdefault')

    cache_key = {'template': template, 'args': args}
    cache_key = json.dumps(
        cache_key, sort_keys=True, ensure_ascii=True).encode('utf-8')
    cache_key = md5(cache_key).hexdigest()[:5]

    # Replace fonts from config file, if available
    google_fonts = set()
    for key in ('font-family-base', 'headings-font-family'):
        if key in args and args[key] in config['fonts']:
            fontinfo = config['fonts'][args[key]]
            args[key] = fontinfo['stack']
            if 'google' in fontinfo:
                google_fonts.add(fontinfo['google'])

    # Cache based on the dict and config as template..css
github gramener / gramex / gramex / handlers / formhandler.py View on Github external
if 'url' in conf_kwargs:
            cls.datasets = {'data': conf_kwargs}
            cls.single = True
        else:
            if 'modify' in conf_kwargs:
                cls.modify_all = staticmethod(build_transform(
                    conf={'function': conf_kwargs.pop('modify', None)},
                    vars=cls.function_vars['modify'],
                    filename='%s.%s' % (cls.name, 'modify'), iter=False))
            cls.datasets = conf_kwargs
            cls.single = False
        # Apply defaults to each key
        if isinstance(default_config, dict):
            for key in cls.datasets:
                config = cls.datasets[key].get('default', {})
                cls.datasets[key]['default'] = merge(config, default_config, mode='setdefault')
        # Ensure that each dataset is a dict with a url: key at least
        for key, dataset in list(cls.datasets.items()):
            if not isinstance(dataset, dict):
                app_log.error('%s: %s: must be a dict, not %r' % (cls.name, key, dataset))
                del cls.datasets[key]
            elif 'url' not in dataset:
                app_log.error('%s: %s: does not have a url: key' % (cls.name, key))
                del cls.datasets[key]
            # Ensure that id: is a list -- if it exists
            if 'id' in dataset and not isinstance(dataset['id'], list):
                dataset['id'] = [dataset['id']]
            # Convert function: into a data = transform(data) function
            conf = {
                'function': dataset.pop('function', None),
                'args': dataset.pop('args', None),
                'kwargs': dataset.pop('kwargs', None)
github gramener / gramex / gramex / pptgen / __init__.py View on Github external
- 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)