Coverage for e2xgrader/server_extensions/base/extension.py: 40%

30 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-03-14 13:22 +0100

1from importlib import import_module 

2 

3from e2xcore.utils.utils import get_nbgrader_config 

4from jinja2 import Environment, FileSystemLoader 

5from traitlets import Any, List, TraitError, validate 

6from traitlets.config import Application 

7 

8 

9class BaseExtension(Application): 

10 apps = List( 

11 trait=Any(), 

12 default_value=[], 

13 ).tag(config=True) 

14 

15 def __init__(self, **kwargs): 

16 super().__init__(**kwargs) 

17 self.log = self.parent.log 

18 self.config = get_nbgrader_config() 

19 self.log.info(self.apps) 

20 self.initialize_jinja_environment() 

21 self.initialize_apps() 

22 

23 @validate("apps") 

24 def validate_apps(self, proposal): 

25 apps = [] 

26 for app in proposal["value"]: 

27 if isinstance(app, str): 

28 module, klass = app.rsplit(".", 1) 

29 app = getattr(import_module(module), klass) 

30 if not callable(app): 

31 raise TraitError("apps must be callable") 

32 apps.append(app) 

33 return apps 

34 

35 def initialize_apps(self): 

36 for app in self.apps: 

37 app(parent=self.parent).load_app() 

38 

39 def initialize_jinja_environment(self): 

40 self.parent.web_app.settings["e2xgrader"] = { 

41 "jinja_env": Environment(loader=FileSystemLoader([])) 

42 }