Compare commits

..

No commits in common. "main" and "usr-bin-env-python3" have entirely different histories.

140
crystal_orb_cli.py Executable file → Normal file
View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import dataclasses import dataclasses
import sys import sys
@ -34,26 +33,22 @@ class Departure:
def __post_init__(self): def __post_init__(self):
self.departure_time = datetime.fromisoformat(self.departure_time) self.departure_time = datetime.fromisoformat(self.departure_time)
def colorize(self, timestamp = None): def colorize(self):
out_str = "\033[0;37m" out_str = ""
if timestamp is not None and self.departure_time < timestamp: out_str += "\033[2;20;35m" + str(self.departure_time.time()) + "\033[0m"
out_str += "\033[9m"
out_str += "\033[2;35m" + str(self.departure_time.time()) + "\033[22;37m"
out_str += ": " out_str += ": "
out_str += pad(str(self.line), 4, justification="r") out_str += pad(str(self.line), 4, justification="r")
out_str += " " out_str += " "
out_str += "\033[3;37m" + pad(self.destination, 35+19) + "\033[23;37m" out_str += "\033[3;37m" + pad(self.destination, 35+19) + "\033[0m"
out_str += " (" out_str += " ("
if self.is_realtime: out_str += "\033[32mR\033[37m" if self.is_realtime: out_str += "\033[0;32mR\033[0m"
else: out_str += " " else: out_str += " "
out_str += ", " out_str += ", "
if self.towards_midtbyen: out_str += "\033[32mTo \033[37m" if self.towards_midtbyen: out_str += "\033[0;32mTo \033[0m"
else: out_str += "\033[31mFrom\033[37m" else: out_str += "\033[0;31mFrom\033[0m"
out_str += ")" out_str += ")"
if timestamp is not None and self.departure_time < timestamp:
out_str += "\033[0m"
return out_str return out_str
@ -75,57 +70,94 @@ def print_colorized_departures(stop_place: str):
for dep in departures: for dep in departures:
print(dep.colorize()) print(dep.colorize())
def print_help_info():
print(
"""Help info for the crystal orb cli.
Possible commands:
<stop place>
query the api for all busses from the given stop place
-h or --help
Show this screen
-l or --list-stop-places
List the admissible stop places.
This is configured in the STOP_PLACES dict in crystal_orb.py
-n or --next
Limit the response to only the next bus matching the query
-b <line> or --bus <line>
Limit the response to only the supplied bus line
-s <place> or --stop <place>
Change the stop place to the supplied place
-t or --to
Limit the response to buses going into town
-f or --from
Limit the response to buses going away from town""")
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() args = sys.argv
DEFAULT_STOP_PLACE = "gløshaugen" # If no arguments are passed, we just print the default stop
if len(args) < 2:
# Requiring value print_colorized_departures("gløshaugen")
parser.add_argument("-b", "--bus",
help="limit output to only the supplied bus line")
parser.add_argument("-s", "--stop",
help="limit output to only the supplied stop place")
# Boolean flags
parser.add_argument("-l", "--list-stop-places", action="store_true",
help="list admissible stop places")
parser.add_argument("-n", "--next", action="store_true",
help="limit output to one response, the next matching the query")
parser.add_argument("-t", "--to", action="store_true",
help="show only buses going into town")
parser.add_argument("-f", "--from", action="store_true", dest="from_",
help="show only buses going away from town")
args = parser.parse_args()
# list places and exit
if args.list_stop_places:
for place in STOP_PLACES.keys():
print(place)
sys.exit(0) sys.exit(0)
# use stop input to fetch buses if "-h" in args or "--help" in args:
if args.stop is None: print_help_info()
deps = get_departure_as_classes(DEFAULT_STOP_PLACE) sys.exit(0)
else:
# Values for control flow and processing on the available departures
stop_place = "gløshaugen"
bus_line = None
towards_city_center = None
take_one = False
# Parse input arguments to the program
for i, arg in enumerate(args):
if i == 1 and arg[0] != "-": # First argument is taken as the stop place
stop_place = arg.lower()
elif arg == "-n" or arg == "--next": # Limits to only giving "the next bus" matching the query
take_one = True
elif arg == "-b" or arg == "--bus": # Limit to specific bus line
try: try:
deps = get_departure_as_classes(args.stop) bus_line = args[i+1]
except: except:
print(f"Stop {args.stop} not admissible. Run --list-stop-places for help.") print("Bus line not supplied to -b or --bus.")
elif arg == "--list-stop-places" or arg == "-l": # Just print stop places
print("Admissible stop places:")
for place in STOP_PLACES.keys():
print(f"\t{place}")
sys.exit()
elif arg == "-t" or arg == "--to": # Limit to buses going to town
towards_city_center = True
elif arg == "-f" or arg == "--from": # Limit to buses leaving town
towards_city_center = False
elif arg == "-s" or arg == "--stop": # Change stop
try:
stop_place = args[i+1].lower()
except:
print("Stop place not supplied to -s or --stop")
else:
pass
# Arguments are parsed, we can execute the query
try:
deps = get_departure_as_classes(stop_place)
except:
print(f"Stop {stop_place} not admissible. Run --list-stop-places for help.")
sys.exit(0) sys.exit(0)
# filter output if bus_line is not None:
if args.bus is not None: deps = list(filter(lambda d: str(d.line) == bus_line, deps))
deps = [d for d in deps if d.line == args.bus] if towards_city_center is not None:
if args.to: if towards_city_center:
deps = [d for d in deps if d.towards_midtbyen] deps = list(filter(lambda d: d.towards_midtbyen, deps))
if args.from_: else:
deps = [d for d in deps if not d.towards_midtbyen] deps = list(filter(lambda d: not d.towards_midtbyen, deps))
current_time = datetime.now()
if args.next: if take_one:
deps = list(filter(lambda d: d.departure_time > current_time, deps)) print(deps[0].colorize())
print(deps[0].colorize(current_time))
else: else:
for dep in deps: for dep in deps:
print(dep.colorize(current_time)) print(dep.colorize())
sys.exit(0) sys.exit(0)