Ejemplo n.º 1
0
def purify(indent=False):
    global env
    env = Environment(extensions=[WithGenericView, FieldShortcut, FieldsListShortcut, HamlishExtension])
    called_from = sys._getframe(1).f_globals["__file__"]
    module_directory = os.path.split(called_from)[0]

    if not os.path.exists(os.path.join(module_directory, "__openerp__.py")):
        return

    env.loader = FileSystemLoader(module_directory)

    module_settings = eval(open(os.path.join(module_directory, "__openerp__.py")).read())
    xml_files = list(set(filter(lambda x: x.strip().endswith(".xml"), module_settings.get("init_xml", []) + module_settings.get("data", []) + module_settings.get("update_xml", []))))

    for xml_file in xml_files:
        haml_file = xml_file[:-3] + "haml"
        haml_file_path = os.path.join(module_directory, xml_file[:-3] + "haml")
        if not os.path.exists(haml_file_path):
            continue
        to_write = env.get_template(haml_file).render()
        if not re.match("^\s*<\s*openerp\s*>\s*<\s*data ", to_write):
            to_write = env.hamlish_from_string("%openerp\n  %data\n    =body").render(body=to_write)
        to_write = format_xml(xml_file, to_write)
        open(os.path.join(module_directory, xml_file), "w").write(to_write)