Skip to content

Commit 5131891

Browse files
committed
✨ Refactor service key handling by consolidating key prefixes and regex patterns for improved maintainability
1 parent 364fd9c commit 5131891

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

packages/models-library/src/models_library/services_regex.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,69 @@
1515
FILENAME_RE = r".+"
1616

1717

18-
# Add key prefixes for dynamic and computational services
19-
DYNAMIC_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/dynamic"
20-
COMPUTATIONAL_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/comp"
21-
FRONTEND_SERVICE_KEY_PREFIX: Final[str] = "simcore/services/frontend"
22-
18+
SERVICE_TYPE_TO_NAME_MAP = MappingProxyType(
19+
{
20+
ServiceType.COMPUTATIONAL: "comp",
21+
ServiceType.DYNAMIC: "dynamic",
22+
ServiceType.FRONTEND: "frontend",
23+
}
24+
)
2325

26+
# e.g. simcore/services/comp/opencor
2427
SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
25-
rf"^(?P<key_prefix>{COMPUTATIONAL_SERVICE_KEY_PREFIX}|{DYNAMIC_SERVICE_KEY_PREFIX}|{FRONTEND_SERVICE_KEY_PREFIX})"
26-
r"/(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
28+
r"^simcore/services/"
29+
rf"(?P<type>({ '|'.join(SERVICE_TYPE_TO_NAME_MAP.values()) }))/"
30+
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
2731
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
2832
)
33+
34+
# e.g. simcore%2Fservices%2Fcomp%2Fopencor
2935
SERVICE_ENCODED_KEY_RE: Final[re.Pattern[str]] = re.compile(
30-
rf"^(?P<key_prefix>{COMPUTATIONAL_SERVICE_KEY_PREFIX.replace('/', '%2F')}|{DYNAMIC_SERVICE_KEY_PREFIX.replace('/', '%2F')}|{FRONTEND_SERVICE_KEY_PREFIX.replace('/', '%2F')})"
31-
r"(?P<subdir>(%2F[a-z0-9][a-z0-9_.-]*)*%2F)?"
36+
r"^simcore%2Fservices%2F"
37+
rf"(?P<type>({'|'.join(SERVICE_TYPE_TO_NAME_MAP.values())}))%2F"
38+
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*%2F)*"
3239
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
3340
)
3441

3542

36-
DYNAMIC_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
37-
rf"^{DYNAMIC_SERVICE_KEY_PREFIX}/"
38-
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
39-
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
40-
)
41-
DYNAMIC_SERVICE_KEY_FORMAT: Final[str] = (
42-
f"{DYNAMIC_SERVICE_KEY_PREFIX}/{{service_name}}"
43-
)
43+
def create_key_prefix(service_type: ServiceType) -> str:
44+
return f"simcore/services/{SERVICE_TYPE_TO_NAME_MAP[service_type]}"
4445

4546

46-
COMPUTATIONAL_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
47-
rf"^{COMPUTATIONAL_SERVICE_KEY_PREFIX}/"
48-
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
49-
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
50-
)
51-
COMPUTATIONAL_SERVICE_KEY_FORMAT: Final[str] = (
52-
f"{COMPUTATIONAL_SERVICE_KEY_PREFIX}/{{service_name}}"
47+
COMPUTATIONAL_SERVICE_KEY_PREFIX: Final[str] = create_key_prefix(
48+
ServiceType.COMPUTATIONAL
5349
)
50+
DYNAMIC_SERVICE_KEY_PREFIX: Final[str] = create_key_prefix(ServiceType.DYNAMIC)
51+
FRONTEND_SERVICE_KEY_PREFIX: Final[str] = create_key_prefix(ServiceType.FRONTEND)
5452

5553

56-
FRONTEND_SERVICE_KEY_RE: Final[re.Pattern[str]] = re.compile(
57-
rf"^{FRONTEND_SERVICE_KEY_PREFIX}/"
58-
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
59-
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
54+
def create_key_regex(service_type: ServiceType) -> re.Pattern[str]:
55+
return re.compile(
56+
rf"^simcore/services/{SERVICE_TYPE_TO_NAME_MAP[service_type]}/"
57+
r"(?P<subdir>[a-z0-9][a-z0-9_.-]*/)*"
58+
r"(?P<name>[a-z0-9-_]+[a-z0-9])$"
59+
)
60+
61+
62+
def create_key_format(service_type: ServiceType) -> str:
63+
return f"simcore/services/{SERVICE_TYPE_TO_NAME_MAP[service_type]}/{{service_name}}"
64+
65+
66+
COMPUTATIONAL_SERVICE_KEY_RE: Final[re.Pattern[str]] = create_key_regex(
67+
ServiceType.COMPUTATIONAL
6068
)
61-
FRONTEND_SERVICE_KEY_FORMAT: Final[str] = (
62-
f"{FRONTEND_SERVICE_KEY_PREFIX}/{{service_name}}"
69+
COMPUTATIONAL_SERVICE_KEY_FORMAT: Final[str] = create_key_format(
70+
ServiceType.COMPUTATIONAL
6371
)
6472

73+
DYNAMIC_SERVICE_KEY_RE: Final[re.Pattern[str]] = create_key_regex(ServiceType.DYNAMIC)
74+
DYNAMIC_SERVICE_KEY_FORMAT: Final[str] = create_key_format(ServiceType.DYNAMIC)
75+
76+
FRONTEND_SERVICE_KEY_RE: Final[re.Pattern[str]] = create_key_regex(ServiceType.FRONTEND)
77+
FRONTEND_SERVICE_KEY_FORMAT: Final[str] = create_key_format(ServiceType.FRONTEND)
78+
6579

66-
SERVICE_TYPE_PREFIXES = MappingProxyType(
80+
SERVICE_TYPE_TO_PREFIX_MAP = MappingProxyType(
6781
{
6882
ServiceType.COMPUTATIONAL: COMPUTATIONAL_SERVICE_KEY_PREFIX,
6983
ServiceType.DYNAMIC: DYNAMIC_SERVICE_KEY_PREFIX,
@@ -72,5 +86,5 @@
7286
)
7387

7488
assert all( # nosec
75-
not prefix.endswith("/") for prefix in SERVICE_TYPE_PREFIXES.values()
89+
not prefix.endswith("/") for prefix in SERVICE_TYPE_TO_PREFIX_MAP.values()
7690
), "Service type prefixes must not end with '/'"

packages/service-integration/src/service_integration/osparc_config.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" 'osparc config' is a set of stardard file forms (yaml) that the user fills to describe how his/her service works and
1+
"""'osparc config' is a set of stardard file forms (yaml) that the user fills to describe how his/her service works and
22
integrates with osparc.
33
44
- config files are stored under '.osparc/' folder in the root repo folder (analogous to other configs like .github, .vscode, etc)
@@ -26,11 +26,7 @@
2626
RestartPolicy,
2727
)
2828
from models_library.service_settings_nat_rule import NATRule
29-
from models_library.services import BootOptions, ServiceMetaDataPublished, ServiceType
30-
from models_library.services_regex import (
31-
COMPUTATIONAL_SERVICE_KEY_FORMAT,
32-
DYNAMIC_SERVICE_KEY_FORMAT,
33-
)
29+
from models_library.services import BootOptions, ServiceMetaDataPublished
3430
from models_library.services_types import ServiceKey
3531
from models_library.utils.labels_annotations import (
3632
OSPARC_LABEL_PREFIXES,
@@ -62,12 +58,6 @@
6258
OSPARC_CONFIG_RUNTIME_NAME: Final[str] = "runtime.yml"
6359

6460

65-
SERVICE_KEY_FORMATS = {
66-
ServiceType.COMPUTATIONAL: COMPUTATIONAL_SERVICE_KEY_FORMAT,
67-
ServiceType.DYNAMIC: DYNAMIC_SERVICE_KEY_FORMAT,
68-
}
69-
70-
7161
class DockerComposeOverwriteConfig(ComposeSpecification):
7262
"""Content of docker-compose.overwrite.yml configuration file"""
7363

@@ -231,9 +221,9 @@ class RuntimeConfig(BaseModel):
231221

232222
containers_allowed_outgoing_internet: set[str] | None = None
233223

234-
settings: Annotated[
235-
list[SettingsItem], Field(default_factory=list)
236-
] = DEFAULT_FACTORY
224+
settings: Annotated[list[SettingsItem], Field(default_factory=list)] = (
225+
DEFAULT_FACTORY
226+
)
237227

238228
@model_validator(mode="before")
239229
@classmethod

services/catalog/src/simcore_service_catalog/repository/_services_sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sqlalchemy as sa
44
from models_library.products import ProductName
55
from models_library.services_regex import (
6-
SERVICE_TYPE_PREFIXES,
6+
SERVICE_TYPE_TO_PREFIX_MAP,
77
)
88
from models_library.services_types import ServiceKey, ServiceVersion
99
from models_library.users import UserID
@@ -121,7 +121,7 @@ def apply_services_filters(
121121
filters: ServiceFiltersDB,
122122
):
123123
if filters.service_type:
124-
prefix = SERVICE_TYPE_PREFIXES.get(filters.service_type)
124+
prefix = SERVICE_TYPE_TO_PREFIX_MAP.get(filters.service_type)
125125
if prefix is None:
126126
msg = f"Undefined service type {filters.service_type}. Please update prefix expressions"
127127
raise ValueError(msg)

services/catalog/tests/unit/with_dbs/test_repositories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from models_library.services_regex import (
1818
COMPUTATIONAL_SERVICE_KEY_PREFIX,
1919
DYNAMIC_SERVICE_KEY_PREFIX,
20-
SERVICE_TYPE_PREFIXES,
20+
SERVICE_TYPE_TO_PREFIX_MAP,
2121
)
2222
from models_library.users import UserID
2323
from packaging import version
@@ -633,7 +633,7 @@ async def test_get_service_history_page(
633633

634634

635635
@pytest.mark.parametrize(
636-
"expected_service_type,service_prefix", SERVICE_TYPE_PREFIXES.items()
636+
"expected_service_type,service_prefix", SERVICE_TYPE_TO_PREFIX_MAP.items()
637637
)
638638
async def test_get_service_history_page_with_filters(
639639
target_product: ProductName,

0 commit comments

Comments
 (0)