-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
175 lines (163 loc) · 4.75 KB
/
config.py
File metadata and controls
175 lines (163 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#
#Copyright (C) 2009 asylumfunk
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You may obtain a copy of this license at:
# http://www.gnu.org/licenses/gpl-3.0.html
"""Project configuration"""
#Standard modules
import os
import xml.dom.minidom
#Project modules
import crypt
class Config:
"""Handles project configuration settings"""
_DEFAULT_VALUE = ""
_FILE_DEFAULT = "settings.default.xml"
_FILE_LEGACY_AUTHENTICATION = "settings.ini"
_FILE_USER = "settings.user.xml"
_KEY_ATTRIBUTE_NAME = "key"
_KVP_TAG_NAME = "setting"
_ROOT_TAG_NAME = "settings"
def __init__( self ):
fileDefault = os.path.join( os.getcwd(), self._FILE_DEFAULT )
fileUser = os.path.join( os.getcwd(), self._FILE_USER )
self._settingsDefault = self._parse( fileDefault )
self._settingsUser = self._parse( fileUser )
self._migrateLegacyAuthentication()
"""
Description:
Reads the user's credentials from the configuration file
Returns:
Success: { USERNAME, PASSWORD }
Failure: { None, None }
"""
def _loadCredentials( self, file ):
try:
file = open ( file, "r" )
username = file.readline().strip()
password = file.readline().strip()
except:
username = None
password = None
return username, password
"""
Description:
Migrates authentication from the legacy system to the current system
Removes the legacy file if it exists
"""
def _migrateLegacyAuthentication( self ):
legacyFile = os.path.join( os.getcwd(), self._FILE_LEGACY_AUTHENTICATION )
if os.path.isfile( legacyFile ):
if not self.get( "auth.username" ):
username, password = self._loadCredentials( legacyFile )
username = username or ""
password = crypt.en( password )
self.set({
"auth.username" : username,
"auth.password" : password,
"auth.method" : ""
})
self.save()
os.remove( legacyFile )
"""
Description:
Parses key-value pairs from a configuration file
Args:
file::string - absolute path of the input file
Returns:
dictionary - collection of key-value pairs from the input file
"""
def _parse( self, file ):
data = {}
if os.path.isfile( file ):
try:
doc = xml.dom.minidom.parse( file )
root = doc.documentElement
if ( not root or root.tagName != self._ROOT_TAG_NAME ):
print "XML root not found: " + file
kvps = root.getElementsByTagName( self._KVP_TAG_NAME )
for kvp in kvps:
key, value = self._parseKvp( kvp )
if key:
data[ key ] = value
try:
doc.unlink()
except:
print "Unable to unlink file: " + file
except:
print "Unable to parse file: " + file
else:
print "File does not exist: " + file
return data
"""
Description:
Parses a key-value pair from an XML element node
Args:
kvp::XMLElementNode - a non-null element node to be parsed
Returns:
( key::string, value::string ) - the node's key-value data
"""
def _parseKvp( self, kvp ):
value = self._DEFAULT_VALUE
key = kvp.getAttribute( self._KEY_ATTRIBUTE_NAME )
if key and kvp.hasChildNodes():
value = kvp.firstChild.nodeValue
return key, value
"""
Description:
Retrieves the specified configuration setting
Args:
key::string - the key of the item to be retrieved
Returns:
string - value from user-specific settings OR default value OR None
"""
def get( self, key ):
if key in self._settingsUser:
return self._settingsUser[ key ]
elif key in self._settingsDefault:
return self._settingsDefault[ key ]
else:
return None
"""
Description:
Writes the user-specific settings to its XML file
"""
def save( self ):
implementation = xml.dom.minidom.getDOMImplementation()
doc = implementation.createDocument( None, self._ROOT_TAG_NAME, None )
for key, value in self._settingsUser.iteritems():
ele = doc.createElement( self._KVP_TAG_NAME )
text = doc.createTextNode( value )
ele.setAttribute( self._KEY_ATTRIBUTE_NAME, key )
ele.appendChild( text )
doc.documentElement.appendChild( ele )
try:
file = open( os.path.join( os.getcwd(), self._FILE_USER ), "w" )
doc.writexml( file, encoding = "utf-8" )
finally:
file.close()
"""
Description:
Sets the user-specific configuration settings
Writes the settings to file
Args:
pairs::Dictionary - key-value pairs of user settings
Returns:
self
"""
def set( self, pairs ):
if pairs:
for key in pairs:
self._settingsUser[ key ] = pairs[ key ]
self.save()
return self