Skip to content

Instantly share code, notes, and snippets.

@adamauckland
Created September 28, 2018 09:12
Show Gist options
  • Save adamauckland/4315fa97b7bf1ef882ef108a03680cb4 to your computer and use it in GitHub Desktop.
Save adamauckland/4315fa97b7bf1ef882ef108a03680cb4 to your computer and use it in GitHub Desktop.

Revisions

  1. adamauckland created this gist Sep 28, 2018.
    43 changes: 43 additions & 0 deletions create-njk.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #
    # Put this file in the root of the directory your JSON files are in and run it
    #
    # $ python create-njk.py
    #
    # The stub content is at line 27. It supports adding more lines. Percentages must be doubled to escape them.

    import os
    import os.path

    def get_extension(file_path):
    try:
    parts = file_path.split(".")
    last_part = parts[len(parts) - 1]

    return last_part.lower()
    except Exception:
    pass

    def create_stub(main_dir, file_path):
    print("Creating stub for %s" % file_path)

    json_path = file_path[:len(file_path) - 4] + "njk"
    extends_path = file_path[len(main_dir) + 1:]

    with open(json_path, "wt") as write_handle:
    write_handle.write("""{%% extends '%s' %%}""" % extends_path)

    def check_directory(main_dir, dir_path):
    print("Checking %s" % dir_path)

    for loop_file in os.listdir(dir_path):
    new_file_path = os.path.join(dir_path, loop_file)

    print(new_file_path)
    if os.path.isdir(new_file_path):
    check_directory(main_dir, new_file_path)
    else:
    if get_extension(new_file_path) == "json":
    create_stub(main_dir, new_file_path)

    if __name__ == "__main__":
    check_directory(os.path.abspath("."), os.path.abspath("."))