Skip to content

Commit

Permalink
Support for webform changes
Browse files Browse the repository at this point in the history
Arjun Gopalakrishnan committed Dec 2, 2024
1 parent a3b7c2e commit 02ab43e
Showing 18 changed files with 191 additions and 138 deletions.
4 changes: 2 additions & 2 deletions dsms/__init__.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@

from dsms.apps import AppConfig
from dsms.core.configuration import Configuration
from dsms.core.context import Context
from dsms.core.session import Session
from dsms.core.dsms import DSMS
from dsms.knowledge.kitem import KItem
from dsms.knowledge.ktype import KType

__all__ = ["DSMS", "Configuration", "Context", "KItem", "KType", "AppConfig"]
__all__ = ["DSMS", "Configuration", "Session", "KItem", "KType", "AppConfig"]
38 changes: 19 additions & 19 deletions dsms/apps/config.py
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
logger.propagate = False

if TYPE_CHECKING:
from dsms import DSMS, Context
from dsms import DSMS, Session


class AppConfig(BaseModel):
@@ -76,15 +76,15 @@ def __init__(self, **kwargs: "Any") -> None:
# add app config to buffer
if (
not self.in_backend
and self.name not in self.context.buffers.created
and self.name not in self.session.buffers.created
):
logger.debug(
"""Marking AppConfig with name `%s` as created
and updated during AppConfig initialization.""",
self.name,
)
self.context.buffers.created.update({self.name: self})
self.context.buffers.updated.update({self.name: self})
self.session.buffers.created.update({self.name: self})
self.session.buffers.updated.update({self.name: self})

logger.debug("AppConfig initialization successful.")

@@ -94,12 +94,12 @@ def __setattr__(self, name, value) -> None:
logger.debug(
"Setting property with key `%s` on KItem level: %s.", name, value
)
if self.name not in self.context.buffers.updated:
if self.name not in self.session.buffers.updated:
logger.debug(
"Setting AppConfig with name `%s` as updated during AppConfig.__setattr__",
self.name,
)
self.context.buffers.updated.update({self.name: self})
self.session.buffers.updated.update({self.name: self})

def __str__(self) -> str:
"""Pretty print the app config fields"""
@@ -156,20 +156,20 @@ def validate_specification(cls, self: "AppConfig") -> str:
raise RuntimeError(
f"Invalid yaml specification path: `{error.args[0]}`"
) from error
self.context.buffers.updated.update({self.name: self})
self.session.buffers.updated.update({self.name: self})
elif isinstance(self.specification, dict) and self.in_backend:
spec = _get_app_specification(self.name)
if (
not yaml.safe_load(spec) == self.specification
and self.name not in self.context.buffers.updated
and self.name not in self.session.buffers.updated
):
self.context.buffers.updated.update({self.name: self})
self.session.buffers.updated.update({self.name: self})
elif (
isinstance(self.specification, dict)
and not self.in_backend
and self.name not in self.context.buffers.updated
and self.name not in self.session.buffers.updated
):
self.context.buffers.updated.update({self.name: self})
self.session.buffers.updated.update({self.name: self})
if self.expose_sdk_config:
self.specification["spec"]["arguments"]["parameters"] += [
{
@@ -190,20 +190,20 @@ def in_backend(self) -> bool:
return _app_spec_exists(self.name)

@property
def context(cls) -> "Context":
"""Getter for Context"""
def session(cls) -> "Session":
"""Getter for Session"""
from dsms import ( # isort:skip
Context,
Session,
)

return Context
return Session

@property
def dsms(self) -> "DSMS":
"""DSMS context getter"""
return self.context.dsms
"""DSMS session getter"""
return self.session.dsms

@dsms.setter
def dsms(self, value: "DSMS") -> None:
"""DSMS context setter"""
self.context.dsms = value
"""DSMS session setter"""
self.session.dsms = value
4 changes: 2 additions & 2 deletions dsms/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""DSMS core module
"""
from dsms.core.configuration import Configuration
from dsms.core.context import Context
from dsms.core.session import Session

__all__ = ["Context", "Configuration"]
__all__ = ["Session", "Configuration"]
16 changes: 8 additions & 8 deletions dsms/core/dsms.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@

from dsms.apps.utils import _get_available_apps_specs
from dsms.core.configuration import Configuration
from dsms.core.context import Context
from dsms.core.session import Session
from dsms.core.utils import _ping_dsms
from dsms.knowledge.sparql_interface import SparqlInterface
from dsms.knowledge.utils import _search
@@ -24,7 +24,7 @@
from typing import Optional, Union

from dsms.apps import AppConfig
from dsms.core.context import Buffers
from dsms.core.session import Buffers
from dsms.knowledge.kitem import KItem
from dsms.knowledge.ktype import KType
from dsms.knowledge.search import SearchResult
@@ -49,7 +49,7 @@ class DSMS:
"""

_context = Context
_session = Session

def __init__(
self,
@@ -73,7 +73,7 @@ def __init__(

self._config = None
self._ktypes = None
self._context.dsms = self
self._session.dsms = self

if env:
if not os.path.exists(env):
@@ -207,12 +207,12 @@ def app_configs(cls) -> "List[AppConfig]":
@property
def buffers(cls) -> "Buffers":
"""Return buffers of the DSMS session"""
return cls._context.buffers
return cls._session.buffers

@property
def context(cls) -> "Context":
"""Return DSMS context"""
return cls._context
def context(cls) -> "Session":
"""Return DSMS session"""
return cls._session

@classmethod
def __get_pydantic_core_schema__(cls):
2 changes: 1 addition & 1 deletion dsms/core/context.py → dsms/core/session.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class Buffers:
deleted: "Dict[UUID, KItem]" = {}


class Context:
class Session:
"""Object giving the current DSMS context."""

dsms: "Optional[DSMS]" = None
14 changes: 7 additions & 7 deletions dsms/core/utils.py
Original file line number Diff line number Diff line change
@@ -14,16 +14,16 @@

def _kitem_id2uri(kitem_id: UUID) -> str:
"Convert a kitem id in the DSMS to the full resolvable URI"
from dsms import Context
from dsms import Session

return urljoin(str(Context.dsms.config.host_url), str(kitem_id))
return urljoin(str(Session.dsms.config.host_url), str(kitem_id))


def _uri2kitem_idi(uri: str) -> str:
"Extract the kitem id from an URI of the DSMS"
from dsms import Context
from dsms import Session

return uri.replace(f"{Context.dsms.config.host_url}/", "").split("/")[0]
return uri.replace(f"{Session.dsms.config.host_url}/", "").split("/")[0]


def _ping_dsms():
@@ -35,9 +35,9 @@ def _perform_request(route: str, method: str, **kwargs: "Any") -> Response:
"""Perform a general request for a certain route and with a certain method.
Kwargs are general arguments which can be passed to the `requests.request`-function.
"""
from dsms import Context
from dsms import Session

dsms = Context.dsms
dsms = Session.dsms
response = requests.request(
method,
url=urljoin(str(dsms.config.host_url), route),
@@ -46,7 +46,7 @@ def _perform_request(route: str, method: str, **kwargs: "Any") -> Response:
verify=dsms.config.ssl_verify,
**kwargs,
)
response.encoding = Context.dsms.config.encoding
response.encoding = Session.dsms.config.encoding
return response


30 changes: 15 additions & 15 deletions dsms/knowledge/kitem.py
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@
from dsms.knowledge.sparql_interface.utils import _get_subgraph # isort:skip

if TYPE_CHECKING:
from dsms import Context
from dsms import Session
from dsms.core.dsms import DSMS

logger = logging.getLogger(__name__)
@@ -411,34 +411,34 @@ def validate_user_groups(
@classmethod
def validate_created(cls, value: str) -> Any:
"""Convert the str for `created_at` in to a `datetime`-object"""
from dsms import Context
from dsms import Session

if isinstance(value, str):
value = datetime.strptime(
value, Context.dsms.config.datetime_format
value, Session.dsms.config.datetime_format
)
return value

@field_validator("updated_at")
@classmethod
def validate_updated(cls, value: str) -> Any:
"""Convert the str for `created_at` in to a `datetime`-object"""
from dsms import Context
from dsms import Session

if isinstance(value, str):
value = datetime.strptime(
value, Context.dsms.config.datetime_format
value, Session.dsms.config.datetime_format
)
return value

@field_validator("ktype_id")
@classmethod
def validate_ktype_id(cls, value: Union[str, Enum]) -> KType:
"""Validate the ktype id of the KItem"""
from dsms import Context
from dsms import Session

if isinstance(value, str):
value = Context.ktypes.get(value)
value = Session.ktypes.get(value)
if not value:
raise TypeError(
f"KType for `ktype_id={value}` does not exist."
@@ -452,13 +452,13 @@ def validate_ktype(
cls, value: Optional[Union[KType, str, Enum]], info: ValidationInfo
) -> KType:
"""Validate the ktype of the KItem"""
from dsms import Context
from dsms import Session

if not value:
value = info.data.get("ktype_id")

if isinstance(value, str):
value = Context.ktypes.get(value)
value = Session.ktypes.get(value)
if not value:
raise TypeError(
f"KType for `ktype_id={value}` does not exist."
@@ -479,7 +479,7 @@ def validate_in_backend(cls, value: bool, info: ValidationInfo) -> bool:
@classmethod
def validate_slug(cls, value: str, info: ValidationInfo) -> str:
"""Validate slug"""
from dsms import Context
from dsms import Session

ktype_id = info.data["ktype_id"]
kitem_id = info.data.get("id")
@@ -499,7 +499,7 @@ def validate_slug(cls, value: str, info: ValidationInfo) -> str:
raise ValueError(
"Slug length must have a minimum length of 4."
)
if Context.dsms.config.individual_slugs:
if Session.dsms.config.individual_slugs:
value += f"-{str(kitem_id).split('-', maxsplit=1)[0]}"
if not kitem_exists and not _slug_is_available(ktype, value):
raise ValueError(f"Slug for `{value}` is already taken.")
@@ -615,13 +615,13 @@ def subgraph(cls) -> Optional[Graph]:
)

@property
def context(cls) -> "Context":
"""Getter for Context"""
def context(cls) -> "Session":
"""Getter for Session"""
from dsms import ( # isort:skip
Context,
Session,
)

return Context
return Session

@property
def url(cls) -> str:
Loading

0 comments on commit 02ab43e

Please sign in to comment.