Make validate_car raise exception on failure instead of returning 0

This commit is contained in:
noelle 2025-06-02 16:49:43 -04:00
parent a07aa2685b
commit 0b53c462de
Signed by: noelle
SSH Key Fingerprint: SHA256:EDORP92Wc6hxhvIeQjJvQ71ko3pgKc58DHduv2+tYg8

View File

@ -90,6 +90,9 @@ default_car = [
r" \__/ \__/ \__/ \__/ ",
]
class CarError(Exception):
"""Error related validating a car."""
def print_help():
print(
cleandoc(
@ -206,9 +209,11 @@ def validate_car(car: list[str]):
while car_list[(len(car_list)-1)].strip() == "":
car_list.pop() # clear bottom
# len(choochoo_list) is the height of the train car, in number of rows.
if len(car_list) > max_y+1 or len(car_list) == 0:
return 0 # train car too tall or non-existant; skip it.
# len(car_list) is the height of the train car, in number of rows.
if len(car_list) > max_y+1:
raise CarError(f"Car is too tall ({len(car_list)} > {max_y + 1}).")
if len(car_list) == 0:
raise CarError(f"Car is empty.")
# vertically pad short cars with 1 space (lines will be lengthened later).
while len(car_list) < max_y:
@ -221,9 +226,9 @@ def validate_car(car: list[str]):
for idx,row in enumerate(car_list):
if len(row) > max_x+1: # check length of each row in .choochoo file.
return 0 # train car too long; skip it.
raise CarError(f"Car is too wide ({len(row)} > {max_x + 1}).")
elif "\t" in row or "\v" in row or "\f" in row or "\r" in row:
return 0 # skip if contains tabs, vert tabs, line feeds or carriage ret
raise CarError(f"Car contains illegal control characters.")
elif len(row) < longest_line:
padding = " "*(longest_line - len(row))
car_list[idx] += padding # add padding spaces.
@ -241,13 +246,12 @@ def print_all_cars():
continue # the train car was too tall; skip it.
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
if car != 0:
print("")
print(fname + ":")
print("\n".join(car)) # print the car to stdout
except:
pass;
pass
def chuggachugga(stdscr: curses.window):
@ -306,11 +310,10 @@ for fname in glob.glob('/home/*/' + traincarFN):
continue # the train car was too tall; skip it.
car = validate_car(choochoo_list) # printing is only a DEBUG feature.
if car != 0:
cars.append(car) # start a list of lists (list of cars) here.
except:
pass;
pass
while len(cars) < max_cars:
cars.append([*default_car]) # add copies of default cars if train too short