Verified Commit bbded930 authored by Dan Jones's avatar Dan Jones
Browse files

lint: fix black and flake8 errors

parent 673cab5c
1 merge request!31Resolve "Test validating messages with references to GeoJson schema in both JS and python"
...@@ -114,25 +114,25 @@ swagger_config = { ...@@ -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 Return None if either fails
""" """
try: try:
res = requests.get(ref) res = requests.get(ref)
if res.status_code == 200: if res.status_code == 200:
return res.json() return res.json()
else: else:
return None return None
except (json.JSONDecodeError, ValueError): except (json.JSONDecodeError, ValueError):
return None return None
def rename_ref(ref):
def rename_ref(ref):
""" """
Convert remote ref URL into a name that can Convert remote ref URL into a name that can
be used for a local ref in the schema be used for a local ref in the schema
Remote the URL scheme and replace / with . Remote the URL scheme and replace / with .
""" """
# remove url scheme # remove url scheme
...@@ -143,10 +143,10 @@ def rename_ref(ref): ...@@ -143,10 +143,10 @@ def rename_ref(ref):
def nested_replace(source, key, value, replace_with): 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 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: if k == key and v == value:
source[k] = replace_with source[k] = replace_with
elif type(v) is list: elif type(v) is list:
...@@ -160,32 +160,32 @@ def nested_replace(source, key, value, replace_with): ...@@ -160,32 +160,32 @@ def nested_replace(source, key, value, replace_with):
def inject_schema(schema, remote_ref): def inject_schema(schema, remote_ref):
""" """
Given a parent schema and a remote ref Given a parent schema and a remote ref
1. get the remote ref schema 1. get the remote ref schema
2. create a local reference name (without path separators) 2. create a local reference name (without path separators)
3. insert into components.schemas 3. insert into components.schemas
4. replace remote references with local references 4. replace remote references with local references
returns True if resolved and injected returns True if resolved and injected
""" """
local_name = rename_ref(remote_ref) local_name = rename_ref(remote_ref)
local_ref = f"#/components/schemas/{local_name}" local_ref = f"#/components/schemas/{local_name}"
ref_schema = resolve_ref(remote_ref) 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) nested_replace(schema, "$ref", remote_ref, local_ref)
schema["components"]["schemas"][local_name] = ref_schema schema["components"]["schemas"][local_name] = ref_schema
return True return True
else: else:
return False return False
def import_remote_refs(): 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 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 = [ ref_imports = [
"https://geojson.org/schema/Feature.json", "https://geojson.org/schema/Feature.json",
...@@ -195,10 +195,7 @@ def import_remote_refs(): ...@@ -195,10 +195,7 @@ def import_remote_refs():
"https://geojson.org/schema/Polygon.json", "https://geojson.org/schema/Polygon.json",
] ]
return all([ return all([inject_schema(swagger_config, ref) for ref in ref_imports])
inject_schema(swagger_config, ref)
for ref in ref_imports
])
def configure_flask(swagger_config): def configure_flask(swagger_config):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment