Last active
December 23, 2015 04:49
-
-
Save pbanaszkiewicz/6583124 to your computer and use it in GitHub Desktop.
Suggestion of API to make loading settings for GWM work well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import string | |
from django.core.exceptions import ImproperlyConfigured | |
def random_secret(n=20): | |
"Create randomly selected character sequence." | |
return "".join(random.sample(string.digits + string.printable, n)) | |
def load_setting_env(envname): | |
"""Load specific envvar. Raise ImproperlyConfigured if no such envvar is | |
found.""" | |
try: | |
return os.environ[envname] | |
except KeyError: | |
raise ImproperlyConfigured | |
def load_setting_file(filename, create=False): | |
"""Load specific file contents as a string. Create first, in case it | |
doesn't exists (and populate with random characters).""" | |
if create: | |
try: | |
with open(filename, "w") as f: | |
f.write(random_secret(20)) | |
except IOError: | |
raise ImproperlyConfigured | |
# read from file (contents get stripped) | |
try: | |
contents = "" | |
with open(filename, "r") as f: | |
contents = f.read() | |
return contents.strip() | |
except IOError: | |
raise ImproperlyConfigured |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment