Skip to content

Commit

Permalink
[build] Translate edit_nws.sh into python
Browse files Browse the repository at this point in the history
  • Loading branch information
PiaNumworks authored and MarcNumworks committed Apr 25, 2024
1 parent f46d0bf commit 27b6c25
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 94 deletions.
84 changes: 84 additions & 0 deletions build/screenshots/edit_nws.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os, shutil, argparse
from subprocess import Popen, DEVNULL

from helpers.args_types import *
from helpers.miscellaneous import *
from helpers.screenshot_helper import *
from helpers.nws_helper import *

parser = argparse.ArgumentParser(
description="This script enables editing a state file. You can give a executable to follow the changes."
)
parser.add_argument(
"state_file",
metavar="STATE_FILE",
type=existing_state_file,
help="state file (with extension .nws)",
)
parser.add_argument(
"-e",
"--executable",
type=existing_file,
help="epsilon executable used to compare state file before the changes and after the changes",
)
parser.add_argument(
"-c",
"--copy",
action="store_true",
default=0,
help="Use this flag if you want to create a copy instead of editing you original nws",
)


EDITOR = os.environ.get("EDITOR", "vim")


def main():
# Parse args
args = parser.parse_args()
original_nws = args.state_file

# Create output folder and temp files
output_folder = "edit_nws_output"
clean_or_create_folder(output_folder)
temp_txt = os.path.join(output_folder, "temp.txt")
edited_nws = os.path.join(output_folder, "edited.nws")

# Convert the original nws into txt
convert_nws_to_txt(original_nws, temp_txt)

# Open vim to edit the txt
p = Popen(EDITOR + " " + temp_txt, shell=True, stderr=DEVNULL)
p.wait()

# Convert the txt into nws
convert_txt_to_nws(temp_txt, edited_nws)

# Generate gifs before and after the changes
if args.executable is not None:
print("Creating gifs before and after the changes")
create_gif_from_state_file(
original_nws, args.executable, output_folder, "before"
)
create_gif_from_state_file(edited_nws, args.executable, output_folder, "after")

print("==============================")

# Clean folder
copy_text = ""
os.remove(temp_txt)
if args.copy:
copy_text = "The copy of "
else:
# Replace original nws with copy
shutil.copy(edited_nws, original_nws)

if args.executable is None:
# Folder is empty
shutil.rmtree(output_folder)

print(copy_text + original_nws + " was successfully edited !")


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import sys
import sys, os

BIN_HEADER = b"NWSF"
TXT_HEADER = "NWS"

# This script detects the format (binary or textual) of the path given as its
# first and only argument and prints the state file in the other format.
#
# You can setup git to use it to show diff of nws files with :
# echo "*.nws diff=nws" >> .git/info/attributes
# git config diff.nws.textconv "python3 build/screenshots/nws2txt.py"
Expand All @@ -29,56 +26,50 @@ def import_events_names(path, array):
events_ids = {events_names[i]: i for i in range(256) if events_names[i]}


def is_binary_nws(path):
with open(path, "rb") as f:
header = f.read(4)
if header == b"":
exit(0)
if header == BIN_HEADER:
return True
assert header == (TXT_HEADER + "\n").encode()
return False
def convert_nws_to_txt(nwspath, txtpath):
if not os.path.isfile(nwspath) or os.path.splitext(nwspath)[1] != ".nws":
raise argparse.ArgumentTypeError(nwspath + " is not a .nws")


def print_as_text(nwspath):
with open(nwspath, "rb") as f:
assert f.read(4) == BIN_HEADER
if f.read(4) != BIN_HEADER:
print("Error:", nwspath, "is ill formatted")
sys.exit(1)
version = f.read(8)
formatVersion = f.read(1)
language = f.read(2)
events = f.read()

print(TXT_HEADER)
print(version.decode())
print(formatVersion[0])
print(language.decode())
with open(txtpath, "w", encoding="ascii") as f:
f.write(TXT_HEADER)
f.write("\n")
f.write(version.decode())
f.write("\n")
f.write(str(formatVersion[0]))
f.write("\n")
f.write(language.decode())
for c in events:
f.write("\n")
f.write(events_names_extended[c])

for c in events:
print(events_names_extended[c])

def convert_txt_to_nws(txtpath, nwspath):
if not os.path.isfile(txtpath) or os.path.splitext(txtpath)[1] != ".txt":
raise argparse.ArgumentTypeError(txtpath + " is not a .txt")

def print_as_nws(txtpath):
with open(txtpath, encoding="ascii") as f:
assert f.readline().strip() == TXT_HEADER
if f.readline().strip() != TXT_HEADER:
print("Error:", txtpath, "is ill formatted")
sys.exit(1)
version = f.readline().strip()
formatVersion = int(f.readline())
assert 0 < formatVersion < 256
language = f.readline().strip()
assert len(language) == 2
events = [events_ids[line.strip()] for line in f]

out = sys.stdout.buffer
out.write(BIN_HEADER)
out.write(version.encode())
out.write(bytes([formatVersion]))
out.write(language.encode())
out.write(bytes(events))


if __name__ == "__main__":
assert len(sys.argv) == 2
path = sys.argv[1]
if is_binary_nws(path):
print_as_text(path)
else:
print_as_nws(path)
with open(nwspath, "wb") as f:
f.write(BIN_HEADER)
f.write(version.encode())
f.write(bytes([formatVersion]))
f.write(language.encode())
f.write(bytes(events))
55 changes: 0 additions & 55 deletions build/state_file/edit_nws.sh

This file was deleted.

0 comments on commit 27b6c25

Please sign in to comment.