From 107352932cb1ed19d801cdaf8061c3368b309962 Mon Sep 17 00:00:00 2001 From: Dan Jones <danjon@noc.ac.uk> Date: Tue, 20 Aug 2024 14:25:46 +0100 Subject: [PATCH] lint: fix black and flake8 errors --- generate_schema_config.py | 45 ++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/generate_schema_config.py b/generate_schema_config.py index 07dcae7..f0df964 100644 --- a/generate_schema_config.py +++ b/generate_schema_config.py @@ -114,25 +114,25 @@ swagger_config = { } -def resolve_ref(ref): +def resolve_ref(ref): """ - Get schema URL, parse JSON + Get schema URL, parse JSON Return None if either fails """ try: res = requests.get(ref) if res.status_code == 200: return res.json() - else: + else: return None except (json.JSONDecodeError, ValueError): return None - -def rename_ref(ref): + +def rename_ref(ref): """ - Convert remote ref URL into a name that can - be used for a local ref in the schema + Convert remote ref URL into a name that can + be used for a local ref in the schema Remote the URL scheme and replace / with . """ # remove url scheme @@ -143,10 +143,10 @@ def rename_ref(ref): def nested_replace(source, key, value, replace_with): """ - Find all instances of a key value pair in a nested + Find all instances of a key value pair in a nested dictionary and replace the value with replace_with - """ - for k,v in source.items(): + """ + for k, v in source.items(): if k == key and v == value: source[k] = replace_with elif type(v) is list: @@ -160,32 +160,32 @@ def nested_replace(source, key, value, replace_with): def inject_schema(schema, remote_ref): """ Given a parent schema and a remote ref - + 1. get the remote ref schema 2. create a local reference name (without path separators) - 3. insert into components.schemas - 4. replace remote references with local references + 3. insert into components.schemas + 4. replace remote references with local references + - returns True if resolved and injected - """ + """ local_name = rename_ref(remote_ref) local_ref = f"#/components/schemas/{local_name}" ref_schema = resolve_ref(remote_ref) - if (ref_schema is not None): + if ref_schema is not None: nested_replace(schema, "$ref", remote_ref, local_ref) schema["components"]["schemas"][local_name] = ref_schema return True - else: - return False + else: + return False def import_remote_refs(): """ - inject the following remote refs into the schema + inject the following remote refs into the schema and replace the remote refs with local refs - returns True if all schemas resolved and injected + returns True if all schemas resolved and injected """ ref_imports = [ "https://geojson.org/schema/Feature.json", @@ -195,10 +195,7 @@ def import_remote_refs(): "https://geojson.org/schema/Polygon.json", ] - return all([ - inject_schema(swagger_config, ref) - for ref in ref_imports - ]) + return all([inject_schema(swagger_config, ref) for ref in ref_imports]) def configure_flask(swagger_config): -- GitLab