📄 setup.py
字号:
#Licensed to the Apache Software Foundation (ASF) under one#or more contributor license agreements. See the NOTICE file#distributed with this work for additional information#regarding copyright ownership. The ASF licenses this file#to you under the Apache License, Version 2.0 (the#"License"); you may not use this file except in compliance#with the License. You may obtain a copy of the License at# http://www.apache.org/licenses/LICENSE-2.0#Unless required by applicable law or agreed to in writing, software#distributed under the License is distributed on an "AS IS" BASIS,#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#See the License for the specific language governing permissions and#limitations under the License.# $Id:setup.py 5158 2007-04-09 00:14:35Z zim $# $Id:setup.py 5158 2007-04-09 00:14:35Z zim $##------------------------------------------------------------------------------"""'setup' provides for reading and verifing configuration files based on Python's SafeConfigParser class."""import sys, os, re, pprintfrom ConfigParser import SafeConfigParserfrom optparse import OptionParser, IndentedHelpFormatter, OptionGroupfrom util import get_perms, replace_escapesfrom types import typeValidator, typeValidatorInstance, is_valid_type, \ typeToStringfrom hodlib.Hod.hod import hodHelpreEmailAddress = re.compile("^.*@.*$")reEmailDelimit = re.compile("@")reComma = re.compile("\s*,\s*")reDot = re.compile("\.")reCommentHack = re.compile("^.*?\s+#|;.*", flags=re.S)reCommentNewline = re.compile("\n|\r$")reKeyVal = r"(?<!\\)="reKeyVal = re.compile(reKeyVal)reKeyValList = r"(?<!\\),"reKeyValList = re.compile(reKeyValList)errorPrefix = 'error'requiredPerms = '0660'class definition: def __init__(self): """Generates a configuration definition object.""" self.__def = {} self.__defOrder = [] def __repr__(self): return pprint.pformat(self.__def) def __getitem__(self, section): return self.__def[section] def __iter__(self): return iter(self.__def) def sections(self): """Returns a list of sections/groups.""" if len(self.__defOrder): return self.__defOrder else: return self.__def.keys() def add_section(self, section): """Add a configuration section / option group.""" if self.__def.has_key(section): raise Exception("Section already exists: '%s'" % section) else: self.__def[section] = {} def add_def(self, section, var, type, desc, help = True, default = None, req = True, validate = True, short = None): """ Add a variable definition. section - section name var - variable name type - valid hodlib.types desc - description of variable help - display help for this variable default - default value req - bool, requried? validate - bool, validate type value? short - short symbol (1 character), help - bool, display help?""" if self.__def.has_key(section): if not is_valid_type(type): raise Exception("Type (type) is invalid: %s.%s - '%s'" % (section, var, type)) if not isinstance(desc, str): raise Exception("Description (desc) must be a string: %s.%s - '%s'" % ( section, var, desc)) if not isinstance(req, bool): raise Exception("Required (req) must be a bool: %s.%s - '%s'" % (section, var, req)) if not isinstance(validate, bool): raise Exception("Validate (validate) must be a bool: %s.%s - '%s'" % ( section, var, validate)) if self.__def[section].has_key(var): raise Exception("Variable name already defined: '%s'" % var) else: self.__def[section][var] = { 'type' : type, 'desc' : desc, 'help' : help, 'default' : default, 'req' : req, 'validate' : validate, 'short' : short } else: raise Exception("Section does not exist: '%s'" % section) def add_defs(self, defList, defOrder=None): """ Add a series of definitions. defList = { section0 : ((name0, type0, desc0, help0, default0, req0, validate0, short0), .... (nameN, typeN, descN, helpN, defaultN, reqN, validateN, shortN)), .... sectionN : ... } Where the short synmbol is optional and can only be one char.""" for section in defList.keys(): self.add_section(section) for defTuple in defList[section]: if isinstance(defTuple, tuple): if len(defTuple) < 7: raise Exception( "section %s is missing an element: %s" % ( section, pprint.pformat(defTuple))) else: raise Exception("section %s of defList is not a tuple" % section) if len(defTuple) == 7: self.add_def(section, defTuple[0], defTuple[1], defTuple[2], defTuple[3], defTuple[4], defTuple[5], defTuple[6]) else: self.add_def(section, defTuple[0], defTuple[1], defTuple[2], defTuple[3], defTuple[4], defTuple[5], defTuple[6], defTuple[7]) if defOrder: for section in defOrder: if section in self.__def: self.__defOrder.append(section) for section in self.__def: if not section in defOrder: raise Exception( "section %s is missing from specified defOrder." % section) class baseConfig: def __init__(self, configDef, originalDir=None): self.__toString = typeToString() self.__validated = False self._configDef = configDef self._options = None self._mySections = [] self._dict = {} self.configFile = None self.__originalDir = originalDir if self._configDef: self._mySections = configDef.sections() def __repr__(self): """Returns a string representation of a config object including all normalizations.""" print_string = ''; for section in self._mySections: print_string = "%s[%s]\n" % (print_string, section) options = self._dict[section].keys() for option in options: print_string = "%s%s = %s\n" % (print_string, option, self._dict[section][option]) print_string = "%s\n" % (print_string) print_string = re.sub("\n\n$", "", print_string) return print_string def __getitem__(self, section): """ Returns a dictionary of configuration name and values by section. """ return self._dict[section] def __setitem__(self, section, value): self._dict[section] = value def __iter__(self): return iter(self._dict) def has_key(self, section): status = False if section in self._dict: status = True return status # Prints configuration error messages def var_error(self, section, option, *addData): errorStrings = [] if not self._dict[section].has_key(option): self._dict[section][option] = None errorStrings.append("%s: invalid '%s' specified in section %s (--%s.%s): %s" % ( errorPrefix, option, section, section, option, self._dict[section][option])) if addData: errorStrings.append("%s: additional info: %s\n" % (errorPrefix, addData[0])) return errorStrings def var_error_suggest(self, errorStrings): if self.configFile: errorStrings.append("Check your command line options and/or " + \ "your configuration file %s" % self.configFile) def __get_args(self, section): def __dummyToString(type, value): return value toString = __dummyToString if self.__validated: toString = self.__toString args = [] if isinstance(self._dict[section], dict): for option in self._dict[section]: if section in self._configDef and \ option in self._configDef[section]: if self._configDef[section][option]['type'] == 'bool': if self._dict[section][option] == 'True' or \ self._dict[section][option] == True: args.append("--%s.%s" % (section, option)) else: args.append("--%s.%s" % (section, option)) args.append(toString( self._configDef[section][option]['type'], self._dict[section][option])) else: if section in self._configDef: if self._configDef[section][option]['type'] == 'bool': if self._dict[section] == 'True' or \ self._dict[section] == True: args.append("--%s" % section) else: if self._dict[section] != 'config': args.append("--%s" % section) args.append(toString(self._configDef[section]['type'], self._dict[section])) return args def values(self): return self._dict.values() def keys(self): return self._dict.keys() def get_args(self, exclude=None, section=None): """Retrieve a tuple of config arguments.""" args = [] if section: args = self.__get_args(section) else: for section in self._dict: if exclude: if not section in exclude: args.extend(self.__get_args(section)) else: args.extend(self.__get_args(section)) return tuple(args) def verify(self): """Verifies each configuration variable, using the configValidator class, based on its type as defined by the dictionary configDef. Upon encountering a problem an error is printed to STDERR and false is returned.""" oldDir = os.getcwd() if self.__originalDir: os.chdir(self.__originalDir) status = True statusMsgs = [] if self._configDef: errorCount = 0 configValidator = typeValidator(self.__originalDir) # foreach section and option by type string as defined in configDef # add value to be validated to validator for section in self._mySections: for option in self._configDef[section].keys(): configVarName = "%s.%s" % (section, option) if self._dict[section].has_key(option): if self._configDef[section][option].has_key('validate'): if self._configDef[section][option]['validate']: # is the section.option needed to be validated? configValidator.add(configVarName, self._configDef[section][option]['type'], self._dict[section][option]) else: # If asked not to validate, just normalize self[section][option] = \ configValidator.normalize( self._configDef[section][option]['type'], self._dict[section][option]) if self._configDef[section][option]['default'] != \ None: self._configDef[section][option]['default'] = \ configValidator.normalize( self._configDef[section][option]['type'], self._configDef[section][option]['default'] ) self._configDef[section][option]['default'] = \ self.__toString( self._configDef[section][option]['type'], self._configDef[section][option]['default'] ) else: # This should not happen. Just in case, take this as 'to be validated' case.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -