Skip to content

Commit

Permalink
refactor: split up models/helpers.py (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jan 24, 2024
1 parent fd05080 commit f97cce8
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 150 deletions.
8 changes: 4 additions & 4 deletions pyproject.toml
Expand Up @@ -73,8 +73,6 @@ check-links = """
"""
darglint = """
find . -name "*.py" \
-not -path "./src/dsp_tools/models/*" \
-not -path "./src/dsp_tools/commands/xmlupload/models/*" \
-not -path "./src/dsp_tools/commands/project/models/*" \
-not -path "./.git/*" \
-not -path "./.venv/*" \
Expand Down Expand Up @@ -132,13 +130,15 @@ ignore_missing_imports = true # TODO: deactivate
show_column_numbers = true
strict = true
exclude = [
"src/dsp_tools/models/helpers.py", # TODO: activate this
"src/dsp_tools/models/datetimestamp.py", # TODO: activate this
"src/dsp_tools/models/langstring.py", # TODO: activate this
"src/dsp_tools/models/projectContext.py", # TODO: activate this
"src/dsp_tools/commands/project/models/context.py", # TODO: activate this
"src/dsp_tools/commands/project/models/group.py", # TODO: activate this
"src/dsp_tools/commands/project/models/helpers.py", # TODO: activate this
"src/dsp_tools/commands/project/models/listnode.py", # TODO: activate this
"src/dsp_tools/commands/project/models/ontology.py", # TODO: activate this
"src/dsp_tools/commands/project/models/project.py", # TODO: activate this
"src/dsp_tools/models/projectContext.py", # TODO: activate this
"src/dsp_tools/commands/project/models/propertyclass.py", # TODO: activate this
"src/dsp_tools/commands/project/models/resourceclass.py", # TODO: activate this
"src/dsp_tools/commands/project/models/user.py", # TODO: activate this
Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/excel2xml/excel2xml_lib.py
Expand Up @@ -14,8 +14,8 @@
from regex import Match

from dsp_tools.commands.excel2xml.propertyelement import PropertyElement
from dsp_tools.models.datetimestamp import DateTimeStamp
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import DateTimeStamp
from dsp_tools.utils.date_util import is_full_date
from dsp_tools.utils.shared import check_notna, simplify_name, validate_xml_against_schema
from dsp_tools.utils.uri_util import is_uri
Expand Down
4 changes: 3 additions & 1 deletion src/dsp_tools/commands/project/create/project_create.py
Expand Up @@ -9,14 +9,16 @@
from dsp_tools.commands.excel2json.lists import expand_lists_from_excel
from dsp_tools.commands.project.create.project_create_lists import create_lists_on_server
from dsp_tools.commands.project.create.project_validate import validate_project
from dsp_tools.commands.project.models.context import Context
from dsp_tools.commands.project.models.group import Group
from dsp_tools.commands.project.models.helpers import Cardinality
from dsp_tools.commands.project.models.ontology import Ontology
from dsp_tools.commands.project.models.project import Project
from dsp_tools.commands.project.models.propertyclass import PropertyClass
from dsp_tools.commands.project.models.resourceclass import ResourceClass
from dsp_tools.commands.project.models.user import User
from dsp_tools.models.datetimestamp import DateTimeStamp
from dsp_tools.models.exceptions import BaseError, UserError
from dsp_tools.models.helpers import Cardinality, Context, DateTimeStamp
from dsp_tools.models.langstring import LangString
from dsp_tools.utils.connection import Connection
from dsp_tools.utils.connection_live import ConnectionLive
Expand Down
@@ -1,54 +1,16 @@
from __future__ import annotations

from dataclasses import dataclass
from enum import Enum, unique
from typing import Any, Optional, Pattern, Union
from typing import Optional, Pattern

import regex

from dsp_tools.commands.project.models.helpers import ContextType, OntoIri
from dsp_tools.models.exceptions import BaseError
from dsp_tools.utils.iri_util import is_iri

#
# here we do some data typing that should help
#


@dataclass(frozen=True)
class OntoIri:
"""
Holds an ontology IRI
Attributes:
iri: the ontology IRI
hashtag: True if "#" is used to separate elements, False if element name is appended after "/"
"""

iri: str
hashtag: bool


ContextType = dict[str, OntoIri]


@unique
class Actions(Enum):
Create = 1
Read = 2
Update = 3
Delete = 4


@unique
class Cardinality(Enum):
C_1 = "1"
C_0_1 = "0-1"
C_1_n = "1-n"
C_0_n = "0-n"


class ContextIterator:
_context: "Context"
_context: Context
_prefixes: list[str]
_index: int

Expand Down Expand Up @@ -367,90 +329,3 @@ def toJsonObj(self) -> dict[str, str]:
def get_externals_used(self) -> dict[str, str]:
exclude = ["rdf", "rdfs", "owl", "xsd", "knora-api", "salsah-gui"]
return {prefix: onto.iri for prefix, onto in self._context.items() if prefix not in exclude}


class DateTimeStamp:
"""
Class to hold and process an xsd:dateTimeStamp
"""

_dateTimeStamp: str
_validation_regex = (
r"^-?([1-9][0-9]{3,}|0[0-9]{3})"
r"-(0[1-9]|1[0-2])"
r"-(0[1-9]|[12][0-9]|3[01])"
r"T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))"
r"(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))$"
)

def __init__(self, val: Any):
"""
The constructor works for different inputs:
- a string
- an instance of "DateTimeStamp"
- json-ld construct of the form { "@type": "xsd:dateTimeStamp", "@value": "date-str" }
:param val: xsd:dateTimeStamp as string, instance of "DateTimeStamp" or json-ld construct
"""
if isinstance(val, str):
if not regex.search(self._validation_regex, val):
raise BaseError(f"Invalid xsd:dateTimeStamp: '{val}'")
self._dateTimeStamp = val
elif isinstance(val, DateTimeStamp):
self._dateTimeStamp = str(val)
else:
if val.get("@type") == "xsd:dateTimeStamp" and regex.search(self._validation_regex, str(val.get("@value"))):
self._dateTimeStamp = val["@value"]
else:
raise BaseError(f"Invalid xsd:dateTimeStamp: '{val}'")

def __eq__(self, other: Union[str, "DateTimeStamp"]) -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp == other._dateTimeStamp

def __lt__(self, other: "DateTimeStamp") -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp < other._dateTimeStamp

def __le__(self, other: "DateTimeStamp") -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp <= other._dateTimeStamp

def __gt__(self, other: "DateTimeStamp") -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp > other._dateTimeStamp

def __ge__(self, other: "DateTimeStamp") -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp >= other._dateTimeStamp

def __ne__(self, other: DateTimeStamp) -> bool:
if isinstance(other, str):
other = DateTimeStamp(other)
return self._dateTimeStamp != other._dateTimeStamp

def __str__(self: "DateTimeStamp") -> str:
return self._dateTimeStamp

def toJsonObj(self) -> dict[str, str]:
return {"@type": "xsd:dateTimeStamp", "@value": self._dateTimeStamp}


class WithId:
"""
Class helper to get json-ld "@id" thingies
"""

_tmp: str = None

def __init__(self, obj: Optional[dict[str, str]]):
if obj is None:
return
self._tmp = obj.get("@id")

def to_string(self) -> Optional[str]:
return self._tmp
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/project/models/group.py
Expand Up @@ -26,10 +26,10 @@
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.helpers import Actions
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import LangString
from dsp_tools.utils.connection import Connection

Expand Down
54 changes: 54 additions & 0 deletions src/dsp_tools/commands/project/models/helpers.py
@@ -0,0 +1,54 @@
from __future__ import annotations

from dataclasses import dataclass
from enum import Enum, unique
from typing import Optional


@dataclass(frozen=True)
class OntoIri:
"""
Holds an ontology IRI
Attributes:
iri: the ontology IRI
hashtag: True if "#" is used to separate elements, False if element name is appended after "/"
"""

iri: str
hashtag: bool


ContextType = dict[str, OntoIri]


@unique
class Actions(Enum):
Create = 1
Read = 2
Update = 3
Delete = 4


@unique
class Cardinality(Enum):
C_1 = "1"
C_0_1 = "0-1"
C_1_n = "1-n"
C_0_n = "0-n"


class WithId:
"""
Class helper to get json-ld "@id" thingies
"""

_tmp: str = None

def __init__(self, obj: Optional[dict[str, str]]):
if obj is None:
return
self._tmp = obj.get("@id")

def to_string(self) -> Optional[str]:
return self._tmp
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/project/models/listnode.py
Expand Up @@ -25,10 +25,10 @@
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.helpers import Actions
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import LangString, Languages
from dsp_tools.utils.connection import Connection

Expand Down
4 changes: 3 additions & 1 deletion src/dsp_tools/commands/project/models/ontology.py
Expand Up @@ -32,12 +32,14 @@

import regex

from dsp_tools.commands.project.models.context import Context
from dsp_tools.commands.project.models.helpers import Actions, WithId
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.commands.project.models.propertyclass import PropertyClass
from dsp_tools.commands.project.models.resourceclass import ResourceClass
from dsp_tools.models.datetimestamp import DateTimeStamp
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions, Context, DateTimeStamp, WithId
from dsp_tools.utils.connection import Connection


Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/project/models/project.py
Expand Up @@ -27,9 +27,9 @@
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.helpers import Actions
from dsp_tools.commands.project.models.model import Model
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import LangString, Languages
from dsp_tools.utils.connection import Connection

Expand Down
4 changes: 3 additions & 1 deletion src/dsp_tools/commands/project/models/propertyclass.py
Expand Up @@ -3,10 +3,12 @@

import regex

from dsp_tools.commands.project.models.context import Context
from dsp_tools.commands.project.models.helpers import Actions, WithId
from dsp_tools.commands.project.models.listnode import ListNode
from dsp_tools.commands.project.models.model import Model
from dsp_tools.models.datetimestamp import DateTimeStamp
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions, Context, DateTimeStamp, WithId
from dsp_tools.models.langstring import LangString, Languages
from dsp_tools.utils.connection import Connection

Expand Down
4 changes: 3 additions & 1 deletion src/dsp_tools/commands/project/models/resourceclass.py
Expand Up @@ -13,9 +13,11 @@

import regex

from dsp_tools.commands.project.models.context import Context
from dsp_tools.commands.project.models.helpers import Actions, Cardinality
from dsp_tools.commands.project.models.model import Model
from dsp_tools.models.datetimestamp import DateTimeStamp
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions, Cardinality, Context, DateTimeStamp
from dsp_tools.models.langstring import LangString, Languages
from dsp_tools.utils.connection import Connection

Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/commands/project/models/user.py
Expand Up @@ -29,10 +29,10 @@
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.group import Group
from dsp_tools.commands.project.models.helpers import Actions
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import Languages
from dsp_tools.utils.connection import Connection

Expand Down
3 changes: 0 additions & 3 deletions src/dsp_tools/commands/xmlupload/models/xmlallow.py
Expand Up @@ -19,9 +19,6 @@ def __init__(self, node: etree._Element, project_context: ProjectContext) -> Non
node: The DOM node to be processed (represents a single right in a permission set)
project_context: Context for DOM node traversal
Returns:
None
Raises:
XmlUploadError: If an upload fails
"""
Expand Down
8 changes: 4 additions & 4 deletions src/dsp_tools/commands/xmlupload/models/xmlresource.py
Expand Up @@ -6,7 +6,7 @@
from dsp_tools.commands.xmlupload.models.permission import Permissions
from dsp_tools.commands.xmlupload.models.xmlbitstream import XMLBitstream
from dsp_tools.commands.xmlupload.models.xmlproperty import XMLProperty
from dsp_tools.models.helpers import DateTimeStamp
from dsp_tools.models.datetimestamp import DateTimeStamp


@dataclass(frozen=True)
Expand Down Expand Up @@ -56,9 +56,6 @@ def __init__(self, node: etree._Element, default_ontology: str) -> None:
Args:
node: The DOM node to be processed representing a resource (which is a child of the <knora> element)
default_ontology: The default ontology (given in the attribute default-ontology of the <knora> element)
Returns:
None
"""
self.res_id = node.attrib["id"]
self.iri = node.attrib.get("iri")
Expand Down Expand Up @@ -92,6 +89,9 @@ def get_props_with_links(self) -> list[XMLProperty]:
"""
Get a list of all XMLProperties that have an outgoing link to another resource, be it a resptr-prop link
or a standoff link in a text.
Returns:
list of all XMLProperties
"""
link_properties: list[XMLProperty] = []
for prop in self.properties:
Expand Down

0 comments on commit f97cce8

Please sign in to comment.