def m_override(target, sparse): """Override items in a target pdict. Target keys must already exist unless there is a "__MANY__" placeholder in the right position.""" if not sparse: target = OrderedDict() return for key, val in sparse.items(): if isinstance(val, dict): if key not in target: if '__MANY__' in target: target[key] = OrderedDict() replicate(target[key], target['__MANY__']) else: # TODO - validation prevents this, but handle properly for completeness. raise Exception( "parsec dict override: no __MANY__ placeholder") m_override(target[key], val) else: if key not in target: if '__MANY__' in target: if isinstance(val, list): target[key] = val[:] else: target[key] = val else: # TODO - validation prevents this, but handle properly for completeness. raise Exception( "parsec dict override: no __MANY__ placeholder") if isinstance(val, list): target[key] = val[:] else: target[key] = val
def __init__(self, parent, name): # Parent is the Triggers object that created this one. # name is a unique name for this trigger. self.parent = parent self.name = name self.matches = OrderedDict() self.actions = OrderedDict()
def __init__(self, cutflow): if isinstance(cutflow, CutflowDecision): self._cutflow = OrderedDict(cutflow._cutflow) else: self._cutflow = OrderedDict(cutflow) #just need functions and flags for this for key in self._cutflow.keys(): self._cutflow[key] = self._cutflow[key][:2] + [False]
def __init__(self, spec, upgrader=None, write_proc=False, tvars=[], tvars_file=None): self.sparse = OrderedDict() self.dense = OrderedDict() self.upgrader = upgrader self.tvars = tvars self.tvars_file = tvars_file self.write_proc = write_proc self.checkspec(spec) self.spec = spec
def upgrade( self ): warnings = OrderedDict() do_warn = False for vn, upgs in self.upgrades.items(): warnings[vn] = [] for u in upgs: for upg in self.expand(u): try: old = self.get_item( upg['old'] ) except: # OK: deprecated item not found pass else: msg = self.show_keys(upg['old']) if upg['new']: msg += ' -> ' + self.show_keys(upg['new']) else: upg['new'] = upg['old'] msg += " - " + upg['cvt'].describe() warnings[vn].append( msg ) do_warn = True self.del_item( upg['old'] ) if upg['cvt'].describe() != "DELETED (OBSOLETE)": self.put_item( upg['new'], upg['cvt'].convert(old) ) if do_warn and self.verbose: print >> sys.stderr, "WARNING: deprecated items were automatically upgraded in '" + self.descr + "':" for vn,msgs in warnings.items(): for m in msgs: print >> sys.stderr, " * (" + vn + ")", m
def process(groups, rows): columnsClean = [c.split()[-1] for c in groups[0]] if len(groups) == 1: return [ OrderedDict((col, r[col]) for col in columnsClean) for r in rows ] g_col = columnsClean[0] output = [] cur = None cur_rows = [] def add_to_output(): if len(cur_rows) > 0: obj = OrderedDict( (col, cur_rows[0][col]) for col in columnsClean) obj['expand_list'] = process(groups[1:], cur_rows) output.append(obj) for r in rows: if cur is None or r[g_col] != cur: add_to_output() cur_rows = [] cur = r[g_col] cur_rows.append(r) add_to_output() return output
def get_raw(self, query): raw = list(self.conn.execute(query)) columns = raw[0].keys() if len(raw) > 0 else [] columns = [ c.replace('"', '') for c in columns ] #sqlite weirdly returns the quotes around columns sometimes? return [OrderedDict(zip(columns, r)) for r in raw]
def _orderData(data): data_o = OrderedDict() attrs_list = ("id", "version", "timestamp", "uid", "user", "changeset", "lat", "lon") for a in attrs_list: if a in data: data_o[a] = data[a] return data_o
def __init__(self, widget, label=None, displayLabel=True, includeInReports=True, items=None, editable=False, orientation='horizontal', callback=None): widgetState.__init__(self, widget, label, includeInReports) QComboBox.__init__(self, self.controlArea) if displayLabel: self.hb = widgetBox(self.controlArea, orientation=orientation) widgetLabel(self.hb, label) self.hb.layout().addWidget(self) self.hasLabel = True else: self.controlArea.layout().addWidget(self) self.hasLabel = False self.label = label self.items = OrderedDict() self.setEditable(editable) if items: self.addItems(items) if callback: QObject.connect(self, SIGNAL('activated(int)'), callback)
def n_minus_one(tree, sel): result = OrderedDict(sel) for key in sel.keys(): result[key] = apply_selection_except( tree, sel, key, False) # don't save n-1 info in cut table #print 'n-1 result:', result return result
def __init__( self, cfg, spec, descr, verbose=True ): """Store the config dict to be upgraded if necessary.""" self.cfg = cfg self.spec = spec self.descr = descr self.verbose = verbose # upgrades must be ordered in case several act on the same item self.upgrades = OrderedDict()
def replicate(target, source): """ Replicate source *into* target. Source elements need not exist in target already, so source overrides common elements in target and otherwise adds elements to it. """ if not source: target = OrderedDict() return for key, val in source.items(): if isinstance(val, dict): if key not in target: target[key] = OrderedDict() replicate(target[key], val) elif isinstance(val, list): target[key] = val[:] else: target[key] = val
def __init__(self, widget, label=None, displayLabel=True, includeInReports=True, buttons=None, toolTips=None, setChecked=None, orientation='vertical', callback=None): if toolTips and len(toolTips) != len(buttons): raise RuntimeError( _('Number of buttons and toolTips must be equal')) QWidget.__init__(self, widget) widgetState.__init__(self, widget, label, includeInReports) self.controlArea.layout().setAlignment(Qt.AlignTop | Qt.AlignLeft) self.controlArea.layout().addWidget(self) if displayLabel: self.box = groupBox(self.controlArea, label=label, orientation=orientation) # self.layout().addWidget(self.box) else: self.box = widgetBox(self.controlArea, orientation=orientation) # if orientation=='vertical': # self.box.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, # QSizePolicy.MinimumExpanding)) # else: # self.box.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, # QSizePolicy.Preferred)) self.label = label self.items = OrderedDict() self.buttons = QButtonGroup(self.box) self.buttons.setExclusive(False) if buttons: self.addButtons(buttons) # if buttons: # for i,b in zip(range(len(buttons)),buttons): # w = QCheckBox(b,self.box) # if toolTips: # w.setToolTip(toolTips[i]) # self.buttons.addButton(w,i) # self.box.layout().addWidget(w) if callback: QObject.connect(self.buttons, SIGNAL('buttonClicked(int)'), callback) if setChecked: self.setChecked(setChecked)
def getentries(self): entries = OrderedDict() for name in self.listdir(): if isinstance(name, tuple): name, subnode = name else: subnode = None entries[name] = subnode return entries
def _populate_spec_defaults( defs, spec ): """Populate a nested dict with default values from a spec.""" for key,val in spec.items(): if isinstance( val, dict ): if key not in defs: defs[key] = OrderedDict() _populate_spec_defaults( defs[key], spec[key] ) else: defs[key] = spec[key].args['default']
def __init__(self, widget, value=None, label=None, displayLabel=True, includeInReports=True, orientation='vertical', selectionMode=QAbstractItemView.SingleSelection, enableDragDrop=0, dragDropCallback=None, dataValidityCallback=None, sizeHint=None, callback=None, toolTip=None, items=None, *args): widgetState.__init__(self, widget, label, includeInReports) QListWidget.__init__(self, *args) self.label = label self.widget = self.controlArea if displayLabel: self.hb = groupBox(self.controlArea, label=label, orientation=orientation) else: self.hb = widgetBox(self.controlArea, orientation=orientation) self.hb.layout().addWidget(self) self.ogValue = value self.ogLabels = label self.enableDragDrop = enableDragDrop self.dragDopCallback = dragDropCallback self.dataValidityCallback = dataValidityCallback if not sizeHint: self.defaultSizeHint = QSize(150, 100) else: self.defaultSizeHint = sizeHint self.setSelectionMode(selectionMode) if enableDragDrop: self.setDragEnabled(1) self.setAcceptDrops(1) self.setDropIndicatorShown(1) #self.setDragDropMode(QAbstractItemView.DragDrop) self.dragStartPosition = 0 if toolTip: self.setToolTip(toolTip) self.listItems = OrderedDict() if items: self.addItems(items) if callback: QObject.connect(self, SIGNAL('itemClicked(QListWidgetItem*)'), callback)
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, verbose): """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 verbose: print 'Section [' + ']['.join(parents + [sname]) + '] already encountered' else: cfig[sname] = OrderedDict()
def updateRedRItems(self): ## we go through all of the items and remake the items OrderedDict newDict = OrderedDict() for r in range(self.count()): t = unicode(self.itemAt(r).text()) # get the text of the item if t not in self.listItems.values(): newDict[t] = t else: for i, ov in self.listItems.items(): if ov == t: newDict[i] = ov self.listItems = newDict
def poverride(target, sparse): """Override items in a target pdict, target sub-dicts must already exist.""" if not sparse: target = OrderedDict() return for key, val in sparse.items(): if isinstance(val, dict): poverride(target[key], val) elif isinstance(val, list): target[key] = val[:] else: target[key] = val
def getMergedStandards(): """ @summary: returns a dictionary containing all listed standards """ all = OrderedDict() all.update(SANS1878) all.update(EML) all.update(DublinCore) all.update(ISO19115) all.update(ISO19115p2) all.update(ISO19139) all.update(SANS1878) all.update(COMMON) return all
def __init__(self, *args, **kwargs): tmt.EclipseProject.__init__(self, *args, **kwargs) tmt.WinstoneServer = self self.main = "net.gnehzr.tnoodle.server.TNoodleServer" self.argv = ['--nobrowser', '--consoleLevel=INFO'] # Winstone does all of these things =(. self.ignoredWarnings += ['unchecked'] self.ignoredWarnings += ['deprecation'] self.ignoredWarnings += ['serial'] self.ignoredWarnings += ['dep-ann'] self.ignoredWarnings += ['rawtypes'] # It is important that when we iterate through the plugins # in topological sorted order. This way if B uses A, B can clobber # A's settings. self.plugins = OrderedDict()
def __init__(self, name, comment=None, Child=None, strict=True): """ @param name: The name for this group. An argument could be made for making the group itself name-agnostic and only giving it a name upon registration with another group, but that would cripple unregistered groups. @param comment: A helpful comment indicating the usage/meaning of a particular group. This comment will be written to configuration files and used as the help text of the optparse OptionParser the group can generate. @param Child: A callable (usually a class) which, if not None, will be used in the get() method to create a requested child rather than raising KeyError. """ # All of these are prefixed with underscores so they won't conflict with # registered children. if name.startswith('_'): raise ValueError( 'Names beginning with an underscore are forbidden: %r' % name) self._name = name self._parent = None self._Child = Child self._strict = strict self._comment = comment self._children = OrderedDict()
def getentries(self, node): if isinstance(node, dict): return node try: if not self.USE_DIR_CACHE: raise KeyError return self.dircache[node] except KeyError: entries = OrderedDict() if hasattr(node, 'listdir'): for name in node.listdir(): if isinstance(name, tuple): name, subnode = name else: subnode = None entries[name] = subnode if self.USE_DIR_CACHE: self.dircache[node] = entries return entries
def load_single(FILE, SPEC, descr, upgrader=None, do_expand=False, verbose=True, strict=False): """ Parse, upgrade, validate, combine, and expand a single parsec config file. If FILE fails to parse or validate just fall back on spec defaults. """ cfg = OrderedDict() try: cfg = parse(FILE) except FileNotFoundError, x: if strict: raise # not an error - may be relying on defaults pass
def __init__(self,widget,label=None, displayLabel=True, includeInReports=True, buttons=None,toolTips = None, setChecked = None, orientation='vertical',callback = None, **args): QWidget.__init__(self,widget) widgetState.__init__(self,widget,label,includeInReports,**args) self.controlArea.layout().setAlignment(Qt.AlignTop | Qt.AlignLeft) self.label = label if displayLabel: self.box = groupBox(self.controlArea,label=label,orientation=orientation) self.controlArea.layout().addWidget(self.box) else: self.box = widgetBox(self.controlArea,orientation=orientation) # if orientation=='vertical': # self.box.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, # QSizePolicy.MinimumExpanding)) # else: # self.box.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, # QSizePolicy.Preferred)) self.items = OrderedDict() self.buttons = QButtonGroup(self.box) if buttons: self.addButtons(buttons) # for i,b in zip(range(len(buttons)),buttons): # w = QRadioButton(b) # if toolTips: # w.setToolTip(toolTips[i]) # self.buttons.addButton(w) # self.box.layout().addWidget(w) if callback: QObject.connect(self.buttons, SIGNAL('buttonClicked(int)'), callback) if setChecked: self.setChecked(setChecked)
def get_defaults( spec ): """Return a nested dict of default values from a parsec spec.""" defs = OrderedDict() _populate_spec_defaults( defs, spec ) return defs
def parse(fpath, verbose=False, write_processed_file=False, template_vars=[], template_vars_file=None): """ Parse a nested config file and return a corresponding nested dict. """ # read and process the file (jinja2, include-files, line continuation) flines = read_and_proc(fpath, verbose, template_vars, template_vars_file) # write the processed for suite.rc if it lives in a writable directory if write_processed_file and \ os.access(os.path.dirname(fpath), os.W_OK): fpath_processed = fpath + '.processed' if verbose: print "Writing file " + fpath_processed f = open(fpath_processed, 'w') f.write('\n'.join(flines)) f.close() nesting_level = 0 config = OrderedDict() sect_name = None parents = [] maxline = len(flines) - 1 index = -1 # parse file lines one-by-one while index < maxline: index += 1 line = flines[index] if re.match(_LINECOMMENT, line): # skip full-line comments continue if re.match(_BLANKLINE, line): # skip blank lines continue m = re.match(_HEADING, line) if m: # matched a section heading indent, s_open, sect_name, s_close, comment = m.groups() s_open = s_open.strip() s_close = s_close.strip() nb = len(s_open) if nb != len(s_close): print >> sys.stderr, line raise ParseError('Section bracket mismatch, line ' + str(index + 1)) elif nb == nesting_level: # sibling section parents = parents[:-1] + [sect_name] elif nb == nesting_level + 1: # child section parents = parents + [sect_name] elif nb < nesting_level: # back up one or more levels ndif = nesting_level - nb parents = parents[:-ndif - 1] + [sect_name] else: print >> sys.stderr, line raise ParseError('Section nesting error, line ' + str(index + 1)) nesting_level = nb addsect(config, sect_name, parents[:-1], verbose) else: m = re.match(_KEY_VALUE, line) if m: # matched a key=value item indent, key, val = m.groups() if val.startswith('"""') or val.startswith("'''"): # triple quoted - may be a multiline value val, index = multiline(flines, val, index, maxline) else: m = re.match(_SQ_VALUE, val) if m: # single quoted value: unquote and strip comment val = m.groups()[0] #print 'SINGLE ', key, ' = ', val else: m = re.match(_DQ_VALUE, val) if m: # double quoted value: unquote and strip comment val = m.groups()[0] #print 'DOUBLE ', key, ' = ', val elif val.startswith("'") or val.startswith('"'): # must be a quoted list: unquote and strip comment #print 'QUOTED LIST ', key, ' = ', val if val[0] == "'": reg = _SQ_VALUE else: reg = _DQ_VALUE vals = re.split('\s*,\s*', val) if len(vals) == 1: # not a list print >> sys.stderr, line raise ParseError('Invalid line at ' + str(index + 1)) val = '' for v in vals: m = re.match(reg, v) if m: val += m.groups()[0] + ',' val = val.rstrip(',') else: m = re.match(_UQ_VALUE, val) if m: # unquoted value: strip comment val = m.groups()[0] #print 'UNQUOTED ', key, ' = ', val else: print >> sys.stderr, line raise ParseError('Invalid line at ' + str(index + 1)) addict(config, key, val, parents, verbose) else: # no match print >> sys.stderr, line raise ParseError('Invalid line at ' + str(index + 1)) return config
def __reduce__(self): return self.__class__, (OrderedDict(self), )
def __repr__(self): return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))