[Zodb-checkins] CVS: Zope/lib/python/zdaemon - zdoptions.py:1.17
Fred L. Drake, Jr.
fred@zope.com
Tue, 11 Feb 2003 18:14:39 -0500
Update of /cvs-repository/Zope/lib/python/zdaemon
In directory cvs.zope.org:/tmp/cvs-serv10240
Modified Files:
zdoptions.py
Log Message:
Add support for environment variables overriding values from a
configuration file.
=== Zope/lib/python/zdaemon/zdoptions.py 1.16 => 1.17 ===
--- Zope/lib/python/zdaemon/zdoptions.py:1.16 Wed Feb 5 13:44:06 2003
+++ Zope/lib/python/zdaemon/zdoptions.py Tue Feb 11 18:14:07 2003
@@ -33,6 +33,7 @@
self.options_map = {}
self.default_map = {}
self.required_map = {}
+ self.environ_map = {}
self.add(None, None, "h", "help", self.help)
self.add("configfile", None, "C:", "configure=")
@@ -92,6 +93,7 @@
default=None, # default value
required=None, # message if not provided
flag=None, # if not None, flag value
+ env=None, # if not None, environment variable
):
"""Add information about a configuration option.
@@ -111,6 +113,8 @@
default=... -- if not None, the default value
required=... -- if nonempty, an error message if no value provided
flag=... -- if not None, flag value for command line option
+ env=... -- if not None, name of environment variable that
+ overrides the configuration file or default
"""
if flag is not None:
@@ -153,6 +157,9 @@
self.options_map[key] = (name, handler)
self.long_options.append(long)
+ if env:
+ self.environ_map[env] = (name, handler)
+
if name:
if not hasattr(self, name):
setattr(self, name, None)
@@ -209,6 +216,20 @@
if getattr(self, name) is not None:
self.usage("conflicting command line option %r" % opt)
setattr(self, name, arg)
+
+ # Process environment variables
+ for envvar in self.environ_map.keys():
+ name, handler = self.environ_map[envvar]
+ if os.environ.has_key(envvar) and getattr(self, name) is None:
+ value = os.environ[envvar]
+ if handler is not None:
+ try:
+ value = handler(value)
+ except ValueError, msg:
+ self.usage("invalid environment value for %s %r: %s"
+ % (envvar, value, msg))
+ if name and value is not None:
+ setattr(self, name, value)
if self.configfile is not None:
# Process config file