def makeHumanReadable(self, remapTable = {}): # Takes element tree object, and returns a modified version in which all # non-printable 'text' fields (which may contain numeric data or binary strings) # are replaced by printable strings # # Property values in original tree may be mapped to alternative (more user-friendly) # reportable values using a rempapTable, which is a nested dictionary. # Destination tree: copy of source (?? think this is just a reference, Johan) for elt in self.iter(): # Text field of this element textIn = elt.text # Tag name tag = elt.tag # Step 1: replace property values by values defined in enumerationsMap, # if applicable try: # If tag is in enumerationsMap, replace property values parameterMap = remapTable[tag] try: # Map original property values to values in dictionary remappedValue = parameterMap[textIn] except KeyError: # If value doesn't match any key: use original value instead remappedValue = textIn except KeyError: # If tag doesn't match any key in enumerationsMap, use original value remappedValue = textIn # Step 2: convert all values to text strings if remappedValue != None: # Data type textType = type(remappedValue) # Convert text field, depending on type if textType == bytes: textOut = strToText(remappedValue) else: textOut = str(remappedValue) # Update output tree elt.text = textOut
def makeHumanReadable(self, remapTable={}): # Takes element tree object, and returns a modified version in which all # non-printable 'text' fields (which may contain numeric data or binary strings) # are replaced by printable strings # # Property values in original tree may be mapped to alternative (more user-friendly) # reportable values using a rempapTable, which is a nested dictionary. # Destination tree: copy of source (?? think this is just a reference, Johan) for elt in self.iter(): # Text field of this element textIn = elt.text # Tag name tag = elt.tag # Step 1: replace property values by values defined in enumerationsMap, # if applicable try: # If tag is in enumerationsMap, replace property values parameterMap = remapTable[tag] try: # Map original property values to values in dictionary remappedValue = parameterMap[textIn] except KeyError: # If value doesn't match any key: use original value instead remappedValue = textIn except KeyError: # If tag doesn't match any key in enumerationsMap, use original value remappedValue = textIn # Step 2: convert all values to text strings if remappedValue != None: # Data type textType = type(remappedValue) # Convert text field, depending on type if textType == bytes: textOut = strToText(remappedValue) else: textOut = str(remappedValue) # Update output tree elt.text = textOut
def test_strToText_fail(self): self.assertEqual(bc.strToText(b'\x0e\x0f\x10\x11\x0e\x0f\x10\x10'), "")
def test_strToText(self): self.assertEqual(bc.strToText(b'\x44\x41\x44\x44\x59'), "DADDY")