django12factor
django12factor copied to clipboard
Custom loader
Very much a WIP, but to allow type coercion, setting of defaults, etc.
@avengerpenguin, @mekhami, @haraball, @jdelic, @carlio, @jvc26 - as users of/contributors to d12f, I'd be very interested in your thoughts and opinions of this feature proposal please!
(Oh, also if you have advice on a) why that code block isn't rendering b) good tools for syntax/style checking of RST, that'd be amazing please)
I think "For example:" needs 2 colons after to get the code bock to render. Not sure though as I don't use the ..code-block
directive in my stuff.
Heh cheers, I thought that, but it seems identical to earlier codeblocks in my rst...
Possible alternative syntax:
custom_settings = EVL(
("LISTEN_PORT", 8080, int),
("OTHER_VAL", "default"),
)
Not sure if that is nicer or not, basically just removes lots of EVL
, but could lead to parsing faff.
Yeah I'm not sure how much I like the syntax, but I've not found a happier alternative - tuples make it a pain to have optional arguments; dicts might be better, but basically I've taken a leaf from Django's url
...
Fun observations: DATABASE_URL
is a bit of a faff because it breaks the "setting name == env var name" assumption.
For customisable defaults you can do something like:
evl = EVL(
"DATABASE_URL",
parser=dj_database_url.parse,
default=dj_database_url.parse("your://url")
)
DATABASES['default'] = evl.load_value()
which is icky but I'm not sure how best to improve it
Coverage remained the same at 100.0% when pulling 3d69fc3ed87d3e2db30ff51ad51b4db16fb94a82 on feature/custom-loader into 75c1d75bb831f539530fee42e245c88f9844b71e on master.
Changes Unknown when pulling d8d7bbede3ac9c63fb0b4ec4200bc1fda8608ce1 on feature/custom-loader into * on master*.
Is there anything I can do to move this PR, or django12factor in general, forward?
I have stopped using it for now and switched to a combination of os.environ.get()
, dj-database-url and custom helper functions for parsing env vars, but it seems to be a waste of time if people keep reinventing this, even if it's quite simple.
I realize this idea is somewhat radical, but do you think we could have a "just a bunch of functions" API?
DEBUG = d12f_bool('DEBUG', default=True)
SECRET_KEY = d12f_string('SECRET_KEY', require=True) # raise an exception if not set.
DATABASES['default'] = d12f_database_url('DATABASE_URL', default='your://url')
LOGGING = d12f_logging()
WHATEVER = d12f_custom('MY_ENV_VAR', parser=lambda s: s.lower())