Exemplo n.º 1
0
    def test_not_rule_roundtrip_through_astformatter(self):
        yaml_data = dict(
            filters=[dict(
                filter={
                    'foo': 'bar',
                    'not': {
                        'any': [
                            dict(baz='quux'),
                            dict(fleem='morx'),
                        ]
                    }
                },
                min_zoom=7,
                output=dict(kind='triggered')
            )],
        )
        from vectordatasource.meta.python import make_empty_ast_state
        from vectordatasource.meta.python import output_kind
        from vectordatasource.meta.python import make_function_name_props
        from vectordatasource.meta.python import parse_layer_from_yaml
        ast_state = make_empty_ast_state()
        ast_fn = parse_layer_from_yaml(
            ast_state, yaml_data, 'fn_name', output_kind,
            make_function_name_props)

        # first check that if we compile the function from the ast, we
        # get an expected result
        import ast
        mod = ast.Module([ast_fn])
        mod_with_linenos = ast.fix_missing_locations(mod)
        code = compile(mod_with_linenos, '<string>', 'exec')
        scope = {}
        exec code in scope
        fn = scope['fn_name_props']

        shape = None
        props = dict(some='value')
        fid = 42
        meta = make_test_metadata()
        result = fn(shape, props, fid, meta)
        self.assertIsNone(result)

        # now, round trip it through the ast formatter
        # and see if we get the same result
        import astformatter
        formatter = astformatter.ASTFormatter()
        code_str = formatter.format(ast_fn, mode='exec')

        mod = ast.parse(code_str)
        mod_with_linenos = ast.fix_missing_locations(mod)
        code = compile(mod_with_linenos, '<string>', 'exec')
        scope = {}
        exec code in scope
        fn = scope['fn_name_props']

        result = fn(shape, props, fid, meta)
        self.assertIsNone(result)
Exemplo n.º 2
0
    def test_not_rule_roundtrip_through_astformatter(self):
        yaml_data = dict(
            filters=[dict(
                filter={
                    'foo': 'bar',
                    'not': {
                        'any': [
                            dict(baz='quux'),
                            dict(fleem='morx'),
                        ]
                    }
                },
                min_zoom=7,
                output=dict(kind='triggered')
            )],
        )
        from vectordatasource.meta.python import FilterCompiler
        from vectordatasource.meta.python import create_matcher
        from vectordatasource.meta.python import output_kind

        matchers = []
        for yaml_datum in yaml_data['filters']:
            matcher = create_matcher(yaml_datum, output_kind)
            matchers.append(matcher)

        fc = FilterCompiler()
        ast_fn, compiled_fn = fc.compile(matchers, 'fn_name_props')

        shape = None
        props = dict(some='value')
        fid = 42
        meta = make_test_metadata()
        result = compiled_fn(shape, props, fid, meta)
        self.assertIsNone(result)

        # now, round trip it through the ast formatter
        # and see if we get the same result
        import astformatter
        formatter = astformatter.ASTFormatter()
        code_str = formatter.format(ast_fn, mode='exec')

        import ast
        mod = ast.parse(code_str)
        mod_with_linenos = ast.fix_missing_locations(mod)
        code = compile(mod_with_linenos, '<string>', 'exec')
        scope = {}
        exec code in scope
        fn = scope['fn_name_props']

        result = fn(shape, props, fid, meta)
        self.assertIsNone(result)
Exemplo n.º 3
0
def main(argv=None):
    from vectordatasource.meta import find_yaml_path
    if argv is None:
        argv = sys.argv
    yaml_path = find_yaml_path()

    formatter = astformatter.ASTFormatter()

    all_layer_data = []
    for output_fn, make_fn_name in ((output_kind, make_function_name_props),
                                    (output_min_zoom,
                                     make_function_name_min_zoom)):
        layer_parse_result = parse_layers(yaml_path, output_fn, make_fn_name)
        all_layer_data.append(layer_parse_result.layer_data)

    for import_ast in layer_parse_result.import_asts:
        print formatter.format(import_ast, mode='exec')

    for layer_data in all_layer_data:
        for layer_datum in layer_data:
            ast_fn = layer_datum.ast
            print formatter.format(ast_fn, mode='exec')