]>
Softwares of Agnibho - ddstorm.git/blob - conf.py
6 DDStorm is a Python application for
7 brainstorming medical differential diagnosis.
9 Homepage: http://code.agnibho.com/ddstorm
11 Copyright (c) 2015 Agnibho Mondal
14 This file is part of DDStorm.
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.
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.
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/>.
31 from const
import * # Import constants from const.py
32 logging
.basicConfig(filename
=LOG_FILE
) # Set up logging
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
45 conf
={} #Initiates configuration list
47 def __init__(self
, filename
=CONF_FILE
):
49 The constructor accepts a configuration filename
50 If none is provided it defaults to CONF_FILE from const
52 self
.filename
=filename
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"
68 ''' Reads the configuration file and collects the values '''
69 if(os
.path
.isfile(self
.filename
)): # If file is actually present
71 with
open(self
.filename
) as f
: # Open file
73 line
="".join(line
.split()) # Removes any stray whitespaces
74 if(line
.startswith("#")): # Ignores comments
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:]
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.")
112 def get(self
, key
=False):
114 Return the requested configuration item.
115 Return all if none specified.
118 key - string specifying the configuration item name
121 return self
.conf
[key
]
125 def set(self
, key
, value
):
127 Allow modification of a configuration to a new value
130 key - configuration item name
131 value - the value to set the configuration item to
136 ''' Writes configurations to file '''
137 with
open(CONF_FILE
, "w") as f
:
139 print(k
+"="+self
.conf
[k
], file=f
)
141 #If invoked directly, prints a list of available configurations
142 if(__name__
=="__main__"):