55import logging
66import warnings
77
8- from traitlets .config import LoggingConfigurable , Config
8+ from traitlets .config import LoggingConfigurable , Config , get_config
99from traitlets import Instance , Enum , Unicode , observe
1010
1111from ..coursedir import CourseDirectory
1212from ..converters import GenerateAssignment , Autograde , GenerateFeedback
13- from ..exchange import ExchangeList , ExchangeReleaseAssignment , ExchangeReleaseFeedback , ExchangeFetchFeedback , ExchangeCollect , ExchangeError , ExchangeSubmit
13+ from ..exchange import ExchangeFactory , ExchangeError
1414from ..api import MissingEntry , Gradebook , Student , SubmittedAssignment
1515from ..utils import parse_utc , temp_attrs , capture_log , as_timezone , to_numeric_tz
1616from ..auth import Authenticator
@@ -21,6 +21,7 @@ class NbGraderAPI(LoggingConfigurable):
2121
2222 coursedir = Instance (CourseDirectory , allow_none = True )
2323 authenticator = Instance (Authenticator , allow_none = True )
24+ exchange = Instance (ExchangeFactory , allow_none = True )
2425
2526 # The log level for the application
2627 log_level = Enum (
@@ -48,7 +49,7 @@ def _log_level_changed(self, change):
4849 self .log_level = new
4950 self .log .setLevel (new )
5051
51- def __init__ (self , coursedir = None , authenticator = None , ** kwargs ):
52+ def __init__ (self , coursedir = None , authenticator = None , exchange = None , ** kwargs ):
5253 """Initialize the API.
5354
5455 Arguments
@@ -58,6 +59,9 @@ def __init__(self, coursedir=None, authenticator=None, **kwargs):
5859 authenticator : :class:~`nbgrader.auth.BaseAuthenticator`
5960 (Optional) An authenticator instance for communicating with an
6061 external database.
62+ exchange : :class:~`nbgrader.exchange.ExchangeFactory`
63+ (Optional) A factory for creating the exchange classes used
64+ for distributing assignments and feedback.
6165 kwargs:
6266 Additional keyword arguments (e.g. ``parent``, ``config``)
6367
@@ -75,13 +79,22 @@ def __init__(self, coursedir=None, authenticator=None, **kwargs):
7579 else :
7680 self .authenticator = authenticator
7781
82+ if exchange is None :
83+ self .exchange = ExchangeFactory (parent = self )
84+ else :
85+ self .exchange = exchange
86+
7887 if sys .platform != 'win32' :
79- lister = ExchangeList (
88+ lister = self . exchange . List (
8089 coursedir = self .coursedir ,
8190 authenticator = self .authenticator ,
8291 parent = self )
8392 self .course_id = self .coursedir .course_id
84- self .exchange = lister .root
93+ if hasattr (lister , "root" ):
94+ self .exchange_root = lister .root
95+ else :
96+ # For non-fs based exchanges
97+ self .exchange_root = ''
8598
8699 try :
87100 lister .start ()
@@ -92,7 +105,7 @@ def __init__(self, coursedir=None, authenticator=None, **kwargs):
92105
93106 else :
94107 self .course_id = ''
95- self .exchange = ''
108+ self .exchange_root = ''
96109 self .exchange_missing = True
97110
98111 @property
@@ -155,7 +168,7 @@ def get_released_assignments(self):
155168
156169 """
157170 if self .exchange_is_functional :
158- lister = ExchangeList (
171+ lister = self . exchange . List (
159172 coursedir = self .coursedir ,
160173 authenticator = self .authenticator ,
161174 parent = self )
@@ -640,7 +653,7 @@ def _filter_existing_notebooks(self, assignment_id, notebooks):
640653 # should be here already so we don't need to filter for only
641654 # existing notebooks in that case.
642655 if self .exchange_is_functional :
643- app = ExchangeSubmit (
656+ app = self . exchange . Submit (
644657 coursedir = self .coursedir ,
645658 authenticator = self .authenticator ,
646659 parent = self )
@@ -924,7 +937,7 @@ def unrelease(self, assignment_id):
924937 """
925938 if sys .platform != 'win32' :
926939 with temp_attrs (self .coursedir , assignment_id = assignment_id ):
927- app = ExchangeList (
940+ app = self . exchange . List (
928941 coursedir = self .coursedir ,
929942 authenticator = self .authenticator ,
930943 parent = self )
@@ -960,7 +973,7 @@ def release_assignment(self, assignment_id):
960973 """
961974 if sys .platform != 'win32' :
962975 with temp_attrs (self .coursedir , assignment_id = assignment_id ):
963- app = ExchangeReleaseAssignment (
976+ app = self . exchange . ReleaseAssignment (
964977 coursedir = self .coursedir ,
965978 authenticator = self .authenticator ,
966979 parent = self )
@@ -989,7 +1002,7 @@ def collect(self, assignment_id, update=True):
9891002 """
9901003 if sys .platform != 'win32' :
9911004 with temp_attrs (self .coursedir , assignment_id = assignment_id ):
992- app = ExchangeCollect (
1005+ app = self . exchange . Collect (
9931006 coursedir = self .coursedir ,
9941007 authenticator = self .authenticator ,
9951008 parent = self )
@@ -1095,14 +1108,14 @@ def release_feedback(self, assignment_id, student_id=None):
10951108 """
10961109 if student_id is not None :
10971110 with temp_attrs (self .coursedir , assignment_id = assignment_id , student_id = student_id ):
1098- app = ExchangeReleaseFeedback (
1111+ app = self . exchange . ReleaseFeedback (
10991112 coursedir = self .coursedir ,
11001113 authentictor = self .authenticator ,
11011114 parent = self )
11021115 return capture_log (app )
11031116 else :
11041117 with temp_attrs (self .coursedir , assignment_id = assignment_id , student_id = '*' ):
1105- app = ExchangeReleaseFeedback (
1118+ app = self . exchange . ReleaseFeedback (
11061119 coursedir = self .coursedir ,
11071120 authentictor = self .authenticator ,
11081121 parent = self )
@@ -1130,15 +1143,15 @@ def fetch_feedback(self, assignment_id, student_id):
11301143
11311144 """
11321145 with temp_attrs (self .coursedir , assignment_id = assignment_id , student_id = student_id ):
1133- app = ExchangeFetchFeedback (
1146+ app = self . exchange . FetchFeedback (
11341147 coursedir = self .coursedir ,
11351148 authentictor = self .authenticator ,
11361149 parent = self )
11371150 ret_dic = capture_log (app )
11381151 # assignment tab needs a 'value' field with the info needed to repopulate
11391152 # the tables.
11401153 with temp_attrs (self .coursedir , assignment_id = '*' , student_id = student_id ):
1141- lister_rel = ExchangeList (
1154+ lister_rel = self . exchange . List (
11421155 inbound = False , cached = True ,
11431156 coursedir = self .coursedir ,
11441157 authenticator = self .authenticator ,
0 commit comments