]>
Softwares of Agnibho - ddstorm.git/blob - conf.py
3 ''' This module handles the different configuration options of DDStorm. '''
6 Copyright (c) 2015 Agnibho Mondal
9 This file is part of DDStorm.
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.
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.
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/>.
28 from const
import * # Import constants
30 logging
.basicConfig(filename
=LOG_FILE
)
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
43 _conf
={} #Initiates configuration dictionary
45 def __init__(self
, filename
=CONF_FILE
):
47 The constructor accepts a configuration filename.
48 If none is provided it defaults to CONF_FILE from const.
49 Calls default() and read()
51 self
.filename
=filename
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"
67 ''' Read the configuration file and collect the values '''
68 if(os
.path
.isfile(self
.filename
)): # If file is actually present
70 with
open(self
.filename
) as f
: # Open file
72 line
="".join(line
.split()) # Removes any stray whitespaces
73 if(line
.startswith("#")): # Ignores comments
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:]
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.")
111 def get(self
, key
=False):
113 Return the requested configuration item.
114 Return all if none specified.
117 key - string specifying the configuration item name
120 String value if key is specified
121 Dictionary containing all options if key not specified
124 return self
._conf
[key
]
128 def set(self
, key
, value
):
130 Allow modification of a configuration to a new value
133 key - configuration item name
134 value - the value to set the configuration item to
136 self
._conf
[key
]=value
139 ''' Write configurations to file '''
140 with
open(CONF_FILE
, "w") as f
:
142 print(k
+"="+self
._conf
[k
], file=f
)
144 #If invoked directly, prints a list of available configurations
145 if(__name__
=="__main__"):