From 2ccf3c6370e13bec92586935d44a08e99298ac8f Mon Sep 17 00:00:00 2001 From: Eratosthenes Date: Fri, 26 Sep 2008 18:21:38 -0500 Subject: [PATCH] support HUD_config in default location, in cwd, or passed by caller --- pyfpdb/Configuration.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pyfpdb/Configuration.py b/pyfpdb/Configuration.py index 55b8ed2b..84439da8 100644 --- a/pyfpdb/Configuration.py +++ b/pyfpdb/Configuration.py @@ -23,6 +23,8 @@ Handles HUD configuration files. ######################################################################## # Standard Library modules +import os +import sys import shutil import xml.dom.minidom from xml.dom.minidom import Node @@ -166,7 +168,35 @@ class Popup: return temp + "\n" class Config: - def __init__(self, file = 'HUD_config.xml'): + def __init__(self, file = None): + +# "file" is a path to an xml file with the fpdb/HUD configuration +# we check the existence of "file" and try to recover if it doesn't exist + + if not file == None: # configuration file path has been passed + if not os.path.exists(file): + print "Configuration file %s not found. Using defaults." % (file) + sys.stderr.write("Configuration file %s not found. Using defaults." % (file)) + file = None + +# if "file" is invalid or None, we look for a HUD_config in the cwd + if file == None: # configuration file path not passed or invalid + if os.path.exists('HUD_config.xml'): # there is a HUD_config in the cwd + file = 'HUD_config.xml' # so we use it + else: # no HUD_config in the cwd, look where it should be in the first place +# find the path to the default HUD_config for the current os + if os.name == 'posix': + config_path = os.path.join(os.path.expanduser("~"), '.fpdb', 'HUD_config.xml') + elif os.name == 'nt': + config_path = os.path.join(os.environ["APPDATA"], 'fpdb', 'HUD_config.xml') + else: config_path = False + + if config_path and os.path.exists(config_path): + file = config_path + else: + print "No HUD_config_xml found. Exiting" + sys.stderr.write("No HUD_config_xml found. Exiting") + sys.exit() doc = xml.dom.minidom.parse(file)