Skip to content

Commit

Permalink
U/gblackadder/private types (#15)
Browse files Browse the repository at this point in the history
* fix warnings on schema private attrbs

* fix two typos elctronicMailAddress -> electronicMailAddress FormatDistributor -> formatDistributor

* make the attrs private
  • Loading branch information
gblackadder authored Jan 28, 2025
1 parent 0c737ba commit 2c63676
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 33 deletions.
8 changes: 5 additions & 3 deletions pydantic_schemas/document_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field
from pydantic import ConfigDict, Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -595,8 +595,10 @@ class ScriptSchemaDraft(SchemaBaseModel):
"""
Schema for Document data type
"""
_metadata_type__ = "document"
_metadata_type_version__ = "0.1.0"

_metadata_type__:str = PrivateAttr("document")
_metadata_type_version__:str = PrivateAttr("0.1.0")


idno: Optional[str] = Field(
None, description="Project unique identifier", title="Project unique identifier"
Expand Down
13 changes: 12 additions & 1 deletion pydantic_schemas/generators/generate_pydantic_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@
with open(output_path) as file:
content = file.read()

# version=version: f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n _metadata_type__:str = PrivateAttr("{section}")\n _metadata_type_version__:str = PrivateAttr("{version}") """,
# version = f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n\n def __init__(self, **kwargs):\n super().__init__(**kwargs)\n self._metadata_type__ = "{section}"\n self._metadata_type_version__ = "{version}"\n"""
updated_content = re.sub(
f'class {model_name}\(SchemaBaseModel\):\n( """\n.*\n """)', #
lambda match,
model_name=model_name,
section=section,
version=version: f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n _metadata_type__ = "{section}"\n _metadata_type_version__ = "{version}" """,
# version=version: f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n _metadata_type__ = "{section}"\n _metadata_type_version__ = "{version}" """,
# version = version: f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n\n def __init__(self, **kwargs):\n super().__init__(**kwargs)\n self._metadata_type__ = "{section}"\n self._metadata_type_version__ = "{version}"\n""",
version=version: f"""class {model_name}(SchemaBaseModel):\n{match.group(1)}\n _metadata_type__:str = PrivateAttr("{section}")\n _metadata_type_version__:str = PrivateAttr("{version}") """,
content,
)

# find the line in updated_content that begins "from pydantic import " and append to that line ", PrivateAttr"
updated_content = re.sub(
r"from pydantic import (.*)",
lambda match: f"from pydantic import {match.group(1)}, PrivateAttr",
updated_content,
)

with open(output_path, "w") as file:
file.write(updated_content)
6 changes: 3 additions & 3 deletions pydantic_schemas/geospatial_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field, RootModel, confloat
from pydantic import ConfigDict, Field, RootModel, confloat, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -1513,8 +1513,8 @@ class GeospatialSchema(SchemaBaseModel):
"""
Geospatial draft schema
"""
_metadata_type__ = "geospatial"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("geospatial")
_metadata_type_version__:str = PrivateAttr("0.1.0")

idno: Optional[str] = Field(
None, description="Project unique identifier", title="Project unique identifier"
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/image_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import AnyUrl, AwareDatetime, ConfigDict, Field, confloat
from pydantic import AnyUrl, AwareDatetime, ConfigDict, Field, confloat, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -1170,8 +1170,8 @@ class ImageDataTypeSchema(SchemaBaseModel):
"""
Uses IPTC JSON schema. See for more details - http://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata.
"""
_metadata_type__ = "image"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("image")
_metadata_type_version__:str = PrivateAttr("0.1.0")

repositoryid: Optional[str] = Field(
"central",
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/indicator_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Union

from pydantic import ConfigDict, Field
from pydantic import ConfigDict, Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -791,8 +791,8 @@ class TimeseriesSchema(SchemaBaseModel):
"""
Schema for timeseries data type
"""
_metadata_type__ = "indicator"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("indicator")
_metadata_type_version__:str = PrivateAttr("0.1.0")

idno: Optional[str] = Field(
None, description="Project unique identifier", title="Project unique identifier"
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/indicators_db_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field
from pydantic import ConfigDict, Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -432,8 +432,8 @@ class TimeseriesDatabaseSchema(SchemaBaseModel):
"""
Schema for timeseries database
"""
_metadata_type__ = "indicators_db"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("indicators_db")
_metadata_type_version__:str = PrivateAttr("0.1.0")

published: Optional[int] = Field(
0, description="0=draft, 1=published", title="Status"
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/microdata_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Union

from pydantic import ConfigDict, Field, constr
from pydantic import ConfigDict, Field, constr, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -1222,8 +1222,8 @@ class DdiSchema(SchemaBaseModel):
"""
Schema for Microdata data type based on DDI 2.5
"""
_metadata_type__ = "microdata"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("microdata")
_metadata_type_version__:str = PrivateAttr("0.1.0")

doc_desc: Optional[DocDesc] = None
study_desc: Optional[StudyDesc] = None
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/resource_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from typing import Optional

from pydantic import Field
from pydantic import Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand All @@ -14,8 +14,8 @@ class Model(SchemaBaseModel):
"""
External resource schema
"""
_metadata_type__ = "resource"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("resource")
_metadata_type_version__:str = PrivateAttr("0.1.0")

dctype: Optional[str] = Field(
"doc/oth",
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/script_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field
from pydantic import ConfigDict, Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -682,8 +682,8 @@ class ResearchProjectSchemaDraft(SchemaBaseModel):
"""
Schema for documenting research projects and data analysis scripts
"""
_metadata_type__ = "script"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("script")
_metadata_type_version__:str = PrivateAttr("0.1.0")

repositoryid: Optional[str] = Field(
None,
Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field, RootModel
from pydantic import ConfigDict, Field, RootModel, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -525,8 +525,8 @@ class Model(SchemaBaseModel):
"""
Draft Schema for Table data type
"""
_metadata_type__ = "table"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("table")
_metadata_type_version__:str = PrivateAttr("0.1.0")

repositoryid: Optional[str] = Field(
None,
Expand Down
2 changes: 1 addition & 1 deletion pydantic_schemas/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_list_annotation(anno: typing._UnionGenericAlias) -> bool:
def is_optional_list(anno: typing._UnionGenericAlias) -> bool:
if is_optional_annotation(anno):
args = typing.get_args(anno)
if len(args) == 1 and is_list_annotation(args[0]):
if len(args) == 2 and is_list_annotation(args[0]):
return True
return False

Expand Down
6 changes: 3 additions & 3 deletions pydantic_schemas/video_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import ConfigDict, Field
from pydantic import ConfigDict, Field, PrivateAttr

from .utils.schema_base_model import SchemaBaseModel

Expand Down Expand Up @@ -320,8 +320,8 @@ class Model(SchemaBaseModel):
"""
Video schema based on the elements from Dublin Core and Schema.org's VideoObject
"""
_metadata_type__ = "video"
_metadata_type_version__ = "0.1.0"
_metadata_type__:str = PrivateAttr("video")
_metadata_type_version__:str = PrivateAttr("0.1.0")

repositoryid: Optional[str] = Field(
None,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "metadataschemas"
version = "0.1.0"
description = ""
description = "A library of metadata schemas for documents, geospatial, image, indicator (timeseries), microdata (ddi), script, table and video data"
authors = ["Mehmood Asghar <[email protected]>", "Gordon Blackadder <[email protected]>"]
readme = "README.md"
packages = [
Expand Down

0 comments on commit 2c63676

Please sign in to comment.