|
| 1 | +from scratchattach.cli import db |
| 2 | +from scratchattach.cli.context import console, ctx |
| 3 | + |
| 4 | +from rich.markup import escape |
| 5 | +from rich.table import Table |
| 6 | + |
| 7 | +def _list(): |
| 8 | + table = Table(title="All groups") |
| 9 | + table.add_column("Name") |
| 10 | + table.add_column("Description") |
| 11 | + table.add_column("Usernames") |
| 12 | + |
| 13 | + db.cursor.execute("SELECT NAME, DESCRIPTION FROM GROUPS") |
| 14 | + for name, description in db.cursor.fetchall(): |
| 15 | + db.cursor.execute("SELECT USERNAME FROM GROUP_USERS WHERE GROUP_NAME=?", (name,)) |
| 16 | + usernames = db.cursor.fetchall() |
| 17 | + |
| 18 | + table.add_row(escape(name), escape(description), |
| 19 | + '\n'.join(f"{i}. {u}" for i, (u,) in enumerate(usernames))) |
| 20 | + |
| 21 | + console.print(table) |
| 22 | + |
| 23 | +def add(group_name: str): |
| 24 | + accounts = input("Add accounts (split by space): ").split() |
| 25 | + for account in accounts: |
| 26 | + ctx.db_add_to_group(group_name, account) |
| 27 | + |
| 28 | +def remove(group_name: str): |
| 29 | + accounts = input("Remove accounts (split by space): ").split() |
| 30 | + for account in accounts: |
| 31 | + ctx.db_remove_from_group(group_name, account) |
| 32 | + |
| 33 | +def new(): |
| 34 | + console.rule(f"New group {escape(ctx.args.group_name)}") |
| 35 | + if ctx.db_group_exists(ctx.args.group_name): |
| 36 | + raise ValueError(f"Group {escape(ctx.args.group_name)} already exists") |
| 37 | + |
| 38 | + db.conn.execute("BEGIN") |
| 39 | + db.cursor.execute("INSERT INTO GROUPS (NAME, DESCRIPTION) " |
| 40 | + "VALUES (?, ?)", (ctx.args.group_name, input("Description: "))) |
| 41 | + db.conn.commit() |
| 42 | + add(ctx.args.group_name) |
| 43 | + |
| 44 | + _group(ctx.args.group_name) |
| 45 | + |
| 46 | +def _group(group_name: str): |
| 47 | + """ |
| 48 | + Display information about a group |
| 49 | + """ |
| 50 | + db.cursor.execute( |
| 51 | + "SELECT NAME, DESCRIPTION FROM GROUPS WHERE NAME = ?", (group_name,)) |
| 52 | + result = db.cursor.fetchone() |
| 53 | + if result is None: |
| 54 | + print("No group selected!!") |
| 55 | + return |
| 56 | + |
| 57 | + name, description = result |
| 58 | + |
| 59 | + db.cursor.execute("SELECT USERNAME FROM GROUP_USERS WHERE GROUP_NAME = ?", (name,)) |
| 60 | + usernames = [name for (name,) in db.cursor.fetchall()] |
| 61 | + |
| 62 | + table = Table(title=escape(group_name)) |
| 63 | + table.add_column(escape(name)) |
| 64 | + table.add_column('Usernames') |
| 65 | + |
| 66 | + table.add_row(escape(description), |
| 67 | + '\n'.join(f"{i}. {u}" for i, u in enumerate(usernames))) |
| 68 | + |
| 69 | + console.print(table) |
| 70 | + |
| 71 | +def switch(): |
| 72 | + console.rule(f"Switching to {escape(ctx.args.group_name)}") |
| 73 | + if not ctx.db_group_exists(ctx.args.group_name): |
| 74 | + raise ValueError(f"Group {escape(ctx.args.group_name)} does not exist") |
| 75 | + |
| 76 | + ctx.current_group_name = ctx.args.group_name |
| 77 | + _group(ctx.current_group_name) |
| 78 | + |
| 79 | +def delete(group_name: str): |
| 80 | + print(f"Deleting {group_name}") |
| 81 | + if not ctx.db_group_exists(group_name): |
| 82 | + raise ValueError(f"Group {group_name} does not exist") |
| 83 | + if ctx.db_group_count == 1: |
| 84 | + raise ValueError(f"Make another group first") |
| 85 | + |
| 86 | + ctx.db_group_delete(group_name) |
| 87 | + |
| 88 | +def copy(group_name: str, new_name: str): |
| 89 | + print(f"Copying {group_name} as {new_name}") |
| 90 | + if not ctx.db_group_exists(group_name): |
| 91 | + raise ValueError(f"Group {group_name} does not exist") |
| 92 | + |
| 93 | + ctx.db_group_copy(group_name, new_name) |
| 94 | + |
| 95 | +def rename(group_name: str, new_name: str): |
| 96 | + copy(group_name, new_name) |
| 97 | + delete(group_name) |
| 98 | + |
| 99 | +def group(): |
| 100 | + match ctx.args.group_command: |
| 101 | + case "list": |
| 102 | + _list() |
| 103 | + case "new": |
| 104 | + new() |
| 105 | + case "switch": |
| 106 | + switch() |
| 107 | + case "add": |
| 108 | + add(ctx.current_group_name) |
| 109 | + case "remove": |
| 110 | + remove(ctx.current_group_name) |
| 111 | + case "delete": |
| 112 | + if input("Are you sure? (y/N): ").lower() != "y": |
| 113 | + return |
| 114 | + delete(ctx.current_group_name) |
| 115 | + new_current = ctx.db_first_group_name |
| 116 | + print(f"Switching to {new_current}") |
| 117 | + ctx.current_group_name = new_current |
| 118 | + _group(new_current) |
| 119 | + |
| 120 | + case "copy": |
| 121 | + copy(ctx.current_group_name, ctx.args.group_name) |
| 122 | + case "rename": |
| 123 | + rename(ctx.current_group_name, ctx.args.group_name) |
| 124 | + ctx.current_group_name = ctx.args.group_name |
| 125 | + _group(ctx.args.group_name) |
| 126 | + case None: |
| 127 | + _group(ctx.current_group_name) |
0 commit comments