Changeset 11028
- Timestamp:
- 11/19/08 18:41:44 (7 weeks ago)
- Files:
-
- 1 modified
-
sandboxen/kells/3403/CmdBase.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sandboxen/kells/3403/CmdBase.py
r10813 r11028 16 16 Provide utility functions for logging and config file parsing 17 17 to command line programs 18 19 20 $Id: CmdBase.py,v 1.10 2004/04/04 02:22:21 edahl Exp $""" 21 22 __version__ = "$Revision: 1.10 $"[11:-2] 18 """ 23 19 24 20 import os … … 36 32 unused(pkg_resources) 37 33 38 def parseconfig(options):39 """parse a config file which has key value pairs delimited by white space"""40 if not os.path.exists(options.configfile):41 print >>sys.stderr, "WARN: config file %s not found skipping" % (42 options.configfile)43 return44 lines = open(options.configfile).readlines()45 for line in lines:46 if line.lstrip().startswith('#'): continue47 if line.strip() == '': continue48 key, value = line.split(None, 1)49 value = value.rstrip('\r\n')50 key = key.lower()51 defval = getattr(options, key, None)52 # hack around for #2290: ignore config file values for53 # list types when the user has provided a value54 if type(defval) == type([]) and defval: continue55 if defval: value = type(defval)(value)56 setattr(options, key, value)57 58 59 34 class DMDError: pass 60 35 61 36 class CmdBase: 37 """ 38 Class used for all Zenoss commands 39 """ 62 40 63 41 doesLogging = True … … 76 54 self.buildParser() 77 55 self.buildOptions() 56 78 57 self.parseOptions() 79 58 if self.options.configfile: 80 parseconfig(self.options) 59 self.getConfigFileDefaults( self.options.configfile ) 60 self.parseOptions() 61 81 62 if self.doesLogging: 82 63 self.setupLogging() 83 64 84 65 66 def getConfigFileDefaults(self, filename): 67 """ 68 Parse a config file which has key-value pairs delimited by white space, 69 and update the parser's option defaults with these values. 70 """ 71 try: 72 lines = open(filename).readlines() 73 except: 74 import traceback 75 print >>sys.stderr, "WARN: unable to read config file %s -- skipping" % \ 76 filename 77 traceback.print_exc(0) 78 return 79 80 for line in lines: 81 if line.lstrip().startswith('#'): continue 82 if line.strip() == '': continue 83 84 key, value = line.split(None, 1) 85 value = value.rstrip('\r\n') 86 87 flag= "--%s" % key 88 option= self.parser.get_option( flag ) 89 if option is None: 90 print >>sys.stderr, "WARN: Ignoring unknown option '%s' found in config file" % key 91 continue 92 93 # NB: At this stage, optparse accepts even bogus values 94 # It will report unhappiness when it parses the arguments 95 self.parser.set_default( option.dest, type(option.type)(value) ) 96 97 85 98 def setupLogging(self): 99 """ 100 Set common logging options 101 """ 86 102 rlog = logging.getLogger() 87 103 rlog.setLevel(logging.WARN) … … 90 106 zlog = logging.getLogger("zen") 91 107 zlog.setLevel(self.options.logseverity) 108 92 109 if self.options.logpath: 93 110 logdir = self.options.logpath … … 105 122 106 123 def buildParser(self): 124 """ 125 Create the options parser 126 """ 107 127 if not self.parser: 128 from Products.ZenModel.ZenossInfo import ZenossInfo 129 try: 130 zinfo= ZenossInfo() 131 version= zinfo.getZenossVersion() 132 except: 133 from Products.ZenModel.ZVersion import VERSION 134 version= VERSION 108 135 self.parser = OptionParser(usage=self.usage, 109 version="%prog " + __version__)136 version="%prog " + version ) 110 137 111 138 def buildOptions(self): 112 """basic options setup sub classes can add more options here""" 139 """ 140 Basic options setup. Other classes should call this before adding 141 more options 142 """ 113 143 self.buildParser() 114 144 if self.doesLogging: … … 118 148 type='int', 119 149 help='Logging severity threshold') 150 120 151 self.parser.add_option('--logpath',dest='logpath', 121 help='override default logging path') 152 help='Override the default logging path') 153 122 154 self.parser.add_option("-C", "--configfile", 123 155 dest="configfile", 124 help="config file must define all params (see man)") 156 help="Use an alternate configuration file" ) 157 158 self.parser.add_option("--genconf", 159 action="store_true", 160 default=False, 161 help="Generate a template configuration file" ) 162 163 self.parser.add_option("--genxmltable", 164 action="store_true", 165 default=False, 166 help="Generate a Docbook table showing command-line switches." ) 167 125 168 126 169 127 170 128 171 def pretty_print_config_comment( self, comment ): 129 """Quick and dirty pretty printer for comments that happen to be longer than can comfortably 130 be seen on the display.""" 172 """ 173 Quick and dirty pretty printer for comments that happen to be longer than can comfortably 174 be seen on the display. 175 """ 131 176 132 177 max_size= 40 … … 181 226 182 227 def generate_configs( self, parser, options ): 183 """Create a configuration file based on the long-form of the option names""" 228 """ 229 Create a configuration file based on the long-form of the option names 230 231 @parameter parser: an optparse parser object which contains defaults, help 232 @parameter options: parsed options list containing actual values 233 """ 184 234 185 235 # … … 263 313 264 314 def generate_xml_table( self, parser, options ): 265 """Create a Docbook table based on the long-form of the option names""" 315 """ 316 Create a Docbook table based on the long-form of the option names 317 318 @parameter parser: an optparse parser object which contains defaults, help 319 @parameter options: parsed options list containing actual values 320 """ 266 321 267 322 # … … 378 433 379 434 def parseOptions(self): 380 381 self.parser.add_option("--genconf", 382 action="store_true", 383 default=False, 384 help="Generate a template configuration file" ) 385 386 self.parser.add_option("--genxmltable", 387 action="store_true", 388 default=False, 389 help="Generate a Docbook table showing command-line switches." ) 435 """ 436 Uses the optparse parse previously populated and performs common options. 437 """ 390 438 391 439 if self.noopts: