forked from loktacar/wallpapermaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
129 lines (100 loc) · 3.36 KB
/
config.py
File metadata and controls
129 lines (100 loc) · 3.36 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
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
import copy
import imp
import sys
import os
import io
import logging
from appdirs import AppDirs
from docopt import docopt
import ConfigParser
appname = 'wpmaker'
appauthor = 'viktor'
config_file_name = '%s.conf' % appname
class ConfigurationError(ValueError):
def cmdline_message(self):
return "Configuration error: {}".format(self.message)
def get_appdirs():
return AppDirs(appname, appauthor)
def get_appdirs_paths():
appdirs = get_appdirs()
dirs = (appdirs.user_data_dir, appdirs.site_data_dir)
return [os.path.join(dir, config_file_name) for dir in dirs]
def get_doc(options):
doc = """Usage: wpmaker.py [options]
Options:
"""
for op in sorted(options, key=lambda op: (op.folder_module(), op.option)):
doc += """%s
""" % op.get_doc_line()
doc += """ -h --help Displays this help message
Configuration files:
"""
for i, dir in enumerate(get_appdirs_paths()):
doc += """ (%s) %s
""" % (i, dir)
return doc
def get_file_config():
files = get_appdirs_paths()
cfg = ConfigParser.RawConfigParser()
cfg_files = cfg.read(files)
return cfg
def save_config(section, option, value):
# Get file config and update
cfg = get_file_config()
# Check if it has the section, create if neccesary
if not cfg.has_section(section):
cfg.add_section(section)
# Set the option
cfg.set(section, option, value)
# Confirm that the folders exist
appdirs = get_appdirs()
if not os.path.exists(appdirs.user_data_dir):
os.makedirs(appdirs.user_data_dir)
# Open file stream and write
files = get_appdirs_paths()
cf = open(files[0], 'w')
cfg.write(cf)
cf.close()
def get_config(options):
logging.debug('Reading config file and parsing options')
# keys are "plugin_module.option_name"
config = {}
# options in each plugin
module_options = {}
# Set config to defaults
for op in options:
op_module = op.__module__.split('.')[1]
# fill config dict
if op_module == 'options':
config[op.option] = op.default
else:
config['%s.%s' % (op_module, op.option)] = op.default
# fill module_options dict
if op_module in module_options:
module_options[op_module].append(op)
else:
module_options[op_module] = [op]
cfg = get_file_config()
for section in cfg.sections():
module_pref = '' if section == 'options' else '%s.' % section
if not section in module_options:
logging.warning('Section %s in configuration file not found' % section)
continue
for op in module_options[section]:
if cfg.has_option(section, op.option):
config[module_pref + op.option] = op.parse(cfg.get(section, op.option))
# Parse command line options
from docopt import docopt
doc_options = docopt(get_doc(options))
for key in doc_options:
if key[:2] == '--' and doc_options[key] is not None:
option = key[2:]
module = 'options'
if '.' in key[2:]:
split_key = key[2:].split('.')
option = split_key[-1]
module = split_key[-2]
for op in module_options[module]:
if op.option == option:
config[key[2:]] = op.parse(doc_options[key])
return config