Coverage for e2xgrader/server_extensions/apps/validate_assignment/handlers.py: 40%
48 statements
« prev ^ index » next coverage.py v7.4.2, created at 2024-03-14 13:22 +0100
« prev ^ index » next coverage.py v7.4.2, created at 2024-03-14 13:22 +0100
1import json
2import os
3import traceback
5from e2xcore.handlers import E2xHandler
6from jupyter_core.paths import jupyter_config_path
7from nbgrader.apps import NbGrader
8from nbgrader.nbgraderformat import SchemaTooNewError, SchemaTooOldError
9from nbgrader.server_extensions.validate_assignment.handlers import (
10 NbGraderVersionHandler,
11)
12from tornado import web
14from .validator import E2XValidator
16static = os.path.join(os.path.dirname(__file__), "static")
19class ValidateAssignmentHandler(E2xHandler):
20 @property
21 def root_dir(self):
22 return self.settings["root_dir"]
24 def load_config(self):
25 paths = jupyter_config_path()
26 paths.insert(0, os.getcwd())
28 app = NbGrader()
29 app.config_file_paths.append(paths)
30 app.load_config_file()
32 return app.config
34 def validate_notebook(self, path):
35 fullpath = os.path.join(self.root_dir, path)
37 try:
38 config = self.load_config()
39 validator = E2XValidator(config=config)
40 result = validator.validate(fullpath)
42 except SchemaTooOldError:
43 self.log.error(traceback.format_exc())
44 msg = (
45 "The notebook '{}' uses an old version "
46 "of the nbgrader metadata format. Please **back up this "
47 "notebook** and then update the metadata using:\n\nnbgrader update {}\n"
48 ).format(fullpath, fullpath)
49 self.log.error(msg)
50 retvalue = {"success": False, "value": msg}
52 except SchemaTooNewError:
53 self.log.error(traceback.format_exc())
54 msg = (
55 "The notebook '{}' uses a newer version "
56 "of the nbgrader metadata format. Please update your version of "
57 "nbgrader to the latest version to be able to use this notebook."
58 ).format(fullpath)
59 self.log.error(msg)
60 retvalue = {"success": False, "value": msg}
62 except Exception:
63 self.log.error(traceback.format_exc())
64 retvalue = {"success": False, "value": traceback.format_exc()}
66 else:
67 retvalue = {"success": True, "value": result}
69 return retvalue
71 @web.authenticated
72 def post(self):
73 output = self.validate_notebook(self.get_argument("path"))
74 self.finish(json.dumps(output))
77# -----------------------------------------------------------------------------
78# URL to handler mappings
79# -----------------------------------------------------------------------------
81default_handlers = [
82 (r"/assignments/validate", ValidateAssignmentHandler),
83 (r"/nbgrader_version", NbGraderVersionHandler),
84]