Skip to content

Commit

Permalink
manage.py: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
duncandewhurst committed Mar 15, 2024
1 parent 448c3ba commit 8422a7c
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,21 +1071,21 @@ def update_sustainability_docs():
@cli.command()
def update_sustainability_fields():
"""
Update the list of fields used in each elements example in mapping/sustainability.yaml.
Update the list of fields used in each element's example in mapping/sustainability.yaml.
"""

def __get_paths(d):
"""
Get a list of paths from a JSON object.
"""
if isinstance(d, dict):
for key, value in d.items():
yield f'/{key}'
yield from (f'/{key}{p}' for p in __get_paths(value))
elif isinstance(d, list):
for i, value in enumerate(d):
yield from (f'{p}' for p in __get_paths(value))
"""
Get a list of paths from a JSON object.
"""
if isinstance(d, dict):
for key, value in d.items():
yield f'/{key}'
yield from (f'/{key}{p}' for p in __get_paths(value))

elif isinstance(d, list):
for i, value in enumerate(d):
yield from (f'{p}' for p in __get_paths(value))

# Load sustainability modules mapping
filename = basedir / 'mapping' / 'sustainability.yaml'
Expand All @@ -1098,12 +1098,15 @@ def __get_paths(d):
for element in mapping.values():
if element["example"] != '':
fields = set()
element["fields"] = [fields.add(path) or path for path in __get_paths(json.loads(element["example"])) if path not in fields]
element["fields"] = [fields.add(path) or path for path in __get_paths(
json.loads(element["example"])) if path not in fields]
# Handle elements that reference another element and have a blank example
elif element["refs"] != '':
element["fields"] = [fields.add(path) or path for path in __get_paths(json.loads(mapping[element["refs"]]["example"])) if path not in fields]
element["fields"] = [fields.add(path) or path for path in __get_paths(
json.loads(mapping[element["refs"]]["example"])) if path not in fields]

write_yaml_file(filename, list(mapping.values()))


if __name__ == '__main__':
cli()

0 comments on commit 8422a7c

Please sign in to comment.