- added a django app into the project
- little tests trying to calculate how SC calcs survivability (not resolved yet) - dejaqt born as idea for standalone library.
This commit is contained in:
5
config/common.py
Normal file
5
config/common.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
|
||||
|
||||
|
||||
"""
|
||||
@@ -2,6 +2,7 @@
|
||||
Simple brainstorm to display a config file.
|
||||
"""
|
||||
import os, logging
|
||||
from settings import settings
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
# import ET:
|
||||
try:
|
||||
@@ -24,13 +25,80 @@ finally:
|
||||
raise NotImplementedError, "XML Parser not found in your Python."
|
||||
##################################################################################################
|
||||
|
||||
|
||||
CONFIG_FILE = os.path.join(os.path.expanduser('~'),
|
||||
'Documents',
|
||||
'My Games',
|
||||
'StarConflict',
|
||||
'user_config.xml')
|
||||
|
||||
class ConfigFile(object):
|
||||
def __init__(self, config_file=None):
|
||||
self.cvars = []
|
||||
if config_file:
|
||||
self.config_file = config_file
|
||||
elif settings:
|
||||
# settings based loading.
|
||||
self.config_file = os.path.join(settings.get_path(), 'user_config.xml')
|
||||
|
||||
def open(self, filename = None):
|
||||
# reads a config file.
|
||||
filename = filename or self.config_file
|
||||
self.tree = ET.parse(filename)
|
||||
doc = self.tree.getroot()
|
||||
if doc.tag == 'UserConfig' \
|
||||
and len(doc) == 1\
|
||||
and doc[0].tag == 'CVars'\
|
||||
and doc[0].attrib['version'] == '4':
|
||||
logging.info( "Found valid config file." )
|
||||
# save my cvars
|
||||
self.cvars = doc[0]
|
||||
else:
|
||||
logging.info( "Config File not supported." )
|
||||
return self
|
||||
|
||||
def pprint(self):
|
||||
# print out my cvars
|
||||
for child in self.cvars:
|
||||
print '%s = %s' % (child.tag, child.attrib['val'])
|
||||
|
||||
def write(self, filename):
|
||||
output = '<?xml version="1.0"?>\n'
|
||||
doc = self.tree.getroot()
|
||||
# we manually serialize it to keep it exactly the same
|
||||
# like original SC to avoid problems with their software.
|
||||
def append_node(node, depth=0):
|
||||
# xml serializing helper function...
|
||||
s = ['%s<%s' % (' '*depth*2, node.tag),]
|
||||
for key, val in node.attrib.items():
|
||||
s.append(' %s="%s"' % (key, val))
|
||||
if len(node):
|
||||
s.append('>\n')
|
||||
# append children
|
||||
for child in node:
|
||||
s.extend(append_node(child, depth+1))
|
||||
s.append('%s</%s>\n' % (' '*depth*2, node.tag))
|
||||
else:
|
||||
s.append(' />\n')
|
||||
return s
|
||||
l = append_node(doc)
|
||||
output = output + ''.join( l )
|
||||
if filename is None:
|
||||
# dev.
|
||||
assert output[-1], '\n'
|
||||
else:
|
||||
try:
|
||||
f = open(filename, 'w')
|
||||
f.write(output)
|
||||
finally:
|
||||
f.close()
|
||||
return output
|
||||
|
||||
def debug_serializing(self):
|
||||
# detects if output would result in the same data as input
|
||||
input, output = None, None
|
||||
try:
|
||||
f = open(self.config_file, 'r')
|
||||
input = f.read()
|
||||
finally:
|
||||
f.close()
|
||||
output = self.write(None)
|
||||
return output == input
|
||||
|
||||
|
||||
def read_config(config_file):
|
||||
tree = ET.parse(config_file)
|
||||
# doc = tree.getroot()
|
||||
@@ -38,15 +106,14 @@ def read_config(config_file):
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Read the config
|
||||
tree = read_config(CONFIG_FILE)
|
||||
doc = tree.getroot()
|
||||
if doc.tag == 'UserConfig' \
|
||||
and len(doc) == 1\
|
||||
and doc[0].tag == 'CVars'\
|
||||
and doc[0].attrib['version'] == '4':
|
||||
print "Found valid config file."
|
||||
cvars = doc[0]
|
||||
for child in cvars:
|
||||
print '%s = %s' % (child.tag, child.attrib['val'])
|
||||
else:
|
||||
print "Not found valid config file."
|
||||
settings.autodetect()
|
||||
c = ConfigFile().open()
|
||||
print '#' * 80
|
||||
print "Output File would be:"
|
||||
print c.write(None)
|
||||
print '#' * 80
|
||||
print "Detected Settings:"
|
||||
c.pprint()
|
||||
print '#' * 80
|
||||
print 'Serializing Test successful: %s' % c.debug_serializing()
|
||||
|
||||
11
config/readme.txt
Normal file
11
config/readme.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
This Package deals with config files, but also deals with configuration itself.
|
||||
|
||||
This includes:
|
||||
- The config files for SCON itself (config_user.xml)
|
||||
- The config files for the applications in the scon package itself
|
||||
- basic config throughout the apps in this app, including...
|
||||
|
||||
* loading/importing XML Libraries
|
||||
* OS detection
|
||||
* logging setup
|
||||
etc.
|
||||
31
config/settings.py
Normal file
31
config/settings.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
import platform
|
||||
|
||||
class Settings(dict):
|
||||
def autodetect(self, path=None):
|
||||
# autodetect settings.
|
||||
d = path
|
||||
system = platform.system()
|
||||
if system == 'Windows' or system.startswith('CYGWIN_NT'):
|
||||
# try to find user folder:
|
||||
d = d or os.path.join(os.path.expanduser('~'),
|
||||
'Documents',
|
||||
'My Games',
|
||||
'StarConflict',)
|
||||
elif system == 'Linux':
|
||||
raise NotImplementedError, "Implement Linux!"
|
||||
elif system == 'Darwin':
|
||||
raise NotImplementedError, "Implement Mac!"
|
||||
else:
|
||||
raise NotImplementedError, "Unknown System! %s" % platform.system()
|
||||
if not os.path.exists(d) or not os.path.isdir(d):
|
||||
raise Exception, "Configuration Autodetection failed. "
|
||||
self['root_path'] = d
|
||||
|
||||
def get_path(self):
|
||||
return self.get('root_path', None)
|
||||
|
||||
def get_logs_path(self):
|
||||
return os.path.join(self.get_path, 'logs')
|
||||
|
||||
settings = Settings()
|
||||
Reference in New Issue
Block a user