forked from hforge/usine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
83 lines (62 loc) · 2.48 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: UTF-8 -*-
# Copyright (C) 2009-2010 Juan David Ibáñez Palomar <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from ConfigParser import RawConfigParser
from os.path import expanduser
# Import from itools
from itools.fs import lfs
# Import from usine
from modules import modules
class configuration(object):
def __init__(self):
self.by_type = {} # type: [<data>, ..]
self.by_type_and_name = {} # (type, name): <data>
def load(self):
path = expanduser('~/.usine')
if lfs.is_file(path):
return 'ERROR: %s is a file, remove it first' % path
# Make the user configuration file if needed
if not lfs.exists(path):
print 'Making the configuration folder:', path
lfs.make_folder(path)
return 'Now add the INI files within the folder'
# Read the user configuration file
ini = [ '%s/%s' % (path, x)
for x in lfs.get_names(path) if x[-4:] == '.ini' ]
if len(ini) == 0:
return 'ERROR: zero INI files found in %s/' % path
# Read the ini file
cfg = RawConfigParser()
cfg.read(ini)
# Get the data
for section in cfg._sections:
options = cfg._sections[section]
type, name = section.split()
module = modules[type]
obj = module(options)
# Keep the data unit
self.by_type.setdefault(type, []).append(obj)
self.by_type_and_name[(type, name)] = obj
# Sort
for type in self.by_type:
self.by_type[type].sort(key=lambda x: x.name)
def get_sections_by_type(self, type):
return self.by_type.get(type, [])
def get_section(self, type, name):
key = (type, name)
return self.by_type_and_name.get(key)
# singleton
config = configuration()