I have a Python script that generates a mapping JSON based off a JSON template.
The script reads the JSON template, then compares a folder structure that has to match the template, so that it can output a new JSON with corresponding values.
This is an example of a line in the JSON template.
"path_structure": "Folder/Apples/{apple_ids}/*",
Then, the script will receive a folder structure like so:
Folder/
└── Apples/
├── 43952/
├── 53453/
├── 12342/
└── 76552/
The resulting separate JSONs from the script would have the lines:
"value": "43952"
"value": "53453"
"value": "12342"
"value": "76552"
The script looks like this right now.
def get_relative_path(file, start_dir):
relative_path = os.path.relpath(file, start=start_dir)
return os.path.normpath(relative_path).split(os.sep)
file_components = get_relative_path(file, start_dir)
def get_path_components(mapping_file_json):
path_structure = mapping_file_json["path_structure"].rstrip("/*")
return path_structure.split("/")
path_components = get_path_components(mapping_file_json)
def get_linking_identifier(path_components):
linking_identifiers = ["{apple_ids}"]
for linking_identifier in linking_identifiers:
try:
linking_identifier_index = path_components.index(linking_identifier)
return linking_identifier_index
except ValueError:
continue
value = file_components[linking_identifier_index]
"value": str(value)
My inquiry is regarding supporting changing the template structure.
I need to support when the JSON template may look like:
"path_structure": "Folder/Apples/notimportant_{apple_ids}/*"
or
"path_structure": "Folder/Apples/{apple_ids}_random/*"
or
"path_structure": "Folder/Apples/random{apple_ids}words/*"
Example folder structures:
Folder/
└── Apples/
├── notimportant_43952/
├── notimportant_53453/
├── notimportant_12342/
└── notimportant_76552/
or
Folder/
└── Apples/
├── 43952_random/
├── 53453_random/
├── 12342_random/
└── 76552_random/
And the outputted search values should still look like:
"value": "43952"
"value": "53453"
"value": "12342"
"value": "76552"
How can I best edit my script to support this new functionality?