def main(_, mapcss): path = os.path.dirname(mapcss) class_name = original_class_name = '.'.join(os.path.basename(mapcss).replace('.validator.', '.').split('.')[:-1]) global item_default, class_map, subclass_blacklist, class_index, meta_tags if class_name in item_map: i = item_map[class_name] item_default = i['item'] class_map = i.get('class', {}) subclass_blacklist = i.get('subclass_blacklist', []) only_for = i.get('only_for', []) not_for = i.get('not_for', []) prefix = i.get('prefix', '') class_index = max(class_map.values()) meta_tags = ('["' + ('", "').join(i.get('tags')) + '"]') if i.get('tags') else None else: item_default = 0 class_map = {} subclass_blacklist = [] only_for = [] not_for = [] prefix = '' class_index = item_default * 1000 meta_tags = None class_name = class_name.replace('.', '_').replace('-', '_') input = FileStream(mapcss, encoding='utf-8') lexer = MapCSSLexer(input) stream = CommonTokenStream(lexer) parser = MapCSSParser(stream) tree = parser.stylesheet() listener = MapCSSListenerL() walker = ParseTreeWalker() walker.walk(listener, tree) selectors_by_complexity = segregate_selectors_by_complexity(listener.stylesheet) if len(selectors_by_complexity['rules_complex']) > 0: print("W: Drop %d complex rules" % len(selectors_by_complexity['rules_complex'])) tree = rewrite_tree(selectors_by_complexity['rules_meta'] + selectors_by_complexity['rules_simple']) tree = filter_non_productive_rules(tree) tree = filter_osmose_none_rules(tree) selectors_type = segregate_selectors_type(tree) global class_, tests, regex_store, set_store rules = dict(map(lambda t: [t, to_p({'type': 'stylesheet', 'rules': selectors_type[t]})], sorted(selectors_type.keys(), key = lambda a: {'node': 0, 'way': 1, 'relation':2}[a]))) items = build_items(class_) asserts = build_tests(tests) mapcss = ("""#-*- coding: utf-8 -*- import modules.mapcss_lib as mapcss import regex as re from plugins.Plugin import Plugin, with_options class """ + prefix + class_name + """(Plugin): """ + ("\n only_for = ['" + "', '".join(only_for) + "']\n" if only_for != [] else "") + """ """ + ("\n not_for = ['" + "', '".join(not_for) + "']\n" if not_for != [] else "") + """ def init(self, logger): Plugin.init(self, logger) tags = capture_tags = {} """ + items.replace("\n", "\n ") + """ """ + "".join(map(lambda r: """ self.""" + r[1] + " = re.compile(ur'" + r[0].replace('(?U)', '').replace("'", "\\'") + "')", sorted(regex_store.items(), key = lambda s: s[1]))) + """ """ + "".join(map(lambda t: """ def """ + t + """(self, data, tags, *args): capture_tags = {} keys = tags.keys() err = [] """ + ((" = ".join(list(map(lambda s: "set_" + s, sorted(set_store)))) + " = False") if len(set_store) > 0 else "") + """ """ + rules[t].replace("\n", "\n ") + """ return err """, sorted(rules.keys(), key = lambda a: {'node': 0, 'way': 1, 'relation':2}[a]))) + """ from plugins.Plugin import TestPluginCommon class Test(TestPluginCommon): def test(self): n = """ + prefix + class_name + """(None) class _config: options = {"country": None, "language": None} class father: config = _config() n.father = father() n.init(None) data = {'id': 0, 'lat': 0, 'lon': 0} """ + asserts.replace("\n", "\n ") + """ """).replace(" \n", "\n") f = open((path or '.') + '/' + prefix + class_name + '.py', 'w') f.write(mapcss) f.close() if original_class_name in item_map: item_map[original_class_name]['class'] = class_map f = open("item_map.py", "w") f.write("#-*- coding: utf-8 -*-\n") f.write("item_map = \\\n") pprint(item_map, f) f.close()
def main(_, mapcss): path = os.path.dirname(mapcss) class_name = original_class_name = '.'.join(os.path.basename(mapcss).replace('.validator.', '.').split('.')[:-1]) global item_default, class_map, subclass_blacklist, class_index, meta_tags if class_name in item_map: i = item_map[class_name] item_default = i['item'] class_map = i.get('class', {}) subclass_blacklist = i.get('subclass_blacklist', []) only_for = i.get('only_for', []) not_for = i.get('not_for', []) prefix = i.get('prefix', '') class_index = max(class_map.values()) meta_tags = ('["' + ('", "').join(i.get('tags')) + '"]') if i.get('tags') else None else: item_default = 0 class_map = {} subclass_blacklist = [] only_for = [] not_for = [] prefix = '' class_index = item_default * 1000 meta_tags = None class_name = class_name.replace('.', '_').replace('-', '_') input = FileStream(mapcss, encoding='utf-8') lexer = MapCSSLexer(input) stream = CommonTokenStream(lexer) parser = MapCSSParser(stream) tree = parser.stylesheet() listener = MapCSSListenerL() walker = ParseTreeWalker() walker.walk(listener, tree) selectors_by_complexity = segregate_selectors_by_complexity(listener.stylesheet) if len(selectors_by_complexity['rules_complex']) > 0: print("W: Drop %d complex rules" % len(selectors_by_complexity['rules_complex'])) tree = rewrite_tree(selectors_by_complexity['rules_meta'] + selectors_by_complexity['rules_simple']) tree = filter_non_productive_rules(tree) tree = filter_osmose_none_rules(tree) selectors_type = segregate_selectors_type(tree) global class_, tests, regex_store, set_store rules = dict(map(lambda t: [t, to_p({'type': 'stylesheet', 'rules': selectors_type[t]})], sorted(selectors_type.keys(), key = lambda a: {'node': 0, 'way': 1, 'relation':2}[a]))) items = build_items(class_) asserts = build_tests(tests) mapcss = ("""#-*- coding: utf-8 -*- from __future__ import unicode_literals import modules.mapcss_lib as mapcss import regex as re from plugins.Plugin import Plugin, with_options class """ + prefix + class_name + """(Plugin): """ + ("\n only_for = ['" + "', '".join(only_for) + "']\n" if only_for != [] else "") + """ """ + ("\n not_for = ['" + "', '".join(not_for) + "']\n" if not_for != [] else "") + """ def init(self, logger): Plugin.init(self, logger) tags = capture_tags = {} """ + items.replace("\n", "\n ") + """ """ + "".join(map(lambda r: """ self.""" + r[1] + " = re.compile(r'" + r[0].replace('(?U)', '').replace("'", "\\'") + "')", sorted(regex_store.items(), key = lambda s: s[1]))) + """ """ + "".join(map(lambda t: """ def """ + t + """(self, data, tags""" + {'node': "", 'way': ", nds", 'relation': ", members"}[t] + """): capture_tags = {} keys = tags.keys() err = [] """ + ((" = ".join(list(map(lambda s: "set_" + s, sorted(set_store)))) + " = False") if len(set_store) > 0 else "") + """ """ + rules[t].replace("\n", "\n ") + """ return err """, sorted(rules.keys(), key = lambda a: {'node': 0, 'way': 1, 'relation':2}[a]))) + """ from plugins.Plugin import TestPluginCommon class Test(TestPluginCommon): def test(self): n = """ + prefix + class_name + """(None) class _config: options = {"country": None, "language": None} class father: config = _config() n.father = father() n.init(None) data = {'id': 0, 'lat': 0, 'lon': 0} """ + asserts.replace("\n", "\n ") + """ """).replace(" \n", "\n") f = open((path or '.') + '/' + prefix + class_name + '.py', 'w') f.write(mapcss) f.close() if original_class_name in item_map: item_map[original_class_name]['class'] = class_map f = open("item_map.py", "w") f.write("#-*- coding: utf-8 -*-\n") f.write("item_map = \\\n") pprint(item_map, f) f.close()