-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscarlett_config.py
141 lines (122 loc) · 4.4 KB
/
scarlett_config.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
import StringIO
import os
import sys
import re
import ConfigParser
import signal
import pprint
pp = pprint.PrettyPrinter(indent=4)
# Config
try:
os.path.expanduser('~')
expanduser = os.path.expanduser
except (AttributeError, ImportError):
# This is probably running on App Engine.
expanduser = (lambda x: x)
# By default we use two locations for the scarlett configurations,
# /etc/scarlett.cfg and ~/.scarlett (which works on Windows and Unix).
ScarlettConfigPath = '/etc/scarlett.cfg'
UserConfigPath = os.path.join(expanduser('~'), '.scarlett')
ScarlettConfigLocations = [ScarlettConfigPath, UserConfigPath]
# If there's a SCARLETT_CONFIG variable set, we load ONLY
# that variable
if 'SCARLETT_CONFIG' in os.environ:
ScarlettConfigLocations = [expanduser(os.environ['SCARLETT_CONFIG'])]
elif 'SCARLETT_PATH' in os.environ:
ScarlettConfigLocations = [
expanduser(path) for path in os.environ['SCARLETT_PATH'].split(":")
]
class Config(ConfigParser.SafeConfigParser):
"""Scarlett Config Class."""
def __init__(self, path=None, fp=None, do_load=True):
# We don't use ``super`` here, because ``ConfigParser`` still uses
# old-style classes.
ConfigParser.SafeConfigParser.__init__(
self, {
'working_dir': '/mnt/scarlett', 'debug': '0'})
if do_load:
if path:
self.load_from_path(path)
elif fp:
self.readfp(fp)
else:
print 'Scarlett Config Path'
pp.pprint(ScarlettConfigLocations)
self.read(ScarlettConfigLocations)
def load_from_path(self, path):
file = open(path)
for line in file.readlines():
if match := re.match("^#import[\s\t]*([^\s^\t]*)[\s\t]*$", line):
extended_file = match[1]
(dir, file) = os.path.split(path)
self.load_from_path(os.path.join(dir, extended_file))
self.read(path)
def save_option(self, path, section, option, value):
"""
Write the specified Section.Option to the config file specified by path.
Replace any previous value. If the path doesn't exist, create it.
Also add the option the the in-memory config.
"""
config = ConfigParser.SafeConfigParser()
config.read(path)
if not config.has_section(section):
config.add_section(section)
config.set(section, option, value)
with open(path, 'w') as fp:
config.write(fp)
if not self.has_section(section):
self.add_section(section)
self.set(section, option, value)
def save_user_option(self, section, option, value):
self.save_option(UserConfigPath, section, option, value)
def save_system_option(self, section, option, value):
self.save_option(ScarlettConfigPath, section, option, value)
def get_user(self, name, default=None):
try:
val = self.get('User', name)
except:
val = default
return val
def getint_user(self, name, default=0):
try:
val = self.getint('User', name)
except:
val = default
return val
def get_value(self, section, name, default=None):
return self.get(section, name, default)
def get(self, section, name, default=None):
try:
val = ConfigParser.SafeConfigParser.get(self, section, name)
except:
val = default
return val
def getint(self, section, name, default=0):
try:
val = ConfigParser.SafeConfigParser.getint(self, section, name)
except:
val = int(default)
return val
def getfloat(self, section, name, default=0.0):
try:
val = ConfigParser.SafeConfigParser.getfloat(self, section, name)
except:
val = float(default)
return val
def getbool(self, section, name, default=False):
if self.has_option(section, name):
val = self.get(section, name)
val = val.lower() == 'true'
else:
val = default
return val
def setbool(self, section, name, value):
if value:
self.set(section, name, 'true')
else:
self.set(section, name, 'false')
def dump(self):
s = StringIO.StringIO()
self.write(s)
print s.getvalue()