def build_input_xml(self): """Builds XML data for input modules and writes to file """ path = self.project_path + ".L5X" xml = ET.parse(path) root = xml.getroot() modules = root.find("./Controller/Modules") new_module = ET.SubElement(modules, 'Module') new_module.attrib["Name"] = self.module_name new_module.attrib["CatalogNumber"] = self.input_module_type new_module.attrib["Vendor"] = "1" new_module.attrib["ProductType"] = "7" new_module.attrib["ProductCode"] = "11" new_module.attrib["Major"] = self.major_rev new_module.attrib["Minor"] = self.minor_rev new_module.attrib["ParentModule"] = "Local" new_module.attrib["ParentModPortID"] = "1" new_module.attrib["Inhibited"] = "false" new_module.attrib["MajorFault"] = "false" new_module_ekey = ET.SubElement(new_module, 'EKey') new_module_ekey.attrib["State"] = self.ekey_state new_module_ports = ET.SubElement(new_module, 'Ports') new_module_port_data = ET.SubElement(new_module_ports, 'Port') new_module_port_data.attrib["ID"] = "1" new_module_port_data.attrib["Address"] = self.bp_slot new_module_port_data.attrib["Type"] = "ICP" new_module_port_data.attrib["Upstream"] = "true" indent(root) xml.write(path, encoding="utf-8", xml_declaration=True)
def _doSaveProject(self, fileName): print fileName node = self.xmlHandler.find("graphics") indent(node) node = self.xmlHandler.find("sounds") indent(node) self.xmlHandler.write(fileName, encoding="utf-8", xml_declaration=True) self.export()
def print_module_info(module): iprint(module.name) with indent(): for x, segment in enumerate(module.segments): iprint("Segment {}".format(x + 1)) with indent(): iprint("Start: 0x{:x}".format(segment.start)) iprint("Size: 0x{:x} bytes".format(segment.size)) iprint("Attributes: 0x{:x} ({})".format( segment.attr, str_attr[segment.attr & 0xF])) iprint("Alignment: 0x{:x}".format(segment.align))
def trycatchblock(*expressions): arrayList = ["me","myself","and","I"] linkedList = LinkedList(arrayList) exceptions = [] for expr in expressions: try: exec expr in locals() except Exception, why: exceptions.append(why) from indent import indent,wrap_onspace print indent([("Expression","Exception class","Message")] + \ zip(expressions, map(lambda ex:ex.__class__,exceptions),exceptions), hasHeader=True, separateRows=True, wrapfunc=lambda text:wrap_onspace(str(text),28))
def __prettyPrint(self, required, optional): rows = [] for optionKind in (required, optional): # column[0]: the option's switch(es); column[1]: the option's description columns = [[], []] for option in optionKind: buffers = [ ', '.join(map(_dashAdder, option.switches)), option.description ] if isinstance(option, Option): if len(option.switches) > 1: buffers[0] = '(%s)' % buffers[0] arg = '<%s>' % option.argName if option.optionalArg: arg = '[%s]' % arg buffers[0] = '%s %s' % (buffers[0], arg) if option.defaultVal != None: buffers[1] = '%s [default=%s]' % (buffers[1], option.defaultVal) for i in xrange(0, len(columns)): columns[i].append(buffers[i]) # create rows from columns and sort them # ignore leading non-alphabetic characters for sorting m = re.compile(r'^\W*') tmprows = [[(m.sub('', s).lower(), s) for s in row] for row in zip(*columns)] tmprows.sort() rows += [[t[1] for t in row] for row in tmprows] optionRows = indent.indent(rows, delim=' ', justify="left", prefix=4 * ' ').splitlines() return splitByLengths(optionRows, [len(required), len(optional)])
def __prettyPrint(self,required,optional): rows = [] for optionKind in (required,optional): # column[0]: the option's switch(es); column[1]: the option's description columns = [[],[]] for option in optionKind: buffers = [', '.join(map(_dashAdder,option.switches)), option.description] if isinstance(option,Option): if len(option.switches) > 1: buffers[0] = '(%s)' % buffers[0] arg = '<%s>' % option.argName if option.optionalArg: arg = '[%s]' % arg buffers[0] = '%s %s' % (buffers[0],arg) if option.defaultVal != None: buffers[1] = '%s [default=%s]' % (buffers[1], option.defaultVal) for i in xrange(0,len(columns)): columns[i].append(buffers[i]) # create rows from columns and sort them # ignore leading non-alphabetic characters for sorting m = re.compile(r'^\W*') tmprows = [[(m.sub('',s).lower(),s) for s in row] for row in zip(*columns)] tmprows.sort() rows += [ [t[1] for t in row] for row in tmprows] optionRows = indent.indent(rows, delim=' ', justify="left", prefix=4*' ').splitlines() return splitByLengths(optionRows, [len(required),len(optional)])
def build_tags_xml(self): """Builds XML data for tags and writes to file """ path = self.project_path + ".L5X" xml = ET.parse(path) root = xml.getroot() modules = root.find("./Controller/Tags") new_tag = ET.SubElement(modules, 'Tag') new_tag.attrib["Name"] = self.tag_name new_tag.attrib["TagType"] = self.tag_type new_tag.attrib["DataType"] = self.data_type new_tag.attrib["Radix"] = self.radix new_tag.attrib["Constant"] = self.constant new_tag.attrib["ExternalAccess"] = self.external_access indent(root) xml.write(path, encoding="utf-8", xml_declaration=True)
def indent_line(edit, view, line, options): line_str = view.substr(line) current_indent = indent.current_indent(line_str) new_indent = indent.indent(view, line.begin(), options) if not current_indent == new_indent: view.replace( edit, sublime.Region(line.begin(), line.begin() + current_indent), " " * new_indent)
def indent_line(edit, view, line, options): line_str = view.substr(line) current_indent = indent.current_indent(line_str) new_indent = indent.indent(view, line.begin(), options) if not current_indent == new_indent: view.replace(edit, sublime.Region(line.begin(), line.begin() + current_indent), " " * new_indent)
def build_prog_xml(self): """Builds XML data for programs and writes to file """ path = self.project_path + ".L5X" xml = ET.parse(path) root = xml.getroot() modules = root.find("./Controller/Programs") new_prog = ET.SubElement(modules, 'Program') new_prog.attrib["Name"] = self.prog_name new_prog.attrib["TestEdits"] = "false" new_prog.attrib["MainRoutineName"] = self.main_rout_name new_prog.attrib["Disabled"] = "false" new_prog.attrib["UseAsFolder"] = "false" new_prog_routines = ET.SubElement(new_prog, "Routines") new_prog_base_routine = ET.SubElement(new_prog_routines, "Routine") new_prog_base_routine.attrib["Name"] = "MainRoutine" new_prog_base_routine.attrib["Type"] = "RLL" indent(root) xml.write(path, encoding="utf-8", xml_declaration=True)
def print_thread_info(thread): iprint(thread.name) with indent(): iprint("ID: 0x{:x}".format(thread.uid)) iprint("Stop reason: 0x{:x} ({})".format( thread.stop_reason, str_stop_reason[thread.stop_reason])) iprint("Status: 0x{:x} ({})".format(thread.status, str_status[thread.status])) pc = core.get_address_notation("PC", thread.pc) iprint(pc) if not pc.is_located(): iprint(core.get_address_notation("LR", thread.regs.gpr[14]))
def print_thread_info(thread): iprint(thread.name) with indent(): iprint("ID: 0x{:x}".format(thread.uid)) iprint("Stop reason: 0x{:x} ({})".format( thread.stop_reason, str_stop_reason[thread.stop_reason])) iprint("Status: 0x{:x} ({})".format(thread.status, str_status[thread.status])) module, segment, addr = core.vaddr_to_offset(thread.pc) if module: iprint("PC: 0x{:x} ({}@{} + 0x{:x})".format( thread.pc, module.name, segment.num, addr)) else: module, segment, addr = core.vaddr_to_offset(thread.regs.gpr[14]) iprint("PC: 0x{:x} ".format(thread.pc)) if module: iprint("LR: 0x{:x} ({}@{} + 0x{:x})".format( thread.regs.gpr[14], module.name, segment.num, addr))
def main(): global core elf = ElfParser(argv[2]) core = CoreParser(argv[1]) isPC = True # iprint("=== MODULES ===") # with indent(): # for module in core.modules: # print_module_info(module) # iprint() iprint("=== THREADS ===") crashed = [] with indent(): for thread in core.threads: if thread.stop_reason != 0: crashed.append(thread) print_thread_info(thread) iprint() for thread in crashed: iprint('=== THREAD "{}" <0x{:x}> CRASHED ({}) ==='.format( thread.name, thread.uid, str_stop_reason[thread.stop_reason])) module, segment, addr = core.vaddr_to_offset(thread.pc) if module and module.name.endswith(".elf"): iprint() iprint('DISASSEMBLY AROUND PC: 0x{:x}:'.format(thread.pc)) elf.disas_around_addr(addr) module, segment, addr = core.vaddr_to_offset(thread.regs.gpr[14]) if module and module.name.endswith(".elf"): iprint() iprint('DISASSEMBLY AROUND LR: 0x{:x}:'.format( thread.regs.gpr[14])) elf.disas_around_addr(addr) isPC = False else: iprint("DISASSEMBLY IS NOT AVAILABLE") iprint("REGISTERS:") with indent(): for x in range(14): reg = reg_names.get(x, "R{}".format(x)) iprint("{}: 0x{:x}".format(reg, thread.regs.gpr[x])) if module and isPC: reg = reg_names.get(14, "R{}".format(14)) iprint("{}: 0x{:x}".format(reg, thread.regs.gpr[14])) iprint("PC: 0x{:x} ({}@{} + 0x{:x})".format( thread.pc, module.name, segment.num, addr)) elif module: reg = reg_names.get(14, "R{}".format(14)) iprint("{}: 0x{:x} ({}@{} + 0x{:x})".format( reg, thread.regs.gpr[14], module.name, segment.num, addr)) iprint("PC: 0x{:x} ".format(thread.pc)) else: reg = reg_names.get(14, "R{}".format(14)) iprint("{}: 0x{:x} ".format(reg, thread.regs.gpr[14])) iprint("PC: 0x{:x} ".format(thread.pc)) iprint() iprint("STACK CONTENTS AROUND SP:") with indent(): sp = thread.regs.gpr[13] for x in range(-16, 0x18): addr = 4 * x + sp data = core.read_vaddr(addr, 4) if data: data = u32(data, 0) prefix = " " if addr == sp: prefix = "SP =>" suffix = "" module, segment, off = core.vaddr_to_offset(data) if module: suffix = "=> {}@{} + 0x{:x}".format( module.name, segment.num, off) if module.name.endswith(".elf") and segment.num == 1: suffix += " => {}".format(elf.addr2line(off)) iprint("{} 0x{:x}: 0x{:x} {}".format( prefix, addr, data, suffix))
def saveProject(self): if self.path == None: return node = self.xmlHandler.find("graphics") indent(node) self.xmlHandler.write(self.path)
def saveProjectAs(self, fileName): node = self.xmlHandler.find("graphics") indent(node) self.xmlHandler.write(fileName)
def test_indents_simple(self): data = "{}" result = indent.indent(data) print("====\n", result, "\n====") self.assertEqual(result, "{\n \n}\n")
def test_indents_multiple(self): data = "{([hello])}" result = indent.indent(data) print("====\n", result, "\n====") self.assertEqual( result, "{\n (\n [\n hello\n ]\n \n )\n \n}\n")
def test_breaks_splitters(self): data = "[fish, waffles, cake]" result = indent.indent(data) print("====\n", result, "\n====") self.assertEqual(result, "[\n fish,\n waffles,\n cake\n]\n")
def main(): global core parser = ArgumentParser() parser.add_argument("-s", "--stack-size-to-print", dest="stacksize", type=int, help="Number of addresses of the stack to print", metavar="SIZE", default=24) parser.add_argument("corefile") parser.add_argument("elffile") args = parser.parse_args() stackSize = args.stacksize elf = ElfParser(args.elffile) core = CoreParser(args.corefile) # iprint("=== MODULES ===") # with indent(): # for module in core.modules: # print_module_info(module) # iprint() iprint("=== THREADS ===") crashed = [] with indent(): for thread in core.threads: if thread.stop_reason != 0: crashed.append(thread) print_thread_info(thread) iprint() for thread in crashed: iprint('=== THREAD "{}" <0x{:x}> CRASHED ({}) ==='.format( thread.name, thread.uid, str_stop_reason[thread.stop_reason])) pc = core.get_address_notation('PC', thread.pc) pc.print_disas_if_available(elf) lr = core.get_address_notation('LR', thread.regs.gpr[14]) lr.print_disas_if_available(elf) iprint("REGISTERS:") with indent(): for x in range(14): reg = reg_names.get(x, "R{}".format(x)) iprint("{}: 0x{:x}".format(reg, thread.regs.gpr[x])) iprint(pc) iprint(lr) iprint() iprint("STACK CONTENTS AROUND SP:") with indent(): sp = thread.regs.gpr[13] for x in range(-16, stackSize): addr = 4 * x + sp data = core.read_vaddr(addr, 4) if data: data = u32(data, 0) prefix = " " if addr == sp: prefix = "SP =>" data_notation = core.get_address_notation( "{} 0x{:x}".format(prefix, addr), data) iprint(data_notation)
def csvToXml(): infile = S.argv[1] csvfile = open(infile, 'r') csvlist = [x.strip() for x in csvfile.readlines()] csvfile.close() AnnoDict = C.defaultdict(list) for csv in csvlist: print(csv) with open(csv, 'r') as file: fileReader = CS.reader(file, delimiter=';', quotechar='|') print('opened ' + csv) header = next(fileReader) # note Python 3 & 2 next differences while not 'sentence id' in header: next(header) IdCol = None AnnoCols = {} for (i, col) in enumerate(header): if col.endswith(' id'): # get id column index print('ID column found!') IdCol = i if col.endswith(' class'): # get annotation column indices AnnoCols[i] = col[:-6] for row in fileReader: AnnotationID = row[IdCol] # sentence id for anno_col in AnnoCols.keys(): if row[anno_col].strip(): AnnoDict[AnnotationID].append( (AnnoCols[anno_col], row[anno_col])) print(AnnoDict) # print the resulting annotation dictionary script_dir = os.path.dirname(__file__) rel_xml_path = "xmlsource.txt" abs_xml_path = os.path.join(script_dir, rel_xml_path) xmlfile = open(abs_xml_path, "r") xmllist = xmlfile.read().splitlines() for xml in xmllist: tree = ET.parse(xml) print(xml + " parsed to tree") root = tree.getroot() for node in root.findall('.//Sentence'): if node.attrib['sentence_id']: #Sentence node with sentence_id found! if node.attrib['sentence_id'] in AnnoDict: print("AnnoDict has same attr:") print(node.attrib['sentence_id']) #id shared with Dict print(AnnoDict[node.attrib['sentence_id']]) CurDict = AnnoDict[node.attrib['sentence_id']] for Tupl in CurDict: a, b = Tupl print(a + b) sub = ET.SubElement(node, 'Annotation') sub.set('type', a) sub.text = b print("finished one loop of xml writing") #imported function, indent IND.indent(root) xmlName = xml.split("\\")[-1] print(xmlName) res_path = os.path.join('Results', xmlName) tree.write(res_path, encoding="UTF-8") print(res_path) xmlfile.close() print("xmlfile closed")