def build_map_index(infile, outfile): """ Produces list of map cell values, and cell offsets where they first appear """ hex_content = infile.read() binary_content = hextream.decode(hex_content) map_index = {} for position, value in enumerate(binary_content): if not value in map_index: map_index[value] = position csv_map_index = "\n".join([ "{},{}".format(value, position) for value, position in sorted(map_index.items()) ]) outfile.write("value,position\n{}\n".format(csv_map_index))
def main(): """ Program entry point """ parser = argparse.ArgumentParser( description= "Strip comments and normalize whitespace for Hextream format content") parser.add_argument('infile', nargs='?', type=argparse.FileType('r', encoding="UTF-8"), help="Name of file with content to be cleaned", default=sys.stdin) parser.add_argument( 'outfile', nargs='?', type=argparse.FileType('w', encoding="UTF-8"), help="Name of file in which to write the cleaned content", default=sys.stdout) args = parser.parse_args() hex_content = args.infile.read() binary_content = hextream.decode(hex_content) hex_content = hextream.encode(binary_content) args.outfile.write(hex_content)
def main(): """ Program entry point """ parser = argparse.ArgumentParser( description="Packs/unpacks Hextream content to/from the Varipacker format" ) commands = parser.add_mutually_exclusive_group(required=True) commands.add_argument("-p", "--pack", action="store_true", help="Pack Hextream content into Varipacker format") commands.add_argument("-u", "--unpack", action="store_true", help="Unpack Varipacker content into Hextream format") parser.add_argument('infile', nargs='?', type=argparse.FileType('r', encoding="UTF-8"), help="Name of file with content to be packed/unpacked", default=sys.stdin) parser.add_argument('outfile', nargs='?', type=argparse.FileType('w', encoding="UTF-8"), help="Name of file in which to write packed/unpacked content", default=sys.stdout) parser.add_argument("-c", "--comment", type=str, help="Prepend the output with specified comment string") parser.add_argument("-n", "--omit-newline", action="store_true", help="The ending newline character(s) will be omitted from the output") args = parser.parse_args() source_content = args.infile.read() if args.pack: binary_content = hextream.decode(source_content) output_content = varipacker.encode(binary_content) else: binary_content = varipacker.decode(varipacker.distill(source_content)) output_content = hextream.encode(binary_content) if args.comment: args.outfile.write("# {}\n".format(args.comment)) args.outfile.write(output_content) if not args.omit_newline: args.outfile.write("\n")
def test_decode_with_prefixes(): """ decode byte sequence from hextream with common hex prefix characters """ result = hextream.decode("AB $01 0x9A \\xA9") assert result == b"\xab\x01\x9a\xa9"
def test_decode_comments_whitespace(): """ decode byte sequence from hextream w/comments and whitespace """ result = hextream.decode("AB 01 9A A9FF\n#a comment\nbbcc#inlinecomment\n\tdD") assert result == b"\xab\x01\x9a\xa9\xff\xbb\xcc\xdd"
def test_decode_multiple_bytes(): """ decode several byte sequence """ result = hextream.decode("AB019AA9FF") assert result == b"\xab\x01\x9a\xa9\xff"
def test_decode_single_byte(): """ decode one byte sequence """ result = hextream.decode("AB") assert result == b"\xab"
def test_decode_empty(): """ decode empty bytes sequence """ result = hextream.decode("") assert result == b""