First go at rationalizing config paths and logging defaults.
This commit is contained in:
parent
b33fd03754
commit
0bbbc7222b
|
@ -37,13 +37,79 @@ from xml.dom.minidom import Node
|
||||||
import logging, logging.config
|
import logging, logging.config
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
|
|
||||||
try: # local path
|
##############################################################################
|
||||||
logging.config.fileConfig(os.path.join(sys.path[0],"logging.conf"))
|
# Functions for finding config files and setting up logging
|
||||||
except ConfigParser.NoSectionError: # debian package path
|
# Also used in other modules that use logging.
|
||||||
logging.config.fileConfig('/usr/share/python-fpdb/logging.conf')
|
|
||||||
|
def get_default_config_path():
|
||||||
|
"""Returns the path where the fpdb config file _should_ be stored."""
|
||||||
|
if os.name == 'posix':
|
||||||
|
config_path = os.path.join(os.path.expanduser("~"), '.fpdb')
|
||||||
|
elif os.name == 'nt':
|
||||||
|
config_path = os.path.join(os.environ["APPDATA"], 'fpdb')
|
||||||
|
else: config_path = False
|
||||||
|
return config_path
|
||||||
|
|
||||||
|
def get_exec_path():
|
||||||
|
"""Returns the path to the fpdb.(py|exe) file we are executing"""
|
||||||
|
if hasattr(sys, "frozen"): # compiled by py2exe
|
||||||
|
print "executable path is", os.path.dirname(sys.executable)
|
||||||
|
print "exec path is", os.path.dirname(sys.executable)
|
||||||
|
print "0 path is", os.path.dirname(sys.path[0])
|
||||||
|
return os.path.dirname(sys.executable)
|
||||||
|
else:
|
||||||
|
print "executable path is", os.path.dirname(sys.executable)
|
||||||
|
print "exec path is", os.path.dirname(sys.argv[0])
|
||||||
|
print "0 path is", os.path.dirname(sys.path[0])
|
||||||
|
return os.path.dirname(sys.path[0])
|
||||||
|
|
||||||
|
def get_config(file_name, fallback = True):
|
||||||
|
"""Looks in cwd and in self.default_config_path for a config file."""
|
||||||
|
config_path = os.path.join(get_exec_path(), file_name)
|
||||||
|
if os.path.exists(config_path): # there is a file in the cwd
|
||||||
|
return config_path # so we use it
|
||||||
|
else: # no file in the cwd, look where it should be in the first place
|
||||||
|
config_path = os.path.join(get_default_config_path(), file_name)
|
||||||
|
if os.path.exists(config_path):
|
||||||
|
return config_path
|
||||||
|
|
||||||
|
# No file found
|
||||||
|
if not fallback:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# OK, fall back to the .example file, should be in the start dir
|
||||||
|
if os.path.exists(file_name + ".example"):
|
||||||
|
try:
|
||||||
|
shutil.copyfile(file_name + ".example", file_name)
|
||||||
|
print "No %s found, using %s.example.\n" % (file_name, file_name)
|
||||||
|
print "A %s file has been created. You will probably have to edit it." % file_name
|
||||||
|
sys.stderr.write("No %s found, using %s.example.\n" % (file_name, file_name) )
|
||||||
|
except:
|
||||||
|
print "No %s found, cannot fall back. Exiting.\n" % file_name
|
||||||
|
sys.stderr.write("No %s found, cannot fall back. Exiting.\n" % file_name)
|
||||||
|
sys.exit()
|
||||||
|
return file_name
|
||||||
|
|
||||||
|
def get_logger(file_name, fallback = False):
|
||||||
|
conf = get_config(file_name, fallback = fallback)
|
||||||
|
if conf:
|
||||||
|
try:
|
||||||
|
logging.config.fileConfig(conf)
|
||||||
|
log = logging.getLogger("config")
|
||||||
|
log.debug("config logger initialised")
|
||||||
|
return log
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
log = logging.basicConfig()
|
||||||
|
log = logging.getLogger()
|
||||||
|
log.debug("config logger initialised")
|
||||||
|
return log
|
||||||
|
|
||||||
|
# find a logging.conf file and set up logging
|
||||||
|
log = get_logger("logging.conf")
|
||||||
|
log.error("FARTS")
|
||||||
|
|
||||||
log = logging.getLogger("config")
|
|
||||||
log.debug("config logger initialised")
|
|
||||||
########################################################################
|
########################################################################
|
||||||
# application wide consts
|
# application wide consts
|
||||||
|
|
||||||
|
@ -335,7 +401,7 @@ class Config:
|
||||||
# "file" is a path to an xml file with the fpdb/HUD configuration
|
# "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
|
# we check the existence of "file" and try to recover if it doesn't exist
|
||||||
|
|
||||||
self.default_config_path = self.get_default_config_path()
|
# self.default_config_path = self.get_default_config_path()
|
||||||
if file is not None: # config file path passed in
|
if file is not None: # config file path passed in
|
||||||
file = os.path.expanduser(file)
|
file = os.path.expanduser(file)
|
||||||
if not os.path.exists(file):
|
if not os.path.exists(file):
|
||||||
|
@ -343,28 +409,12 @@ class Config:
|
||||||
sys.stderr.write("Configuration file %s not found. Using defaults." % (file))
|
sys.stderr.write("Configuration file %s not found. Using defaults." % (file))
|
||||||
file = None
|
file = None
|
||||||
|
|
||||||
if file is None: # configuration file path not passed or invalid
|
file = get_config("HUD_config.xml")
|
||||||
file = self.find_config() #Look for a config file in the normal places
|
|
||||||
|
|
||||||
if file is None: # no config file in the normal places
|
|
||||||
file = self.find_example_config() #Look for an example file to edit
|
|
||||||
|
|
||||||
if file is None: # that didn't work either, just die
|
|
||||||
print "No HUD_config_xml found after looking in current directory and "+self.default_config_path+"\nExiting"
|
|
||||||
sys.stderr.write("No HUD_config_xml found after looking in current directory and "+self.default_config_path+"\nExiting")
|
|
||||||
print "press enter to continue"
|
|
||||||
sys.stdin.readline()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
# Parse even if there was no real config file found and we are using the example
|
# Parse even if there was no real config file found and we are using the example
|
||||||
# If using the example, we'll edit it later
|
# If using the example, we'll edit it later
|
||||||
# sc 2009/10/04 Example already copied to main filename, is this ok?
|
|
||||||
log.info("Reading configuration file %s" % file)
|
log.info("Reading configuration file %s" % file)
|
||||||
if os.sep in file:
|
print "\nReading configuration file %s\n" % file
|
||||||
print "\nReading configuration file %s\n" % file
|
|
||||||
else:
|
|
||||||
print "\nReading configuration file %s" % file
|
|
||||||
print "in %s\n" % os.getcwd()
|
|
||||||
try:
|
try:
|
||||||
doc = xml.dom.minidom.parse(file)
|
doc = xml.dom.minidom.parse(file)
|
||||||
except:
|
except:
|
||||||
|
@ -460,28 +510,6 @@ class Config:
|
||||||
def set_hhArchiveBase(self, path):
|
def set_hhArchiveBase(self, path):
|
||||||
self.imp.node.setAttribute("hhArchiveBase", path)
|
self.imp.node.setAttribute("hhArchiveBase", path)
|
||||||
|
|
||||||
def find_config(self):
|
|
||||||
"""Looks in cwd and in self.default_config_path for a config file."""
|
|
||||||
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
|
|
||||||
config_path = os.path.join(self.default_config_path, 'HUD_config.xml')
|
|
||||||
if os.path.exists(config_path):
|
|
||||||
file = config_path
|
|
||||||
else:
|
|
||||||
file = None
|
|
||||||
return file
|
|
||||||
|
|
||||||
def get_default_config_path(self):
|
|
||||||
"""Returns the path where the fpdb config file _should_ be stored."""
|
|
||||||
if os.name == 'posix':
|
|
||||||
config_path = os.path.join(os.path.expanduser("~"), '.fpdb')
|
|
||||||
elif os.name == 'nt':
|
|
||||||
config_path = os.path.join(os.environ["APPDATA"], 'fpdb')
|
|
||||||
else: config_path = None
|
|
||||||
return config_path
|
|
||||||
|
|
||||||
|
|
||||||
def find_default_conf(self):
|
def find_default_conf(self):
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
config_path = os.path.join(os.path.expanduser("~"), '.fpdb', 'default.conf')
|
config_path = os.path.join(os.path.expanduser("~"), '.fpdb', 'default.conf')
|
||||||
|
@ -495,30 +523,6 @@ class Config:
|
||||||
file = None
|
file = None
|
||||||
return file
|
return file
|
||||||
|
|
||||||
def read_default_conf(self, file):
|
|
||||||
parms = {}
|
|
||||||
with open(file, "r") as fh:
|
|
||||||
for line in fh:
|
|
||||||
line = string.strip(line)
|
|
||||||
(key, value) = line.split('=')
|
|
||||||
parms[key] = value
|
|
||||||
return parms
|
|
||||||
|
|
||||||
def find_example_config(self):
|
|
||||||
if os.path.exists('HUD_config.xml.example'): # there is a HUD_config in the cwd
|
|
||||||
file = 'HUD_config.xml' # so we use it
|
|
||||||
try:
|
|
||||||
shutil.copyfile(file+'.example', file)
|
|
||||||
except:
|
|
||||||
file = ''
|
|
||||||
print "No HUD_config.xml found, using HUD_config.xml.example.\n", \
|
|
||||||
"A HUD_config.xml has been created. You will probably have to edit it."
|
|
||||||
sys.stderr.write("No HUD_config.xml found, using HUD_config.xml.example.\n" + \
|
|
||||||
"A HUD_config.xml has been created. You will probably have to edit it.")
|
|
||||||
else:
|
|
||||||
file = None
|
|
||||||
return file
|
|
||||||
|
|
||||||
def get_site_node(self, site):
|
def get_site_node(self, site):
|
||||||
for site_node in self.doc.getElementsByTagName("site"):
|
for site_node in self.doc.getElementsByTagName("site"):
|
||||||
if site_node.getAttribute("site_name") == site:
|
if site_node.getAttribute("site_name") == site:
|
||||||
|
|
|
@ -45,15 +45,8 @@ import Card
|
||||||
import Tourney
|
import Tourney
|
||||||
from Exceptions import *
|
from Exceptions import *
|
||||||
|
|
||||||
import logging, logging.config
|
log = Configuration.get_logger("logging.conf")
|
||||||
import ConfigParser
|
log.error("TURDS")
|
||||||
|
|
||||||
try: # local path
|
|
||||||
logging.config.fileConfig(os.path.join(sys.path[0],"logging.conf"))
|
|
||||||
except ConfigParser.NoSectionError: # debian package path
|
|
||||||
logging.config.fileConfig('/usr/share/python-fpdb/logging.conf')
|
|
||||||
|
|
||||||
log = logging.getLogger('db')
|
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
|
@ -2690,13 +2683,11 @@ class HandToWrite:
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
c = Configuration.Config()
|
c = Configuration.Config()
|
||||||
|
|
||||||
db_connection = Database(c, 'fpdb', 'holdem') # mysql fpdb holdem
|
db_connection = Database(c) # mysql fpdb holdem
|
||||||
# db_connection = Database(c, 'fpdb-p', 'test') # mysql fpdb holdem
|
# db_connection = Database(c, 'fpdb-p', 'test') # mysql fpdb holdem
|
||||||
# db_connection = Database(c, 'PTrackSv2', 'razz') # mysql razz
|
# db_connection = Database(c, 'PTrackSv2', 'razz') # mysql razz
|
||||||
# db_connection = Database(c, 'ptracks', 'razz') # postgres
|
# db_connection = Database(c, 'ptracks', 'razz') # postgres
|
||||||
print "database connection object = ", db_connection.connection
|
print "database connection object = ", db_connection.connection
|
||||||
print "database type = ", db_connection.type
|
|
||||||
|
|
||||||
db_connection.recreate_tables()
|
db_connection.recreate_tables()
|
||||||
|
|
||||||
h = db_connection.get_last_hand()
|
h = db_connection.get_last_hand()
|
||||||
|
@ -2710,18 +2701,12 @@ if __name__=="__main__":
|
||||||
for p in stat_dict.keys():
|
for p in stat_dict.keys():
|
||||||
print p, " ", stat_dict[p]
|
print p, " ", stat_dict[p]
|
||||||
|
|
||||||
#print "nutOmatics stats:"
|
|
||||||
#stat_dict = db_connection.get_stats_from_hand(h, "ring")
|
|
||||||
#for p in stat_dict.keys():
|
|
||||||
# print p, " ", stat_dict[p]
|
|
||||||
|
|
||||||
print "cards =", db_connection.get_cards(u'1')
|
print "cards =", db_connection.get_cards(u'1')
|
||||||
db_connection.close_connection
|
db_connection.close_connection
|
||||||
|
|
||||||
print "press enter to continue"
|
print "press enter to continue"
|
||||||
sys.stdin.readline()
|
sys.stdin.readline()
|
||||||
|
|
||||||
|
|
||||||
#Code borrowed from http://push.cx/2008/caching-dictionaries-in-python-vs-ruby
|
#Code borrowed from http://push.cx/2008/caching-dictionaries-in-python-vs-ruby
|
||||||
class LambdaDict(dict):
|
class LambdaDict(dict):
|
||||||
def __init__(self, l):
|
def __init__(self, l):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user