From a11c61f22f3df968f4c00f9a8f8b996e6a143df7 Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Wed, 7 Jul 2010 19:18:34 -0700 Subject: Separate out the configs from the args parsing code. This will allow basic configs for api consumers that are changeable. --- README.api | 4 +- bin/layman | 4 +- layman/api.py | 12 ++---- layman/config.py | 111 ++++++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 97 insertions(+), 34 deletions(-) diff --git a/README.api b/README.api index 2cdc7f7..976006d 100644 --- a/README.api +++ b/README.api @@ -7,8 +7,8 @@ big_daddy layman # python Python 2.6.5 (release26-maint, May 15 2010, 18:26:37) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. ->>> import layman;from layman.api import LaymanAPI, create_fd;from layman.config import Config ->>> e=create_fd();o=create_fd();from layman.debug import Message;M=Message(module='layman',out=o[1], err=e[1]);c=Config(output=M, stdout=o[1], stderr=e[1]) +>>> import layman;from layman.api import LaymanAPI, create_fd;from layman.config import BareConfig +>>> e=create_fd();o=create_fd();from layman.debug import Message;M=Message(module='layman',out=o[1], err=e[1]);c=BareConfig(output=M, stdout=o[1], stderr=e[1]) >>> # connect the e[0] and o[0] file descriptors to where you want to capture/direct the output # diff --git a/bin/layman b/bin/layman index c454510..fa1a3ab 100755 --- a/bin/layman +++ b/bin/layman @@ -22,7 +22,7 @@ __version__ = "$Id$" # #------------------------------------------------------------------------------- -from layman.config import Config +from layman.config import ArgsParser from layman.action import main #=============================================================================== @@ -31,4 +31,4 @@ from layman.action import main # #------------------------------------------------------------------------------- -main(Config()) +main(ArgsParser()) diff --git a/layman/api.py b/layman/api.py index 52dcd3d..f163f86 100644 --- a/layman/api.py +++ b/layman/api.py @@ -16,7 +16,7 @@ from sys import stderr, stdin, stdout import os, types -from layman.config import Config +from layman.config import BareConfig #from layman.action import Sync from layman.dbbase import UnknownOverlayException @@ -30,7 +30,7 @@ ERROR_REPO_NOT_FOUND = -1 ERROR_INTERNAL_ERROR = -2 UNKNOWN_REPO_ID = "Repo ID '%s' " + \ "is not listed in the current available overlays list" - + # In order to redirect output you need to get a Message class instance with the # stderr, stdout, stddebug directed to where you want. # eg: output = Message('layman', err=mystderr, dbg=mydebug, out=myoutput) @@ -49,7 +49,7 @@ class LaymanAPI(object): self.output = output if output else OUT - self.config = config if config else Config(output=output) + self.config = config if config else BareConfig(output=output) self.report_errors = report_errors @@ -176,10 +176,6 @@ class LaymanAPI(object): @param repos: ['repo-id1', ...] @rtype bool """ - # currently uses a modified Sync class with a few added parameters, - # but should be re-written into here for a better fit and output - #_sync = Sync(self.config, repos, db=self._installed_db, rdb=self._available_db) - #_sync.run() fatals = [] warnings = [] @@ -231,7 +227,7 @@ class LaymanAPI(object): success.append((id,'Successfully synchronized overlay "' + id + '".')) except Exception, error: fatals.append((id, - 'Failed to sync overlay "' + i + '".\nError was: ' + 'Failed to sync overlay "' + id + '".\nError was: ' + str(error))) return (warnings, success, fatals) diff --git a/layman/config.py b/layman/config.py index 5b84c17..5ad67f2 100644 --- a/layman/config.py +++ b/layman/config.py @@ -15,6 +15,7 @@ # Author(s): # Gunnar Wrobel # Sebastian Pipping +# Brian Dolbec # '''Defines the configuration options and provides parsing functionality.''' @@ -43,8 +44,92 @@ _USAGE = """ layman -f [-o URL] layman (-l|-L|-S)""" -class Config(object): - '''Handles the configuration.''' +class BareConfig(object): + '''Handles the configuration only.''' + + def __init__(self, output=None, stdout=None, stdin=None, stderr=None): + ''' + Creates a bare config with defaults and a few output options. + + >>> a = BareConfig() + >>> a['overlays'] + '\\nhttp://www.gentoo.org/proj/en/overlays/repositories.xml' + >>> sorted(a.keys()) + ['bzr_command', 'cache', 'config', 'cvs_command', 'darcs_command', + 'git_command', 'local_list', 'make_conf', 'mercurial_command', + 'nocheck', 'overlays', 'proxy', 'quietness', 'rsync_command', 'storage', + 'svn_command', 'tar_command', 'umask', 'width'] + ''' + self._defaults = {'config' : '/etc/layman/layman.cfg', + 'storage' : '/var/lib/layman', + 'cache' : '%(storage)s/cache', + 'local_list': '%(storage)s/overlays.xml', + 'make_conf' : '%(storage)s/make.conf', + 'nocheck' : 'yes', + 'proxy' : '', + 'umask' : '0022', + 'overlays' : + 'http://www.gentoo.org/proj/en/overlays/repositories.xml', + 'bzr_command': '/usr/bin/bzr', + 'cvs_command': '/usr/bin/cvs', + 'darcs_command': '/usr/bin/darcs', + 'git_command': '/usr/bin/git', + 'mercurial_command': '/usr/bin/hg', + 'rsync_command': '/usr/bin/rsync', + 'svn_command': '/usr/bin/svn', + 'tar_command': '/bin/tar' + } + self._options = { + 'stdout': stdout if stdout else sys.stdout, + 'stdin': stdin if stdin else sys.stdin, + 'stderr': stderr if stderr else sys.stderr, + 'output': output if output else OUT, + 'quietness': '4', + 'width': 0, + 'verbose': False, + 'quiet': False, + } + + + def keys(self): + '''Special handler for the configuration keys. + ''' + self._options['output'].debug('Retrieving BareConfig options', 8) + keys = [i for i in self._options] + self._options['output'].debug('Retrieving BareConfig defaults', 8) + keys += [i for i in self._defaults + if not i in keys] + self._options['output'].debug('Retrieving BareConfig done...', 8) + return keys + + + def get_defaults(self): + """returns our defaults dictionary""" + return self._defaults + + + def set_option(self, option, value): + """Sets an option to the value """ + self._options[option] = value + + + def __getitem__(self, key): + self._options['output'].debug('Retrieving BareConfig option', 8) + if (key in self._options + and not self._options[key] is None): + return self._options[key] + self._options['output'].debug('Retrieving BareConfig default', 8) + if key in self._defaults: + if '%(storage)s' in self._defaults[key]: + return self._defaults[key] %{'storage': self._defaults['storage']} + return self._defaults[key] + return None + + + + +class ArgsParser(object): + '''Handles the configuration and option parser.''' def __init__(self, args=None, output=None, stdout=None, stdin=None, stderr=None): ''' @@ -55,7 +140,7 @@ class Config(object): >>> here = os.path.dirname(os.path.realpath(__file__)) >>> sys.argv.append('--config') >>> sys.argv.append(here + '/../etc/layman.cfg') - >>> a = Config() + >>> a = ArgsParser() >>> a['overlays'] '\\nhttp://www.gentoo.org/proj/en/overlays/repositories.xml' >>> sorted(a.keys()) @@ -69,25 +154,7 @@ class Config(object): self.stdin = stdin if stdin else sys.stdin self.output = output if output else OUT - self.defaults = {'config' : '/etc/layman/layman.cfg', - 'storage' : '/var/lib/layman', - 'cache' : '%(storage)s/cache', - 'local_list': '%(storage)s/overlays.xml', - 'make_conf' : '%(storage)s/make.conf', - 'nocheck' : 'yes', - 'proxy' : '', - 'umask' : '0022', - 'overlays' : - 'http://www.gentoo.org/proj/en/overlays/repositories.xml', - 'bzr_command': '/usr/bin/bzr', - 'cvs_command': '/usr/bin/cvs', - 'darcs_command': '/usr/bin/darcs', - 'git_command': '/usr/bin/git', - 'mercurial_command': '/usr/bin/hg', - 'rsync_command': '/usr/bin/rsync', - 'svn_command': '/usr/bin/svn', - 'tar_command': '/bin/tar', } - + self.defaults = BareConfig().get_defaults() self.parser = OptionParser( usage = _USAGE, -- cgit v1.2.3-1-g7c22