forked from brack3t/django-modular-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodular_settings.py
More file actions
77 lines (62 loc) · 1.97 KB
/
modular_settings.py
File metadata and controls
77 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import sys
import re
init_content = """from .base import *
try:
from .local import *
except ImportError:
pass
"""
def get_project_dir():
dir_list = re.split(r'[\\/]', os.path.abspath(__file__))[:-1]
return '/'.join(dir_list)
def make_it_so():
"""
Read the current default settings.py into memory.
Create the settings directory.
Create the basic __init__.py file which imports base.py and looks
for local.py.
Write settings to base.py.
Clean up and dump the old settings.py and the pyc file if it exists.
"""
PROJECT_PATH = get_project_dir()
if os.path.exists(PROJECT_PATH + '/settings'):
raise Exception("A settings directory already exists.")
try:
settings = open(PROJECT_PATH + '/settings.py', 'r')
except IOError:
raise Exception("Cannot open your default settings file.")
try:
os.makedirs(PROJECT_PATH + '/settings')
except Error:
raise Exception("Could not create the settings folder.")
try:
init = open(PROJECT_PATH + '/settings/__init__.py', 'wb')
init.write(init_content)
init.close()
except IOError:
raise IOError("Could not create the __init__.py file.")
try:
base = open(PROJECT_PATH + '/settings/base.py', 'wb')
base.write(settings.read())
base.close()
settings.close()
except IOError:
raise IOError("WTF")
try:
os.remove(PROJECT_PATH + '/settings.py')
except OSError:
raise OSError("Cannot delete the default settings file.")
try:
os.remove(PROJECT_PATH + '/settings.pyc')
except OSError:
pass
try:
local = open(PROJECT_PATH + '/settings/local.py', 'wb')
local.write('# your local dev settings go here.')
local.close()
except IOError:
raise IOError("Cannot write local.py file.")
sys.stdout.write("Your settings are now a little more awesome.\n")
if __name__ == '__main__':
make_it_so()