|
| 1 | +import sys |
| 2 | + |
| 3 | +import typer |
| 4 | + |
| 5 | +from pythonanywhere.scripts_commons import get_logger |
| 6 | +from pythonanywhere.students import Students |
| 7 | + |
| 8 | +app = typer.Typer() |
| 9 | + |
| 10 | + |
| 11 | +def setup(quiet: bool) -> Students: |
| 12 | + logger = get_logger(set_info=True) |
| 13 | + if quiet: |
| 14 | + logger.disabled = True |
| 15 | + return Students() |
| 16 | + |
| 17 | + |
| 18 | +@app.command() |
| 19 | +def get( |
| 20 | + numbered: bool = typer.Option( |
| 21 | + False, "-n", "--numbered", help="Add ordering numbers." |
| 22 | + ), |
| 23 | + quiet: bool = typer.Option( |
| 24 | + False, "-q", "--quiet", help="Disable additional logging." |
| 25 | + ), |
| 26 | + raw: bool = typer.Option( |
| 27 | + False, "-a", "--raw", help="Print list of usernames from the API response." |
| 28 | + ), |
| 29 | + sort: bool = typer.Option(False, "-s", "--sort", help="Sort alphabetically"), |
| 30 | + sort_reverse: bool = typer.Option( |
| 31 | + False, "-r", "--reverse", help="Sort in reverse order" |
| 32 | + ), |
| 33 | +): |
| 34 | + """ |
| 35 | + Get list of student usernames. |
| 36 | + """ |
| 37 | + |
| 38 | + api = setup(quiet) |
| 39 | + students = api.get() |
| 40 | + |
| 41 | + if students is None or students == []: |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + if raw: |
| 45 | + typer.echo(students) |
| 46 | + sys.exit() |
| 47 | + |
| 48 | + if sort or sort_reverse: |
| 49 | + students.sort(reverse=sort_reverse) |
| 50 | + |
| 51 | + for number, student in enumerate(students, start=1): |
| 52 | + line = f"{number:>3}. {student}" if numbered else student |
| 53 | + typer.echo(line) |
| 54 | + |
| 55 | + |
| 56 | +@app.command() |
| 57 | +def delete( |
| 58 | + student: str = typer.Argument(..., help="Username of a student to be removed."), |
| 59 | + quiet: bool = typer.Option( |
| 60 | + False, "-q", "--quiet", help="Disable additional logging." |
| 61 | + ), |
| 62 | +): |
| 63 | + """ |
| 64 | + Remove a student from the students list. |
| 65 | + """ |
| 66 | + |
| 67 | + api = setup(quiet) |
| 68 | + result = 0 if api.delete(student) else 1 |
| 69 | + sys.exit(result) |
| 70 | + |
| 71 | + |
| 72 | +@app.command() |
| 73 | +def holidays( |
| 74 | + quiet: bool = typer.Option( |
| 75 | + False, "-q", "--quiet", help="Disable additional logging." |
| 76 | + ), |
| 77 | +): |
| 78 | + """ |
| 79 | + School's out for summer! School's out forever! (removes all students) |
| 80 | + """ |
| 81 | + |
| 82 | + api = setup(quiet) |
| 83 | + students = api.get() |
| 84 | + |
| 85 | + if not students: |
| 86 | + if not quiet: |
| 87 | + typer.echo("No students found!") |
| 88 | + sys.exit(1) |
| 89 | + |
| 90 | + result = 0 if all(api.delete(s) for s in students) else 1 |
| 91 | + if not quiet: |
| 92 | + typer.echo( |
| 93 | + [ |
| 94 | + f"Removed all {len(students)} students!", |
| 95 | + f"Something went wrong, try again", |
| 96 | + ][result] |
| 97 | + ) |
| 98 | + sys.exit(result) |
0 commit comments