~crc_/block-tools

e5b8c512412aae2d1badfa4daac28f336ad846df — crc 2 years ago
initial checkin
5 files changed, 173 insertions(+), 0 deletions(-)

A README.md
A block_config.py
A block_export.py
A block_import.py
A block_merge.py
A  => README.md +53 -0
@@ 1,53 @@
# Block Tools

These are a collection of tools for operating on block files,
specifically aimed at use with the block files for ilo, napia,
and arks.

## Block Format

As defined by ilo (and drawing from Forth tradition), blocks
are 1024 byte sequences of characters. These are traditionally
displayed as 16 lines of 64 characters.

## Configuration

Adjust the constants in `block_config.py` to match your block
file limits.

## The Tools

### block_export.py

This takes a block file as input and converts it to a plain text
file format. Each line gets a line number, and horizontal rules
are provided as a visual aid.

### block_import.py

This takes a plain text file (as created by `block_export.py`)
and converts it into a block file. The horizontal rules and line
numbers are stripped in the process.

### block_merge.py

This takes in two exported block sets, and allows you to select
which blocks to keep for any blocks that differ between the two.

## Legal

    Copyright (c) 2022 Arland Childers <arland@childe.rs>

Permission to use, copy, modify, and distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

A  => block_config.py +1 -0
@@ 1,1 @@
BLOCKS = 4096

A  => block_export.py +37 -0
@@ 1,37 @@
import block_config

def read_line(in_file):
    return str(in_file.read(64)).rstrip()


def read_block(in_file, out_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):
    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):
    wle = b"\r\n"
    ule = b"\n"
    content = ""
    with open(in_file, "rb") as f:
        content = f.read().replace(wle, ule)
    with open(in_file, "wb") as f:
        f.write(content)


def main():
    with open("ilo.blocks.txt", "w") as out_file:
        out_file.write("   +---" + ("-5----+---" * 6) + ":0\n")
        with open("ilo.blocks", "r") as in_file:
            read_blocks(in_file, out_file)
    normalize("ilo.blocks.txt")


if __name__ == "__main__":
    main()

A  => block_import.py +29 -0
@@ 1,29 @@
import block_config

def write_blocks(file):
    out = open("ilo.blocks", "w")
    text = ""
    for block in range(0, block_config.BLOCKS * 17):
        line = file.readline()
        if line.rstrip()[:67] != "   +---" + ("-5----+---" * 6):
            out.write(line[3:].rstrip().ljust(64, " "))
    out.close()


def main():
    with open("ilo.blocks.txt", "r") as in_file:
        c = len(in_file.readlines()) - ((block_config.BLOCKS * 17) + 1)
        if c < 0:
            print(f"missing {-c} lines")
        elif c > 0:
            print(f"{c} extra lines")
        else:
            print("All good!")
            in_file.seek(0)
            write_blocks(in_file)
    if input('Press " " to run again, or press enter to exit') == " ":
        main()


if __name__ == "__main__":
    main()

A  => block_merge.py +53 -0
@@ 1,53 @@
initial = {}
incoming = {}
compare = {}

def load_file(fileName):
    out = {}
    temp = ""
    with open(fileName, "r") as f:
        id = 16
        block = 0
        for line in f.readlines():
            if id == 16:
                id = -1
                out[block] = temp
                temp = ""
                block += 1
            else:
                temp += line
            id += 1
    return out


def load_blocks():
    global initial, incoming
    initial = load_file("ilo.blocks.txt")
    incoming = load_file("incoming.blocks.txt")


def conflicts():
    global compare
    for i in initial:
        if initial[i] != incoming[i]:
            compare[i] = [initial[i], incoming[i]]


def save_blocks():
    with open("ilo.blocks.txt", "w") as f:
        for block in initial:
            f.write(str(initial[block]))
            f.write("   +---" + ("-5----+---" * 6) + ":{}\n".format(block))


if __name__ == "__main__":
    load_blocks()
    conflicts()
    for i in compare:
        print(i)
        print(compare[i][0])
        print(compare[i][1])
        choice = input("Select 1 or 2?")
        if choice == "2":
            initial[i] = incoming[i]
    save_blocks()