def field(self, tuple4, buffer): """Process a bibentry field and return tuple of name, value.""" (tag, start, stop, subtags) = tuple4 str = '' for t in subtags[1][3]: if (t): str += dispatch(self, t, buffer) # concatenate hashed together strings return (dispatch(self, subtags[0], buffer), str)
def name(self, tuple4, buffer): """Prduction function to process a single name in a nameslist""" tag, start, stop, subtags = tuple4 self.names_dicts.append({}) # add new dict to list for part in subtags: dispatch(self, part, buffer) # Create empty lists for missing parts for p in nameparts: if p not in self.names_dicts[-1]: self.names_dicts[-1][p] = []
def entry(self, tuple4, buffer): """Process the bibentry and its children. """ (tag, start, stop, subtags) = tuple4 entry = BibEntry() entry.entry_type = dispatch(self, subtags[0], buffer) entry.citekey = dispatch(self, subtags[1], buffer) for field in subtags[2][3]: #bibfile_logger.debug("entry: ready to add field: "+str(dispatch(self, field, buffer))) k, v = dispatch(self, field, buffer) #:note: entry will force k to lowercase entry[k] = v self.entries.append(entry)
def macro(self, tuple4, buffer): """Process a macro entry and add macros to macro map""" (tag, start, stop, subtags) = tuple4 name, str = dispatch(self, subtags[0], buffer) """ the_type = getString(subtags[0], buffer) if the_type.upper() != 'STRING' : # it looks like a macro, but is not: could be a regular entry with no key lineno = lines(0, start, buffer)+1 bibfile_logger.warning("Entry at line %d has macro syntax, but entry_type is %s" % (lineno , the_type)) if not __strict__: # we can add a dummy key and treat this entry as a regular entry entry = BibEntry() entry.entry_type = dispatch(self, subtags[0], buffer) entry.citekey = 'KEY' # dummy key -- or should we be strict? for field in subtags[1][3] : k,v = dispatch(self, field, buffer) #:note: entry will force k to lowercase entry[k] = v self.entries.append(entry) bibfile_logger.warning("Dummy key added to entry at line %d" % lineno) else : # otherwise it is really a macro entry for field in subtags[1][3]: name, str = dispatch(self, field, buffer) self._macroMap[name] = str """ self._macroMap[name] = str
class MyProcessorClass( DispatchProcessor ): def __init__(self): self.nodes = {} self._id_is_class = False def _generic(self,(tag,start,stop,subtags),buffer,todo): for subtag in subtags: if subtag[0] in todo: dispatch(self,subtag,buffer)
class SLAProcessor(DispatchProcessor): def SLA(self, (tag, start, stop, subtags), buffer): groups = [] general_terms = [] guarantees = [] for x in subtags: if x[0] == 'ID': #Get the id of the SLA iD = buffer[x[1]:x[2]] elif x[0] == 'Group': #Add group to the groups list: NAME, List of Metrics, Goup_Metrics groups.append(dispatch(self, x, buffer)) elif x[0] == 'Term': #Add terms to the SLA: type, source_party, consumer. If BM: BM_name, BOolean IF LM: LM_name, Elements, List_of_Alternative_Elements (list of list os elements). #IF NM: nm_name, Interval(lower, upper bound), OpenClosed(lower, upper), unit general_terms.append(dispatch(self, x, buffer)) elif x[0] == 'Guarantee': guarantees.append(dispatch(self, x, buffer)) SLA = { 'iD': iD, 'groups': groups, 'terms': general_terms, 'guarantees': guarantees } #print 'PARSER2: ',guarantees #print 'ID: ', iD #print 'Groups: ' #for group in groups: #print '\tGroup Name' #print '\t\t', group['Group_Name'] #print '\tGroup Metrics:' #for metric in group['Terms']: #print '\t\tMetric: ', metric #print 'General Terms: ' #for term in general_terms: #print '\tTerms', term return SLA
class MonitoringDataProcessor(DispatchProcessor): def SLAMonitoring(self, (tag, start, stop, subtags), buffer): metrics = [] for x in subtags: if x[0] == 'ID': #Get the id of the SLA sla_iD = buffer[x[1]:x[2]] elif x[0] == 'Metric': metrics.append(dispatch(self, x, buffer)) monitoring = {'iD': sla_iD, 'metrics': metrics} #print 'ID: ', sla_iD #print 'Metrics: ' #for metric in metrics: #print '\t\tMetric: ', metric return monitoring
class BibName(simpleparse.dispatchprocessor.DispatchProcessor): """Processes a bibtex names entry (author, editor, etc) and stores the resulting raw_names_parts. :note: a BibName object should be bibstyle independent. """ def __init__(self, raw_names=None, from_field=None ): #:note: 2006-07-25 add initialization based on raw name """initialize a BibName instance :Parameters: `raw_names` : str the raw names (e.g., unparsed author field of a BibEntry instance) `from_field` : str the entry field for the raw name :note: 2006-08-02 add `from_field` argument (set by `BibEntry.make_names`) """ self.from_field = from_field self.raw_names = raw_names self.names_dicts = [] #populate self.names_dicts from raw_names if raw_names: self.parse_raw_names(raw_names) ############### PRODUCTION FUNCTIONS ####################### # Handle each name by adding new dict to list "names_dicts", then # handle each name part by adding to last dict in names_dict list. def name(self, (tag, start, stop, subtags), buffer): """Prduction function to process a single name in a nameslist""" self.names_dicts.append({}) # add new dict to list for part in subtags: dispatch(self, part, buffer) # Create empty lists for missing parts for p in nameparts: if not self.names_dicts[-1].has_key(p): self.names_dicts[-1][p] = []
def tableline(self, tup, prsbuf): print "###", tup[3][0] self.table = dispatchprocessor.dispatch(self, tup[3][0], prsbuf)
def key( self, (tag,start,stop,subtags), buffer ): """Return the entry key""" return getString((tag,start,stop,subtags), buffer) # macro name def name(self, (tag,start,stop,subtags), buffer ): """Return lookup on name or name if not in map.""" return self._macroMap.get(buffer[start:stop],buffer[start:stop]) def field(self, (tag,start,stop,subtags), buffer ): """Process a bibentry field and return tuple of name, value.""" str = '' for t in subtags[1][3]: if(t) : str += dispatch(self, t, buffer) # concatenate hashed together strings return (dispatch(self, subtags[0], buffer), str) def entry( self, (tag,start,stop,subtags), buffer ): """Process the bibentry and its children. """ entry = BibEntry() entry.entry_type = dispatch(self, subtags[0], buffer) entry.citekey = dispatch(self, subtags[1], buffer) for field in subtags[2][3] : #bibfile_logger.debug("entry: ready to add field: "+str(dispatch(self, field, buffer))) k,v = dispatch(self, field, buffer) #:note: entry will force k to lowercase entry[k] = v self.entries.append(entry)
def ruleline(self, tup, prsbuf): rule = dispatchprocessor.dispatch(self, tup[3][0], prsbuf) fw.fromParser(rule)
return SLA def Group(self, (tag, start, stop, subtags), buffer): terms = [] for x in subtags: if x[0] == 'GroupName': #Get the name of the group #TODO Remove old group name after verifing where it is used group = {'Group_Name': buffer[x[1]:x[2]]} specific = { 'Metric_Name': buffer[x[1]:x[2]], 'Original_Name': buffer[x[1]:x[2]] } group['specific'] = specific elif x[0] == 'Term': terms.append(dispatch(self, x, buffer)) group['Terms'] = terms return group def Term(self, (tag, start, stop, subtags), buffer): for x in subtags: if x[0] == 'Standard_Term': return dispatch(self, x, buffer) if x[0] == 'Group_Metric': return dispatch(self, x, buffer) def Standard_Term(self, (tag, start, stop, subtags), buffer): term = dict() for x in subtags: if x[0] == 'Party':
monitoring = {'iD': sla_iD, 'metrics': metrics} #print 'ID: ', sla_iD #print 'Metrics: ' #for metric in metrics: #print '\t\tMetric: ', metric return monitoring def Metric(self, (tag, start, stop, subtags), buffer): metric = dict() for x in subtags: if x[0] == 'Constraint': metric['Metric_Name'] = buffer[x[1]:x[2]] elif x[0] == 'Type': metric['Value'], metric['type'] = dispatch(self, x, buffer) return metric def Type(self, (tag, start, stop, subtags), buffer): for x in subtags: return dispatch(self, x, buffer) def Boolean(self, (tag, start, stop, subtags), buffer): boolean = buffer[start:stop] if boolean.lower() == 'true': boolean = True elif boolean == 'false': boolean = False else: print 'Error: Boolean must be true or false' return boolean, 'BM'
def read_dnet2(fobj,get_positions=False): """Return CPTs of a Bayesian net in Netica 'dnet' format Does not work if inheritance is being used in the dnet file! @param fobj: File or name of file containing the input @type fobj: File or String @return: A dictionary. Each key is a node name each value is again a dictionary mapping fields to values. Fields typically include 'states', 'probs', etc @rtype: Dictionary """ from simpleparse.parser import Parser from simpleparse.dispatchprocessor import DispatchProcessor, dispatch dnet_bnf_grammar = r''' file := ws*, (bnet, ws*, ";")+, ws* bnet := "bnet", ws*, idname, ws*, "{", ws*, netstmt*, ws*, "}", ws* netstmt := (netequality/bnode/param/visnet/define), ws*, ";", ws* define := "define", whitespace, bnode netequality := netfield, ws*, "=", ws*, value, ws* bnode := "node", whitespace, idname, ws*, list?, "{", ws*, nodestmt*, ws*, "}", ws* list := "(", idname, ")", ws* # only deal with single element lists nodestmt := (nodeequality/visnode), ws*, ";", ws* nodeequality := nodefield, ws*, "=", ws*, value, ws*, ws* param := "param", whitespace, idname, ws*, "{", ws*, paramstmt*, ws*, "}", ws* paramstmt := paramfield, ws*, "=", ws*, value, ws*, ";", ws* visnet := "visual", whitespace, idname, ws*, "{", ws*, vnetstmt*, ws*, "}", ws* vnetstmt := (vnetstmt1/vnetstmt2) vnetstmt1 := vnetfield, ws*, "=", ws*, value, ws*, ";", ws* vnetstmt2 := -"{"+, "{", ws*, vnetstmt1*, ws*, "}", ws*, ";" #workaround visnode := "visual", whitespace, idname, ws*, "{", ws*, vnodestmt*, ws*, "}", ws* vnodestmt := (vnodestmt1/vnodestmt2/vnodestmt3), ws* vnodestmt1 := vnodefield, ws*, "=", ws*, value, ";" vnodestmt2 := vislink, ws*, ";" vnodestmt3 := "font", -[\n]+ # work around, since this value has embedded semi-colons vislink := "link", whitespace, idname, ws*, "{", ws*, vlinkstmt*, ws*, "}", ws* vlinkstmt := vlinkfield, ws*, "=", ws*, value, ws*, ";", ws* netfield := ("numdimensions"/"eqncontext"/"user"/ "title"/"comment"/"author"/"whochanged"/"whenchanged"/ "locked") nodefield := ("kind"/"discrete"/"measure"/"chance"/"numstates"/"states"/ "levels"/"units"/"inputs"/"parents"/"functable"/ "equation"/"probs"/"numcases"/"fading"/"delays"/ "persist"/"position"/"evidcost"/"user"/"title"/ "comment"/"author"/"whochanged"/"whenchanged"/"locked"/ "value"/"evidence"/"belief") paramfield := ("discrete"/"measure"/"numstates"/"states"/"levels"/"units") vnetfield := [A-Za-z0-9_]+ #since lots of extras now! vnodefield := ("center"/"size"/"dispform"/"hidden"/"height"/"links"/"user"/"parts") vlinkfield := ("path"/"labelposn"/"linewidth"/"hidden"/"shareseg"/"user") idname := [A-Za-z0-9_]+ # since 1 is apparently OK as an idname ws := (whitespace/comment1/comment2/comment3) comment1 := "//", -[\n]*, [\n] comment2 := "/*", -"*/"*, "*/" comment3 := "/", hash, -(hash, "/")*, hash, "/" whitespace := [ \t\n\r]+ hash := "\x23" value := -";"+ ''' class MyProcessorClass( DispatchProcessor ): def __init__(self): self.nodes = {} self._id_is_class = False def _generic(self,(tag,start,stop,subtags),buffer,todo): for subtag in subtags: if subtag[0] in todo: dispatch(self,subtag,buffer) def bnet(self,info,buffer): self._generic(info, buffer, ["netstmt"]) def ws(self,info,buffer): pass def netstmt(self,info,buffer): self._generic(info,buffer,["bnode","define"]) def bnode(self,info,buffer): self._generic(info,buffer,["idname","list", "nodestmt"]) def define(self,info,buffer): self._generic(info,buffer,["bnode"]) def list(self,info,buffer): self._id_is_class = True self._generic(info,buffer,["idname"]) self._id_is_class = False def nodestmt(self,info,buffer): self._generic(info,buffer,["nodeequality","visnode"]) def nodeequality(self,(tag,start,stop,subtags),buffer): for subtag in subtags: if subtag[0] == 'nodefield': field = dispatch(self,subtag,buffer) elif subtag[0] == 'value': value = dispatch(self,subtag,buffer) self.nodes[self._current_node][field] = value
def citekey(self, (tag, start, stop, subtags), buffer): """Return the entry's citekey""" return getString((tag, start, stop, subtags), buffer) # macro name def name(self, (tag, start, stop, subtags), buffer): """Return lookup on name or name if not in map.""" return self._macroMap.get(buffer[start:stop], buffer[start:stop]) def field(self, (tag, start, stop, subtags), buffer): """Process a bibentry field and return tuple of name, value.""" str = '' for t in subtags[1][3]: if (t): str += dispatch(self, t, buffer) # concatenate hashed together strings return (dispatch(self, subtags[0], buffer), str) def entry(self, (tag, start, stop, subtags), buffer): """Process the bibentry and its children. """ entry = BibEntry() entry.entry_type = dispatch(self, subtags[0], buffer) entry.citekey = dispatch(self, subtags[1], buffer) for field in subtags[2][3]: #bibfile_logger.debug("entry: ready to add field: "+str(dispatch(self, field, buffer))) k, v = dispatch(self, field, buffer) #:note: entry will force k to lowercase entry[k] = v self.entries.append(entry)
Comments = [] # # Parsing infrastructure # class ACLProcessor(DispatchProcessor): pass def default_processor(self, (tag, start, stop, subtags), buffer): if not subtags: return buffer[start:stop] elif len(subtags) == 1: return dispatch(self, subtags[0], buffer) else: return dispatchList(self, subtags, buffer) def make_nondefault_processor(action): if callable(action): def processor(self, (tag, start, stop, subtags), buffer): if tag in subtagged: results = [ getattr(self, subtag[0])(subtag, buffer) for subtag in subtags ] return action(strip_comments(results)) else:
# Temporary resting place for comments, so the rest of the parser can # ignore them. Yes, this makes the library not thread-safe. Comments = [] # # Parsing infrastructure # class ACLProcessor(DispatchProcessor): pass def default_processor(self, (tag, start, stop, subtags), buffer): if not subtags: return buffer[start:stop] elif len(subtags) == 1: return dispatch(self, subtags[0], buffer) else: return dispatchList(self, subtags, buffer) def make_nondefault_processor(action): if callable(action): def processor(self, (tag, start, stop, subtags), buffer): if tag in subtagged: results = [getattr(self, subtag[0])(subtag, buffer) for subtag in subtags] return action(strip_comments(results)) else: return action(buffer[start:stop]) else: def processor(self, (tag, start, stop, subtags), buffer): return action