Skip to content

Commit 6c0ccda

Browse files
committed
refactor: reduce complexity of cli.group
not a great refactor but cli is low priority right now
1 parent 45809b7 commit 6c0ccda

3 files changed

Lines changed: 50 additions & 31 deletions

File tree

scratchattach/cli/cmd/group.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from rich.markup import escape
55
from rich.table import Table
66

7+
78
def _list():
89
table = Table(title="All groups")
910
table.add_column("Name")
@@ -15,40 +16,41 @@ def _list():
1516
db.cursor.execute("SELECT USERNAME FROM GROUP_USERS WHERE GROUP_NAME=?", (name,))
1617
usernames = db.cursor.fetchall()
1718

18-
table.add_row(escape(name), escape(description),
19-
'\n'.join(f"{i}. {u}" for i, (u,) in enumerate(usernames)))
19+
table.add_row(escape(name), escape(description), "\n".join(f"{i}. {u}" for i, (u,) in enumerate(usernames)))
2020

2121
console.print(table)
2222

23+
2324
def add(group_name: str):
2425
accounts = input("Add accounts (split by space): ").split()
2526
for account in accounts:
2627
ctx.db_add_to_group(group_name, account)
2728

29+
2830
def remove(group_name: str):
2931
accounts = input("Remove accounts (split by space): ").split()
3032
for account in accounts:
3133
ctx.db_remove_from_group(group_name, account)
3234

35+
3336
def new():
3437
console.rule(f"New group {escape(ctx.args.group_name)}")
3538
if ctx.db_group_exists(ctx.args.group_name):
3639
raise ValueError(f"Group {escape(ctx.args.group_name)} already exists")
3740

3841
db.conn.execute("BEGIN")
39-
db.cursor.execute("INSERT INTO GROUPS (NAME, DESCRIPTION) "
40-
"VALUES (?, ?)", (ctx.args.group_name, input("Description: ")))
42+
db.cursor.execute("INSERT INTO GROUPS (NAME, DESCRIPTION) VALUES (?, ?)", (ctx.args.group_name, input("Description: ")))
4143
db.conn.commit()
4244
add(ctx.args.group_name)
4345

4446
_group(ctx.args.group_name)
4547

48+
4649
def _group(group_name: str):
4750
"""
4851
Display information about a group
4952
"""
50-
db.cursor.execute(
51-
"SELECT NAME, DESCRIPTION FROM GROUPS WHERE NAME = ?", (group_name,))
53+
db.cursor.execute("SELECT NAME, DESCRIPTION FROM GROUPS WHERE NAME = ?", (group_name,))
5254
result = db.cursor.fetchone()
5355
if result is None:
5456
print("No group selected!!")
@@ -61,13 +63,13 @@ def _group(group_name: str):
6163

6264
table = Table(title=escape(group_name))
6365
table.add_column(escape(name))
64-
table.add_column('Usernames')
66+
table.add_column("Usernames")
6567

66-
table.add_row(escape(description),
67-
'\n'.join(f"{i}. {u}" for i, u in enumerate(usernames)))
68+
table.add_row(escape(description), "\n".join(f"{i}. {u}" for i, u in enumerate(usernames)))
6869

6970
console.print(table)
7071

72+
7173
def switch():
7274
console.rule(f"Switching to {escape(ctx.args.group_name)}")
7375
if not ctx.db_group_exists(ctx.args.group_name):
@@ -76,6 +78,7 @@ def switch():
7678
ctx.current_group_name = ctx.args.group_name
7779
_group(ctx.current_group_name)
7880

81+
7982
def delete(group_name: str):
8083
print(f"Deleting {group_name}")
8184
if not ctx.db_group_exists(group_name):
@@ -85,17 +88,30 @@ def delete(group_name: str):
8588

8689
ctx.db_group_delete(group_name)
8790

91+
8892
def copy(group_name: str, new_name: str):
8993
print(f"Copying {group_name} as {new_name}")
9094
if not ctx.db_group_exists(group_name):
9195
raise ValueError(f"Group {group_name} does not exist")
9296

9397
ctx.db_group_copy(group_name, new_name)
9498

99+
95100
def rename(group_name: str, new_name: str):
96101
copy(group_name, new_name)
97102
delete(group_name)
98103

104+
105+
def delete_cmd():
106+
if input("Are you sure? (y/N): ").lower() != "y":
107+
return
108+
delete(ctx.current_group_name)
109+
new_current = ctx.db_first_group_name
110+
print(f"Switching to {new_current}")
111+
ctx.current_group_name = new_current
112+
_group(new_current)
113+
114+
99115
def group():
100116
match ctx.args.group_command:
101117
case "list":
@@ -109,14 +125,7 @@ def group():
109125
case "remove":
110126
remove(ctx.current_group_name)
111127
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-
128+
delete_cmd()
120129
case "copy":
121130
copy(ctx.current_group_name, ctx.args.group_name)
122131
case "rename":

scratchattach/cli/context.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Holds objects that should be available for the whole CLI system
44
Also provides wrappers for some SQL info.
55
"""
6+
67
import argparse
78
from dataclasses import dataclass, field
89

@@ -54,9 +55,8 @@ def session(self):
5455

5556
@property
5657
def current_group_name(self):
57-
return db.cursor \
58-
.execute("SELECT * FROM CURRENT WHERE GROUP_NAME IS NOT NULL") \
59-
.fetchone()[0]
58+
# FIXME: raises error when there are no groups
59+
return db.cursor.execute("SELECT * FROM CURRENT WHERE GROUP_NAME IS NOT NULL").fetchone()[0]
6060

6161
@current_group_name.setter
6262
def current_group_name(self, value: str):
@@ -75,14 +75,12 @@ def db_session_exists(name: str) -> bool:
7575

7676
@staticmethod
7777
def db_users_in_group(name: str) -> list[str]:
78-
return [i for (i,) in db.cursor.execute(
79-
"SELECT USERNAME FROM GROUP_USERS WHERE GROUP_NAME = ?", (name,)).fetchall()]
78+
return [i for (i,) in db.cursor.execute("SELECT USERNAME FROM GROUP_USERS WHERE GROUP_NAME = ?", (name,)).fetchall()]
8079

8180
def db_remove_from_group(self, group_name: str, username: str):
8281
if username in self.db_users_in_group(group_name):
8382
db.conn.execute("BEGIN")
84-
db.cursor.execute("DELETE FROM GROUP_USERS "
85-
"WHERE USERNAME = ? AND GROUP_NAME = ?", (username, group_name))
83+
db.cursor.execute("DELETE FROM GROUP_USERS WHERE USERNAME = ? AND GROUP_NAME = ?", (username, group_name))
8684
db.conn.commit()
8785

8886
@staticmethod
@@ -96,8 +94,7 @@ def db_add_to_group(self, group_name: str, username: str):
9694
if username in self.db_users_in_group(group_name) or not self.db_session_exists(username):
9795
return
9896
db.conn.execute("BEGIN")
99-
db.cursor.execute("INSERT INTO GROUP_USERS (GROUP_NAME, USERNAME) "
100-
"VALUES (?, ?)", (group_name, username))
97+
db.cursor.execute("INSERT INTO GROUP_USERS (GROUP_NAME, USERNAME) VALUES (?, ?)", (group_name, username))
10198
db.conn.commit()
10299

103100
@staticmethod
@@ -113,11 +110,21 @@ def db_group_delete(group_name: str):
113110
def db_group_copy(group_name: str, copy_name: str):
114111
db.conn.execute("BEGIN")
115112
# copy group metadata
116-
db.cursor.execute("INSERT INTO GROUPS (NAME, DESCRIPTION) "
117-
"SELECT ?, DESCRIPTION FROM GROUPS WHERE NAME = ?", (copy_name, group_name,))
113+
db.cursor.execute(
114+
"INSERT INTO GROUPS (NAME, DESCRIPTION) SELECT ?, DESCRIPTION FROM GROUPS WHERE NAME = ?",
115+
(
116+
copy_name,
117+
group_name,
118+
),
119+
)
118120
# copy sessions
119-
db.cursor.execute("INSERT INTO GROUP_USERS (GROUP_NAME, USERNAME) "
120-
"SELECT ?, USERNAME FROM GROUP_USERS WHERE GROUP_NAME = ?", (copy_name, group_name,))
121+
db.cursor.execute(
122+
"INSERT INTO GROUP_USERS (GROUP_NAME, USERNAME) SELECT ?, USERNAME FROM GROUP_USERS WHERE GROUP_NAME = ?",
123+
(
124+
copy_name,
125+
group_name,
126+
),
127+
)
121128

122129
db.conn.commit()
123130

scratchattach/site/user.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def __rich__(self):
191191
from rich.markup import escape
192192

193193
featured_data = self.featured_data() or {}
194-
ocular_data = self.ocular_status()
194+
195+
ocular_data = {}
196+
# FIXME: ocular is down right now, so this is disabled
197+
# ocular_data = self.ocular_status()
195198
ocular = "No ocular status"
196199

197200
if status := ocular_data.get("status"):

0 commit comments

Comments
 (0)