[Zope-Checkins] CVS: Zope/lib/python/Controller - Directives.py:1.1.2.4
Chris McDonough
chrism@zope.com
Wed, 9 Oct 2002 01:05:56 -0400
Update of /cvs-repository/Zope/lib/python/Controller
In directory cvs.zope.org:/tmp/cvs-serv12260
Modified Files:
Tag: chrism-install-branch
Directives.py
Log Message:
Use ZConfig instead of internal parser to obtain configuration object.
=== Zope/lib/python/Controller/Directives.py 1.1.2.3 => 1.1.2.4 ===
--- Zope/lib/python/Controller/Directives.py:1.1.2.3 Mon Sep 16 01:49:42 2002
+++ Zope/lib/python/Controller/Directives.py Wed Oct 9 01:05:55 2002
@@ -21,6 +21,7 @@
import zLOG
from MetaDirective import EnvironmentDirective, SwitchEnvironmentDirective,\
CommandLineDirective
+import ZConfig
_marker = []
@@ -75,11 +76,11 @@
def reconfigure(self, config_filename):
self.resetDefaults()
- self.location = config_filename
- d = self.parseConfig()
- for name, value in d.items():
+ self.location = munge_config_loc_name(config_filename)
+ zconfig = ZConfig.load(self.location)
+ for name, value in zconfig.items():
directive = self._names[name][1]
- directive.set(value)
+ directive.set(value, zconfig)
self.activateDirectives()
def unregister(self, name):
@@ -200,62 +201,13 @@
klass = dispatch[d['type']]
return apply(klass, (), d)
- def parseConfig(self):
- if self.location is None:
- raise ValueError, ('Need to set config file location before '
- 'calling parseConfig')
- if self.location.startswith('http://'):
- f = getConfigFromURL(self.location)
- else:
- f = getConfigFromFile(self.location)
- regex = re.compile(r'(\w+)\s+(.*)')
- i = 1
- seen = {}
- d = {}
- while 1:
- line = f.readline()
- if not line:
- break
- if line.startswith('#'):
- i = i + 1
- continue
- line = line.strip()
- if not line:
- i = i + 1
- continue
- sre = regex.match(line)
- if sre is None:
- if len(line) > 40:
- line = line[:40] + '...'
- raise ConfigParseError, 'malformed line %s: %s"' % (i, line)
- name, value = sre.groups()
- if seen.has_key(name):
- raise ConfigParseError, (
- 'Illegal multiple directive declarations for %s' % name
- )
- seen[name] = 1
- if not self._names.has_key(name):
- raise ConfigParseError, (
- 'Unknown directive "%s" at line %s' % (name, i)
- )
- d[name] = value
- return d
-
-def getConfigFromFile(filename):
- filename = os.path.expanduser(filename)
- filename = os.path.normpath(filename)
- filename = os.path.abspath(filename)
- if not os.path.exists(filename):
- raise 'Cannot find configuration file at %s' % filename
- f = open(filename, 'r')
- return f
-
-def getConfigFromURL(url):
- try:
- return urllib2.urlopen(url)
- except:
- import traceback; traceback.print_exc()
- raise 'Cannot open url %s' % url
+def munge_config_loc_name(name):
+ name.strip()
+ prefixes = ('http://', 'file://', 'https://')
+ has_prefix = not not filter(lambda x,n=name: n.startswith(x), prefixes)
+ if has_prefix:
+ return name
+ return 'file://%s' % name
DirectiveRegistry = DirectiveRegistry()