-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hierarchy_from_folder_structure.py
76 lines (64 loc) · 2.87 KB
/
get_hierarchy_from_folder_structure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
__ __ __
____/ /___ ______ ___ / /__ ____ ___ ___ ____ / /_
/ __ / __ `/ ___/ / _ \/ / _ \/ __ `__ \/ _ \/ __ \/ __/
/ /_/ / /_/ (__ ) / __/ / __/ / / / / / __/ / / / /_
\__,_/\__,_/____/ \___/_/\___/_/ /_/ /_/\___/_/ /_/\__/
Script to convert a folder structure into the hierarchy tree format for das element
1. change the 'path' at the bottom of the script
2. copy&paste or merge the result into the 'hierarchy.json' file
"""
import json
from pathlib import Path
def get_hierarchy_from_folder(root_path):
def get_folders_as_dict(root_path):
result = {}
root_path += '/' # need to add a trailing slash here - otherwise we get an empty data dict
for path in Path(root_path).rglob('*'):
data = result
if path.is_file():
# only validate folders
continue
# path should only have forward slashes - that's important for Windows
path_string = str(path).replace('\\', '/')
# remove root folder since we are only interessted in the folder structure
path_strip_root = path_string.replace(root_path, '')
items = path_strip_root.split('/')
for item in items:
data = data.setdefault(item, {})
return result
def convert_to_hierarchy(data, foo='', result=[]):
# recursive function
result = []
for key, entity in data.items():
item_id = '{}-{}'.format(foo, key)
item_id = item_id.lstrip('-') # remove any dashes at the front
item_id = item_id.lower() # make the ID lowercase
item_id = item_id.replace(' ', '-') # remove spaces in the ID
new_item = {'id': item_id, 'name': key, 'synonyms': []}
if entity:
# only create children entry if there are any
new_item['children'] = convert_to_hierarchy(entity,
item_id,
result=[])
result.append(new_item)
return result
# path should only have forward slashes - that's important for Windows
root_path = root_path.replace('\\', '/')
# get the folder strucutre as a dictionary
paths_as_dict = get_folders_as_dict(root_path)
# and now use a recursive function to convert it to the correct format
hierarchy_data = convert_to_hierarchy(paths_as_dict)
# this is the default hierarchy structure
# the root / (Q2574811) is required
return {
"hierarchy": [{
"children": hierarchy_data,
"id": "Q2574811",
"name": "/"
}],
"version": "my custom hierarchy"
}
path = '/path/to/your/folder'
result = get_hierarchy_from_folder(path)
print(json.dumps(result, indent=4))