From 0b53c462de7a0ec2df99e8876db8bbfa8ec5c873 Mon Sep 17 00:00:00 2001 From: Noelle Leigh Date: Mon, 2 Jun 2025 16:49:43 -0400 Subject: [PATCH] Make validate_car raise exception on failure instead of returning 0 --- tilde-train.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tilde-train.py b/tilde-train.py index efede48..c71ff1d 100755 --- a/tilde-train.py +++ b/tilde-train.py @@ -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 + 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. + 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