Returns a string representation of the structure.
(self, indentation=0)
| 1053 | ) |
| 1054 | |
| 1055 | def dump(self, indentation=0): |
| 1056 | """Returns a string representation of the structure.""" |
| 1057 | |
| 1058 | dump = [] |
| 1059 | |
| 1060 | dump.append("[{0}]".format(self.name)) |
| 1061 | |
| 1062 | printable_bytes = [ |
| 1063 | ord(i) for i in string.printable if i not in string.whitespace |
| 1064 | ] |
| 1065 | |
| 1066 | # Refer to the __set_format__ method for an explanation |
| 1067 | # of the following construct. |
| 1068 | for keys in self.__keys__: |
| 1069 | for key in keys: |
| 1070 | |
| 1071 | val = getattr(self, key) |
| 1072 | if isinstance(val, (int, long)): |
| 1073 | if key.startswith("Signature_"): |
| 1074 | val_str = "{:<8X}".format(val) |
| 1075 | else: |
| 1076 | val_str = "0x{:<8X}".format(val) |
| 1077 | if key == "TimeDateStamp" or key == "dwTimeStamp": |
| 1078 | try: |
| 1079 | val_str += " [%s UTC]" % time.asctime(time.gmtime(val)) |
| 1080 | except ValueError: |
| 1081 | val_str += " [INVALID TIME]" |
| 1082 | else: |
| 1083 | val_str = bytearray(val) |
| 1084 | if key.startswith("Signature"): |
| 1085 | val_str = "".join( |
| 1086 | ["{:02X}".format(i) for i in val_str.rstrip(b"\x00")] |
| 1087 | ) |
| 1088 | else: |
| 1089 | val_str = "".join( |
| 1090 | [ |
| 1091 | chr(i) |
| 1092 | if (i in printable_bytes) |
| 1093 | else "\\x{0:02x}".format(i) |
| 1094 | for i in val_str.rstrip(b"\x00") |
| 1095 | ] |
| 1096 | ) |
| 1097 | |
| 1098 | dump.append( |
| 1099 | "0x%-8X 0x%-3X %-30s %s" |
| 1100 | % ( |
| 1101 | self.__field_offsets__[key] + self.__file_offset__, |
| 1102 | self.__field_offsets__[key], |
| 1103 | key + ":", |
| 1104 | val_str, |
| 1105 | ) |
| 1106 | ) |
| 1107 | |
| 1108 | return dump |
| 1109 | |
| 1110 | def dump_dict(self): |
| 1111 | """Returns a dictionary representation of the structure.""" |