Skip to content

Commit 8c4bcc6

Browse files
committed
fix override storage driver for json schemas
due to heavy reliance on `classmethod`, adding the ability to swap out the storage driver arbitrarily withouth using a clobber-able class attribute leads to peppering the storage_driver argument in all the class method calls. ugly but right now, looks like the shortest path
1 parent bfb6814 commit 8c4bcc6

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

nbs/00_core.ipynb

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"cells": [
33
{
4-
"attachments": {},
54
"cell_type": "markdown",
65
"metadata": {},
76
"source": [
@@ -255,8 +254,8 @@
255254
" '''\n",
256255
" self.storage_driver = storage_driver or self.__class__.DEFAULT_STORAGE_DRIVER\n",
257256
" if isinstance(json_schema, (str, dict)):\n",
258-
" self._json_schema = self.__class__.load_json(json_schema)\n",
259-
" elif (default_schema := self.__class__.get_default_json_schema()):\n",
257+
" self._json_schema = self.__class__.load_json(json_schema, storage_driver=self.storage_driver)\n",
258+
" elif (default_schema := self.__class__.get_default_json_schema(storage_driver=self.storage_driver)):\n",
260259
" self._json_schema = default_schema\n",
261260
" else:\n",
262261
" raise Exception('did not receive or find a JSON schema')\n",
@@ -286,7 +285,8 @@
286285
" with storage_driver.open(dotenv_path) as ifile:\n",
287286
" config = dotenv.dotenv_values(stream=ifile)\n",
288287
" return cls.load_validated_config(\n",
289-
" json_schema or cls.get_default_json_schema(), config)"
288+
" json_schema or cls.get_default_json_schema(storage_driver=storage_driver),\n",
289+
" config, storage_driver=storage_driver)"
290290
]
291291
},
292292
{
@@ -507,9 +507,7 @@
507507
"outputs": [],
508508
"source": [
509509
"#| hide\n",
510-
"# FIXME this test clobbers the environment\n",
511-
"# FIXME this is a code smell on the class design\n",
512-
"# here we're testing the ability to override the storage driver (memoryfs here)\n",
510+
"# test ability to override the storage driver (memoryfs here)\n",
513511
"\n",
514512
"from fs.memoryfs import MemoryFS\n",
515513
"memfs = MemoryFS()\n",
@@ -519,8 +517,7 @@
519517
" ofile.write(json.dumps(example_properties_schema))\n",
520518
" os.environ['CONFIG_VALIDATOR_JSON_SCHEMA'] = ofile.name\n",
521519
"\n",
522-
"ConfigValidator.DEFAULT_STORAGE_DRIVER = memfs\n",
523-
"validator = ConfigValidator()\n",
520+
"validator = ConfigValidator(storage_driver=memfs)\n",
524521
"validated_config = validator.load_config({\n",
525522
" 'string_value_with_enum': 'these',\n",
526523
" 'MY_INTEGER_VALUE': '-85',\n",
@@ -532,9 +529,18 @@
532529
" 'MY_INTEGER_VALUE': -85,\n",
533530
" 'A_NUMERIC_VALUE': 12300.0,\n",
534531
" '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n",
535-
"})\n",
536-
"\n",
532+
"})"
533+
]
534+
},
535+
{
536+
"cell_type": "code",
537+
"execution_count": null,
538+
"metadata": {},
539+
"outputs": [],
540+
"source": [
541+
"#| hide\n",
537542
"# test loading dotenv from an arbitrary file\n",
543+
"\n",
538544
"memfs.makedirs('special-bespoke-location', recreate=True)\n",
539545
"with memfs.open('special-bespoke-location/my-own.env', 'w') as ofile:\n",
540546
" ofile.write('\\n'.join([\n",
@@ -543,12 +549,42 @@
543549
" 'A_NUMERIC_VALUE=1167.89',\n",
544550
" ]))\n",
545551
"\n",
546-
"validated_dotenv = validator.load_dotenv(dotenv_path='special-bespoke-location/my-own.env')\n",
552+
"validated_dotenv = validator.load_dotenv(\n",
553+
" dotenv_path='special-bespoke-location/my-own.env',\n",
554+
" storage_driver=memfs,\n",
555+
")\n",
556+
"\n",
547557
"test_eq(validated_dotenv, {\n",
548558
" 'string_value_with_enum': 'only',\n",
549559
" 'MY_INTEGER_VALUE': 9989998,\n",
550560
" 'A_NUMERIC_VALUE': 1167.89,\n",
551561
" '_____A_STRING_VALUE____with_default__': 'underscores_and spaces',\n",
562+
"})"
563+
]
564+
},
565+
{
566+
"cell_type": "code",
567+
"execution_count": null,
568+
"metadata": {},
569+
"outputs": [],
570+
"source": [
571+
"#| hide\n",
572+
"# test using custom json schema\n",
573+
"\n",
574+
"with memfs.open('foo.schema.json', 'w') as ofile:\n",
575+
" ofile.write(json.dumps({\n",
576+
" 'type': 'object',\n",
577+
" 'properties': {\n",
578+
" 'A_NUMERIC_VALUE': { 'type': 'number' },\n",
579+
" }\n",
580+
" }))\n",
581+
"validated_dotenv = ConfigValidator.load_dotenv(\n",
582+
" json_schema='foo.schema.json',\n",
583+
" dotenv_path='special-bespoke-location/my-own.env',\n",
584+
" storage_driver=memfs,\n",
585+
")\n",
586+
"test_eq(validated_dotenv, {\n",
587+
" 'A_NUMERIC_VALUE': 1167.89,\n",
552588
"})\n",
553589
"\n",
554590
"test_fail(validator.load_dotenv, kwargs={'dotenv_path': 'non-existent-location-own.env'})"

schematized_config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.3"
1+
__version__ = "0.0.4"

schematized_config/core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ def __init__(self, json_schema: Union[str, dict]=None, storage_driver: FS=None):
131131
'''
132132
self.storage_driver = storage_driver or self.__class__.DEFAULT_STORAGE_DRIVER
133133
if isinstance(json_schema, (str, dict)):
134-
self._json_schema = self.__class__.load_json(json_schema)
135-
elif (default_schema := self.__class__.get_default_json_schema()):
134+
self._json_schema = self.__class__.load_json(json_schema, storage_driver=self.storage_driver)
135+
elif (default_schema := self.__class__.get_default_json_schema(storage_driver=self.storage_driver)):
136136
self._json_schema = default_schema
137137
else:
138138
raise Exception('did not receive or find a JSON schema')
@@ -162,4 +162,5 @@ def load_dotenv(cls, json_schema: Union[str, dict]=None, dotenv_path: str=None,
162162
with storage_driver.open(dotenv_path) as ifile:
163163
config = dotenv.dotenv_values(stream=ifile)
164164
return cls.load_validated_config(
165-
json_schema or cls.get_default_json_schema(), config)
165+
json_schema or cls.get_default_json_schema(storage_driver=storage_driver),
166+
config, storage_driver=storage_driver)

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[DEFAULT]
22
repo = python-schematized-config
33
lib_name = python-schematized-config
4-
version = 0.0.3
4+
version = 0.0.4
55
min_python = 3.7
66
license = apache2
77
black_formatting = False

0 commit comments

Comments
 (0)