def addict( cfig, key, val, parents, index ): """Add a new [parents...]key=value pair to a nested dict.""" for p in parents: # drop down the parent list cfig = cfig[p] if not isinstance( cfig, dict ): # an item of this name has already been encountered at this level print >> sys.stderr, itemstr( parents, key, val ) raise ParseError( 'ERROR line ' + str(index) + ': already encountered ' + itemstr( parents )) if key in cfig: # this item already exists if key == 'graph' and \ ( len( parents ) == 2 and parents == ['scheduling','dependencies'] or \ len( parents ) == 3 and parents[-3:-1] == ['scheduling','dependencies'] ): # append the new graph string to the existing one if cylc.flags.verbose: print 'Merging graph strings under ' + itemstr( parents ) if not isinstance( cfig[key], list ): cfig[key] = [cfig[key]] cfig[key].append(val) else: # otherwise override the existing item if cylc.flags.verbose: print >> sys.stderr, 'WARNING: overriding ' + itemstr( parents, key ) print >> sys.stderr, ' old value: ' + cfig[key] print >> sys.stderr, ' new value: ' + val cfig[key] = val else: cfig[key] = val
def addict(cfig, key, val, parents, index): """Add a new [parents...]key=value pair to a nested dict.""" for p in parents: # drop down the parent list cfig = cfig[p] if not isinstance(cfig, dict): # an item of this name has already been encountered at this level print >> sys.stderr, itemstr(parents, key, val) raise ParseError('ERROR line ' + str(index) + ': already encountered ' + itemstr(parents)) if key in cfig: # this item already exists if key == 'graph' and \ ( len( parents ) == 2 and parents == ['scheduling','dependencies'] or \ len( parents ) == 3 and parents[-3:-1] == ['scheduling','dependencies'] ): # append the new graph string to the existing one if cylc.flags.verbose: print 'Merging graph strings under ' + itemstr(parents) if not isinstance(cfig[key], list): cfig[key] = [cfig[key]] cfig[key].append(val) else: # otherwise override the existing item if cylc.flags.verbose: print >> sys.stderr, 'WARNING: overriding ' + itemstr( parents, key) print >> sys.stderr, ' old value: ' + cfig[key] print >> sys.stderr, ' new value: ' + val cfig[key] = val else: cfig[key] = val
def checkspec( self, spec, parents=[] ): "check that the file spec is a nested dict of validators" for key, value in spec.items(): pars = parents + [key] if isinstance( value, dict ): self.checkspec( value, pars ) else: if not isinstance( value, validator ): raise ParsecError( "Illegal file spec item: " + itemstr( pars, repr(value)) )
def checkspec(self, spec, parents=[]): "check that the file spec is a nested dict of validators" for key, value in spec.items(): pars = parents + [key] if isinstance(value, dict): self.checkspec(value, pars) else: if not isinstance(value, validator): raise ParsecError("Illegal file spec item: " + itemstr(pars, repr(value)))
def addsect( cfig, sname, parents ): """Add a new section to a nested dict.""" for p in parents: # drop down the parent list cfig = cfig[p] if sname in cfig: # this doesn't warrant a warning unless contained items are repeated if cylc.flags.verbose: print 'Section already encountered: ' + itemstr( parents + [sname] ) else: cfig[sname] = OrderedDict()
def addsect(cfig, sname, parents): """Add a new section to a nested dict.""" for p in parents: # drop down the parent list cfig = cfig[p] if sname in cfig: # this doesn't warrant a warning unless contained items are repeated if cylc.flags.verbose: print 'Section already encountered: ' + itemstr(parents + [sname]) else: cfig[sname] = OrderedDict()
def mdump( self, mkeys=[], sparse=False, pnative=False, prefix='', oneline=False, none_str='' ): if oneline: items = [] for keys in mkeys: item = self.get( keys, sparse ) if isinstance( item, list ) or isinstance( item, dict ): raise NotSingleItemError( itemstr(keys) ) if not item: item = none_str or "None" items.append(str(item)) # TODO - quote items if they contain spaces or comment delimiters? print prefix + ' '.join( items ) else: for keys in mkeys: self.dump( keys, sparse, pnative, prefix, none_str )
def mdump(self, mkeys=[], sparse=False, pnative=False, prefix='', oneline=False, none_str=''): if oneline: items = [] for keys in mkeys: item = self.get(keys, sparse) if isinstance(item, list) or isinstance(item, dict): raise NotSingleItemError(itemstr(keys)) if not item: item = none_str or "None" items.append(str(item)) # TODO - quote items if they contain spaces or comment delimiters? print prefix + ' '.join(items) else: for keys in mkeys: self.dump(keys, sparse, pnative, prefix, none_str)
def get(self, keys=[], sparse=False): """ Retrieve items or sections, sparse or dense, by list of keys: [sec1,sec2,item] => [sec1] [[sec2]] item = value """ if sparse: cfg = self.sparse else: self.expand() cfg = self.dense parents = [] for key in keys: try: cfg = cfg[key] except KeyError, x: raise ItemNotFoundError(itemstr(parents, key)) else: parents.append(key)
def get( self, keys=[], sparse=False ): """ Retrieve items or sections, sparse or dense, by list of keys: [sec1,sec2,item] => [sec1] [[sec2]] item = value """ if sparse: cfg = self.sparse else: self.expand() cfg = self.dense parents = [] for key in keys: try: cfg = cfg[key] except KeyError, x: raise ItemNotFoundError( itemstr(parents,key) ) else: parents.append(key)
def __init__( self, keys, key ): msg = 'Illegal item: ' + itemstr( keys, key ) ValidationError.__init__( self, msg )
def __init__( self, vtype, keys, value ): msg = 'Illegal ' + vtype + ' value: ' + itemstr( keys, value=value ) ValidationError.__init__( self, msg )