~crc_/block-tools

fcfdfc722e0358ec2ff4073c6cb390cf61893aa4 — crc 1 year, 8 months ago 8604550 master
python: add docstrings to import/export tools
2 files changed, 14 insertions(+), 1 deletions(-)

M block_export.py
M block_import.py
M block_export.py => block_export.py +11 -0
@@ 2,22 2,33 @@

import block_config


def read_line(in_file):
    """Read 64 characters from the input and strip
    trailing whitespace."""
    return str(in_file.read(64)).rstrip()


def read_block(in_file, out_file):
    """Read in the 16 lines from a block and write them
    to the output file."""
    for line in range(16):
        out_file.write(f"{str(line).rjust(2, ' ')} {read_line(in_file)}\n")


def read_blocks(in_file, out_file):
    """Read in all blocks, and pass them to read_block()
    to generate output. This also injects the horizontal
    ruler and block number."""
    for block in range(block_config.BLOCKS):
        read_block(in_file, out_file)
        out_file.write("   +---" + ("-5----+---" * 6) + ":{}\n".format(block + 1))


def normalize(in_file):
    """Convert a file from Windows-style newlines to
    Unix ones. This is done for greater consistency
    with the rest of the ilo & napia systems."""
    wle = b"\r\n"
    ule = b"\n"
    content = ""

M block_import.py => block_import.py +3 -1
@@ 2,7 2,9 @@

import block_config


def write_blocks(file):
    """Save the blocks from `file` to disk."""
    out = open("ilo.blocks", "w")
    text = ""
    for block in range(0, block_config.BLOCKS * 17):


@@ 23,7 25,7 @@ def main():
            print("All good!")
            in_file.seek(0)
            write_blocks(in_file)
    if input('Press " " to run again, or press enter to exit') == " ":
    if input("Press SPACE then ENTER to run again, or press ENTER to exit") == " ":
        main()