def rewriteDescription(filename): """read and rewrite the description file, adds extra info from parsing""" nd = open("out/newdescription.txt", 'w') #path to new description f = open(filename, 'r') startWithConnectors = True cont = None for line in f: sline = line.strip() if startWithConnectors: #set to false when done conn = lineparser.parse(Connector, sline) if conn is not None: nd.write("%s\n" % str(connectors[connectors.index(conn)])) elif sline[0] == '*' and sline == '*ENDCONNECTORLIST': startWithConnectors = False nd.write(line) elif sline[0:4] == "JACK": #temporarily replace placeholder numbers with mate's, for writing jack = cont.jacks[cont.jacks.index(lineparser.parse(Jack, sline))] oldnumber = jack.number oldmnumber = jack.mate.number if jack.placeholder: jack.number = jack.mate.number if jack.mate.placeholder: jack.mate.number = jack.number nd.write(" %s\n" % str(jack)) jack.number = oldnumber jack.mate.number = oldmnumber elif sline[0:4] == "LINE": line = lineparser.parse(Line, sline) line = cont.lines[cont.lines.index(line)] #temporarily replace jack numbers with global references, for writing newjacknums = [] for jacknum in line.jacknums: jack = cont.jacks[cont.jacks.index(jacknum)] if jack.placeholder: jack = jack.mate newjacknums.append("&J%d" % jack.number) oldjacknums = line.jacknums line.jacknums = newjacknums nd.write(" %s\n" % str(line)) line.jacknums = oldjacknums elif sline[0:5] == "CABLE": cont = containers[containers.index(lineparser.parse(Cable, sline))] nd.write("%s\n" % str(cont)) elif sline == "" or sline[0] == '#': #blank or comment, just reprint nd.write(line) else: #COMPONENT cont = containers[containers.index( lineparser.parse(Component, sline))] nd.write("%s\n" % str(cont))
def rewriteDescription(filename): """read and rewrite the description file, adds extra info from parsing""" nd = open("out/newdescription.txt", 'w') #path to new description f = open(filename, 'r') startWithConnectors = True cont = None for line in f: sline = line.strip() if startWithConnectors: #set to false when done conn = lineparser.parse(Connector, sline) if conn is not None: nd.write("%s\n" % str(connectors[connectors.index(conn)])) elif sline[0] == '*' and sline == '*ENDCONNECTORLIST': startWithConnectors = False nd.write(line) elif sline[0:4] == "JACK": #temporarily replace placeholder numbers with mate's, for writing jack = cont.jacks[cont.jacks.index(lineparser.parse(Jack, sline))] oldnumber = jack.number oldmnumber = jack.mate.number if jack.placeholder: jack.number = jack.mate.number if jack.mate.placeholder: jack.mate.number = jack.number nd.write(" %s\n" % str(jack)) jack.number = oldnumber jack.mate.number = oldmnumber elif sline[0:4] == "LINE": line = lineparser.parse(Line, sline) line = cont.lines[cont.lines.index(line)] #temporarily replace jack numbers with global references, for writing newjacknums = [] for jacknum in line.jacknums: jack = cont.jacks[cont.jacks.index(jacknum)] if jack.placeholder: jack = jack.mate newjacknums.append("&J%d" % jack.number) oldjacknums = line.jacknums line.jacknums = newjacknums nd.write(" %s\n" % str(line)) line.jacknums = oldjacknums elif sline[0:5] == "CABLE": cont = containers[containers.index(lineparser.parse(Cable, sline))] nd.write("%s\n" % str(cont)) elif sline == "" or sline[0] == '#': #blank or comment, just reprint nd.write(line) else: #COMPONENT cont = containers[containers.index(lineparser.parse(Component, sline))] nd.write("%s\n" % str(cont))
def parse_and_compare(self, path: str, fields, exp_values): exp_nrows = len(exp_values) exp_ncols = len(fields) pr = lp.parse(fields, path) assert len(pr) == exp_ncols assert len(pr[0]) == exp_nrows for row in range(exp_nrows): for col in range(exp_ncols): if not pr[col][row] == exp_values[row][col]: if fields[col].ty in FLOAT_TYPES and \ abs(pr[col][row] - exp_values[row][col]) < 0.001: pass else: print( f"{pr[col][row]} != {exp_values[row][col]}; dtype={fields[col].ty}" ) return False return True
def sedparser(filename): """main function allows calls from interactive prompt, or extra cleverness""" startWithConnectors = True #connector list at top. make overridable by args? f = open(filename, 'r') #lists of used cable and jack numbers used_cable_nums = [] used_jack_nums = [] #main loop, red each line of the description file in turn linecount = 0 for line in f: linecount += 1 #print "On line", linecount line = line.strip() #CONNECTORS if startWithConnectors: #set to false when done conn = lineparser.parse(Connector, line) if conn is not None: #successful if conn in connectors: raise Failure("connector exists", linecount) connectors.append(conn) try: mate = connectors[connectors.index(conn.matetype)] #check that each gender mate is reciprocated, or unused if conn.gendersMatch(mate): conn.mate = mate mate.mate = conn else: raise Failure("Incompatible gender mating", linecount) except ValueError: pass #mate not found elif line[0] == '*' and line == '*ENDCONNECTORLIST': startWithConnectors = False for c in connectors: if c.mate is None: print "Warning: unmated connector", c.type print "Info: done parsing connectors, found", len(connectors) elif line != "": raise Failure("Unrecognized connector", linecount) elif line[0:4] == "JACK": jack = lineparser.parse(Jack, line) if jack is None: raise Failure("Bad jack line", linecount) if jack in containers[-1].jacks: raise Failure("non-unique jack identifier", linecount) try: jack.conn = connectors[connectors.index(jack.conn_str[:-2])] if jack.conn.genders[jack.gender] == 'X': raise Failure("Invalid connector gender", linecount) except ValueError: raise Failure("Non-existant connector type", linecount) if jack.internal and hasattr(containers[-1], 'p2p'): raise Failure("Cables can't have internal jacks", linecount) jack.location = containers[-1] containers[-1].jacks.append(jack) try: mateJack(jack) except Failure, err: raise Failure(err.errstr, linecount) if jack.number > 0: if not jack.number in used_jack_nums: used_jack_nums.append(jack.number) elif not jack.mate or not jack.placeholder: raise Failure("repeated global jack number %d"%jack.number, linecount) if jack.cable and jack.cable.number > 0: if not jack.cable.number in used_cable_nums: used_cable_nums.append(jack.cable.number) elif not jack.mate: raise Failure("repeated cable number %d"%jack.cable.number, linecount) elif line[0:4] == "LINE": line = lineparser.parse(Line, line) if line is None: raise Failure("Bad line line", linecount) line.owner = containers[-1] try: addPins(line) #catch failures and reraise with line number except Failure, err: raise Failure(err.errstr, linecount) elif line[0:5] == "CABLE": cable = lineparser.parse(Cable, line) if cable is None: raise Failure("Bad cable line", linecount) if cable in containers: raise Failure("cable exists: rename or use re-edit line", linecount) containers.append(cable) try: expected.remove(cable) except ValueError: pass if cable.number > 0: if not cable.number in used_cable_nums: used_cable_nums.append(cable.number) else: raise Failure("repeated cable number %d"%cable.number, linecount) elif line != "" and line[0] != '#': #COMPONENT comp = lineparser.parse(Component, line) if comp is None: #check if line is a re-edit command try: containers.append(containers.pop(containers.index(line))) print "Info: re-editing", line continue except ValueError: raise Failure("nothing found to re-edit", linecount) if comp in containers: raise Failure("component exists: use re-edit line?", linecount) containers.append(comp) try: expected.remove(comp) except ValueError: pass if comp.partOf is not None and comp.partOf not in containers \ and comp.partOf not in expected: expected.append(Component(comp.partOf, "expected", None)) #print stats, assign cable and jack numbers, check pins and mating for part in expected: print part.ref if len(expected) > 0: raise Failure("above parts used but not declared", -1) print "Info: done parsing, found", len(containers), "components and cables:" sf = open("out/stats.txt", 'w') #print stats to statfile sf.write("%10s%10s%10s\n" % ("Part", "Jacks", "Lines")) all_mated = True cable_count = 1 jack_count = 1 for cont in containers: sf.write("%10s%10s%10s\n" % (cont.ref, len(cont.jacks), len(cont.lines))) #assign number to unnumbered cables if hasattr(cont, 'number') and cont.number <= 0: while cable_count in used_cable_nums: cable_count += 1 cont.number = cable_count cable_count += 1 for ijack in cont.jacks: #check number of pins if ijack.conn.count > 0 and len(ijack.pins) > ijack.conn.count: raise Failure("Jack %s jas too many pins: %d/%d"\ %(ijack.ref,len(ijack.pins),ijack.conn.count)) #check for mating if ijack.mate is None: print ("\tJack %s of %s unmated" % (ijack.ref, cont.ref)) all_mated = False #assign number to unnumbered jacks if ijack.number <= 0: while jack_count in used_jack_nums: jack_count += 1 ijack.number = jack_count jack_count += 1 #assign number to unnumbered p2p cables if ijack.cable and ijack.cablemaster and ijack.cable.number <= 0: while cable_count in used_cable_nums: cable_count += 1 ijack.cable.number = cable_count cable_count += 1 if not all_mated: raise Failure("Not all jacks mated, check for indistinguishable mates")
def sedparser(filename): """main function allows calls from interactive prompt, or extra cleverness""" startWithConnectors = True #connector list at top. make overridable by args? f = open(filename, 'r') #lists of used cable and jack numbers used_cable_nums = [] used_jack_nums = [] #main loop, red each line of the description file in turn linecount = 0 for line in f: linecount += 1 #print "On line", linecount line = line.strip() #CONNECTORS if startWithConnectors: #set to false when done conn = lineparser.parse(Connector, line) if conn is not None: #successful if conn in connectors: raise Failure("connector exists", linecount) connectors.append(conn) try: mate = connectors[connectors.index(conn.matetype)] #check that each gender mate is reciprocated, or unused if conn.gendersMatch(mate): conn.mate = mate mate.mate = conn else: raise Failure("Incompatible gender mating", linecount) except ValueError: pass #mate not found elif line[0] == '*' and line == '*ENDCONNECTORLIST': startWithConnectors = False for c in connectors: if c.mate is None: print "Warning: unmated connector", c.type print "Info: done parsing connectors, found", len(connectors) elif line != "": raise Failure("Unrecognized connector", linecount) elif line[0:4] == "JACK": jack = lineparser.parse(Jack, line) if jack is None: raise Failure("Bad jack line", linecount) if jack in containers[-1].jacks: raise Failure("non-unique jack identifier", linecount) try: jack.conn = connectors[connectors.index(jack.conn_str[:-2])] if jack.conn.genders[jack.gender] == 'X': raise Failure("Invalid connector gender", linecount) except ValueError: raise Failure("Non-existant connector type", linecount) if jack.internal and hasattr(containers[-1], 'p2p'): raise Failure("Cables can't have internal jacks", linecount) jack.location = containers[-1] containers[-1].jacks.append(jack) try: mateJack(jack) except Failure, err: raise Failure(err.errstr, linecount) if jack.number > 0: if not jack.number in used_jack_nums: used_jack_nums.append(jack.number) elif not jack.mate or not jack.placeholder: raise Failure( "repeated global jack number %d" % jack.number, linecount) if jack.cable and jack.cable.number > 0: if not jack.cable.number in used_cable_nums: used_cable_nums.append(jack.cable.number) elif not jack.mate: raise Failure( "repeated cable number %d" % jack.cable.number, linecount) elif line[0:4] == "LINE": line = lineparser.parse(Line, line) if line is None: raise Failure("Bad line line", linecount) line.owner = containers[-1] try: addPins(line) #catch failures and reraise with line number except Failure, err: raise Failure(err.errstr, linecount)
elif not jack.mate: raise Failure( "repeated cable number %d" % jack.cable.number, linecount) elif line[0:4] == "LINE": line = lineparser.parse(Line, line) if line is None: raise Failure("Bad line line", linecount) line.owner = containers[-1] try: addPins(line) #catch failures and reraise with line number except Failure, err: raise Failure(err.errstr, linecount) elif line[0:5] == "CABLE": cable = lineparser.parse(Cable, line) if cable is None: raise Failure("Bad cable line", linecount) if cable in containers: raise Failure("cable exists: rename or use re-edit line", linecount) containers.append(cable) try: expected.remove(cable) except ValueError: pass if cable.number > 0: if not cable.number in used_cable_nums: used_cable_nums.append(cable.number) else: raise Failure("repeated cable number %d" % cable.number, linecount)