Coverage for gws-app/gws/base/template/manager.py: 81%

31 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-23 20:06 +0200

1"""Template manager.""" 

2 

3import gws 

4import gws.lib.mime 

5 

6# @TODO template types should be configurable 

7 

8TEMPLATE_TYPES = { 

9 '.cx.html': 'html', 

10 '.qgs': 'qgis', 

11 '.cx.py': 'py', 

12} 

13 

14 

15class Object(gws.TemplateManager): 

16 """Template manager.""" 

17 

18 def find_templates(self, subjects, where, user=None, mime=None): 

19 return gws.u.compact(self.find_template(s, where, user, mime) for s in subjects) 

20 

21 def find_template(self, subject, where, user=None, mime=None): 

22 for obj in where: 

23 if not obj: 

24 continue 

25 p = self._find(subject, obj, user, mime) 

26 if p: 

27 gws.log.debug(f'find_template: found {subject=} {obj=}') 

28 return p 

29 

30 p = self._find(subject, self.root.app, user, mime) 

31 if p: 

32 gws.log.debug(f'find_template: found {subject=} APP') 

33 return p 

34 

35 def _find(self, subject, obj, user, mime): 

36 """Return the first matching template of an object.""" 

37 for tpl in getattr(obj, 'templates', []): 

38 if tpl.subject != subject: 

39 continue 

40 if user and not user.can_use(tpl): 

41 continue 

42 if mime and tpl.mimeTypes and mime not in tpl.mimeTypes: 

43 continue 

44 return tpl 

45 

46 def template_from_path(self, path, **kwargs): 

47 for ext, typ in TEMPLATE_TYPES.items(): 

48 if path.endswith(ext): 

49 return self.root.create_shared( 

50 gws.ext.object.template, 

51 gws.Config(uid=gws.u.sha256(path), type=typ, path=path), 

52 **kwargs 

53 )