squirrels 0.1.0__py3-none-any.whl → 0.6.0.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dateutils/__init__.py +6 -0
- dateutils/_enums.py +25 -0
- squirrels/dateutils.py → dateutils/_implementation.py +409 -380
- dateutils/types.py +6 -0
- squirrels/__init__.py +21 -18
- squirrels/_api_routes/__init__.py +5 -0
- squirrels/_api_routes/auth.py +337 -0
- squirrels/_api_routes/base.py +196 -0
- squirrels/_api_routes/dashboards.py +156 -0
- squirrels/_api_routes/data_management.py +148 -0
- squirrels/_api_routes/datasets.py +220 -0
- squirrels/_api_routes/project.py +289 -0
- squirrels/_api_server.py +552 -134
- squirrels/_arguments/__init__.py +0 -0
- squirrels/_arguments/init_time_args.py +83 -0
- squirrels/_arguments/run_time_args.py +111 -0
- squirrels/_auth.py +777 -0
- squirrels/_command_line.py +239 -107
- squirrels/_compile_prompts.py +147 -0
- squirrels/_connection_set.py +94 -0
- squirrels/_constants.py +141 -64
- squirrels/_dashboards.py +179 -0
- squirrels/_data_sources.py +570 -0
- squirrels/_dataset_types.py +91 -0
- squirrels/_env_vars.py +209 -0
- squirrels/_exceptions.py +29 -0
- squirrels/_http_error_responses.py +52 -0
- squirrels/_initializer.py +319 -110
- squirrels/_logging.py +121 -0
- squirrels/_manifest.py +357 -187
- squirrels/_mcp_server.py +578 -0
- squirrels/_model_builder.py +69 -0
- squirrels/_model_configs.py +74 -0
- squirrels/_model_queries.py +52 -0
- squirrels/_models.py +1201 -0
- squirrels/_package_data/base_project/.env +7 -0
- squirrels/_package_data/base_project/.env.example +44 -0
- squirrels/_package_data/base_project/connections.yml +16 -0
- squirrels/_package_data/base_project/dashboards/dashboard_example.py +40 -0
- squirrels/_package_data/base_project/dashboards/dashboard_example.yml +22 -0
- squirrels/_package_data/base_project/docker/.dockerignore +16 -0
- squirrels/_package_data/base_project/docker/Dockerfile +16 -0
- squirrels/_package_data/base_project/docker/compose.yml +7 -0
- squirrels/_package_data/base_project/duckdb_init.sql +10 -0
- squirrels/_package_data/base_project/gitignore +13 -0
- squirrels/_package_data/base_project/macros/macros_example.sql +17 -0
- squirrels/_package_data/base_project/models/builds/build_example.py +26 -0
- squirrels/_package_data/base_project/models/builds/build_example.sql +16 -0
- squirrels/_package_data/base_project/models/builds/build_example.yml +57 -0
- squirrels/_package_data/base_project/models/dbviews/dbview_example.sql +17 -0
- squirrels/_package_data/base_project/models/dbviews/dbview_example.yml +32 -0
- squirrels/_package_data/base_project/models/federates/federate_example.py +51 -0
- squirrels/_package_data/base_project/models/federates/federate_example.sql +21 -0
- squirrels/_package_data/base_project/models/federates/federate_example.yml +65 -0
- squirrels/_package_data/base_project/models/sources.yml +38 -0
- squirrels/_package_data/base_project/parameters.yml +142 -0
- squirrels/_package_data/base_project/pyconfigs/connections.py +19 -0
- squirrels/_package_data/base_project/pyconfigs/context.py +96 -0
- squirrels/_package_data/base_project/pyconfigs/parameters.py +141 -0
- squirrels/_package_data/base_project/pyconfigs/user.py +56 -0
- squirrels/_package_data/base_project/resources/expenses.db +0 -0
- squirrels/_package_data/base_project/resources/public/.gitkeep +0 -0
- squirrels/_package_data/base_project/resources/weather.db +0 -0
- squirrels/_package_data/base_project/seeds/seed_categories.csv +6 -0
- squirrels/_package_data/base_project/seeds/seed_categories.yml +15 -0
- squirrels/_package_data/base_project/seeds/seed_subcategories.csv +15 -0
- squirrels/_package_data/base_project/seeds/seed_subcategories.yml +21 -0
- squirrels/_package_data/base_project/squirrels.yml.j2 +61 -0
- squirrels/_package_data/base_project/tmp/.gitignore +2 -0
- squirrels/_package_data/templates/login_successful.html +53 -0
- squirrels/_package_data/templates/squirrels_studio.html +22 -0
- squirrels/_package_loader.py +29 -0
- squirrels/_parameter_configs.py +592 -0
- squirrels/_parameter_options.py +348 -0
- squirrels/_parameter_sets.py +207 -0
- squirrels/_parameters.py +1703 -0
- squirrels/_project.py +796 -0
- squirrels/_py_module.py +122 -0
- squirrels/_request_context.py +33 -0
- squirrels/_schemas/__init__.py +0 -0
- squirrels/_schemas/auth_models.py +83 -0
- squirrels/_schemas/query_param_models.py +70 -0
- squirrels/_schemas/request_models.py +26 -0
- squirrels/_schemas/response_models.py +286 -0
- squirrels/_seeds.py +97 -0
- squirrels/_sources.py +112 -0
- squirrels/_utils.py +540 -149
- squirrels/_version.py +1 -3
- squirrels/arguments.py +7 -0
- squirrels/auth.py +4 -0
- squirrels/connections.py +3 -0
- squirrels/dashboards.py +3 -0
- squirrels/data_sources.py +14 -282
- squirrels/parameter_options.py +13 -189
- squirrels/parameters.py +14 -801
- squirrels/types.py +18 -0
- squirrels-0.6.0.post0.dist-info/METADATA +148 -0
- squirrels-0.6.0.post0.dist-info/RECORD +101 -0
- {squirrels-0.1.0.dist-info → squirrels-0.6.0.post0.dist-info}/WHEEL +1 -2
- {squirrels-0.1.0.dist-info → squirrels-0.6.0.post0.dist-info}/entry_points.txt +1 -0
- squirrels-0.6.0.post0.dist-info/licenses/LICENSE +201 -0
- squirrels/_credentials_manager.py +0 -87
- squirrels/_module_loader.py +0 -37
- squirrels/_parameter_set.py +0 -151
- squirrels/_renderer.py +0 -286
- squirrels/_timed_imports.py +0 -37
- squirrels/connection_set.py +0 -126
- squirrels/package_data/base_project/.gitignore +0 -4
- squirrels/package_data/base_project/connections.py +0 -21
- squirrels/package_data/base_project/database/sample_database.db +0 -0
- squirrels/package_data/base_project/database/seattle_weather.db +0 -0
- squirrels/package_data/base_project/datasets/sample_dataset/context.py +0 -8
- squirrels/package_data/base_project/datasets/sample_dataset/database_view1.py +0 -23
- squirrels/package_data/base_project/datasets/sample_dataset/database_view1.sql.j2 +0 -7
- squirrels/package_data/base_project/datasets/sample_dataset/final_view.py +0 -10
- squirrels/package_data/base_project/datasets/sample_dataset/final_view.sql.j2 +0 -2
- squirrels/package_data/base_project/datasets/sample_dataset/parameters.py +0 -30
- squirrels/package_data/base_project/datasets/sample_dataset/selections.cfg +0 -6
- squirrels/package_data/base_project/squirrels.yaml +0 -26
- squirrels/package_data/static/favicon.ico +0 -0
- squirrels/package_data/static/script.js +0 -234
- squirrels/package_data/static/style.css +0 -110
- squirrels/package_data/templates/index.html +0 -32
- squirrels-0.1.0.dist-info/LICENSE +0 -22
- squirrels-0.1.0.dist-info/METADATA +0 -67
- squirrels-0.1.0.dist-info/RECORD +0 -40
- squirrels-0.1.0.dist-info/top_level.txt +0 -1
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
from typing import TypeVar, Iterable, Any
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from decimal import Decimal, InvalidOperation as InvalidDecimalConversion
|
|
4
|
+
from datetime import datetime, date
|
|
5
|
+
from abc import ABCMeta, abstractmethod
|
|
6
|
+
|
|
7
|
+
from ._utils import ConfigurationError
|
|
8
|
+
|
|
9
|
+
Number = Decimal | int | float | str
|
|
10
|
+
Comparables = TypeVar("Comparables", Decimal, date)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class ParameterOption(metaclass=ABCMeta):
|
|
15
|
+
"""
|
|
16
|
+
Abstract class for parameter options
|
|
17
|
+
"""
|
|
18
|
+
_user_groups: frozenset[Any]
|
|
19
|
+
_parent_option_ids: frozenset[str]
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def __init__(
|
|
23
|
+
self, *, user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
24
|
+
) -> None:
|
|
25
|
+
self._user_groups = frozenset({user_groups} if isinstance(user_groups, str) else user_groups)
|
|
26
|
+
self._parent_option_ids = frozenset({parent_option_ids} if isinstance(parent_option_ids, str) else parent_option_ids)
|
|
27
|
+
|
|
28
|
+
def _validate_lower_upper_values(self, lower_label: str, lower_value: Comparables, upper_label: str, upper_value: Comparables):
|
|
29
|
+
if lower_value > upper_value:
|
|
30
|
+
raise ConfigurationError(f'The {lower_label} "{lower_value}" must be less than or equal to the {upper_label} "{upper_value}"')
|
|
31
|
+
|
|
32
|
+
def _is_valid(self, user_group: Any, selected_parent_option_ids: Iterable[str] | None) -> bool:
|
|
33
|
+
"""
|
|
34
|
+
Checks if this option is valid given the selected parent options and user group of user if applicable.
|
|
35
|
+
|
|
36
|
+
Arguments:
|
|
37
|
+
user_group: The value of the user's "user group attribute". Only None when "user_attribute" is not specified
|
|
38
|
+
for the Parameter factory.
|
|
39
|
+
selected_parent_option_ids: List of selected option ids from the parent parameter. Only None when the Parameter
|
|
40
|
+
object has no parent parameter.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
True if valid, False otherwise
|
|
44
|
+
"""
|
|
45
|
+
if user_group is not None and user_group not in self._user_groups:
|
|
46
|
+
return False
|
|
47
|
+
|
|
48
|
+
if selected_parent_option_ids is not None and self._parent_option_ids.isdisjoint(selected_parent_option_ids):
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
return True
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class SelectParameterOption(ParameterOption):
|
|
56
|
+
"""
|
|
57
|
+
Parameter option for a select parameter
|
|
58
|
+
"""
|
|
59
|
+
_identifier: str
|
|
60
|
+
_label: str
|
|
61
|
+
_is_default: bool
|
|
62
|
+
custom_fields: dict[str, Any]
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self, id: str, label: str, *, is_default: bool = False, user_groups: Iterable[Any] | str = frozenset(),
|
|
66
|
+
parent_option_ids: Iterable[str] | str = frozenset(), custom_fields: dict[str, Any] = {}, **kwargs
|
|
67
|
+
) -> None:
|
|
68
|
+
"""
|
|
69
|
+
Constructor for SelectParameterOption
|
|
70
|
+
|
|
71
|
+
Arguments:
|
|
72
|
+
identifier: Unique identifier for this option that never changes over time
|
|
73
|
+
label: Human readable label that gets shown as a dropdown option
|
|
74
|
+
is_default: True if this is a default option, False otherwise
|
|
75
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
76
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
77
|
+
custom_fields: Dictionary to associate custom attributes to the parameter option
|
|
78
|
+
**kwargs: Any additional keyword arguments specified (except the ones above) gets included into custom_fields as well
|
|
79
|
+
"""
|
|
80
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
81
|
+
self._identifier = id
|
|
82
|
+
self._label = label
|
|
83
|
+
self._is_default = is_default
|
|
84
|
+
self.custom_fields = {
|
|
85
|
+
**kwargs, **custom_fields, **self._to_json_dict()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
def get_custom_field(self, field: str, *, default_field: str | None = None, default: Any = None, **kwargs) -> Any:
|
|
89
|
+
"""
|
|
90
|
+
Get field value from the custom_fields attribute
|
|
91
|
+
|
|
92
|
+
Arguments:
|
|
93
|
+
field: The key to use to fetch the custom field from "custom_fields"
|
|
94
|
+
default_field: If value at "field" key does not exist in "custom_fields", then this is used instead as the field (if not None)
|
|
95
|
+
default: If value at "field" or "default_field" (if not None) key does not exist in "custom_fields", then this value
|
|
96
|
+
is used as default, or throws an error if None
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The type of the custom field
|
|
100
|
+
"""
|
|
101
|
+
if default_field is not None:
|
|
102
|
+
default = self.get_custom_field(default_field, default=default)
|
|
103
|
+
|
|
104
|
+
if default is not None:
|
|
105
|
+
selected_field = self.custom_fields.get(field, default)
|
|
106
|
+
else:
|
|
107
|
+
try:
|
|
108
|
+
selected_field = self.custom_fields[field]
|
|
109
|
+
except KeyError as e:
|
|
110
|
+
raise ConfigurationError(f"Field '{field}' must exist for parameter option {self._to_json_dict()}") from e
|
|
111
|
+
|
|
112
|
+
return selected_field
|
|
113
|
+
|
|
114
|
+
def _to_json_dict(self):
|
|
115
|
+
return {'id': self._identifier, 'label': self._label}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class _DateTypeParameterOption(ParameterOption):
|
|
120
|
+
"""
|
|
121
|
+
Abstract class (or type) for date type parameter options
|
|
122
|
+
"""
|
|
123
|
+
_min_date: date | None
|
|
124
|
+
_max_date: date | None
|
|
125
|
+
_date_format: str
|
|
126
|
+
|
|
127
|
+
@abstractmethod
|
|
128
|
+
def __init__(
|
|
129
|
+
self, *, min_date: str | date | None = None, max_date: str | date | None = None, date_format: str = '%Y-%m-%d',
|
|
130
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
131
|
+
) -> None:
|
|
132
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
133
|
+
self._date_format = date_format
|
|
134
|
+
self._min_date, self._max_date = None, None # preset for using _validate_date()
|
|
135
|
+
self._min_date = self._validate_date(min_date) if min_date is not None else None
|
|
136
|
+
self._max_date = self._validate_date(max_date) if max_date is not None else None
|
|
137
|
+
if self._min_date is not None and self._max_date is not None:
|
|
138
|
+
self._validate_lower_upper_values("min_date", self._min_date, "max_date", self._max_date)
|
|
139
|
+
|
|
140
|
+
def _validate_date(self, date_str: str | date) -> date:
|
|
141
|
+
try:
|
|
142
|
+
date_obj = datetime.strptime(date_str, self._date_format).date() if isinstance(date_str, str) else date_str
|
|
143
|
+
except ValueError as e:
|
|
144
|
+
raise ConfigurationError(f'Invalid format for date "{date_str}".') from e
|
|
145
|
+
|
|
146
|
+
if self._min_date is not None and date_obj < self._min_date:
|
|
147
|
+
raise ConfigurationError(f'The provided date "{date_obj}" is less than the min date "{self._min_date}"')
|
|
148
|
+
if self._max_date is not None and date_obj > self._max_date:
|
|
149
|
+
raise ConfigurationError(f'The provided date "{date_obj}" is greater than the max date "{self._max_date}"')
|
|
150
|
+
|
|
151
|
+
return date_obj
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@dataclass
|
|
155
|
+
class DateParameterOption(_DateTypeParameterOption):
|
|
156
|
+
"""
|
|
157
|
+
Parameter option for default dates if it varies based on selection of another parameter
|
|
158
|
+
"""
|
|
159
|
+
_default_date: date
|
|
160
|
+
|
|
161
|
+
def __init__(
|
|
162
|
+
self, default_date: str | date, *, min_date: str | date | None = None, max_date: str | date | None = None, date_format: str = '%Y-%m-%d',
|
|
163
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
164
|
+
) -> None:
|
|
165
|
+
"""
|
|
166
|
+
Constructor for DateParameterOption
|
|
167
|
+
|
|
168
|
+
Arguments:
|
|
169
|
+
default_date: Default date for this option
|
|
170
|
+
min_date: Minimum date for this option
|
|
171
|
+
max_date: Maximum date for this option
|
|
172
|
+
date_format: Format of the default date, default is '%Y-%m-%d'
|
|
173
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
174
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
175
|
+
"""
|
|
176
|
+
super().__init__(
|
|
177
|
+
date_format=date_format, min_date=min_date, max_date=max_date, user_groups=user_groups, parent_option_ids=parent_option_ids
|
|
178
|
+
)
|
|
179
|
+
self._default_date = self._validate_date(default_date)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
@dataclass
|
|
183
|
+
class DateRangeParameterOption(_DateTypeParameterOption):
|
|
184
|
+
"""
|
|
185
|
+
Parameter option for default dates if it varies based on selection of another parameter
|
|
186
|
+
"""
|
|
187
|
+
_default_start_date: date
|
|
188
|
+
_default_end_date: date
|
|
189
|
+
|
|
190
|
+
def __init__(
|
|
191
|
+
self, default_start_date: str | date, default_end_date: str | date, *, min_date: str | date | None = None,
|
|
192
|
+
max_date: str | date | None = None, date_format: str = '%Y-%m-%d', user_groups: Iterable[Any] | str = frozenset(),
|
|
193
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
194
|
+
) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Constructor for DateRangeParameterOption
|
|
197
|
+
|
|
198
|
+
Arguments:
|
|
199
|
+
default_start_date: Default start date for this option
|
|
200
|
+
default_end_date: Default end date for this option
|
|
201
|
+
min_date: Minimum date for this option
|
|
202
|
+
max_date: Maximum date for this option
|
|
203
|
+
date_format: Format of the default date, default is '%Y-%m-%d'
|
|
204
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
205
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
206
|
+
"""
|
|
207
|
+
super().__init__(
|
|
208
|
+
date_format=date_format, min_date=min_date, max_date=max_date, user_groups=user_groups, parent_option_ids=parent_option_ids
|
|
209
|
+
)
|
|
210
|
+
self._default_start_date = self._validate_date(default_start_date)
|
|
211
|
+
self._default_end_date = self._validate_date(default_end_date)
|
|
212
|
+
self._validate_lower_upper_values("default_start_date", self._default_start_date, "default_end_date", self._default_end_date)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@dataclass
|
|
216
|
+
class _NumericParameterOption(ParameterOption):
|
|
217
|
+
"""
|
|
218
|
+
Abstract class (or type) for numeric parameter options
|
|
219
|
+
"""
|
|
220
|
+
_min_value: Decimal
|
|
221
|
+
_max_value: Decimal
|
|
222
|
+
_increment: Decimal
|
|
223
|
+
|
|
224
|
+
@abstractmethod
|
|
225
|
+
def __init__(
|
|
226
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, user_groups: Iterable[Any] | str = frozenset(),
|
|
227
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
228
|
+
) -> None:
|
|
229
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
230
|
+
try:
|
|
231
|
+
self._min_value = Decimal(str(min_value))
|
|
232
|
+
self._max_value = Decimal(str(max_value))
|
|
233
|
+
self._increment = Decimal(str(increment))
|
|
234
|
+
except InvalidDecimalConversion as e:
|
|
235
|
+
raise ConfigurationError(f'Could not convert either min, max, or increment to number') from e
|
|
236
|
+
|
|
237
|
+
self._validate_lower_upper_values("min_value", self._min_value, "max_value", self._max_value)
|
|
238
|
+
|
|
239
|
+
if (self._max_value - self._min_value) % self._increment != 0:
|
|
240
|
+
raise ConfigurationError(f'The increment "{self._increment}" must fit evenly between ' +
|
|
241
|
+
f'the min_value "{self._min_value}" and max_value "{self._max_value}"')
|
|
242
|
+
|
|
243
|
+
def __value_in_range(self, value: Decimal) -> bool:
|
|
244
|
+
return self._min_value <= value <= self._max_value
|
|
245
|
+
|
|
246
|
+
def __value_on_increment(self, value: Decimal) -> bool:
|
|
247
|
+
diff = (value - self._min_value)
|
|
248
|
+
return diff >= 0 and diff % self._increment == 0
|
|
249
|
+
|
|
250
|
+
def _validate_value(self, value: Number) -> Decimal:
|
|
251
|
+
try:
|
|
252
|
+
value = Decimal(str(value))
|
|
253
|
+
except InvalidDecimalConversion as e:
|
|
254
|
+
raise ConfigurationError(f'Could not convert "{value}" to number', e)
|
|
255
|
+
|
|
256
|
+
if not self.__value_in_range(value):
|
|
257
|
+
raise ConfigurationError(f'The selected value "{value}" is outside of bounds ' +
|
|
258
|
+
f'"{self._min_value}" and "{self._max_value}".')
|
|
259
|
+
if not self.__value_on_increment(value):
|
|
260
|
+
raise ConfigurationError(f'The difference between selected value "{value}" and lower value ' +
|
|
261
|
+
f'"{self._min_value}" must be a multiple of increment "{self._increment}".')
|
|
262
|
+
return value
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass
|
|
266
|
+
class NumberParameterOption(_NumericParameterOption):
|
|
267
|
+
"""
|
|
268
|
+
Parameter option for default numbers if it varies based on selection of another parameter
|
|
269
|
+
"""
|
|
270
|
+
_default_value: Decimal
|
|
271
|
+
|
|
272
|
+
def __init__(
|
|
273
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, default_value: Number | None = None,
|
|
274
|
+
user_groups: Iterable[Any] | str = frozenset(), parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
275
|
+
) -> None:
|
|
276
|
+
"""
|
|
277
|
+
Constructor for NumberParameterOption
|
|
278
|
+
|
|
279
|
+
* Note that the "Number" type denotes an int, a Decimal (from decimal module), or a string that can be parsed to Decimal
|
|
280
|
+
|
|
281
|
+
Arguments:
|
|
282
|
+
min_value: Minimum selectable value
|
|
283
|
+
max_value: Maximum selectable value
|
|
284
|
+
increment: Increment of selectable values, and must fit evenly between min_value and max_value
|
|
285
|
+
default_value: Default value for this option, and must be selectable based on min_value, max_value, and increment
|
|
286
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
287
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
288
|
+
"""
|
|
289
|
+
super().__init__(min_value, max_value, increment=increment, user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
290
|
+
self._default_value = self._validate_value(default_value) if default_value is not None else self._min_value
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
@dataclass
|
|
294
|
+
class NumberRangeParameterOption(_NumericParameterOption):
|
|
295
|
+
"""
|
|
296
|
+
Parameter option for default numeric ranges if it varies based on selection of another parameter
|
|
297
|
+
"""
|
|
298
|
+
_default_lower_value: Decimal
|
|
299
|
+
_default_upper_value: Decimal
|
|
300
|
+
|
|
301
|
+
def __init__(
|
|
302
|
+
self, min_value: Number, max_value: Number, *, increment: Number = 1, default_lower_value: Number | None = None,
|
|
303
|
+
default_upper_value: Number | None = None, user_groups: Iterable[Any] | str = frozenset(),
|
|
304
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
305
|
+
) -> None:
|
|
306
|
+
"""
|
|
307
|
+
Constructor for NumberRangeParameterOption
|
|
308
|
+
|
|
309
|
+
* Note that the "Number" type denotes an int, a Decimal (from decimal module), or a string that can be parsed to Decimal
|
|
310
|
+
|
|
311
|
+
Arguments:
|
|
312
|
+
min_value: Minimum selectable value
|
|
313
|
+
max_value: Maximum selectable value
|
|
314
|
+
increment: Increment of selectable values, and must fit evenly between min_value and max_value
|
|
315
|
+
default_lower_value: Default lower value for this option, and must be selectable based on min_value, max_value, and increment
|
|
316
|
+
default_upper_value: Default upper value for this option, and must be selectable based on min_value, max_value, and increment.
|
|
317
|
+
Must also be greater than default_lower_value
|
|
318
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
319
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
320
|
+
"""
|
|
321
|
+
super().__init__(min_value, max_value, increment=increment, user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
322
|
+
self._default_lower_value = self._validate_value(default_lower_value) if default_lower_value is not None else self._min_value
|
|
323
|
+
self._default_upper_value = self._validate_value(default_upper_value) if default_upper_value is not None else self._max_value
|
|
324
|
+
self._validate_lower_upper_values("default_lower_value", self._default_lower_value, "default_upper_value", self._default_upper_value)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@dataclass
|
|
328
|
+
class TextParameterOption(ParameterOption):
|
|
329
|
+
"""
|
|
330
|
+
Parameter option for default text values if it varies based on selection of another parameter
|
|
331
|
+
"""
|
|
332
|
+
_default_text: str
|
|
333
|
+
|
|
334
|
+
def __init__(
|
|
335
|
+
self, *, default_text: str = "", user_groups: Iterable[Any] | str = frozenset(),
|
|
336
|
+
parent_option_ids: Iterable[str] | str = frozenset(), **kwargs
|
|
337
|
+
) -> None:
|
|
338
|
+
"""
|
|
339
|
+
Constructor for TextParameterOption
|
|
340
|
+
|
|
341
|
+
Arguments:
|
|
342
|
+
default_text: Default text for this option
|
|
343
|
+
user_groups: The user groups this parameter option would show for if "user_attribute" is specified in the Parameter factory
|
|
344
|
+
parent_option_ids: Set of parent option ids this parameter option would show for if "parent" is specified in the Parameter factory
|
|
345
|
+
"""
|
|
346
|
+
super().__init__(user_groups=user_groups, parent_option_ids=parent_option_ids)
|
|
347
|
+
self._default_text = default_text
|
|
348
|
+
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import Optional, Sequence, Callable, Any
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from collections import OrderedDict
|
|
5
|
+
import time, concurrent.futures, polars as pl
|
|
6
|
+
|
|
7
|
+
from . import _parameters as p, _utils as u, _constants as c, _parameter_configs as pc, _py_module as pm
|
|
8
|
+
from ._schemas import response_models as rm
|
|
9
|
+
from ._arguments.init_time_args import ParametersArgs
|
|
10
|
+
from ._manifest import ParametersConfig, ManifestConfig
|
|
11
|
+
from ._connection_set import ConnectionSet
|
|
12
|
+
from ._seeds import Seeds
|
|
13
|
+
from ._schemas.auth_models import AbstractUser
|
|
14
|
+
from ._env_vars import SquirrelsEnvVars
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class ParameterSet:
|
|
19
|
+
"""
|
|
20
|
+
A wrapper class for a sequence of parameters with the selections applied as well
|
|
21
|
+
"""
|
|
22
|
+
_parameters_dict: OrderedDict[str, p.Parameter]
|
|
23
|
+
|
|
24
|
+
def get_parameters_as_dict(self) -> dict[str, p.Parameter]:
|
|
25
|
+
return self._parameters_dict.copy()
|
|
26
|
+
|
|
27
|
+
def to_api_response_model0(self) -> rm.ParametersModel:
|
|
28
|
+
parameters = []
|
|
29
|
+
for x in self._parameters_dict.values():
|
|
30
|
+
parameters.append(x._to_api_response_model0())
|
|
31
|
+
return rm.ParametersModel(parameters=parameters)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class ParameterConfigsSet:
|
|
36
|
+
"""
|
|
37
|
+
Pool of parameter configs, can create multiple for unit testing purposes
|
|
38
|
+
"""
|
|
39
|
+
_data: dict[str, pc.ParameterConfigBase] = field(default_factory=OrderedDict)
|
|
40
|
+
_data_source_params: dict[str, pc.DataSourceParameterConfig] = field(default_factory=dict)
|
|
41
|
+
|
|
42
|
+
def get(self, name: Optional[str]) -> Optional[pc.ParameterConfigBase]:
|
|
43
|
+
try:
|
|
44
|
+
return self._data[name] if name is not None else None
|
|
45
|
+
except KeyError as e:
|
|
46
|
+
raise u.ConfigurationError(f'Unable to find parameter named "{name}"') from e
|
|
47
|
+
|
|
48
|
+
def add(self, param_config: pc.ParameterConfigBase) -> None:
|
|
49
|
+
self._data[param_config.name] = param_config
|
|
50
|
+
if isinstance(param_config, pc.DataSourceParameterConfig):
|
|
51
|
+
self._data_source_params[param_config.name] = param_config
|
|
52
|
+
|
|
53
|
+
def _get_all_ds_param_configs(self) -> Sequence[pc.DataSourceParameterConfig]:
|
|
54
|
+
return list(self._data_source_params.values())
|
|
55
|
+
|
|
56
|
+
def __convert_datasource_params(self, df_dict: dict[str, pl.DataFrame]) -> None:
|
|
57
|
+
done = set()
|
|
58
|
+
for curr_name in self._data_source_params:
|
|
59
|
+
stack = [curr_name] # Note: parents must be converted first before children
|
|
60
|
+
while stack:
|
|
61
|
+
name = stack[-1]
|
|
62
|
+
if name not in done:
|
|
63
|
+
param = self._data_source_params.get(name, self.get(name))
|
|
64
|
+
assert param is not None
|
|
65
|
+
parent_name = param.parent_name
|
|
66
|
+
if parent_name is not None and parent_name not in done:
|
|
67
|
+
stack.append(parent_name)
|
|
68
|
+
continue
|
|
69
|
+
if isinstance(param, pc.DataSourceParameterConfig):
|
|
70
|
+
if name not in df_dict:
|
|
71
|
+
raise u.ConfigurationError(f'No reference data found for parameter "{name}"')
|
|
72
|
+
self._data[name] = param.convert(df_dict[name])
|
|
73
|
+
done.add(name)
|
|
74
|
+
stack.pop()
|
|
75
|
+
|
|
76
|
+
def __validate_param_relationships(self) -> None:
|
|
77
|
+
for param_config in self._data.values():
|
|
78
|
+
assert isinstance(param_config, pc.ParameterConfig)
|
|
79
|
+
parent_name = param_config.parent_name
|
|
80
|
+
parent = self.get(parent_name)
|
|
81
|
+
if parent:
|
|
82
|
+
if not isinstance(param_config, pc.SelectionParameterConfig):
|
|
83
|
+
if not isinstance(parent, pc.SingleSelectParameterConfig):
|
|
84
|
+
raise u.ConfigurationError(f'Only single-select parameters can be parents of non-select parameters. ' +
|
|
85
|
+
f'Parameter "{parent_name}" is the parent of non-select parameter ' +
|
|
86
|
+
f'"{param_config.name}" but "{parent_name}" is not a single-select parameter.')
|
|
87
|
+
seen = set()
|
|
88
|
+
for option in param_config.all_options:
|
|
89
|
+
lookup_keys = option._parent_option_ids
|
|
90
|
+
if len(option._user_groups) > 0:
|
|
91
|
+
lookup_keys = set((x, y) for x in option._parent_option_ids for y in option._user_groups)
|
|
92
|
+
if not seen.isdisjoint(lookup_keys):
|
|
93
|
+
raise u.ConfigurationError(f'Each distinct value of "parent option id" can only appear once (per user group)' +
|
|
94
|
+
f'among the options of non-select parameter "{param_config.name}".')
|
|
95
|
+
seen.update(lookup_keys)
|
|
96
|
+
|
|
97
|
+
if not isinstance(parent, pc.SelectionParameterConfig):
|
|
98
|
+
raise u.ConfigurationError(f'Only selection parameters can be parents. Parameter "{parent_name}" is the parent of ' +
|
|
99
|
+
f'"{param_config.name}" but "{parent_name}" is not a selection parameter.')
|
|
100
|
+
|
|
101
|
+
parent._add_child_mutate(param_config)
|
|
102
|
+
|
|
103
|
+
def _post_process_params(self, df_dict: dict[str, pl.DataFrame]) -> None:
|
|
104
|
+
self.__convert_datasource_params(df_dict)
|
|
105
|
+
self.__validate_param_relationships()
|
|
106
|
+
|
|
107
|
+
def apply_selections(
|
|
108
|
+
self, dataset_params: Optional[Sequence[str]], selections: dict[str, Any], user: AbstractUser, *, parent_param: str | None = None
|
|
109
|
+
) -> ParameterSet:
|
|
110
|
+
if dataset_params is None:
|
|
111
|
+
dataset_params = list(self._data.keys())
|
|
112
|
+
|
|
113
|
+
parameters_by_name: dict[str, p.Parameter] = {}
|
|
114
|
+
params_to_process = [parent_param] if parent_param else dataset_params
|
|
115
|
+
params_to_process_set = set(params_to_process)
|
|
116
|
+
for some_name in params_to_process:
|
|
117
|
+
stack = [some_name] # Note: process parent selections first (if applicable) before children
|
|
118
|
+
while stack:
|
|
119
|
+
curr_name = stack[-1]
|
|
120
|
+
children = []
|
|
121
|
+
if curr_name not in parameters_by_name:
|
|
122
|
+
param_conf = self.get(curr_name)
|
|
123
|
+
assert isinstance(param_conf, pc.ParameterConfig)
|
|
124
|
+
parent_name = param_conf.parent_name
|
|
125
|
+
if parent_name is None:
|
|
126
|
+
parent = None
|
|
127
|
+
elif parent_name in params_to_process_set and parent_name not in parameters_by_name:
|
|
128
|
+
stack.append(parent_name)
|
|
129
|
+
continue
|
|
130
|
+
else:
|
|
131
|
+
parent = parameters_by_name.get(parent_name)
|
|
132
|
+
assert isinstance(parent, p._SelectionParameter) or parent is None
|
|
133
|
+
param = param_conf.with_selection(selections.get(curr_name), user, parent)
|
|
134
|
+
parameters_by_name[curr_name] = param
|
|
135
|
+
if isinstance(param_conf, pc.SelectionParameterConfig):
|
|
136
|
+
children = list(x for x in param_conf.children.keys() if x in dataset_params)
|
|
137
|
+
stack.pop()
|
|
138
|
+
stack.extend(children)
|
|
139
|
+
|
|
140
|
+
ordered_parameters = OrderedDict((key, parameters_by_name[key]) for key in dataset_params if key in parameters_by_name)
|
|
141
|
+
return ParameterSet(ordered_parameters)
|
|
142
|
+
|
|
143
|
+
def get_all_api_field_info(self) -> dict[str, pc.APIParamFieldInfo]:
|
|
144
|
+
api_field_infos = {}
|
|
145
|
+
for param, config in self._data.items():
|
|
146
|
+
assert isinstance(config, pc.ParameterConfig)
|
|
147
|
+
api_field_infos[param] = config.get_api_field_info()
|
|
148
|
+
return api_field_infos
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class ParameterConfigsSetIO:
|
|
152
|
+
"""
|
|
153
|
+
Static class for the singleton object of ParameterConfigsSet
|
|
154
|
+
"""
|
|
155
|
+
param_factories: list[Callable[[ParametersArgs], pc.ParameterConfigBase]] = [] # this is static (set in load_from_file) to stage the functions from pyconfigs/parameters.py before using them
|
|
156
|
+
|
|
157
|
+
@classmethod
|
|
158
|
+
def _get_df_dict_from_data_sources(
|
|
159
|
+
cls, param_configs_set: ParameterConfigsSet, default_conn_name: str, seeds: Seeds, conn_set: ConnectionSet, datalake_db_path: str
|
|
160
|
+
) -> dict[str, pl.DataFrame]:
|
|
161
|
+
|
|
162
|
+
def get_dataframe(ds_param_config: pc.DataSourceParameterConfig) -> tuple[str, pl.DataFrame]:
|
|
163
|
+
return ds_param_config.name, ds_param_config.get_dataframe(default_conn_name, conn_set, seeds, datalake_db_path)
|
|
164
|
+
|
|
165
|
+
ds_param_configs = param_configs_set._get_all_ds_param_configs()
|
|
166
|
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
167
|
+
df_dict = dict(executor.map(get_dataframe, ds_param_configs))
|
|
168
|
+
|
|
169
|
+
return df_dict
|
|
170
|
+
|
|
171
|
+
@classmethod
|
|
172
|
+
def _add_from_dict(cls, param_configs_set: ParameterConfigsSet, param_config: ParametersConfig) -> None:
|
|
173
|
+
ptype = getattr(p, param_config.type)
|
|
174
|
+
factory = getattr(ptype, param_config.factory)
|
|
175
|
+
obj = factory(**param_config.arguments)
|
|
176
|
+
param_configs_set.add(obj)
|
|
177
|
+
|
|
178
|
+
@classmethod
|
|
179
|
+
def load_from_file(
|
|
180
|
+
cls, logger: u.Logger, env_vars: SquirrelsEnvVars, manifest_cfg: ManifestConfig, seeds: Seeds, conn_set: ConnectionSet,
|
|
181
|
+
param_args: ParametersArgs
|
|
182
|
+
) -> ParameterConfigsSet:
|
|
183
|
+
start = time.time()
|
|
184
|
+
param_configs_set = ParameterConfigsSet()
|
|
185
|
+
|
|
186
|
+
for param_as_dict in manifest_cfg.parameters:
|
|
187
|
+
cls._add_from_dict(param_configs_set, param_as_dict)
|
|
188
|
+
|
|
189
|
+
# adds to cls.param_factories as side effect
|
|
190
|
+
main_result = pm.run_pyconfig_main(env_vars.project_path, c.PARAMETERS_FILE, {"sqrl": param_args})
|
|
191
|
+
param_factories = cls.param_factories
|
|
192
|
+
cls.param_factories = []
|
|
193
|
+
|
|
194
|
+
for param_factory in param_factories:
|
|
195
|
+
param_configs_set.add(param_factory(param_args))
|
|
196
|
+
|
|
197
|
+
if isinstance(main_result, list):
|
|
198
|
+
for param_config in main_result:
|
|
199
|
+
param_configs_set.add(param_config)
|
|
200
|
+
|
|
201
|
+
default_conn_name = env_vars.connections_default_name_used
|
|
202
|
+
datalake_db_path = env_vars.vdl_catalog_db_path
|
|
203
|
+
df_dict = cls._get_df_dict_from_data_sources(param_configs_set, default_conn_name, seeds, conn_set, datalake_db_path)
|
|
204
|
+
param_configs_set._post_process_params(df_dict)
|
|
205
|
+
|
|
206
|
+
logger.log_activity_time("loading parameters", start)
|
|
207
|
+
return param_configs_set
|