]>
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/>.
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
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 file is actually present
69 if(os
.path
.isfile(self
.filename
)):
71 with
open(self
.filename
) as f
:
73 # Removes any stray whitespaces
74 line
="".join(line
.split())
75 # Ignores comments starting with #
76 if(line
.startswith("#")):
79 elif(line
.startswith("library_path=")):
80 self
._conf
["library_path"]=line
[13:]
81 if(os
.path
.isdir(self
._conf
["library_path"])):
82 if(not self
._conf
["library_path"].endswith("/")):
83 self
._conf
["library_path"]+="/"
85 elif(line
.startswith("custom_path=")):
86 self
._conf
["custom_path"]=line
[12:]
87 if(os
.path
.isdir(self
._conf
["custom_path"])):
88 if(not self
._conf
["custom_path"].endswith("/")):
89 self
._conf
["custom_path"]+="/"
91 elif(line
.startswith("index_path=")):
92 self
._conf
["index_path"]=line
[11:]
93 if(os
.path
.isdir(self
._conf
["index_path"])):
94 if(not self
._conf
["index_path"].endswith("/")):
95 self
._conf
["index_path"]+="/"
97 elif(line
.startswith("alias_path=")):
98 self
._conf
["alias_path"]=line
[11:]
99 if(os
.path
.isdir(self
._conf
["alias_path"])):
100 if(not self
._conf
["alias_path"].endswith("/")):
101 self
._conf
["alias_path"]+="/"
103 elif(line
.startswith("module_path=")):
104 self
._conf
["module_path"]=line
[12:]
105 if(os
.path
.isdir(self
._conf
["module_path"])):
106 if(not self
._conf
["module_path"].endswith("/")):
107 self
._conf
["module_path"]+="/"
109 elif(line
.startswith("splash_screen=")):
110 self
._conf
["splash_screen"]=line
[14:]
112 elif(line
.startswith("clean_log=")):
113 self
._conf
["clean_log"]=line
[10:]
115 elif(line
.startswith("status_message=")):
116 self
._conf
["status_message"]=line
[15:]
119 logging
.warning("Unrecognized configuration: "+line
)
121 logging
.error("Configuration file "+self
.filename
+" could not be read. Using default configurations.")
123 def get(self
, key
=False):
125 Return the requested configuration item.
126 Return all if none specified.
129 key - string specifying the configuration item name
132 String value if key is specified
133 Dictionary containing all options if key not specified
136 return self
._conf
[key
]
140 def set(self
, key
, value
):
142 Allow modification of a configuration to a new value
145 key - configuration item name
146 value - the value to set the configuration item to
148 self
._conf
[key
]=value
151 ''' Write configurations to file '''
152 with
open(CONF_FILE
, "w") as f
:
154 print(k
+"="+self
._conf
[k
], file=f
)
156 #If invoked directly, prints a list of available configurations
157 if(__name__
=="__main__"):