#! /usr/bin/python3
-'''
-DDStorm
--------
-DDStorm is a Python application for
-brainstorming medical differential diagnosis.
-
-Homepage: http://code.agnibho.com/ddstorm
+''' This module handles the different configuration options of DDStorm. '''
+'''
Copyright (c) 2015 Agnibho Mondal
All rights reserved
along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
'''
-import os, logging
-from const import * # Import constants from const.py
-logging.basicConfig(filename=LOG_FILE) # Set up logging
+import os
+import logging
+
+from const import *
+
+logging.basicConfig(filename=LOG_FILE)
class Conf:
'''
- * This class handles the configuration of DDStorm.
+ * This class manages DDStorm configuration options.
* If the configuration file is found it reads the file
and initiates the configurations.
* If configuration couldn't be read it initiates some
function calls.
'''
- conf={} #Initiates configuration list
+ _conf={}
def __init__(self, filename=CONF_FILE):
'''
- The constructor accepts a configuration filename
- If none is provided it defaults to CONF_FILE from const
+ The constructor accepts a configuration filename.
+ If none is provided it defaults to CONF_FILE from const.
+ Calls default() and read()
'''
self.filename=filename
self.default()
self.read()
def default(self):
- ''' Sets the default values '''
- self.conf["library_path"]="./library/"
- self.conf["custom_path"]="./custom/"
- self.conf["index_path"]="./index/"
- self.conf["alias_path"]="./alias/"
- self.conf["module_path"]="./modules/"
- self.conf["splash_screen"]="yes"
- self.conf["clean_log"]="no"
- self.conf["status_message"]="on"
+ ''' Set the default values '''
+ self._conf["library_path"]="./library/"
+ self._conf["custom_path"]="./custom/"
+ self._conf["index_path"]="./index/"
+ self._conf["alias_path"]="./alias/"
+ self._conf["module_path"]="./modules/"
+ self._conf["splash_screen"]="yes"
+ self._conf["clean_log"]="no"
+ self._conf["status_message"]="on"
def read(self):
- ''' Reads the configuration file and collects the values '''
- if(os.path.isfile(self.filename)): # If file is actually present
+ ''' Read the configuration file and collect the values '''
+ # If file is actually present
+ if(os.path.isfile(self.filename)):
try:
- with open(self.filename) as f: # Open file
+ with open(self.filename) as f:
for line in f:
- line="".join(line.split()) # Removes any stray whitespaces
- if(line.startswith("#")): # Ignores comments
+ # Removes any stray whitespaces
+ line="".join(line.split())
+ # Ignores comments starting with #
+ if(line.startswith("#")):
pass
- elif(line.startswith("library_path=")): # Library files path
- self.conf["library_path"]=line[13:]
- if(os.path.isdir(self.conf["library_path"])):
- if(not self.conf["library_path"].endswith("/")):
- self.conf["library_path"]+="/"
- elif(line.startswith("custom_path=")): # Custom files path
- self.conf["custom_path"]=line[12:]
- if(os.path.isdir(self.conf["custom_path"])):
- if(not self.conf["custom_path"].endswith("/")):
- self.conf["custom_path"]+="/"
- elif(line.startswith("index_path=")): # Index files path
- self.conf["index_path"]=line[11:]
- if(os.path.isdir(self.conf["index_path"])):
- if(not self.conf["index_path"].endswith("/")):
- self.conf["index_path"]+="/"
- elif(line.startswith("alias_path=")): # Alias files path
- self.conf["alias_path"]=line[11:]
- if(os.path.isdir(self.conf["alias_path"])):
- if(not self.conf["alias_path"].endswith("/")):
- self.conf["alias_path"]+="/"
- elif(line.startswith("module_path=")): # Path to save compiled modules
- self.conf["module_path"]=line[12:]
- if(os.path.isdir(self.conf["module_path"])):
- if(not self.conf["module_path"].endswith("/")):
- self.conf["module_path"]+="/"
- elif(line.startswith("splash_screen=")): # Whether to show a splash screen
- self.conf["splash_screen"]=line[14:]
- elif(line.startswith("clean_log=")): # Whether to clean logs before exit
- self.conf["clean_log"]=line[10:]
- elif(line.startswith("status_message=")): # Whether to show status messages
- self.conf["status_message"]=line[15:]
+ # Library path
+ elif(line.startswith("library_path=")):
+ self._conf["library_path"]=line[13:]
+ if(os.path.isdir(self._conf["library_path"])):
+ if(not self._conf["library_path"].endswith("/")):
+ self._conf["library_path"]+="/"
+ # Custom path
+ elif(line.startswith("custom_path=")):
+ self._conf["custom_path"]=line[12:]
+ if(os.path.isdir(self._conf["custom_path"])):
+ if(not self._conf["custom_path"].endswith("/")):
+ self._conf["custom_path"]+="/"
+ # Index path
+ elif(line.startswith("index_path=")):
+ self._conf["index_path"]=line[11:]
+ if(os.path.isdir(self._conf["index_path"])):
+ if(not self._conf["index_path"].endswith("/")):
+ self._conf["index_path"]+="/"
+ # Alias path
+ elif(line.startswith("alias_path=")):
+ self._conf["alias_path"]=line[11:]
+ if(os.path.isdir(self._conf["alias_path"])):
+ if(not self._conf["alias_path"].endswith("/")):
+ self._conf["alias_path"]+="/"
+ # Module path
+ elif(line.startswith("module_path=")):
+ self._conf["module_path"]=line[12:]
+ if(os.path.isdir(self._conf["module_path"])):
+ if(not self._conf["module_path"].endswith("/")):
+ self._conf["module_path"]+="/"
+ # Splash screen
+ elif(line.startswith("splash_screen=")):
+ self._conf["splash_screen"]=line[14:]
+ # Clean log
+ elif(line.startswith("clean_log=")):
+ self._conf["clean_log"]=line[10:]
+ # Status message
+ elif(line.startswith("status_message=")):
+ self._conf["status_message"]=line[15:]
+ # Unknown option
else:
- logging.warning("Unrecognized configuration: "+line) # Log a warning if unrecognized option found
- except: # Go with default if file could not be read and log an error
+ logging.warning("Unrecognized configuration: "+line)
+ except:
logging.error("Configuration file "+self.filename+" could not be read. Using default configurations.")
def get(self, key=False):
Parameters:
key - string specifying the configuration item name
+
+ Return value:
+ String value if key is specified
+ Dictionary containing all options if key not specified
'''
if(key):
- return self.conf[key]
+ return self._conf[key]
else:
- return self.conf
+ return self._conf
def set(self, key, value):
'''
key - configuration item name
value - the value to set the configuration item to
'''
- self.conf[key]=value
+ self._conf[key]=value
def write(self):
- ''' Writes configurations to file '''
+ ''' Write configurations to file '''
with open(CONF_FILE, "w") as f:
- for k in self.conf:
- print(k+"="+self.conf[k], file=f)
+ for k in self._conf:
+ print(k+"="+self._conf[k], file=f)
#If invoked directly, prints a list of available configurations
if(__name__=="__main__"):