mt_metadata.utils.converters ============================ .. py:module:: mt_metadata.utils.converters .. autoapi-nested-parse:: Converters to convert old JSON schema to new JSON schema and then to pydantic basemodel and then to pydantic basemodel with types. Attributes ---------- .. autoapisummary:: 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.type_imports Functions --------- .. autoapisummary:: mt_metadata.utils.converters.load_json mt_metadata.utils.converters.write_json mt_metadata.utils.converters.get_default_value mt_metadata.utils.converters.get_alias_name mt_metadata.utils.converters.get_new_basemodel_filename mt_metadata.utils.converters.get_new_schema_filename mt_metadata.utils.converters.to_json_schema mt_metadata.utils.converters.snake_to_camel mt_metadata.utils.converters.generate_pydantic_basemodel mt_metadata.utils.converters.clean_and_format_code mt_metadata.utils.converters.reformat Module Contents --------------- .. py:data:: STANDARDS_SAVEPATH .. py:data:: MTMETADATA_SAVEPATH .. py:data:: TYPE_MAPPING .. py:data:: JSON_TYPE_MAPPING .. py:data:: TAB :value: ' ' .. py:function:: load_json(filename) Load a JSON file and return its contents as a dictionary. :param filename: The path to the JSON file. :type filename: Union[str, Path] :returns: The contents of the JSON file as a dictionary. :rtype: Dict[str, Any] .. py:function:: write_json(filename, data) Write a dictionary to a JSON file. :param filename: The path to the JSON file. :type filename: Union[str, Path] :param data: The data to write to the file. :type data: Dict[str, Any] .. py:function:: get_default_value(data_type, default_value = None, required = False) Get default value based on information provided. :param data_type: data type name :type data_type: str :param default_value: given default value, by default None :type default_value: Any, optional :param required: is required, by default False :type required: bool, optional :returns: default value :rtype: Any .. py:function:: get_alias_name(alias_name) Get the alias name, and return None if empty :param alias_name: alias name :type alias_name: str .. py:function:: get_new_basemodel_filename(filename, save_path = MTMETADATA_SAVEPATH) Get new file name for new BaseModel. Will place into `mt_metadata/mt_metadata/...` :param filename: json schema standards file name :type filename: Path | str :param save_path: default path to save to, by default MTMETADATA_SAVEPATH :type save_path: Path, optional :returns: new file path to new BaseModel object. :rtype: Path .. py:function:: 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/...` :param filename: old JSON file :type filename: str | Path :param save_path: default directory to save to, by default STANDARDS_SAVEPATH :type save_path: Path, optional :returns: new file path to JSON Schema file. :rtype: Path .. py:function:: to_json_schema(filename) Convert old JSON files to a JSON Schema file. :param filename: file path to old JSON file :type filename: Union[str, Path] :returns: File path to new JSON Schema file :rtype: Path :raises KeyError: if `type` is not in old JSON file .. py:function:: snake_to_camel(snake_str) .. py:data:: type_imports .. py:function:: 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. :param json_schema_filename: path to the JSON schema file :type json_schema_filename: str | Path :returns: _description_ :rtype: Path .. py:function:: clean_and_format_code(code_str, filename = None) Clean and format Python code by removing unused imports and formatting with isort and black. :param code_str: Python code as a string :type code_str: str :param filename: Filename for error reporting, by default None :type filename: str, optional :returns: Cleaned and formatted code :rtype: str .. py:function:: reformat(filename) Reformat a Python file by removing unused imports and formatting with isort and black. :param filename: Path to the Python file to be reformatted :type filename: str | Path