-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
64 lines (53 loc) · 1.91 KB
/
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
import os
import json
try:
import yaml
hasYaml = True
except ImportError:
hasYaml = False
class Config(object):
def __init__(self, config):
# Try each config file until it exists
for config_dir in config_dirs():
for ext in ["json", "yaml"]:
filepath = "%s/cursemail/%s.%s" % (config_dir, config, ext)
print filepath
try:
self.load_config("%s/%s" % (config_dir, filepath))
print filepath
return
except IOError:
# File doesn't exist. Continue with next one.
pass
else:
raise ConfigError("No config file found")
def create_config(self, filename):
#os.touch(next(config_dirs()) + "/cursemail/config.json")
pass # TODO
def load_config(self, filename):
(_, ext) = os.path.splitext(filename)
with open(filename) as configdata:
self.config_filename = filename
if ext == ".yaml":
if hasYaml:
self.config = yaml.load(configdata)
else:
raise ConfigWarning(
"file %s found, but pyyaml was not installed" % filename)
elif ext == ".json":
self.config = json.load(configdata)
else:
raise ConfigError("Unknown file format %s" % filename)
def config_dirs():
# Use xdg standards to find a place to put the config file
yield os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config")
for config in os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(":"):
yield config
yield "."
class ConfigError(Exception):
pass
class ConfigWarning(Warning):
pass
if __name__ == "__main__":
#print "config", getConfig("email")
pass