Skip to content

Commit

Permalink
WIP, managed to populate function attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Nov 8, 2023
1 parent ac27b34 commit f57c78c
Show file tree
Hide file tree
Showing 5 changed files with 918 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,84 @@ def traverse(schema_dictionary, schema_path, schema_paths):

schema_path_joined = "/".join(schema_path)

schema_paths.append(schema_path_joined)

if "properties" in schema_dictionary.keys():
original_dictionary = schema_dictionary
schema_dictionary = schema_dictionary["properties"]

elif "items" in schema_dictionary.keys():
schema_dictionary = schema_dictionary["items"]
if "properties" in schema_dictionary.keys():
original_dictionary = schema_dictionary
schema_dictionary = schema_dictionary["properties"]

if schema_path:

attributes = []

all_attributes = set(schema_dictionary.keys())

positional_attributes = set(
original_dictionary.get("required", [])
)

optional_attributes = (
all_attributes.difference(positional_attributes)
)

for positional_attribute in positional_attributes:

positional_attribute_value = (
schema_dictionary[positional_attribute]
)

if isinstance(positional_attribute_value, dict):
if "type" in positional_attribute_value.keys():

type_ = {
"integer": "int",
"boolean": "bool",
"string": "str",
"array": "list",
"object": "object"
}[positional_attribute_value["type"]]

else:
type_ = "object"

attributes.append(f"{positional_attribute}: {type_}")

for optional_attribute in optional_attributes:

optional_attribute_value = (
schema_dictionary[optional_attribute]
)

if isinstance(optional_attribute_value, dict):
if "type" in optional_attribute_value.keys():

type_ = {
"integer": "int",
"boolean": "bool",
"string": "str",
"array": "list",
"object": "object"
}[optional_attribute_value["type"]]

else:
type_ = "object"

attributes.append(f"{optional_attribute}: {type_} = None")

attributes = ",\n ".join(attributes)

if original_dictionary.get("additionalProperties", False):
attributes = f"{attributes},\n **kwargs"

schema_paths[schema_path_joined] = {
"function_name": schema_path_joined.replace("/", "_"),
"attributes": f"\n {attributes}\n"
}

for schema_dictionary_key, schema_dictionary_value in (
schema_dictionary.items()
):
Expand All @@ -216,20 +284,11 @@ def traverse(schema_dictionary, schema_path, schema_paths):
elif "properties" not in schema_dictionary_value.keys():
continue

if schema_dictionary_key in schema_dictionary.keys():
schema_dictionary_key = schema_dictionary_key
if schema_dictionary_key == ".*":
schema_path.append("dot_asterisk")
elif schema_dictionary_key == "service.name":
schema_path.append("service_name")
else:
schema_path.append(schema_dictionary_key.lower())
else:
1 / 0
schema_path.append(schema_dictionary_key.lower())
traverse(
schema_dictionary_value,
schema_path,
schema_paths
schema_paths,
)
schema_path.pop()

Expand All @@ -241,49 +300,36 @@ def traverse(schema_dictionary, schema_path, schema_paths):
else:
return

if schema_dictionary_key in schema_dictionary.keys():
schema_dictionary_key = schema_dictionary_key
if schema_dictionary_key == ".*":
schema_path.append("dot_asterisk")
elif schema_dictionary_key == "service.name":
schema_path.append("service_name")
else:
schema_path.append(schema_dictionary_key.lower())
else:
1 / 0
schema_path.append(schema_dictionary_key.lower())

for element in schema_dictionary_value:
if "properties" not in schema_dictionary_value.keys():
return
if isinstance(element, dict):
traverse(
element,
schema_path,
schema_paths
schema_paths,
)
schema_path.pop()

schema_paths = []
schema_paths = {}

traverse(schema_dictionary, [], schema_paths)

schema_paths.pop(0)

return schema_paths


def write_schema_paths_functions(
schema_paths: list, schema_paths_functions_file_path: str
schema_paths, schema_paths_functions_file_path: str
):

set_trace()

schema_paths_function_names = {}

for schema_path in schema_paths:
for schema_path_key, schema_path_value in schema_paths.items():

schema_paths_function_names[schema_path] = (
schema_path.
replace("properties/", "").
replace("items/", "").
replace("/", "_")
schema_paths_function_names[schema_path_key] = (
schema_path_value["function_name"]
)

schema_paths_functions = dumps(schema_paths_function_names, indent=4)
Expand All @@ -297,11 +343,12 @@ def write_schema_paths_functions(
):
schema_paths_functions_file.write("# flake8: noqa: E501\n")

for function_name in schema_paths_function_names.values():
for schema_path_value in schema_paths.values():
schema_paths_functions_file.write("\n")
schema_paths_functions_file.write("\n")
schema_paths_functions_file.write(
f"def {function_name}(*args, **kwargs):\n"
f"def {schema_path_value['function_name']}"
f"({schema_path_value['attributes']}):\n"
)
schema_paths_functions_file.write(" pass\n")

Expand Down
Loading

0 comments on commit f57c78c

Please sign in to comment.