<s> from client . protocols . common import models <EOL> from client . utils import auth <EOL> from client . utils import network <EOL> from datetime import datetime <EOL> from urllib import error <EOL> import logging <EOL> import os <EOL> import pickle <EOL> log = logging . getLogger ( __name__ ) <EOL> GAE_DATETIME_FORMAT = "<STR_LIT>" <EOL> EXPORT_CACHE = "<STR_LIT>" <EOL> SUBMISSION_DIR = "<STR_LIT>" <EOL> class ExportProtocol ( models . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , messages ) : <EOL> """<STR_LIT>""" <EOL> if not self . args . export : <EOL> return <EOL> self . access_token = auth . authenticate ( self . args . authenticate ) <EOL> log . info ( '<STR_LIT>' , self . access_token ) <EOL> data = None <EOL> try : <EOL> data = pickle . load ( open ( EXPORT_CACHE , "<STR_LIT:rb>" ) ) <EOL> except IOError : <EOL> data = { <EOL> '<STR_LIT>' : self . assignment . endpoint <EOL> } <EOL> data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] = self . get_ids ( data [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = self . get_students ( data [ '<STR_LIT>' ] ) <EOL> if not data [ '<STR_LIT>' ] : <EOL> return <EOL> data [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> current_student = <NUM_LIT:0> <EOL> try : <EOL> if not os . path . exists ( SUBMISSION_DIR ) : <EOL> os . makedirs ( SUBMISSION_DIR ) <EOL> downloads_left = len ( data [ '<STR_LIT>' ] ) - data [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" . format ( downloads_left ) ) <EOL> for current_student in range ( data [ '<STR_LIT>' ] , len ( data [ '<STR_LIT>' ] ) ) : <EOL> student = data [ '<STR_LIT>' ] [ current_student ] <EOL> try : <EOL> self . download_submission ( student , data [ '<STR_LIT>' ] ) <EOL> except ( IOError , error . HTTPError ) : <EOL> data [ '<STR_LIT>' ] = current_student <EOL> abort ( data , len ( data [ '<STR_LIT>' ] ) , current_student ) <EOL> return <EOL> data [ '<STR_LIT>' ] = current_student + <NUM_LIT:1> <EOL> except KeyboardInterrupt : <EOL> pickle . dump ( data , open ( "<STR_LIT>" , "<STR_LIT:wb>" ) ) <EOL> abort ( data , len ( data [ '<STR_LIT>' ] ) , current_student ) <EOL> return <EOL> print ( "<STR_LIT>" ) <EOL> if os . path . exists ( EXPORT_CACHE ) : <EOL> os . remove ( EXPORT_CACHE ) <EOL> def download_submission ( self , student , assign_id ) : <EOL> """<STR_LIT>""" <EOL> if self . args . latest : <EOL> submission = self . get_latest_submission ( student , assign_id ) <EOL> else : <EOL> submission = self . get_final_submission ( student , assign_id ) <EOL> if not submission : <EOL> return <EOL> contents , timestamp , subm_id = submission <EOL> subm_dir = os . path . join ( SUBMISSION_DIR , student [ <NUM_LIT:0> ] ) <EOL> if not os . path . exists ( subm_dir ) : <EOL> os . makedirs ( subm_dir ) <EOL> for filename in contents : <EOL> if filename != '<STR_LIT>' : <EOL> with open ( os . path . join ( subm_dir , filename ) , "<STR_LIT>" ) as f : <EOL> f . write ( contents [ filename ] ) <EOL> f . flush ( ) <EOL> os . fsync ( f . fileno ( ) ) <EOL> with open ( os . path . join ( subm_dir , "<STR_LIT>" ) , "<STR_LIT>" ) as f : <EOL> f . write ( r"<STR_LIT>" <EOL> . format ( student , subm_id , timestamp ) ) <EOL> f . flush ( ) <EOL> os . fsync ( f . fileno ( ) ) <EOL> def get_final_submission ( self , student , assign_id ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : assign_id } <EOL> response = self . request ( '<STR_LIT>' . format ( student [ <NUM_LIT:0> ] ) , <EOL> params ) <EOL> if not response [ '<STR_LIT:data>' ] : <EOL> print ( "<STR_LIT>" . format ( student [ <NUM_LIT:0> ] ) ) <EOL> return <EOL> raw_data = response [ '<STR_LIT:data>' ] <EOL> contents = raw_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> timestamp = datetime . strptime ( raw_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , GAE_DATETIME_FORMAT ) <EOL> subm_id = raw_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> return contents , timestamp , subm_id <EOL> def get_latest_submission ( self , student , assign_id ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : assign_id } <EOL> data = self . request ( '<STR_LIT>' . format ( student [ <NUM_LIT:0> ] ) , <EOL> params ) [ '<STR_LIT:data>' ] <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" . format ( student [ <NUM_LIT:0> ] ) ) <EOL> return <EOL> newest_time = None <EOL> newest_subm = None <EOL> for subm in data : <EOL> time = datetime . strptime ( subm [ '<STR_LIT>' ] , GAE_DATETIME_FORMAT ) <EOL> if newest_time is None or time > newest_time : <EOL> newest_time = time <EOL> newest_subm = subm <EOL> contents = newest_subm [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return contents , newest_time , newest_subm [ '<STR_LIT:id>' ] <EOL> def get_ids ( self , endpoint ) : <EOL> """<STR_LIT>""" <EOL> response = self . request ( '<STR_LIT>' ) <EOL> for assign in response [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] : <EOL> if assign [ '<STR_LIT:name>' ] == endpoint : <EOL> return assign [ '<STR_LIT:id>' ] , assign [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] <EOL> def request ( self , route , params = { } ) : <EOL> """<STR_LIT>""" <EOL> return network . api_request ( self . access_token , self . args . server , <EOL> route , self . args . insecure , params ) <EOL> def get_students ( self , course ) : <EOL> """<STR_LIT>""" <EOL> response = self . request ( '<STR_LIT>' . format ( course ) , <EOL> { "<STR_LIT>" : '<STR_LIT>' } ) <EOL> if not response : <EOL> return <EOL> return [ student [ '<STR_LIT:user>' ] [ '<STR_LIT:email>' ] for student in response [ '<STR_LIT:data>' ] ] <EOL> def abort ( data , total_students , current_student ) : <EOL> pickle . dump ( data , open ( EXPORT_CACHE , "<STR_LIT:wb>" ) ) <EOL> dl_left = total_students - current_student <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" . format ( dl_left ) ) <EOL> protocol = ExportProtocol </s>
