Пример #1
0
def path_to_section():
    section_config = get_sections()
    sections = {}
    for section, paths in section_config:
        for path in paths:
            sections[path] = section
    return sections
Пример #2
0
def get_pattern_templates():
    templates = base_dict()
    template_dirs = get_template_dirs()

    for lookup_dir in template_dirs:
        for root, dirs, files in os.walk(lookup_dir, topdown=True):
            # Ignore folders without files
            if not files:
                continue

            base_path = os.path.relpath(root, lookup_dir)
            section, path = section_for(base_path)

            # It has no section, ignore it
            if not section:
                continue

            found_templates = []
            for current_file in files:
                pattern_path = os.path.join(root, current_file)
                pattern_path = os.path.relpath(pattern_path, lookup_dir)

                if is_pattern(pattern_path):
                    template = get_template(pattern_path)
                    pattern_config = get_pattern_config(pattern_path)
                    pattern_name = pattern_config.get("name")
                    pattern_filename = os.path.relpath(
                        template.origin.template_name,
                        base_path,
                    )
                    if pattern_name:
                        template.pattern_name = pattern_name
                    else:
                        template.pattern_name = pattern_filename

                    template.pattern_filename = pattern_filename

                    found_templates.append(template)

            if found_templates:
                lookup_dir_relpath = os.path.relpath(root, lookup_dir)
                sub_folders = os.path.relpath(lookup_dir_relpath, path)
                templates_to_store = templates
                for folder in [section, *sub_folders.split(os.sep)]:
                    try:
                        templates_to_store = templates_to_store["template_groups"][
                            folder
                        ]
                    except KeyError:
                        templates_to_store["template_groups"][folder] = base_dict()
                        templates_to_store = templates_to_store["template_groups"][
                            folder
                        ]

                templates_to_store["templates_stored"].extend(found_templates)

    # Order the templates alphabetically
    for templates_objs in templates["template_groups"].values():
        templates_objs["template_groups"] = order_dict(
            templates_objs["template_groups"]
        )

    # Order the top level by the sections
    section_order = [section for section, _ in get_sections()]
    templates["template_groups"] = order_dict(
        templates["template_groups"], key_sort=lambda key: section_order.index(key)
    )

    return templates