comparison src/parpg/settings.py @ 11:4706e0194af3

Various improvements to the build process including support for self-contained builds. * Note that despite all of these changes PARPG still does not run because asset paths are not standardized, * Modified the SCons script so that by default running `scons` with no arguments creates a self-contained "build" under a build subdirectory to make in-source testing easier. To install PARPG, use `scons install` instead. * Got rid of the binary launcher and replaced it with a shell script for unix and a batch script for Windows (batch script is untested). The binary turned out to be too much trouble to maintain. * Modified the parpg.settings module and parpg.main entry script so that PARPG searches through several default search paths for configuration file(s). PARPG thus no longer crashes if it can't find a configuration file in any particular search path, but will crash it if can't find any configuration files. * Paths supplied to parpg.main are now appended as search paths for the configuration file(s). * Changed the default configuration file name to "parpg.cfg" to simplify searches. * Created the site_scons directory tree where SCons extensions and tools should be placed. * Created a new SCons builder, CopyRecurse, which can copy only certain files and folders from a directory tree using filters (files and folders that start with a leading dot "." e.g. ".svn" are ignored by default). * Added the CPython SCons tool (stands for Compile-Python - I didn't name it!), which provides the InstallPython builder for pre-compiling python sources before they are installed. However, it is currently broken and only installs the python sources.
author M. George Hansen <technopolitica@gmail.com>
date Tue, 31 May 2011 02:46:20 -0700
parents 1fd2201f5c36
children aa3d837024a3
comparison
equal deleted inserted replaced
10:5deaf494934a 11:4706e0194af3
127 127
128 class Settings(object): 128 class Settings(object):
129 """ An object that represents a settings file, its sectons, 129 """ An object that represents a settings file, its sectons,
130 and the options defined within those sections. 130 and the options defined within those sections.
131 """ 131 """
132 def __init__(self, settings_path='', system_path='', user_path='', suffix='.cfg'): 132 def __init__(self, settings_path='', system_path='', user_path='',
133 filename='parpg.cfg'):
133 """ initializes a new settings object. If no paths are given, they are 134 """ initializes a new settings object. If no paths are given, they are
134 guessed based on whatever platform the script was run on. 135 guessed based on whatever platform the script was run on.
135 136
136 Examples: 137 Examples:
137 paths = ['/etc/parpg', '/home/user_name/.config/parpg'] 138 paths = ['/etc/parpg', '/home/user_name/.config/parpg']
155 @type user_path: string (must be a valid path) 156 @type user_path: string (must be a valid path)
156 157
157 @param suffix: Suffix of the settings file that will be generated. 158 @param suffix: Suffix of the settings file that will be generated.
158 @type suffix: string 159 @type suffix: string
159 """ 160 """
160 if not suffix.startswith('.'): 161 self.filename = filename
161 suffix = '.' + suffix
162
163 self.suffix = suffix
164 self.settings_file = '' 162 self.settings_file = ''
165 163
166
167 self.paths = {} 164 self.paths = {}
168 if not system_path and not user_path and not settings_path: 165 if not system_path and not user_path and not settings_path:
169 # use platform-specific values as paths 166 # use platform-specific values as paths
170 (self.paths['system'], self.paths['user'], 167 (self.paths['system'], self.paths['user'],
171 self.paths['settings']) = self.platform_paths() 168 self.paths['settings']) = self.platform_paths()
222 @param filenames: name of files to be parsed. 219 @param filenames: name of files to be parsed.
223 @type path: string or list 220 @type path: string or list
224 """ 221 """
225 222
226 if filenames is None: 223 if filenames is None:
227 filenames = [os.path.join(self.paths['settings'], 224 filenames = [os.path.join(self.paths['settings'], self.filename),
228 'system{0}'.format(self.suffix)), 225 os.path.join(self.paths['user'], self.filename)]
229 os.path.join(self.paths['user'],
230 'user{0}'.format(self.suffix))]
231 elif hasattr(filenames, 'split'): 226 elif hasattr(filenames, 'split'):
232 filenames = [filenames] 227 filenames = [filenames]
233 228
234 for filename in filenames: 229 for filename in filenames:
235 section = None 230 section = None
236 231 if os.path.exists(filename):
237 try: 232 try:
238 self.settings_file = open(filename, 'r').readlines() 233 self.settings_file = open(filename, 'r').readlines()
239 except IOError as (errno, strerror): 234 except IOError as (errno, strerror):
240 if errno == 2: 235 if errno == 2:
241 if os.path.basename(filename).startswith('system'): 236 if os.path.basename(filename).startswith('system'):
242 print ('{0} could not be found. Please supply a ' 237 print ('{0} could not be found. Please supply a '
243 'different path or generate a system settings ' 238 'different path or generate a system settings '
244 'file with:\n' 239 'file with:\n'
245 'python2 -m parpg.settings').format(filename) 240 'python2 -m parpg.settings').format(filename)
241 sys.exit(1)
242 else:
243 print 'Error No. {0}: {1} {2}'.format(errno, filename, strerror)
246 sys.exit(1) 244 sys.exit(1)
247 else: 245
248 print 'Error No. {0}: {1} {2}'.format(errno, filename, strerror) 246 for line in self.settings_file:
249 sys.exit(1) 247 if line.startswith('#') or line.strip() == '':
250 248 continue
251 for line in self.settings_file: 249 elif line.startswith('[') and line.endswith(']\n'):
252 if line.startswith('#') or line.strip() == '': 250 getattr(self, line[1:-2])
253 continue 251 section = line[1:-2]
254 elif line.startswith('[') and line.endswith(']\n'): 252 else:
255 getattr(self, line[1:-2]) 253 option, value = [item.strip()
256 section = line[1:-2] 254 for item in line.split('=', 1)]
257 else: 255 setattr(getattr(self, section), option, value)
258 option, value = [item.strip()
259 for item in line.split('=', 1)]
260 setattr(getattr(self, section), option, value)
261 256
262 def write(self, filename=None): 257 def write(self, filename=None):
263 """ Writes a settings file based on the settings object's 258 """ Writes a settings file based on the settings object's
264 sections and options 259 sections and options
265 260