mt_metadata.utils.converters

Converters to convert old JSON schema to new JSON schema and then to pydantic basemodel and then to pydantic basemodel with types.

Attributes

STANDARDS_SAVEPATH

MTMETADATA_SAVEPATH

TYPE_MAPPING

JSON_TYPE_MAPPING

TAB

type_imports

Functions

load_json(filename)

Load a JSON file and return its contents as a dictionary.

write_json(filename, data)

Write a dictionary to a JSON file.

get_default_value(data_type[, default_value, required])

Get default value based on information provided.

get_alias_name(alias_name)

Get the alias name, and return None if empty

get_new_basemodel_filename(filename[, save_path])

Get new file name for new BaseModel.

get_new_schema_filename(filename[, save_path])

Get new file path to a JSON schema file. Will be place into

to_json_schema(filename)

Convert old JSON files to a JSON Schema file.

snake_to_camel(snake_str)

generate_pydantic_basemodel(json_schema_filename)

Generate a Pydantic model from a JSON schema file and save it to a Python file.

clean_and_format_code(code_str[, filename])

Clean and format Python code by removing unused imports and formatting with isort and black.

reformat(filename)

Reformat a Python file by removing unused imports and formatting with isort and black.

Module Contents

mt_metadata.utils.converters.STANDARDS_SAVEPATH
mt_metadata.utils.converters.MTMETADATA_SAVEPATH
mt_metadata.utils.converters.TYPE_MAPPING
mt_metadata.utils.converters.JSON_TYPE_MAPPING
mt_metadata.utils.converters.TAB = '    '
mt_metadata.utils.converters.load_json(filename)

Load a JSON file and return its contents as a dictionary.

Parameters:

filename (Union[str, Path]) – The path to the JSON file.

Returns:

The contents of the JSON file as a dictionary.

Return type:

Dict[str, Any]

mt_metadata.utils.converters.write_json(filename, data)

Write a dictionary to a JSON file.

Parameters:
  • filename (Union[str, Path]) – The path to the JSON file.

  • data (Dict[str, Any]) – The data to write to the file.

mt_metadata.utils.converters.get_default_value(data_type, default_value=None, required=False)

Get default value based on information provided.

Parameters:
  • data_type (str) – data type name

  • default_value (Any, optional) – given default value, by default None

  • required (bool, optional) – is required, by default False

Returns:

default value

Return type:

Any

mt_metadata.utils.converters.get_alias_name(alias_name)

Get the alias name, and return None if empty

Parameters:

alias_name (str) – alias name

mt_metadata.utils.converters.get_new_basemodel_filename(filename, save_path=MTMETADATA_SAVEPATH)

Get new file name for new BaseModel.

Will place into mt_metadata/mt_metadata/…

Parameters:
  • filename (Path | str) – json schema standards file name

  • save_path (Path, optional) – default path to save to, by default MTMETADATA_SAVEPATH

Returns:

new file path to new BaseModel object.

Return type:

Path

mt_metadata.utils.converters.get_new_schema_filename(filename, save_path=STANDARDS_SAVEPATH)

Get new file path to a JSON schema file. Will be place into mt_metadata/mt_metadata/standards/…

Parameters:
  • filename (str | Path) – old JSON file

  • save_path (Path, optional) – default directory to save to, by default STANDARDS_SAVEPATH

Returns:

new file path to JSON Schema file.

Return type:

Path

mt_metadata.utils.converters.to_json_schema(filename)

Convert old JSON files to a JSON Schema file.

Parameters:

filename (Union[str, Path]) – file path to old JSON file

Returns:

File path to new JSON Schema file

Return type:

Path

Raises:

KeyError – if type is not in old JSON file

mt_metadata.utils.converters.snake_to_camel(snake_str)
mt_metadata.utils.converters.type_imports
mt_metadata.utils.converters.generate_pydantic_basemodel(json_schema_filename)

Generate a Pydantic model from a JSON schema file and save it to a Python file. The generated model will use Annotated and Field for type annotations.

Parameters:

json_schema_filename (str | Path) – path to the JSON schema file

Returns:

_description_

Return type:

Path

mt_metadata.utils.converters.clean_and_format_code(code_str, filename=None)

Clean and format Python code by removing unused imports and formatting with isort and black.

Parameters:
  • code_str (str) – Python code as a string

  • filename (str, optional) – Filename for error reporting, by default None

Returns:

Cleaned and formatted code

Return type:

str

mt_metadata.utils.converters.reformat(filename)

Reformat a Python file by removing unused imports and formatting with isort and black.

Parameters:

filename (str | Path) – Path to the Python file to be reformatted