Hi All,
I have been playing with the pylons family of web products and love
them, but I am working on a basic commandline toolset and would like
some of the pylons magic without having a running service as the
toolset will not be allowed to run as a webservice.
So here is my problem, I have setup a couple of modules to handle
configuration and logging for example and want this to be available
across all other modules in the toolset.
here is the basic code.
##########
# utils.py
##########
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import os
import fnmatch
import sys
import subprocess
import stat
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern
in and below supplied root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
def executioner(self, execution_list):
self.execution_list = execution_list
execute = subprocess.Popen(self.execution_list, shell=False,
stdout=subprocess.PIPE)
while 1:
self.logLine = execute.stdout.readline()
self.exitcode = execute.poll()
if (not self.logLine) and (self.exitcode is not None):
break
self.log = self.logLine[:-1]
if not self.log == '':
print self.log
def singleton(cls):
__instances = {}
def getinstance():
if cls not in __instances:
__instances[cls] = cls()
return __instances[cls]
return getinstance
###############
#Configuration.py
###############
# vim: tabstop=4 shiftwidth=4 softtabstop=4
import ConfigParser
import os
from releasetools.classes.utils import singleton
@singleton
class Config(object):
def __init__(self, config_file=None):
self.config = self.read_config_file(config_file)
def save_default_config(self):
default_config = ConfigParser.RawConfigParser()
config_file = os.path.join(os.path.expanduser('~'),
'.releasetools', self.config_file)
default_config.add_section('Authentication')
default_config.set('Authentication', 'username',
'releasetools')
default_config.set('Authentication', 'password',
'releasetools')
default_config.add_section('Nexus')
default_config.set('Nexus', 'hostname', 'localhost')
default_config.set('Nexus', 'port', '8081')
default_config.add_section('Subversion')
default_config.set('Subversion', 'hostname', 'localhost')
default_config.set('Subversion', 'port', '80')
self.write_config(default_config)
def read_config_file(self, config_file):
config = ConfigParser.ConfigParser()
if config_file == None:
self.config_file = os.path.join(os.path.expanduser('~'),
'.releasetools', 'releasetools.cfg')
if os.path.exists(self.config_file):
try:
print
print "Configuration file found, loading
settings..."
config.readfp(open(self.config_file))
except IOError:
pass # TODO CHECK EXCEPTION AND LOG
else:
self.save_default_config()
config.readfp(open(self.config_file))
else:
try:
print "Configuration file found, loading settings..."
config.readfp(open(config_file))
except IOError:
pass # TODO CHECK EXCEPTION AND LOG
return config
def write_config(self, config):
config_file = os.path.join(os.path.expanduser('~'),
'.releasetools', 'releasetools.cfg')
print "Configuration file not found, writing out default
file."
if os.path.exists(os.path.dirname(config_file)):
with open(config_file, 'wb') as configfile:
config.write(configfile)
elif not os.path.exists(os.path.dirname(config_file)):
os.makedirs(os.path.dirname(config_file))
with open(config_file, 'wb') as configfile:
config.write(configfile)
else:
pass
def get_username(self):
return self.config.get('Authentication', 'username')
def set_username(self, username):
self.config.set('Authentication', 'username', username)
self.write_config(C.config)
def get_password(self):
return self.config.get('Authentication', 'password')
def set_password(self, password):
self.config.set('Authentication', 'password', password)
self.write_config(self.config)
def get_nexus_host(self):
return self.config.get('Nexus', 'hostname')
def set_nexus_host(self, hostname):
self.config.set('Nexus', 'hostname', hostname)
self.write_config(self.config)
def get_nexus_port(self):
return self.config.get('Nexus', 'port')
def set_nexus_port(self, port):
self.config.set('Nexus', 'port', port)
self.write_config(self.config)
def get_svn_host(self):
return self.config.get('Subversion', 'hostname')
def set_svn_host(self, hostname):
C.config.set('Subversion', 'hostname', hostname)
self.write_config(self.config)
def get_svn_port(self):
return self.config.get('Subversion', 'port')
def set_svn_port(self, port):
self.config.set('Subversion', 'port', port)
self.write_config(C.config)
def get_sections(self):
return self.config.sections()
def get_section_options(self, section):
return self.config.options(section)
def has_section(self, section):
return self.config.has_section(section)
def has_option(self, section, option):
return self.config.has_option(section, option)
def get_option(self, section, option):
if self.has_option(section, option):
return self.config.get(section, option)
def show_config(self):
sections = self.get_sections()
os.system('clear')
print "Configuration for Release Tools System is as follows: "
for section in sections:
print "Section: ", section
for option in self.get_section_options(section):
print " Option: {0} = {1}".format(option,
self.get_option(section, option))
def show_config():
D = Config()
sections = D.get_sections()
os.system('clear')
print "Configuration for Release Tools System is as follows: "
for section in sections:
print "Section: ", section
for option in D.get_section_options(section):
print " Option: {0} = {1}".format(option,
D.get_option(section, option))
if __name__ == '__main__':
show_config()
#############
# otherscript.py
#############
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#from releasetools.classes.configuration import show_config
from releasetools.classes.configuration import Config
C.show_config()
-----------------
I would like to be able to access the values in the configuration.py
module across all scripts as a global variable if possible without
having to instantiate the Config class.
I would also like to get something similar working for say Logging and
a number of other modules that need to be used across my toolset.
How has pylons implemented this?
Thanks advance
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.