Commit 03fd4f9b by Victor Shnayder

formatting in xqueue_interface.py

parent f5787390
...@@ -20,7 +20,7 @@ def make_hashkey(seed): ...@@ -20,7 +20,7 @@ def make_hashkey(seed):
def make_xheader(lms_callback_url, lms_key, queue_name): def make_xheader(lms_callback_url, lms_key, queue_name):
''' """
Generate header for delivery and reply of queue request. Generate header for delivery and reply of queue request.
Xqueue header is a JSON-serialized dict: Xqueue header is a JSON-serialized dict:
...@@ -28,19 +28,19 @@ def make_xheader(lms_callback_url, lms_key, queue_name): ...@@ -28,19 +28,19 @@ def make_xheader(lms_callback_url, lms_key, queue_name):
'lms_key': secret key used by LMS to protect its state (string), 'lms_key': secret key used by LMS to protect its state (string),
'queue_name': designate a specific queue within xqueue server, e.g. 'MITx-6.00x' (string) 'queue_name': designate a specific queue within xqueue server, e.g. 'MITx-6.00x' (string)
} }
''' """
return json.dumps({ 'lms_callback_url': lms_callback_url, return json.dumps({ 'lms_callback_url': lms_callback_url,
'lms_key': lms_key, 'lms_key': lms_key,
'queue_name': queue_name }) 'queue_name': queue_name })
def parse_xreply(xreply): def parse_xreply(xreply):
''' """
Parse the reply from xqueue. Messages are JSON-serialized dict: Parse the reply from xqueue. Messages are JSON-serialized dict:
{ 'return_code': 0 (success), 1 (fail) { 'return_code': 0 (success), 1 (fail)
'content': Message from xqueue (string) 'content': Message from xqueue (string)
} }
''' """
try: try:
xreply = json.loads(xreply) xreply = json.loads(xreply)
except ValueError, err: except ValueError, err:
...@@ -63,7 +63,7 @@ class XQueueInterface(object): ...@@ -63,7 +63,7 @@ class XQueueInterface(object):
self.session = requests.session(auth=requests_auth) self.session = requests.session(auth=requests_auth)
def send_to_queue(self, header, body, files_to_upload=None): def send_to_queue(self, header, body, files_to_upload=None):
''' """
Submit a request to xqueue. Submit a request to xqueue.
header: JSON-serialized dict in the format described in 'xqueue_interface.make_xheader' header: JSON-serialized dict in the format described in 'xqueue_interface.make_xheader'
...@@ -74,14 +74,16 @@ class XQueueInterface(object): ...@@ -74,14 +74,16 @@ class XQueueInterface(object):
files_to_upload: List of file objects to be uploaded to xqueue along with queue request files_to_upload: List of file objects to be uploaded to xqueue along with queue request
Returns (error_code, msg) where error_code != 0 indicates an error Returns (error_code, msg) where error_code != 0 indicates an error
''' """
# Attempt to send to queue # Attempt to send to queue
(error, msg) = self._send_to_queue(header, body, files_to_upload) (error, msg) = self._send_to_queue(header, body, files_to_upload)
if error and (msg == 'login_required'): # Log in, then try again # Log in, then try again
if error and (msg == 'login_required'):
self._login() self._login()
if files_to_upload is not None: if files_to_upload is not None:
for f in files_to_upload: # Need to rewind file pointers # Need to rewind file pointers
for f in files_to_upload:
f.seek(0) f.seek(0)
(error, msg) = self._send_to_queue(header, body, files_to_upload) (error, msg) = self._send_to_queue(header, body, files_to_upload)
...@@ -91,7 +93,7 @@ class XQueueInterface(object): ...@@ -91,7 +93,7 @@ class XQueueInterface(object):
def _login(self): def _login(self):
payload = { 'username': self.auth['username'], payload = { 'username': self.auth['username'],
'password': self.auth['password'] } 'password': self.auth['password'] }
return self._http_post(self.url+'/xqueue/login/', payload) return self._http_post(self.url + '/xqueue/login/', payload)
def _send_to_queue(self, header, body, files_to_upload): def _send_to_queue(self, header, body, files_to_upload):
...@@ -102,7 +104,7 @@ class XQueueInterface(object): ...@@ -102,7 +104,7 @@ class XQueueInterface(object):
for f in files_to_upload: for f in files_to_upload:
files.update({ f.name: f }) files.update({ f.name: f })
return self._http_post(self.url+'/xqueue/submit/', payload, files=files) return self._http_post(self.url + '/xqueue/submit/', payload, files=files)
def _http_post(self, url, data, files=None): def _http_post(self, url, data, files=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment