|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
| 4 | + "attachments": {}, |
4 | 5 | "cell_type": "markdown", |
5 | 6 | "metadata": {}, |
6 | 7 | "source": [ |
|
221 | 222 | " CONFIG_VALIDATOR_JSON_SCHEMA_ENVVAR_NAME = 'CONFIG_VALIDATOR_JSON_SCHEMA'\n", |
222 | 223 | "\n", |
223 | 224 | " @classmethod\n", |
224 | | - " def load_json(cls, json_source: Union[str, dict]=None) -> dict:\n", |
| 225 | + " def load_json(cls, json_source: Union[str, dict]=None, storage_driver: FS = None) -> dict:\n", |
225 | 226 | " '''\n", |
226 | 227 | " convenience method to return a dict from either\n", |
227 | 228 | " a file path or an already-loaded dict\n", |
228 | 229 | " '''\n", |
| 230 | + " storage_driver = storage_driver or cls.DEFAULT_STORAGE_DRIVER\n", |
229 | 231 | " if isinstance(json_source, str):\n", |
230 | | - " with cls.DEFAULT_STORAGE_DRIVER.open(json_source) as ifile:\n", |
| 232 | + " with storage_driver.open(json_source) as ifile:\n", |
231 | 233 | " return json.load(ifile)\n", |
232 | 234 | " elif isinstance(json_source, dict):\n", |
233 | 235 | " return json_source\n", |
234 | 236 | "\n", |
235 | 237 | " @classmethod\n", |
236 | | - " def get_default_json_schema(cls):\n", |
| 238 | + " def get_default_json_schema(cls, storage_driver: FS = None) -> dict:\n", |
| 239 | + " storage_driver = storage_driver or cls.DEFAULT_STORAGE_DRIVER\n", |
237 | 240 | " if cls.CONFIG_VALIDATOR_JSON_SCHEMA_ENVVAR_NAME in os.environ:\n", |
238 | 241 | " expected_json_schema_path = \\\n", |
239 | 242 | " os.environ[cls.CONFIG_VALIDATOR_JSON_SCHEMA_ENVVAR_NAME]\n", |
240 | | - " with cls.DEFAULT_STORAGE_DRIVER.open(expected_json_schema_path) as ifile:\n", |
| 243 | + " with storage_driver.open(expected_json_schema_path) as ifile:\n", |
241 | 244 | " return json.load(ifile)\n", |
242 | 245 | " return None\n", |
243 | 246 | "\n", |
|
270 | 273 | " return coerced_config\n", |
271 | 274 | " \n", |
272 | 275 | " @classmethod\n", |
273 | | - " def load_validated_config(cls, json_schema: Union[str, dict], config: dict):\n", |
274 | | - " return cls(json_schema).load_config(config)\n", |
| 276 | + " def load_validated_config(cls, json_schema: Union[str, dict], config: dict, **kwargs):\n", |
| 277 | + " return cls(json_schema, **kwargs).load_config(config)\n", |
275 | 278 | "\n", |
276 | 279 | " @classmethod\n", |
277 | | - " def load_validated_environment(cls, json_schema: Union[str, dict]=None):\n", |
278 | | - " return cls.load_validated_config(json_schema, dict(os.environ))\n", |
| 280 | + " def load_validated_environment(cls, json_schema: Union[str, dict]=None, **kwargs):\n", |
| 281 | + " return cls.load_validated_config(json_schema, dict(os.environ), **kwargs)\n", |
279 | 282 | " \n", |
280 | 283 | " @classmethod\n", |
281 | | - " def load_dotenv(cls, json_schema: Union[str, dict]=None, dotenv_path: str=None):\n", |
282 | | - " config = dotenv.dotenv_values(dotenv_path)\n", |
| 284 | + " def load_dotenv(cls, json_schema: Union[str, dict]=None, dotenv_path: str=None, storage_driver: FS=None):\n", |
| 285 | + " storage_driver = storage_driver or cls.DEFAULT_STORAGE_DRIVER\n", |
| 286 | + " with storage_driver.open(dotenv_path) as ifile:\n", |
| 287 | + " config = dotenv.dotenv_values(stream=ifile)\n", |
283 | 288 | " return cls.load_validated_config(\n", |
284 | 289 | " json_schema or cls.get_default_json_schema(), config)" |
285 | 290 | ] |
|
509 | 514 | "from fs.memoryfs import MemoryFS\n", |
510 | 515 | "memfs = MemoryFS()\n", |
511 | 516 | "\n", |
512 | | - "with memfs.open('schema.json', 'w') as ofile:\n", |
| 517 | + "memfs.makedirs('extra-long-directory-place', recreate=True)\n", |
| 518 | + "with memfs.open('extra-long-directory-place/schema.json', 'w') as ofile:\n", |
513 | 519 | " ofile.write(json.dumps(example_properties_schema))\n", |
514 | 520 | " os.environ['CONFIG_VALIDATOR_JSON_SCHEMA'] = ofile.name\n", |
515 | 521 | "\n", |
|
526 | 532 | " 'MY_INTEGER_VALUE': -85,\n", |
527 | 533 | " 'A_NUMERIC_VALUE': 12300.0,\n", |
528 | 534 | " '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n", |
529 | | - "})" |
| 535 | + "})\n", |
| 536 | + "\n", |
| 537 | + "# test loading dotenv from an arbitrary file\n", |
| 538 | + "memfs.makedirs('special-bespoke-location', recreate=True)\n", |
| 539 | + "with memfs.open('special-bespoke-location/my-own.env', 'w') as ofile:\n", |
| 540 | + " ofile.write('\\n'.join([\n", |
| 541 | + " 'string_value_with_enum=only',\n", |
| 542 | + " 'MY_INTEGER_VALUE=9989998',\n", |
| 543 | + " 'A_NUMERIC_VALUE=1167.89',\n", |
| 544 | + " ]))\n", |
| 545 | + "\n", |
| 546 | + "validated_dotenv = validator.load_dotenv(dotenv_path='special-bespoke-location/my-own.env')\n", |
| 547 | + "test_eq(validated_dotenv, {\n", |
| 548 | + " 'string_value_with_enum': 'only',\n", |
| 549 | + " 'MY_INTEGER_VALUE': 9989998,\n", |
| 550 | + " 'A_NUMERIC_VALUE': 1167.89,\n", |
| 551 | + " '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n", |
| 552 | + "})\n", |
| 553 | + "\n", |
| 554 | + "test_fail(validator.load_dotenv, kwargs={'dotenv_path': 'non-existent-location-own.env'})" |
530 | 555 | ] |
531 | 556 | }, |
532 | 557 | { |
|
0 commit comments