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