Created
September 28, 2018 09:12
-
-
Save adamauckland/4315fa97b7bf1ef882ef108a03680cb4 to your computer and use it in GitHub Desktop.
Create stub njk files from a directory structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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(".")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment