Skip to content

Commit 648a3ee

Browse files
committed
refactor: apply suggested changes
1 parent 5e1681c commit 648a3ee

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

questionpy_server/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,25 @@ class PackagePermissionsSettings(BaseModel):
212212

213213
class EnvironmentVariables(RootModel[dict[ENVIRONMENT_VARIABLE, str]]):
214214
interpolation_pattern: ClassVar[re.Pattern] = re.compile(rf"^\$\{{({ENVIRONMENT_VARIABLE_REGEX})}}$")
215+
"""Matches environment variable interpolation syntax for replacement.
216+
217+
Identifies strings in the format ${VAR_NAME} that should be replaced with the
218+
corresponding environment variable value.
219+
220+
Example:
221+
"${MY_VAR}" matches and becomes the value of the environment variable "MY_VAR"
222+
223+
"""
224+
215225
escaped_interpolation_pattern: ClassVar[re.Pattern] = re.compile(rf"^\$(\$+\{{{ENVIRONMENT_VARIABLE_REGEX}}})$")
226+
"""Matches escaped interpolation syntax to prevent variable replacement.
227+
228+
Identifies strings with escaped dollar signs that should be unescaped rather
229+
than treated as environment variable references.
230+
231+
Example:
232+
"$${MY_VAR}" matches and becomes "${MY_VAR}" (literal string)
233+
"""
216234

217235
@model_validator(mode="after")
218236
def check_environment_variables(self) -> Self:

questionpy_server/worker/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__(self, **kwargs: Unpack[WorkerArgs]) -> None:
7474
self.package = kwargs["package"]
7575
self.worker_home = kwargs["worker_home"]
7676
self.permissions = kwargs["permissions"]
77-
self.environment_variables: dict[str, str] = kwargs["environment_variables"]
77+
self.environment_variables = kwargs["environment_variables"]
7878

7979
self.state = WorkerState.NOT_RUNNING
8080
self.loaded_packages: list[LoadedPackage] = []

questionpy_server/worker/selector/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from abc import ABC, abstractmethod
55
from typing import NamedTuple
66

7+
from questionpy_common.error import QPyBaseError
78
from questionpy_server.cache import LRUCacheMemory
89
from questionpy_server.package import Package
910
from questionpy_server.settings import PackageSelector, Selectable
@@ -37,19 +38,26 @@ def _is_matching(selector: PackageSelector, query: SelectorQuery) -> bool:
3738

3839

3940
class Selector[T: Selectable, V](ABC):
41+
"""Provides helpful methods for getting package and request specific data."""
42+
4043
def __init__(self, selectables: list[T]):
4144
self._cache: LRUCacheMemory[SelectorQuery, V] = LRUCacheMemory(max_size=128)
4245
self._selectables = selectables
4346

4447
def _get_matching(self, query: SelectorQuery) -> T | None:
48+
"""Gets the first matching selectable, if any.
49+
50+
It assumes that the selectables are ordered from least specific to most specific.
51+
"""
4552
for selectable in reversed(self._selectables):
4653
if _is_matching(selectable.package_selector, query):
4754
return selectable
4855
return None
4956

5057
def get(self, query: SelectorQuery) -> V:
51-
if cached_environment_variables := self._cache.get(query):
52-
return cached_environment_variables
58+
"""Gets the result of the query, which may be cached."""
59+
if cached := self._cache.get(query):
60+
return cached
5361

5462
result = self._get(query)
5563
self._cache.put(query, result)

questionpy_server/worker/selector/environment_variables.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ class PackageEnvironmentVariablesError(QPyBaseError):
1111

1212

1313
class EnvironmentVariablesHandler(Selector[SpecificPackageEnvironmentVariables, dict[str, str]]):
14+
"""Handles environment variables for a request."""
15+
1416
def __init__(self, settings: EnvironmentVariablesSettings):
1517
super().__init__(settings.packages)
1618

1719
self._global_environment_variables = settings.global_.root
1820

1921
def _get(self, query: SelectorQuery) -> dict[str, str]:
20-
"""Gets the environment variables for a package."""
2122
environment_variables = self._global_environment_variables.copy()
2223

2324
if (specific := self._get_matching(query)) and specific.environment_variables:
@@ -34,11 +35,3 @@ def _get(self, query: SelectorQuery) -> dict[str, str]:
3435
raise PackageEnvironmentVariablesError(msg)
3536

3637
return environment_variables
37-
38-
def get(self, query: SelectorQuery) -> dict[str, str]:
39-
"""Gets the environment variables for a package.
40-
41-
Raises:
42-
PackageEnvironmentVariablesError: If the server is not providing the requested environment variables.
43-
"""
44-
return super().get(query)

questionpy_server/worker/selector/permissions.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
PackagePermissionsSettings,
1313
SpecificPackagePermissions,
1414
)
15-
from questionpy_server.worker.selector import Selector, SelectorQuery
15+
from questionpy_server.worker.selector import Selector, SelectorError, SelectorQuery
1616

1717
_log = logging.getLogger(__name__)
1818

@@ -90,11 +90,3 @@ def _get(self, query: SelectorQuery) -> EnvironmentPackagePermissions:
9090
requested_permissions.lms_attributes.intersection_update(auto_grant_permissions.lms_attributes)
9191

9292
return EnvironmentPackagePermissions(**requested_permissions.model_dump())
93-
94-
def get(self, query: SelectorQuery) -> EnvironmentPackagePermissions:
95-
"""Gets the effective permissions for a package.
96-
97-
Raises:
98-
PackagePermissionError: If the requested package permissions are not granted.
99-
"""
100-
return super().get(query)

0 commit comments

Comments
 (0)