]> Softwares of Agnibho - ddstorm.git/blob - conf.py
Added a new function to compile module
[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 * # Import constants
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={} #Initiates configuration dictionary
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(os.path.isfile(self.filename)): # If file is actually present
69 try:
70 with open(self.filename) as f: # Open file
71 for line in f:
72 line="".join(line.split()) # Removes any stray whitespaces
73 if(line.startswith("#")): # Ignores comments
74 pass
75 elif(line.startswith("library_path=")): # Library files path
76 self._conf["library_path"]=line[13:]
77 if(os.path.isdir(self._conf["library_path"])):
78 if(not self._conf["library_path"].endswith("/")):
79 self._conf["library_path"]+="/"
80 elif(line.startswith("custom_path=")): # Custom files path
81 self._conf["custom_path"]=line[12:]
82 if(os.path.isdir(self._conf["custom_path"])):
83 if(not self._conf["custom_path"].endswith("/")):
84 self._conf["custom_path"]+="/"
85 elif(line.startswith("index_path=")): # Index files path
86 self._conf["index_path"]=line[11:]
87 if(os.path.isdir(self._conf["index_path"])):
88 if(not self._conf["index_path"].endswith("/")):
89 self._conf["index_path"]+="/"
90 elif(line.startswith("alias_path=")): # Alias files path
91 self._conf["alias_path"]=line[11:]
92 if(os.path.isdir(self._conf["alias_path"])):
93 if(not self._conf["alias_path"].endswith("/")):
94 self._conf["alias_path"]+="/"
95 elif(line.startswith("module_path=")): # Path to save compiled modules
96 self._conf["module_path"]=line[12:]
97 if(os.path.isdir(self._conf["module_path"])):
98 if(not self._conf["module_path"].endswith("/")):
99 self._conf["module_path"]+="/"
100 elif(line.startswith("splash_screen=")): # Whether to show a splash screen
101 self._conf["splash_screen"]=line[14:]
102 elif(line.startswith("clean_log=")): # Whether to clean logs before exit
103 self._conf["clean_log"]=line[10:]
104 elif(line.startswith("status_message=")): # Whether to show status messages
105 self._conf["status_message"]=line[15:]
106 else:
107 logging.warning("Unrecognized configuration: "+line) # Log a warning if unrecognized option found
108 except: # Go with default if file could not be read and log an error
109 logging.error("Configuration file "+self.filename+" could not be read. Using default configurations.")
110
111 def get(self, key=False):
112 '''
113 Return the requested configuration item.
114 Return all if none specified.
115
116 Parameters:
117 key - string specifying the configuration item name
118
119 Return value:
120 String value if key is specified
121 Dictionary containing all options if key not specified
122 '''
123 if(key):
124 return self._conf[key]
125 else:
126 return self._conf
127
128 def set(self, key, value):
129 '''
130 Allow modification of a configuration to a new value
131
132 Parameters:
133 key - configuration item name
134 value - the value to set the configuration item to
135 '''
136 self._conf[key]=value
137
138 def write(self):
139 ''' Write configurations to file '''
140 with open(CONF_FILE, "w") as f:
141 for k in self._conf:
142 print(k+"="+self._conf[k], file=f)
143
144 #If invoked directly, prints a list of available configurations
145 if(__name__=="__main__"):
146 c=Conf(CONF_FILE)
147 print(c.get())