1313from temmies .exercise_group import ExerciseGroup
1414import click
1515from tqdm import tqdm
16-
16+ import json
1717
1818def 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
165177def 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