comparison sat_frontends/jp/output_template.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
28 import webbrowser 28 import webbrowser
29 import tempfile 29 import tempfile
30 import os.path 30 import os.path
31 31
32 __outputs__ = ["Template"] 32 __outputs__ = ["Template"]
33 TEMPLATE = u"template" 33 TEMPLATE = "template"
34 OPTIONS = {u"template", u"browser", u"inline-css"} 34 OPTIONS = {"template", "browser", "inline-css"}
35 35
36 36
37 class Template(object): 37 class Template(object):
38 """outputs data using SàT templates""" 38 """outputs data using SàT templates"""
39 39
41 self.host = jp 41 self.host = jp
42 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render) 42 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render)
43 43
44 def _front_url_tmp_dir(self, ctx, relative_url, tmp_dir): 44 def _front_url_tmp_dir(self, ctx, relative_url, tmp_dir):
45 """Get front URL for temporary directory""" 45 """Get front URL for temporary directory"""
46 template_data = ctx[u'template_data'] 46 template_data = ctx['template_data']
47 return u"file://" + os.path.join(tmp_dir, template_data.theme, relative_url) 47 return "file://" + os.path.join(tmp_dir, template_data.theme, relative_url)
48 48
49 def _do_render(self, template_path, css_inline, **kwargs): 49 def _do_render(self, template_path, css_inline, **kwargs):
50 try: 50 try:
51 return self.renderer.render(template_path, css_inline=css_inline, **kwargs) 51 return self.renderer.render(template_path, css_inline=css_inline, **kwargs)
52 except template.TemplateNotFound: 52 except template.TemplateNotFound:
53 self.host.disp(_(u"Can't find requested template: {template_path}") 53 self.host.disp(_("Can't find requested template: {template_path}")
54 .format(template_path=template_path), error=True) 54 .format(template_path=template_path), error=True)
55 self.host.quit(C.EXIT_NOT_FOUND) 55 self.host.quit(C.EXIT_NOT_FOUND)
56 56
57 def render(self, data): 57 def render(self, data):
58 """render output data using requested template 58 """render output data using requested template
70 try: 70 try:
71 template_path = cmd.TEMPLATE 71 template_path = cmd.TEMPLATE
72 except AttributeError: 72 except AttributeError:
73 if not "template" in cmd.args.output_opts: 73 if not "template" in cmd.args.output_opts:
74 self.host.disp(_( 74 self.host.disp(_(
75 u"no default template set for this command, you need to specify a " 75 "no default template set for this command, you need to specify a "
76 u"template using --oo template=[path/to/template.html]"), 76 "template using --oo template=[path/to/template.html]"),
77 error=True, 77 error=True,
78 ) 78 )
79 self.host.quit(C.EXIT_BAD_ARG) 79 self.host.quit(C.EXIT_BAD_ARG)
80 80
81 options = self.host.parse_output_options() 81 options = self.host.parse_output_options()
84 template_path = options["template"] 84 template_path = options["template"]
85 except KeyError: 85 except KeyError:
86 # template is not specified, we use default one 86 # template is not specified, we use default one
87 pass 87 pass
88 if template_path is None: 88 if template_path is None:
89 self.host.disp(_(u"Can't parse template, please check its syntax"), 89 self.host.disp(_("Can't parse template, please check its syntax"),
90 error=True) 90 error=True)
91 self.host.quit(C.EXIT_BAD_ARG) 91 self.host.quit(C.EXIT_BAD_ARG)
92 92
93 try: 93 try:
94 mapping_cb = cmd.template_data_mapping 94 mapping_cb = cmd.template_data_mapping
95 except AttributeError: 95 except AttributeError:
96 kwargs = data 96 kwargs = data
97 else: 97 else:
98 kwargs = mapping_cb(data) 98 kwargs = mapping_cb(data)
99 99
100 css_inline = u"inline-css" in options 100 css_inline = "inline-css" in options
101 101
102 if "browser" in options: 102 if "browser" in options:
103 template_name = os.path.basename(template_path) 103 template_name = os.path.basename(template_path)
104 tmp_dir = tempfile.mkdtemp() 104 tmp_dir = tempfile.mkdtemp()
105 front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir) 105 front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir)
106 self.renderer = template.Renderer( 106 self.renderer = template.Renderer(
107 self.host, front_url_filter=front_url_filter, trusted=True) 107 self.host, front_url_filter=front_url_filter, trusted=True)
108 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) 108 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs)
109 self.host.disp(_( 109 self.host.disp(_(
110 u"Browser opening requested.\n" 110 "Browser opening requested.\n"
111 u"Temporary files are put in the following directory, you'll have to " 111 "Temporary files are put in the following directory, you'll have to "
112 u"delete it yourself once finished viewing: {}").format(tmp_dir)) 112 "delete it yourself once finished viewing: {}").format(tmp_dir))
113 tmp_file = os.path.join(tmp_dir, template_name) 113 tmp_file = os.path.join(tmp_dir, template_name)
114 with open(tmp_file, "w") as f: 114 with open(tmp_file, "w") as f:
115 f.write(rendered.encode("utf-8")) 115 f.write(rendered.encode("utf-8"))
116 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) 116 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path)
117 if theme is None: 117 if theme is None:
127 ) 127 )
128 webbrowser.open(tmp_file) 128 webbrowser.open(tmp_file)
129 else: 129 else:
130 # FIXME: Q&D way to disable template logging 130 # FIXME: Q&D way to disable template logging
131 # logs are overcomplicated, and need to be reworked 131 # logs are overcomplicated, and need to be reworked
132 template_logger = log.getLogger(u"sat.tools.common.template") 132 template_logger = log.getLogger("sat.tools.common.template")
133 template_logger.log = lambda *args: None 133 template_logger.log = lambda *args: None
134 134
135 logging.disable(logging.WARNING) 135 logging.disable(logging.WARNING)
136 self.renderer = template.Renderer(self.host, trusted=True) 136 self.renderer = template.Renderer(self.host, trusted=True)
137 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) 137 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs)