Skip to content

Commit 7359d61

Browse files
committed
Compatibility fixes for the new themis login (#6)
1 parent 94b2a35 commit 7359d61

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

temmies_cli/commands/init.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ def init_assignment(year_course_assignment, path, search, test_folder, file_fold
1414
"""
1515
Initialize a new assignment or course.
1616
"""
17+
18+
# ask for user
19+
user = input("Enter your s-number (s12354): ")
20+
1721
# Authenticate the user
18-
user = input("Enter your Themis username: ")
19-
themis = Themis(user)
22+
themis = Themis(user=user)
2023

2124
if search:
2225
click.echo(f"Searching for assignment: {search}")
@@ -50,11 +53,11 @@ def init_assignment(year_course_assignment, path, search, test_folder, file_fold
5053
if is_course:
5154
root_path = os.path.join(path, course.title.lower().replace(" ", "_"))
5255
click.echo(f"Initializing entire course '{course.title}'...")
53-
create_assignment_files(course, root_path, user, test_folder, file_folder)
56+
create_assignment_files(themis, course, root_path, user, test_folder, file_folder)
5457
else:
5558
root_path = os.path.join(path, assignment.title.lower().replace(" ", "_"))
5659
click.echo(f"Initializing assignment '{assignment.title}'...")
57-
create_assignment_files(assignment, root_path, user, test_folder, file_folder)
60+
create_assignment_files(themis, assignment, root_path, user, test_folder, file_folder)
5861

5962
click.echo(f"Initialized at '{root_path}'.")
6063

temmies_cli/commands/utils.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from temmies.exercise_group import ExerciseGroup
1414
import click
1515
from tqdm import tqdm
16-
16+
import json
1717

1818
def parse_path(themis, path_str):
1919
"""
@@ -108,19 +108,20 @@ def download_assignment_files(assignment, path, test_folder, file_folder):
108108
click.echo(str(e))
109109

110110

111-
def create_metadata_file(root_path, user, assignment_path):
111+
def create_metadata_file(instance:Themis, root_path, user, assignment_path):
112112
"""
113113
Create the .temmies metadata file.
114114
"""
115115
metadata_path = os.path.join(root_path, '.temmies')
116116
with open(metadata_path, 'w', encoding='utf-8') as f:
117117
f.write(f"username={user}\n")
118118
f.write(f"assignment_path={assignment_path}\n")
119+
f.write(f"session_cookies={instance.get_session_cookies()}\n")
119120
os.chmod(metadata_path, 0o600)
120121
click.echo(f"Created .temmies metadata file in '{root_path}'.")
121122

122123

123-
def load_metadata():
124+
def load_metadata() -> Themis:
124125
"""
125126
Load assignment metadata from the .temmies file.
126127
"""
@@ -137,13 +138,23 @@ def load_metadata():
137138
username = metadata.get('username')
138139
assignment_path = metadata.get('assignment_path')
139140

141+
# Create a Themis instance and set the session cookies
142+
session_cookies = json.loads(metadata.get('session_cookies'))
143+
144+
instance = Themis(session_cookies)
145+
146+
# Handle session expiration
147+
if instance.get_session_cookies() != session_cookies:
148+
create_metadata_file(instance, os.getcwd(), username, assignment_path)
149+
140150
if not username or not assignment_path:
141151
click.echo("Missing assignment metadata in .temmies file.", err=True)
142152
return None
143-
return metadata
153+
154+
return instance, assignment_path
144155

145156

146-
def create_assignment_files(group, root_path, user, test_folder, file_folder):
157+
def create_assignment_files(instance:Themis, group, root_path, user, test_folder, file_folder):
147158
"""
148159
Download files and test cases for a group (folder or assignment) recursively.
149160
"""
@@ -152,15 +163,16 @@ def create_assignment_files(group, root_path, user, test_folder, file_folder):
152163

153164
if group.submitable:
154165
# It's an assignment
155-
create_metadata_file(root_path, user, group.path)
166+
create_metadata_file(instance, root_path, user, group.path)
156167
else:
157168
# It's a folder or course
158169
items = group.get_items()
159170
for item in items:
160171
item_path = os.path.join(
161172
root_path, item.title.lower().replace(" ", "_"))
162173
create_assignment_files(
163-
item, item_path, user, test_folder, file_folder)
174+
instance, item, item_path, user, test_folder, file_folder
175+
)
164176

165177
def get_current_assignment():
166178
"""
@@ -170,14 +182,10 @@ def get_current_assignment():
170182
Raises:
171183
ValueError: If metadata is missing or incomplete.
172184
"""
173-
metadata = load_metadata()
174-
if not metadata:
175-
raise ValueError("No assignment metadata found. Run 'temmies init' first.")
176-
177-
username = metadata.get('username')
178-
assignment_path = metadata.get('assignment_path')
179-
180-
themis = Themis(username)
185+
themis, assignment_path = load_metadata()
186+
if not themis:
187+
raise ValueError("Failed to load metadata. Make sure to run 'temmies init' first.")
188+
181189
return ExerciseGroup(
182190
themis.session,
183191
assignment_path,

0 commit comments

Comments
 (0)