]> Softwares of Agnibho - ddstorm.git/blob - conf.py
Major code rearrangement
[ddstorm.git] / conf.py
1 #! /usr/bin/python3
2
3 ''' This module handles the different configuration options of DDStorm. '''
4
5 '''
6 Copyright (c) 2015 Agnibho Mondal
7 All rights reserved
8
9 This file is part of DDStorm.
10
11 DDStorm is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 DDStorm is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
23 '''
24
25 import os
26 import logging
27
28 from const import *
29
30 logging.basicConfig(filename=LOG_FILE)
31
32 class Conf:
33 '''
34 * This class manages DDStorm configuration options.
35 * If the configuration file is found it reads the file
36 and initiates the configurations.
37 * If configuration couldn't be read it initiates some
38 default configurations.
39 * It provides the configurations to other classes through
40 function calls.
41 '''
42
43 _conf={}
44
45 def __init__(self, filename=CONF_FILE):
46 '''
47 The constructor accepts a configuration filename.
48 If none is provided it defaults to CONF_FILE from const.
49 Calls default() and read()
50 '''
51 self.filename=filename
52 self.default()
53 self.read()
54
55 def default(self):
56 ''' Set the default values '''
57 self._conf["library_path"]="./library/"
58 self._conf["custom_path"]="./custom/"
59 self._conf["index_path"]="./index/"
60 self._conf["alias_path"]="./alias/"
61 self._conf["module_path"]="./modules/"
62 self._conf["splash_screen"]="yes"
63 self._conf["clean_log"]="no"
64 self._conf["status_message"]="on"
65
66 def read(self):
67 ''' Read the configuration file and collect the values '''
68 # If file is actually present
69 if(os.path.isfile(self.filename)):
70 try:
71 with open(self.filename) as f:
72 for line in f:
73 # Removes any stray whitespaces
74 line="".join(line.split())
75 # Ignores comments starting with #
76 if(line.startswith("#")):
77 pass
78 # Library path
79 elif(line.startswith("library_path=")):
80 self._conf["library_path"]=line[13:]
81 if(os.path.isdir(self._conf["library_path"])):
82 if(not self._conf["library_path"].endswith("/")):
83 self._conf["library_path"]+="/"
84 # Custom path
85 elif(line.startswith("custom_path=")):
86 self._conf["custom_path"]=line[12:]
87 if(os.path.isdir(self._conf["custom_path"])):
88 if(not self._conf["custom_path"].endswith("/")):
89 self._conf["custom_path"]+="/"
90 # Index path
91 elif(line.startswith("index_path=")):
92 self._conf["index_path"]=line[11:]
93 if(os.path.isdir(self._conf["index_path"])):
94 if(not self._conf["index_path"].endswith("/")):
95 self._conf["index_path"]+="/"
96 # Alias path
97 elif(line.startswith("alias_path=")):
98 self._conf["alias_path"]=line[11:]
99 if(os.path.isdir(self._conf["alias_path"])):
100 if(not self._conf["alias_path"].endswith("/")):
101 self._conf["alias_path"]+="/"
102 # Module path
103 elif(line.startswith("module_path=")):
104 self._conf["module_path"]=line[12:]
105 if(os.path.isdir(self._conf["module_path"])):
106 if(not self._conf["module_path"].endswith("/")):
107 self._conf["module_path"]+="/"
108 # Splash screen
109 elif(line.startswith("splash_screen=")):
110 self._conf["splash_screen"]=line[14:]
111 # Clean log
112 elif(line.startswith("clean_log=")):
113 self._conf["clean_log"]=line[10:]
114 # Status message
115 elif(line.startswith("status_message=")):
116 self._conf["status_message"]=line[15:]
117 # Unknown option
118 else:
119 logging.warning("Unrecognized configuration: "+line)
120 except:
121 logging.error("Configuration file "+self.filename+" could not be read. Using default configurations.")
122
123 def get(self, key=False):
124 '''
125 Return the requested configuration item.
126 Return all if none specified.
127
128 Parameters:
129 key - string specifying the configuration item name
130
131 Return value:
132 String value if key is specified
133 Dictionary containing all options if key not specified
134 '''
135 if(key):
136 return self._conf[key]
137 else:
138 return self._conf
139
140 def set(self, key, value):
141 '''
142 Allow modification of a configuration to a new value
143
144 Parameters:
145 key - configuration item name
146 value - the value to set the configuration item to
147 '''
148 self._conf[key]=value
149
150 def write(self):
151 ''' Write configurations to file '''
152 with open(CONF_FILE, "w") as f:
153 for k in self._conf:
154 print(k+"="+self._conf[k], file=f)
155
156 #If invoked directly, prints a list of available configurations
157 if(__name__=="__main__"):
158 c=Conf(CONF_FILE)
159 print(c.get())