Skip to content

Commit 0bb9ec9

Browse files
committed
Moving Entity.__init__ to static.py to avoid overwrite by fetch_schema
Has implications on the placement of methods and classes in osw.utils.strings, as this module imports osw.model.static.Basemodel - we need to avoid circular imports
1 parent ac7f8f3 commit 0bb9ec9

9 files changed

Lines changed: 399 additions & 391 deletions

File tree

‎scripts/migration/file_page_migration.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import osw.wiki_tools as wt
1313
from osw.core import OSW
1414
from osw.model.entity import Label, WikiFile
15+
from osw.utils.regex import match_first_regex_pattern, test_regex_pattern
1516
from osw.utils.regex_pattern import REGEX_PATTERN_LIB, REGEX_PATTERN_LIST
16-
from osw.utils.strings import match_first_regex_pattern, test_regex_pattern
1717
from osw.utils.util import parallelize
1818
from osw.wtsite import WtPage, WtSite
1919

‎src/osw/controller/page_package.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from osw.model import page_package as package
1515
from osw.model.page_package import NAMESPACE_CONST_TO_NAMESPACE_MAPPING
1616
from osw.model.static import OswBaseModel
17-
from osw.utils.strings import RegExPatternExtended
17+
from osw.utils.regex import RegExPatternExtended
1818
from osw.wtsite import WtSite
1919

2020
# Definition of constants

‎src/osw/data/import_utility.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
from jsonpath_ng import ext as jp
1313
from pydantic.v1.fields import ModelField
1414

15-
import osw.utils.strings as strutil
1615
from osw import wiki_tools as wt
1716
from osw.auth import CredentialManager
1817
from osw.core import OSW
1918
from osw.model import entity as model
19+
from osw.utils.regex import MatchResult, RegExPatternExtended
2020
from osw.utils.regex_pattern import REGEX_PATTERN_LIB, REGEX_PATTERN_LIST
2121
from osw.wtsite import WtSite
2222

@@ -502,8 +502,8 @@ def nan_empty_or_none(inp: Any) -> bool:
502502

503503

504504
def regex_match_list(
505-
pattern: Union[str, strutil.RegExPatternExtended], list_of_strings: List[str]
506-
) -> List[Union[str, strutil.MatchResult]]:
505+
pattern: Union[str, RegExPatternExtended], list_of_strings: List[str]
506+
) -> List[Union[str, MatchResult]]:
507507
"""Returns a subset of the 'list_of_strings' that matched the regex 'pattern'.
508508
509509
Parameters
@@ -522,7 +522,7 @@ def regex_match_list(
522522
if re.match(pattern=pattern, string=string):
523523
matches.append(string)
524524
return matches
525-
elif isinstance(pattern, strutil.RegExPatternExtended):
525+
elif isinstance(pattern, RegExPatternExtended):
526526
matches = []
527527
for string in list_of_strings:
528528
match_result_obj = pattern.match(string)

‎src/osw/model/entity.py‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pydantic.v1 import Field, constr
1111

1212
from osw.model.static import OswBaseModel
13-
from osw.utils.strings import pascal_case
1413

1514

1615
class ReadAccess(OswBaseModel):
@@ -102,28 +101,6 @@ class Entity(OswBaseModel):
102101
attachments: Optional[List[str]] = Field(None, title="File attachments")
103102
meta: Optional[Meta] = None
104103

105-
def __init__(self, **data):
106-
if data.get("label"):
107-
if not isinstance(data["label"], list):
108-
raise ValueError(
109-
"label must be a list of Label objects",
110-
)
111-
labels = []
112-
for label in data["label"]:
113-
if isinstance(label, dict):
114-
labels.append(Label(**label))
115-
else:
116-
# The list element should be a Label object
117-
labels.append(label)
118-
data["label"] = labels
119-
if not all(isinstance(label, Label) for label in data["label"]):
120-
raise ValueError(
121-
"label must be a list of Label objects",
122-
)
123-
if data.get("name") is None and "label" in data:
124-
data["name"] = pascal_case(data["label"][0].text)
125-
super().__init__(**data)
126-
127104

128105
class ObjectStatement(OswBaseModel):
129106
rdf_type: Optional[Any] = "rdf:Statement"

‎src/osw/model/static.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
This module is to be imported in the dynamically created and updated entity.py module.
33
"""
44

5-
from typing import TYPE_CHECKING, Type, TypeVar, Union
5+
from typing import TYPE_CHECKING, Literal, Optional, Type, TypeVar, Union
66
from uuid import UUID
77

8-
from pydantic.v1 import BaseModel
8+
from pydantic.v1 import BaseModel, Field, constr
99

1010
from osw.custom_types import NoneType
11+
from osw.utils.strings import pascal_case
1112

1213
T = TypeVar("T", bound=BaseModel)
1314

@@ -88,6 +89,31 @@ class Config:
8889
validate_assignment = True
8990
# Ensures that the assignment of a value to a field is validated
9091

92+
def __init__(self, **data):
93+
if data.get("label"):
94+
if not isinstance(data["label"], list):
95+
raise ValueError(
96+
"label must be a list of Label objects",
97+
)
98+
labels = []
99+
for label in data["label"]:
100+
if isinstance(label, dict):
101+
labels.append(Label(**label))
102+
else:
103+
# The list element should be a Label object
104+
labels.append(label)
105+
data["label"] = labels
106+
# Ensure that the label attribute is a list of Label objects, but use
107+
# custom_isinstance to avoid circular imports and ValidationError since
108+
# osw.model.entity defines its own Label class
109+
if not all(custom_isinstance(label, "Label") for label in data["label"]):
110+
raise ValueError(
111+
"label must be a list of Label objects",
112+
)
113+
if data.get("name") is None and "label" in data:
114+
data["name"] = pascal_case(data["label"][0].text)
115+
super().__init__(**data)
116+
91117
def full_dict(self, **kwargs): # extent BaseClass export function
92118
d = super().dict(**kwargs)
93119
for key in ("_osl_template", "_osl_footer"):
@@ -300,3 +326,8 @@ class Ontology(OswBaseModel):
300326
name: str
301327
prefix_name: str
302328
link: str
329+
330+
331+
class Label(OswBaseModel):
332+
text: constr(min_length=1) = Field(..., title="Text")
333+
lang: Optional[Literal["en", "de"]] = Field("en", title="Lang code")

0 commit comments

Comments
 (0)