def retrieve(cls, val): """ Retrieve int value and check if it is a valid enumeration string or int on this enumeration class. """ allowedValues = [ attr for attr in get_attributes(cls) if not attr[0].startswith('_') ] try: # Convert integer string values to integer, if possible: val = int(val) except ValueError: pass if type(val) is str: oldVal = val val = cls.fromstring(val) if val is None: raise ValueError("String (%s) does not match any of the allowed values %r." % \ (oldVal, allowedValues)) else: if not val in [attr[1] for attr in allowedValues]: raise ValueError(( "Attempted to retrieve val benchmark " "with a enumeration value which is not allowed. Use one of the followings: " "%r") % allowedValues) return val
def tostring(cls, val): "Transforms val into string." from RingerCore.util import get_attributes for k, v in get_attributes(cls, getProtected=False): if v == val: return k return None
def _parse_standard_args(self): """ Here we deal with the standard arguments """ cmd_str = '' nSpaces = self._nSpaces() # Add extra arguments for name, value in get_attributes(self): csv = False suboption = False parseName = True if name.startswith(self.prefix): name = name.replace(self.prefix, '', 1) if name.startswith('_Group'): if value: name = value value = True parseName = False else: continue elif name.startswith('_CSV'): csv = True name = name.replace('_CSV', '', 1) elif name.startswith('_Suboption_'): suboption = True name = name.replace('_Suboption_', '', 1) option = name.split('') if parseName: name = name[:2].replace('_', '-') + name[2:] else: continue tVal = type(value) if tVal is bool: if value: cmd_str += self._formated_line(name) elif isinstance(value, OptionRetrieve) and value: cmd_str += self._formated_line(str(value)) elif value in (None, NotSet): continue elif isinstance(value, list): if value: if csv: cmd_str += self._formated_line( name + '=' + ','.join([str(v) for v in value])) else: cmd_str += self._formated_line( name + '=' + ' '.join([str(v) for v in value])) else: cmd_str += self._formated_line(name + '=' + str(value)) return cmd_str
def _find_job_submission_option(self, option): import re search = re.compile('^' + self.prefix + '(_.+)?_{1,2}' + option + '$') matches = [ key for key in get_attributes(self, onlyVars=True) if bool(search.match(key)) ] lMatches = len(matches) if lMatches > 1: self._warning( "Found more than one match for option %s, will return first match. Matches are: %r.", option, matches) elif lMatches == 0: self._fatal("Cannot find job submission option: %s", option, KeyError) return matches[0]
def fromstring(cls, str_): "Transforms string into enumeration." if not cls._ignoreCase: return getattr(cls, str_, None) else: allowedValues = [ attr for attr in get_attributes(cls) if not attr[0].startswith('_') ] try: idx = [attr[0].upper() for attr in allowedValues ].index(str_.upper().replace('-', '_')) except ValueError: raise ValueError( "%s is not in enumeration. Use one of the followings: %r" % (str_, allowedValues)) return allowedValues[idx][1]
def intList(cls): from operator import itemgetter return [ v[1] for v in sorted(get_attributes(cls, getProtected=False), key=itemgetter(1)) ]
def run_cmd(self): """ Execute parsed arguments. """ # Try to change our level if we have an output_level option: try: self.setLevel( self.output_level ) except AttributeError: pass # Add program to exec and build exec if available full_cmd_str = self.prog + (' --bexec ' + self.bexec if hasattr(self,'bexec') else '') + ' \\\n' # The number of spaces to add to each following option to improve readability: nSpaces = self.nSpaces() # Add execute grid command if available if hasattr(self,'exec_'): full_cmd_str += (' ' * nSpaces) + '--exec' + ' \\\n' exec_str = [textwrap.dedent(l) for l in self.exec_.split('\n')] exec_str = [l for l in exec_str if l not in (';','"','')] if exec_str[-1][-2:] != ';"': exec_str[-1] += ';"' for i, l in enumerate(exec_str): if i == 0: moreSpaces = 2 else: moreSpaces = 4 full_cmd_str += (' ' * (nSpaces + moreSpaces) ) + l + ' \\\n' if hasattr(self,'mergeExec_'): full_cmd_str += (' ' * nSpaces) + '--mergeScript' + ' \\\n' merge_exec_str = [textwrap.dedent(l) for l in self.mergeExec_.split('\n')] merge_exec_str = [l for l in merge_exec_str if l not in (';','"','')] if merge_exec_str[-1][-1:] != '"': merge_exec_str[-1] += '"' for i, l in enumerate(merge_exec_str): if i == 0: moreSpaces = 2 else: moreSpaces = 4 full_cmd_str += (' ' * (nSpaces + moreSpaces) ) + l + ' \\\n' if not 'grid_mergeOutput' in get_attributes(self, onlyVars = True) or \ not(self.grid_mergeOutput): self.grid_mergeOutput = True # Add needed external files: if self.extFile() and not self.extFile() in self.grid_extFile: if len(self.grid_extFile): self.grid_extFile += ',' self.grid_extFile += self.extFile() if self.grid_long: if self.grid_excludedSite: for shortSite in grid_shortSites.split(','): if not shortSite in self.grid_excludedSite: self.grid_excludedSite += ',' + shortSite else: self.grid_excludedSite = grid_shortSites # Add extra arguments for name, value in get_attributes(self): if 'grid_' in name: name = name.replace('grid_','--') if name == '--outDS': for output in self.grid_outputs.split(','): oList = output.split(':') if len(oList) == 2: did = value + '_' + oList[0].replace('"','') if len(did) > 132: raise LargeDIDError(did) else: if '*' in output and not output.endswith('.tgz'): output += '.tgz' did = value + '_' + output.replace('*','XYZ').replace('"','') if len(did) > 132: raise LargeDIDError(did) elif 'gridExpand_' in name: if value: name = value value = True else: continue else: continue tVal = type(value) if tVal == bool and value: full_cmd_str += (' ' * nSpaces) + name + ' \\\n' elif value: if isinstance(value, list): full_cmd_str += (' ' * nSpaces) + name + '=' + ','.join(value) + ' \\\n' else: full_cmd_str += (' ' * nSpaces) + name + '=' + str(value) + ' \\\n' # Now we show command: self._logger.info("Command:\n%s", full_cmd_str) full_cmd_str = re.sub('\\\\ *\n','', full_cmd_str ) full_cmd_str = re.sub(' +',' ', full_cmd_str) self._logger.debug("Command without spaces:\n%s", full_cmd_str) # And run it: if not self.dry_run: self.__run(full_cmd_str) pass
except ImportError: from RingerCore.parsers import __py_argparse as argparse from RingerCore.util import EnumStringification, get_attributes ############################################################################### # Logger related objects ############################################################################### from RingerCore.Logger import LoggingLevel, Logger loggerParser = argparse.ArgumentParser(add_help = False) logOutput = loggerParser.add_argument_group('Loggging arguments', '') logOutput.add_argument('--output-level', default = LoggingLevel.tostring( LoggingLevel.INFO ), type=str, required = False, dest = '_outputLevel', help = "The output level for the main logger. Options are: " + \ str( get_attributes( LoggingLevel, onlyVars = True, getProtected = False ) )) ############################################################################### ## LoggerNamespace # When using logger parser parent, make sure to use LoggerNamespace when # retrieving arguments class LoggerNamespace( argparse.Namespace ): """ Namespace for dealing with logger parser properties """ def __init__(self, **kw): argparse.Namespace.__init__( self, **kw ) @property def output_level(self): if '_outputLevel' in self.__dict__: return LoggingLevel.retrieve( self.__dict__['_outputLevel'] )