squirrels 0.5.0rc0__py3-none-any.whl → 0.5.1__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.

Potentially problematic release.


This version of squirrels might be problematic. Click here for more details.

Files changed (108) hide show
  1. dateutils/__init__.py +6 -0
  2. dateutils/_enums.py +25 -0
  3. squirrels/dateutils.py → dateutils/_implementation.py +58 -111
  4. dateutils/types.py +6 -0
  5. squirrels/__init__.py +10 -12
  6. squirrels/_api_routes/__init__.py +5 -0
  7. squirrels/_api_routes/auth.py +271 -0
  8. squirrels/_api_routes/base.py +171 -0
  9. squirrels/_api_routes/dashboards.py +158 -0
  10. squirrels/_api_routes/data_management.py +148 -0
  11. squirrels/_api_routes/datasets.py +265 -0
  12. squirrels/_api_routes/oauth2.py +298 -0
  13. squirrels/_api_routes/project.py +252 -0
  14. squirrels/_api_server.py +245 -781
  15. squirrels/_arguments/__init__.py +0 -0
  16. squirrels/{arguments → _arguments}/init_time_args.py +7 -2
  17. squirrels/{arguments → _arguments}/run_time_args.py +13 -35
  18. squirrels/_auth.py +720 -212
  19. squirrels/_command_line.py +81 -41
  20. squirrels/_compile_prompts.py +147 -0
  21. squirrels/_connection_set.py +16 -7
  22. squirrels/_constants.py +29 -9
  23. squirrels/{_dashboards_io.py → _dashboards.py} +87 -6
  24. squirrels/_data_sources.py +570 -0
  25. squirrels/{dataset_result.py → _dataset_types.py} +2 -4
  26. squirrels/_exceptions.py +9 -37
  27. squirrels/_initializer.py +83 -59
  28. squirrels/_logging.py +117 -0
  29. squirrels/_manifest.py +129 -62
  30. squirrels/_model_builder.py +10 -52
  31. squirrels/_model_configs.py +3 -3
  32. squirrels/_model_queries.py +1 -1
  33. squirrels/_models.py +249 -118
  34. squirrels/{package_data → _package_data}/base_project/.env +16 -4
  35. squirrels/{package_data → _package_data}/base_project/.env.example +15 -3
  36. squirrels/{package_data → _package_data}/base_project/connections.yml +4 -3
  37. squirrels/{package_data → _package_data}/base_project/dashboards/dashboard_example.py +4 -4
  38. squirrels/_package_data/base_project/dashboards/dashboard_example.yml +22 -0
  39. squirrels/{package_data → _package_data}/base_project/duckdb_init.sql +1 -0
  40. squirrels/_package_data/base_project/macros/macros_example.sql +17 -0
  41. squirrels/{package_data → _package_data}/base_project/models/builds/build_example.py +2 -2
  42. squirrels/{package_data → _package_data}/base_project/models/builds/build_example.sql +1 -1
  43. squirrels/{package_data → _package_data}/base_project/models/builds/build_example.yml +2 -0
  44. squirrels/_package_data/base_project/models/dbviews/dbview_example.sql +17 -0
  45. squirrels/_package_data/base_project/models/dbviews/dbview_example.yml +32 -0
  46. squirrels/_package_data/base_project/models/federates/federate_example.py +48 -0
  47. squirrels/_package_data/base_project/models/federates/federate_example.sql +21 -0
  48. squirrels/{package_data → _package_data}/base_project/models/federates/federate_example.yml +7 -7
  49. squirrels/{package_data → _package_data}/base_project/models/sources.yml +5 -6
  50. squirrels/{package_data → _package_data}/base_project/parameters.yml +32 -45
  51. squirrels/_package_data/base_project/pyconfigs/connections.py +18 -0
  52. squirrels/{package_data → _package_data}/base_project/pyconfigs/context.py +31 -22
  53. squirrels/_package_data/base_project/pyconfigs/parameters.py +141 -0
  54. squirrels/_package_data/base_project/pyconfigs/user.py +44 -0
  55. squirrels/{package_data → _package_data}/base_project/seeds/seed_categories.yml +1 -1
  56. squirrels/{package_data → _package_data}/base_project/seeds/seed_subcategories.yml +1 -1
  57. squirrels/_package_data/base_project/squirrels.yml.j2 +61 -0
  58. squirrels/_package_data/templates/dataset_results.html +112 -0
  59. squirrels/_package_data/templates/oauth_login.html +271 -0
  60. squirrels/_package_data/templates/squirrels_studio.html +20 -0
  61. squirrels/_parameter_configs.py +76 -55
  62. squirrels/_parameter_options.py +348 -0
  63. squirrels/_parameter_sets.py +53 -45
  64. squirrels/_parameters.py +1664 -0
  65. squirrels/_project.py +403 -242
  66. squirrels/_py_module.py +3 -2
  67. squirrels/_request_context.py +33 -0
  68. squirrels/_schemas/__init__.py +0 -0
  69. squirrels/_schemas/auth_models.py +167 -0
  70. squirrels/_schemas/query_param_models.py +75 -0
  71. squirrels/{_api_response_models.py → _schemas/response_models.py} +48 -18
  72. squirrels/_seeds.py +1 -1
  73. squirrels/_sources.py +23 -19
  74. squirrels/_utils.py +121 -39
  75. squirrels/_version.py +1 -1
  76. squirrels/arguments.py +7 -0
  77. squirrels/auth.py +4 -0
  78. squirrels/connections.py +3 -0
  79. squirrels/dashboards.py +2 -81
  80. squirrels/data_sources.py +14 -563
  81. squirrels/parameter_options.py +13 -348
  82. squirrels/parameters.py +14 -1266
  83. squirrels/types.py +16 -0
  84. {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/METADATA +42 -30
  85. squirrels-0.5.1.dist-info/RECORD +98 -0
  86. squirrels/package_data/base_project/dashboards/dashboard_example.yml +0 -22
  87. squirrels/package_data/base_project/macros/macros_example.sql +0 -15
  88. squirrels/package_data/base_project/models/dbviews/dbview_example.sql +0 -12
  89. squirrels/package_data/base_project/models/dbviews/dbview_example.yml +0 -26
  90. squirrels/package_data/base_project/models/federates/federate_example.py +0 -44
  91. squirrels/package_data/base_project/models/federates/federate_example.sql +0 -17
  92. squirrels/package_data/base_project/pyconfigs/connections.py +0 -14
  93. squirrels/package_data/base_project/pyconfigs/parameters.py +0 -93
  94. squirrels/package_data/base_project/pyconfigs/user.py +0 -23
  95. squirrels/package_data/base_project/squirrels.yml.j2 +0 -71
  96. squirrels-0.5.0rc0.dist-info/RECORD +0 -70
  97. /squirrels/{package_data → _package_data}/base_project/assets/expenses.db +0 -0
  98. /squirrels/{package_data → _package_data}/base_project/assets/weather.db +0 -0
  99. /squirrels/{package_data → _package_data}/base_project/docker/.dockerignore +0 -0
  100. /squirrels/{package_data → _package_data}/base_project/docker/Dockerfile +0 -0
  101. /squirrels/{package_data → _package_data}/base_project/docker/compose.yml +0 -0
  102. /squirrels/{package_data/base_project/.gitignore → _package_data/base_project/gitignore} +0 -0
  103. /squirrels/{package_data → _package_data}/base_project/seeds/seed_categories.csv +0 -0
  104. /squirrels/{package_data → _package_data}/base_project/seeds/seed_subcategories.csv +0 -0
  105. /squirrels/{package_data → _package_data}/base_project/tmp/.gitignore +0 -0
  106. {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/WHEEL +0 -0
  107. {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/entry_points.txt +0 -0
  108. {squirrels-0.5.0rc0.dist-info → squirrels-0.5.1.dist-info}/licenses/LICENSE +0 -0
squirrels/data_sources.py CHANGED
@@ -1,563 +1,14 @@
1
- from __future__ import annotations as _a
2
- import polars as _pl, typing as _t, dataclasses as _d, abc as _abc
3
-
4
- from . import _parameter_configs as _pc, parameter_options as _po
5
- from ._exceptions import ConfigurationError
6
-
7
-
8
- @_d.dataclass
9
- class DataSource(metaclass=_abc.ABCMeta):
10
- """
11
- Abstract class for lookup tables coming from a database
12
- """
13
- _table_or_query: str
14
- _id_col: str | None
15
- _is_from_seeds: bool
16
- _user_group_col: str | None
17
- _parent_id_col: str | None
18
- _connection: str | None
19
-
20
- @_abc.abstractmethod
21
- def __init__(
22
- self, table_or_query: str, *, id_col: str | None = None, from_seeds: bool = False, user_group_col: str | None = None,
23
- parent_id_col: str | None = None, connection: str | None = None, **kwargs
24
- ) -> None:
25
- self._table_or_query = table_or_query
26
- self._id_col = id_col
27
- self._is_from_seeds = from_seeds
28
- self._user_group_col = user_group_col
29
- self._parent_id_col = parent_id_col
30
- self._connection = connection
31
-
32
- def _get_connection_name(self, default_conn_name: str) -> str:
33
- return self._connection if self._connection is not None else default_conn_name
34
-
35
- def _get_query(self) -> str:
36
- """
37
- Get the "table_or_query" attribute as a select query
38
-
39
- Returns:
40
- str: The converted select query
41
- """
42
- if self._table_or_query.strip().lower().startswith('select '):
43
- query = self._table_or_query
44
- else:
45
- query = f'SELECT * FROM {self._table_or_query}'
46
- return query
47
-
48
- @_abc.abstractmethod
49
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.ParameterConfig:
50
- """
51
- An abstract method for converting itself into a parameter
52
- """
53
- pass
54
-
55
- def _validate_parameter_type(self, ds_param: _pc.DataSourceParameterConfig, target_parameter_type: _t.Type[_pc.ParameterConfig]) -> None:
56
- if ds_param.parameter_type != target_parameter_type:
57
- parameter_type_name = ds_param.parameter_type.__name__
58
- datasource_type_name = self.__class__.__name__
59
- raise ConfigurationError(f'Invalid widget type "{parameter_type_name}" for {datasource_type_name}')
60
-
61
- def _get_aggregated_df(self, df: _pl.DataFrame, columns_to_include: _t.Iterable[str]) -> _pl.DataFrame:
62
- if self._id_col is None:
63
- return df
64
-
65
- agg_rules = []
66
- for column in columns_to_include:
67
- if column is not None:
68
- agg_rules.append(_pl.first(column))
69
- if self._user_group_col is not None:
70
- agg_rules.append(_pl.col(self._user_group_col))
71
- if self._parent_id_col is not None:
72
- agg_rules.append(_pl.col(self._parent_id_col))
73
-
74
- try:
75
- df_agg = df.group_by(self._id_col).agg(agg_rules).sort(by=self._id_col)
76
- except _pl.exceptions.ColumnNotFoundError as e:
77
- raise ConfigurationError(e)
78
-
79
- return df_agg
80
-
81
- def _get_key_from_record(self, key: str | None, record: dict[_t.Hashable, _t.Any], default: _t.Any) -> _t.Any:
82
- return record[key] if key is not None else default
83
-
84
- def _get_key_from_record_as_list(self, key: str | None, record: dict[_t.Hashable, _t.Any]) -> _t.Iterable[str]:
85
- value = self._get_key_from_record(key, record, list())
86
- return [str(x) for x in value]
87
-
88
-
89
- @_d.dataclass
90
- class _SelectionDataSource(DataSource):
91
- """
92
- Abstract class for selection parameter data sources
93
- """
94
- _options_col: str
95
- _order_by_col: str | None
96
- _is_default_col: str | None
97
- _custom_cols: dict[str, str]
98
-
99
- @_abc.abstractmethod
100
- def __init__(
101
- self, table_or_query: str, id_col: str, options_col: str, *, order_by_col: str | None = None,
102
- is_default_col: str | None = None, custom_cols: dict[str, str] = {}, from_seeds: bool = False,
103
- user_group_col: str | None = None, parent_id_col: str | None = None, connection: str | None = None,
104
- **kwargs
105
- ) -> None:
106
- super().__init__(
107
- table_or_query, id_col=id_col, from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col,
108
- connection=connection
109
- )
110
- self._options_col = options_col
111
- self._order_by_col = order_by_col
112
- self._is_default_col = is_default_col
113
- self._custom_cols = custom_cols
114
-
115
- def _get_all_options(self, df: _pl.DataFrame) -> _t.Sequence[_po.SelectParameterOption]:
116
- columns = [self._options_col, self._order_by_col, self._is_default_col, *self._custom_cols.values()]
117
- df_agg = self._get_aggregated_df(df, columns)
118
-
119
- if self._order_by_col is None:
120
- df_agg = df_agg.sort(by=self._id_col)
121
- else:
122
- df_agg = df_agg.sort(by=self._order_by_col)
123
-
124
- def get_is_default(record: dict[_t.Hashable, _t.Any]) -> bool:
125
- return int(record[self._is_default_col]) == 1 if self._is_default_col is not None else False
126
-
127
- def get_custom_fields(record: dict[_t.Hashable, _t.Any]) -> dict[str, _t.Any]:
128
- result = {}
129
- for key, val in self._custom_cols.items():
130
- result[key] = record[val]
131
- return result
132
-
133
- records = df_agg.to_pandas().to_dict("records")
134
- return tuple(
135
- _po.SelectParameterOption(
136
- str(record[self._id_col]), str(record[self._options_col]),
137
- is_default=get_is_default(record), custom_fields=get_custom_fields(record),
138
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
139
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
140
- )
141
- for record in records
142
- )
143
-
144
-
145
- @_d.dataclass
146
- class SelectDataSource(_SelectionDataSource):
147
- """
148
- Lookup table for select parameter options
149
- """
150
-
151
- def __init__(
152
- self, table_or_query: str, id_col: str, options_col: str, *, order_by_col: str | None = None,
153
- is_default_col: str | None = None, custom_cols: dict[str, str] = {}, from_seeds: bool = False,
154
- user_group_col: str | None = None, parent_id_col: str | None = None, connection: str | None = None,
155
- **kwargs
156
- ) -> None:
157
- """
158
- Constructor for SelectDataSource
159
-
160
- Arguments:
161
- table_or_query: Either the name of the table to use, or a query to run
162
- id_col: The column name of the id
163
- options_col: The column name of the options
164
- order_by_col: The column name to order the options by. Orders by the id_col instead if this is None
165
- is_default_col: The column name that indicates which options are the default
166
- custom_cols: Dictionary of attribute to column name for custom fields for the SelectParameterOption
167
- from_seeds: Boolean for whether this datasource is created from seeds
168
- user_group_col: The column name of the user group that the user is in for this option to be valid
169
- parent_id_col: The column name of the parent option id that must be selected for this option to be valid
170
- connection: Name of the connection to use defined in connections.py
171
- """
172
- super().__init__(
173
- table_or_query, id_col, options_col, order_by_col=order_by_col, is_default_col=is_default_col, custom_cols=custom_cols,
174
- from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col, connection=connection
175
- )
176
-
177
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.SelectionParameterConfig:
178
- """
179
- Method to convert the associated DataSourceParameter into a SingleSelectParameterConfig or MultiSelectParameterConfig
180
-
181
- Arguments:
182
- ds_param: The parameter to convert
183
- df: The dataframe containing the parameter options data
184
-
185
- Returns:
186
- The converted parameter
187
- """
188
- all_options = self._get_all_options(df)
189
- if ds_param.parameter_type == _pc.SingleSelectParameterConfig:
190
- return _pc.SingleSelectParameterConfig(
191
- ds_param.name, ds_param.label, all_options, description=ds_param.description,
192
- user_attribute=ds_param.user_attribute, parent_name=ds_param.parent_name, **ds_param.extra_args
193
- )
194
- elif ds_param.parameter_type == _pc.MultiSelectParameterConfig:
195
- return _pc.MultiSelectParameterConfig(
196
- ds_param.name, ds_param.label, all_options, description=ds_param.description,
197
- user_attribute=ds_param.user_attribute, parent_name=ds_param.parent_name, **ds_param.extra_args
198
- )
199
- else:
200
- raise ConfigurationError(f'Invalid widget type "{ds_param.parameter_type}" for SelectDataSource')
201
-
202
-
203
- @_d.dataclass
204
- class DateDataSource(DataSource):
205
- """
206
- Lookup table for date parameter default options
207
- """
208
- _default_date_col: str
209
- _date_format: str
210
-
211
- def __init__(
212
- self, table_or_query: str, default_date_col: str, *, min_date_col: str | None = None,
213
- max_date_col: str | None = None, date_format: str = '%Y-%m-%d', id_col: str | None = None,
214
- from_seeds: bool = False, user_group_col: str | None = None, parent_id_col: str | None = None,
215
- connection: str | None = None, **kwargs
216
- ) -> None:
217
- """
218
- Constructor for DateDataSource
219
-
220
- Arguments:
221
- table_or_query: Either the name of the table to use, or a query to run
222
- default_date_col: The column name of the default date
223
- date_format: The format of the default date(s). Defaults to '%Y-%m-%d'
224
- id_col: The column name of the id
225
- from_seeds: Boolean for whether this datasource is created from seeds
226
- user_group_col: The column name of the user group that the user is in for this option to be valid
227
- parent_id_col: The column name of the parent option id that the default date belongs to
228
- connection: Name of the connection to use defined in connections.py
229
- """
230
- super().__init__(
231
- table_or_query, id_col=id_col, from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col,
232
- connection=connection
233
- )
234
- self._default_date_col = default_date_col
235
- self._min_date_col = min_date_col
236
- self._max_date_col = max_date_col
237
- self._date_format = date_format
238
-
239
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.DateParameterConfig:
240
- """
241
- Method to convert the associated DataSourceParameter into a DateParameterConfig
242
-
243
- Arguments:
244
- ds_param: The parameter to convert
245
- df: The dataframe containing the parameter options data
246
-
247
- Returns:
248
- The converted parameter
249
- """
250
- self._validate_parameter_type(ds_param, _pc.DateParameterConfig)
251
-
252
- columns = [self._default_date_col, self._min_date_col, self._max_date_col]
253
- df_agg = self._get_aggregated_df(df, columns)
254
-
255
- records = df_agg.to_pandas().to_dict("records")
256
- options = tuple(
257
- _po.DateParameterOption(
258
- str(record[self._default_date_col]), date_format=self._date_format,
259
- min_date = str(record[self._min_date_col]) if self._min_date_col else None,
260
- max_date = str(record[self._max_date_col]) if self._max_date_col else None,
261
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
262
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
263
- )
264
- for record in records
265
- )
266
- return _pc.DateParameterConfig(
267
- ds_param.name, ds_param.label, options, description=ds_param.description, user_attribute=ds_param.user_attribute,
268
- parent_name=ds_param.parent_name, **ds_param.extra_args
269
- )
270
-
271
-
272
- @_d.dataclass
273
- class DateRangeDataSource(DataSource):
274
- """
275
- Lookup table for date parameter default options
276
- """
277
- _default_start_date_col: str
278
- _default_end_date_col: str
279
- _date_format: str
280
-
281
- def __init__(
282
- self, table_or_query: str, default_start_date_col: str, default_end_date_col: str, *, date_format: str = '%Y-%m-%d',
283
- min_date_col: str | None = None, max_date_col: str | None = None, id_col: str | None = None, from_seeds: bool = False,
284
- user_group_col: str | None = None, parent_id_col: str | None = None, connection: str | None = None, **kwargs
285
- ) -> None:
286
- """
287
- Constructor for DateRangeDataSource
288
-
289
- Arguments:
290
- table_or_query: Either the name of the table to use, or a query to run
291
- default_start_date_col: The column name of the default start date
292
- default_end_date_col: The column name of the default end date
293
- date_format: The format of the default date(s). Defaults to '%Y-%m-%d'
294
- id_col: The column name of the id
295
- from_seeds: Boolean for whether this datasource is created from seeds
296
- user_group_col: The column name of the user group that the user is in for this option to be valid
297
- parent_id_col: The column name of the parent option id that the default date belongs to
298
- connection: Name of the connection to use defined in connections.py
299
- """
300
- super().__init__(
301
- table_or_query, id_col=id_col, from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col,
302
- connection=connection
303
- )
304
- self._default_start_date_col = default_start_date_col
305
- self._default_end_date_col = default_end_date_col
306
- self._min_date_col = min_date_col
307
- self._max_date_col = max_date_col
308
- self._date_format = date_format
309
-
310
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.DateRangeParameterConfig:
311
- """
312
- Method to convert the associated DataSourceParameter into a DateRangeParameterConfig
313
-
314
- Arguments:
315
- ds_param: The parameter to convert
316
- df: The dataframe containing the parameter options data
317
-
318
- Returns:
319
- The converted parameter
320
- """
321
- self._validate_parameter_type(ds_param, _pc.DateRangeParameterConfig)
322
-
323
- columns = [self._default_start_date_col, self._default_end_date_col, self._min_date_col, self._max_date_col]
324
- df_agg = self._get_aggregated_df(df, columns)
325
-
326
- records = df_agg.to_pandas().to_dict("records")
327
- options = tuple(
328
- _po.DateRangeParameterOption(
329
- str(record[self._default_start_date_col]), str(record[self._default_end_date_col]),
330
- min_date=str(record[self._min_date_col]) if self._min_date_col else None,
331
- max_date=str(record[self._max_date_col]) if self._max_date_col else None,
332
- date_format=self._date_format,
333
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
334
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
335
- )
336
- for record in records
337
- )
338
- return _pc.DateRangeParameterConfig(
339
- ds_param.name, ds_param.label, options, description=ds_param.description, user_attribute=ds_param.user_attribute,
340
- parent_name=ds_param.parent_name, **ds_param.extra_args
341
- )
342
-
343
-
344
- @_d.dataclass
345
- class _NumericDataSource(DataSource):
346
- """
347
- Abstract class for number or number range data sources
348
- """
349
- _min_value_col: str
350
- _max_value_col: str
351
- _increment_col: str | None
352
-
353
- @_abc.abstractmethod
354
- def __init__(
355
- self, table_or_query: str, min_value_col: str, max_value_col: str, *, increment_col: str | None = None,
356
- id_col: str | None = None, from_seeds: bool = False, user_group_col: str | None = None,
357
- parent_id_col: str | None = None, connection: str | None = None, **kwargs
358
- ) -> None:
359
- super().__init__(
360
- table_or_query, id_col=id_col, from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col,
361
- connection=connection
362
- )
363
- self._min_value_col = min_value_col
364
- self._max_value_col = max_value_col
365
- self._increment_col = increment_col
366
-
367
-
368
- @_d.dataclass
369
- class NumberDataSource(_NumericDataSource):
370
- """
371
- Lookup table for number parameter default options
372
- """
373
- _default_value_col: str | None
374
-
375
- def __init__(
376
- self, table_or_query: str, min_value_col: str, max_value_col: str, *, increment_col: str | None = None,
377
- default_value_col: str | None = None, id_col: str | None = None, from_seeds: bool = False,
378
- user_group_col: str | None = None, parent_id_col: str | None = None, connection: str | None = None, **kwargs
379
- ) -> None:
380
- """
381
- Constructor for NumberDataSource
382
-
383
- Arguments:
384
- table_or_query: Either the name of the table to use, or a query to run
385
- min_value_col: The column name of the minimum value
386
- max_value_col: The column name of the maximum value
387
- increment_col: The column name of the increment value. Defaults to column of 1's if None
388
- default_value_col: The column name of the default value. Defaults to min_value_col if None
389
- id_col: The column name of the id
390
- from_seeds: Boolean for whether this datasource is created from seeds
391
- user_group_col: The column name of the user group that the user is in for this option to be valid
392
- parent_id_col: The column name of the parent option id that the default value belongs to
393
- connection: Name of the connection to use defined in connections.py
394
- """
395
- super().__init__(
396
- table_or_query, min_value_col, max_value_col, increment_col=increment_col, id_col=id_col, from_seeds=from_seeds,
397
- user_group_col=user_group_col, parent_id_col=parent_id_col, connection=connection
398
- )
399
- self._default_value_col = default_value_col
400
-
401
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.NumberParameterConfig:
402
- """
403
- Method to convert the associated DataSourceParameter into a NumberParameterConfig
404
-
405
- Arguments:
406
- ds_param: The parameter to convert
407
- df: The dataframe containing the parameter options data
408
-
409
- Returns:
410
- The converted parameter
411
- """
412
- self._validate_parameter_type(ds_param, _pc.NumberParameterConfig)
413
-
414
- columns = [self._min_value_col, self._max_value_col, self._increment_col, self._default_value_col]
415
- df_agg = self._get_aggregated_df(df, columns)
416
-
417
- records = df_agg.to_pandas().to_dict("records")
418
- options = tuple(
419
- _po.NumberParameterOption(
420
- record[self._min_value_col], record[self._max_value_col],
421
- increment=self._get_key_from_record(self._increment_col, record, 1),
422
- default_value=self._get_key_from_record(self._default_value_col, record, None),
423
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
424
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
425
- )
426
- for record in records
427
- )
428
- return _pc.NumberParameterConfig(
429
- ds_param.name, ds_param.label, options, description=ds_param.description, user_attribute=ds_param.user_attribute,
430
- parent_name=ds_param.parent_name, **ds_param.extra_args
431
- )
432
-
433
-
434
- @_d.dataclass
435
- class NumberRangeDataSource(_NumericDataSource):
436
- """
437
- Lookup table for number range parameter default options
438
- """
439
- _default_lower_value_col: str | None
440
- _default_upper_value_col: str | None
441
-
442
- def __init__(
443
- self, table_or_query: str, min_value_col: str, max_value_col: str, *, increment_col: str | None = None,
444
- default_lower_value_col: str | None = None, default_upper_value_col: str | None = None, id_col: str | None = None,
445
- from_seeds: bool = False, user_group_col: str | None = None, parent_id_col: str | None = None,
446
- connection: str | None = None, **kwargs
447
- ) -> None:
448
- """
449
- Constructor for NumRangeDataSource
450
-
451
- Arguments:
452
- table_or_query: Either the name of the table to use, or a query to
453
- min_value_col: The column name of the minimum value
454
- max_value_col: The column name of the maximum value
455
- increment_col: The column name of the increment value. Defaults to column of 1's if None
456
- default_lower_value_col: The column name of the default lower value. Defaults to min_value_col if None
457
- default_upper_value_col: The column name of the default upper value. Defaults to max_value_col if None
458
- id_col: The column name of the id
459
- from_seeds: Boolean for whether this datasource is created from seeds
460
- user_group_col: The column name of the user group that the user is in for this option to be valid
461
- parent_id_col: The column name of the parent option id that the default value belongs to
462
- connection: Name of the connection to use defined in connections.py
463
- """
464
- super().__init__(
465
- table_or_query, min_value_col, max_value_col, increment_col=increment_col, id_col=id_col, from_seeds=from_seeds,
466
- user_group_col=user_group_col, parent_id_col=parent_id_col, connection=connection
467
- )
468
- self._default_lower_value_col = default_lower_value_col
469
- self._default_upper_value_col = default_upper_value_col
470
-
471
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.NumberRangeParameterConfig:
472
- """
473
- Method to convert the associated DataSourceParameter into a NumberRangeParameterConfig
474
-
475
- Arguments:
476
- ds_param: The parameter to convert
477
- df: The dataframe containing the parameter options data
478
-
479
- Returns:
480
- The converted parameter
481
- """
482
- self._validate_parameter_type(ds_param, _pc.NumberRangeParameterConfig)
483
-
484
- columns = [self._min_value_col, self._max_value_col, self._increment_col, self._default_lower_value_col, self._default_upper_value_col]
485
- df_agg = self._get_aggregated_df(df, columns)
486
-
487
- records = df_agg.to_pandas().to_dict("records")
488
- options = tuple(
489
- _po.NumberRangeParameterOption(
490
- record[self._min_value_col], record[self._max_value_col],
491
- increment=self._get_key_from_record(self._increment_col, record, 1),
492
- default_lower_value=self._get_key_from_record(self._default_lower_value_col, record, None),
493
- default_upper_value=self._get_key_from_record(self._default_upper_value_col, record, None),
494
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
495
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
496
- )
497
- for record in records
498
- )
499
- return _pc.NumberRangeParameterConfig(
500
- ds_param.name, ds_param.label, options, description=ds_param.description, user_attribute=ds_param.user_attribute,
501
- parent_name=ds_param.parent_name, **ds_param.extra_args
502
- )
503
-
504
-
505
- @_d.dataclass
506
- class TextDataSource(DataSource):
507
- """
508
- Lookup table for text parameter default options
509
- """
510
- _default_text_col: str
511
-
512
- def __init__(
513
- self, table_or_query: str, default_text_col: str, *, id_col: str | None = None, from_seeds: bool = False,
514
- user_group_col: str | None = None, parent_id_col: str | None = None, connection: str | None = None,
515
- **kwargs
516
- ) -> None:
517
- """
518
- Constructor for TextDataSource
519
-
520
- Arguments:
521
- table_or_query: Either the name of the table to use, or a query to run
522
- default_text_col: The column name of the default text
523
- id_col: The column name of the id
524
- from_seeds: Boolean for whether this datasource is created from seeds
525
- user_group_col: The column name of the user group that the user is in for this option to be valid
526
- parent_id_col: The column name of the parent option id that the default date belongs to
527
- connection: Name of the connection to use defined in connections.py
528
- """
529
- super().__init__(
530
- table_or_query, id_col=id_col, from_seeds=from_seeds, user_group_col=user_group_col, parent_id_col=parent_id_col,
531
- connection=connection
532
- )
533
- self._default_text_col = default_text_col
534
-
535
- def _convert(self, ds_param: _pc.DataSourceParameterConfig, df: _pl.DataFrame) -> _pc.TextParameterConfig:
536
- """
537
- Method to convert the associated DataSourceParameter into a TextParameterConfig
538
-
539
- Arguments:
540
- ds_param: The parameter to convert
541
- df: The dataframe containing the parameter options data
542
-
543
- Returns:
544
- The converted parameter
545
- """
546
- self._validate_parameter_type(ds_param, _pc.TextParameterConfig)
547
-
548
- columns = [self._default_text_col]
549
- df_agg = self._get_aggregated_df(df, columns)
550
-
551
- records = df_agg.to_pandas().to_dict("records")
552
- options = tuple(
553
- _po.TextParameterOption(
554
- default_text=str(record[self._default_text_col]),
555
- user_groups=self._get_key_from_record_as_list(self._user_group_col, record),
556
- parent_option_ids=self._get_key_from_record_as_list(self._parent_id_col, record)
557
- )
558
- for record in records
559
- )
560
- return _pc.TextParameterConfig(
561
- ds_param.name, ds_param.label, options, description=ds_param.description, user_attribute=ds_param.user_attribute,
562
- parent_name=ds_param.parent_name, **ds_param.extra_args
563
- )
1
+ from ._data_sources import (
2
+ SourceEnum,
3
+ SelectDataSource,
4
+ DateDataSource,
5
+ DateRangeDataSource,
6
+ NumberDataSource,
7
+ NumberRangeDataSource,
8
+ TextDataSource
9
+ )
10
+
11
+ __all__ = [
12
+ "SourceEnum", "SelectDataSource", "DateDataSource", "DateRangeDataSource",
13
+ "NumberDataSource", "NumberRangeDataSource", "TextDataSource"
14
+ ]