61 lines
1.5 KiB
Django/Jinja
61 lines
1.5 KiB
Django/Jinja
#!/bin/env python
|
|
|
|
import sys
|
|
import os
|
|
import json
|
|
|
|
default_base_dir="{{ evofs_base_dir | mandatory }}"
|
|
|
|
def clean_lines(lines):
|
|
clean_lines=[]
|
|
# TODO: a map/reduce or a comprehension might be better
|
|
for line in lines:
|
|
# remove linebreaks
|
|
# TODO: see if a proper regex is needed
|
|
clean_line = line.replace("\n","")
|
|
# don't return empty lines
|
|
if len(clean_line) > 0:
|
|
clean_lines.append(clean_line)
|
|
return clean_lines
|
|
|
|
def read_entry(path):
|
|
f = open(path,"r")
|
|
lines = clean_lines(f.readlines())
|
|
|
|
if len(lines) > 1:
|
|
# return multiple lines as array
|
|
return lines
|
|
else:
|
|
# return single lines as string
|
|
return lines[0]
|
|
|
|
def build_dict(dir):
|
|
data = {}
|
|
for path in os.listdir(dir):
|
|
if os.path.isfile(os.path.join(dir, path)):
|
|
# if entry is a file, the value is the content of the file
|
|
data[path] = read_entry(os.path.join(dir, path))
|
|
elif os.path.isdir(os.path.join(dir, path)):
|
|
# is entry is a directory, add recursively
|
|
data[path] = build_dict(os.path.join(dir, path))
|
|
return data
|
|
|
|
def main():
|
|
if len(sys.argv) > 1:
|
|
basedir = sys.argv[1]
|
|
else:
|
|
basedir = default_base_dir
|
|
|
|
if os.path.isdir(basedir):
|
|
data = build_dict(basedir)
|
|
|
|
print(json.dumps(data, sort_keys=True))
|
|
sys.exit(0)
|
|
else:
|
|
print("Directory '" + basedir + "' not found", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
sys.exit(0) |