Mercurial > libervia-backend
comparison libervia/cli/output_template.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 47401850dec6 |
children |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
41 self.host = libervia_cli | 41 self.host = libervia_cli |
42 libervia_cli.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render) | 42 libervia_cli.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['template_data'] | 46 template_data = ctx["template_data"] |
47 return "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(_("Can't find requested template: {template_path}") | 53 self.host.disp( |
54 .format(template_path=template_path), error=True) | 54 _("Can't find requested template: {template_path}").format( |
55 template_path=template_path | |
56 ), | |
57 error=True, | |
58 ) | |
55 self.host.quit(C.EXIT_NOT_FOUND) | 59 self.host.quit(C.EXIT_NOT_FOUND) |
56 | 60 |
57 def render(self, data): | 61 def render(self, data): |
58 """render output data using requested template | 62 """render output data using requested template |
59 | 63 |
69 cmd = self.host.command | 73 cmd = self.host.command |
70 try: | 74 try: |
71 template_path = cmd.TEMPLATE | 75 template_path = cmd.TEMPLATE |
72 except AttributeError: | 76 except AttributeError: |
73 if not "template" in cmd.args.output_opts: | 77 if not "template" in cmd.args.output_opts: |
74 self.host.disp(_( | 78 self.host.disp( |
75 "no default template set for this command, you need to specify a " | 79 _( |
76 "template using --oo template=[path/to/template.html]"), | 80 "no default template set for this command, you need to specify a " |
81 "template using --oo template=[path/to/template.html]" | |
82 ), | |
77 error=True, | 83 error=True, |
78 ) | 84 ) |
79 self.host.quit(C.EXIT_BAD_ARG) | 85 self.host.quit(C.EXIT_BAD_ARG) |
80 | 86 |
81 options = self.host.parse_output_options() | 87 options = self.host.parse_output_options() |
84 template_path = options["template"] | 90 template_path = options["template"] |
85 except KeyError: | 91 except KeyError: |
86 # template is not specified, we use default one | 92 # template is not specified, we use default one |
87 pass | 93 pass |
88 if template_path is None: | 94 if template_path is None: |
89 self.host.disp(_("Can't parse template, please check its syntax"), | 95 self.host.disp(_("Can't parse template, please check its syntax"), error=True) |
90 error=True) | |
91 self.host.quit(C.EXIT_BAD_ARG) | 96 self.host.quit(C.EXIT_BAD_ARG) |
92 | 97 |
93 try: | 98 try: |
94 mapping_cb = cmd.template_data_mapping | 99 mapping_cb = cmd.template_data_mapping |
95 except AttributeError: | 100 except AttributeError: |
102 if "browser" in options: | 107 if "browser" in options: |
103 template_name = os.path.basename(template_path) | 108 template_name = os.path.basename(template_path) |
104 tmp_dir = tempfile.mkdtemp() | 109 tmp_dir = tempfile.mkdtemp() |
105 front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir) | 110 front_url_filter = partial(self._front_url_tmp_dir, tmp_dir=tmp_dir) |
106 self.renderer = template.Renderer( | 111 self.renderer = template.Renderer( |
107 self.host, front_url_filter=front_url_filter, trusted=True) | 112 self.host, front_url_filter=front_url_filter, trusted=True |
113 ) | |
108 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) | 114 rendered = self._do_render(template_path, css_inline=css_inline, **kwargs) |
109 self.host.disp(_( | 115 self.host.disp( |
110 "Browser opening requested.\n" | 116 _( |
111 "Temporary files are put in the following directory, you'll have to " | 117 "Browser opening requested.\n" |
112 "delete it yourself once finished viewing: {}").format(tmp_dir)) | 118 "Temporary files are put in the following directory, you'll have to " |
119 "delete it yourself once finished viewing: {}" | |
120 ).format(tmp_dir) | |
121 ) | |
113 tmp_file = os.path.join(tmp_dir, template_name) | 122 tmp_file = os.path.join(tmp_dir, template_name) |
114 with open(tmp_file, "w") as f: | 123 with open(tmp_file, "w") as f: |
115 f.write(rendered.encode("utf-8")) | 124 f.write(rendered.encode("utf-8")) |
116 theme, theme_root_path = self.renderer.get_theme_and_root(template_path) | 125 theme, theme_root_path = self.renderer.get_theme_and_root(template_path) |
117 if theme is None: | 126 if theme is None: |
120 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) | 129 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) |
121 if os.path.exists(static_dir): | 130 if os.path.exists(static_dir): |
122 # we have to copy static files in a subdirectory, to avoid file download | 131 # we have to copy static files in a subdirectory, to avoid file download |
123 # to be blocked by same origin policy | 132 # to be blocked by same origin policy |
124 import shutil | 133 import shutil |
134 | |
125 shutil.copytree( | 135 shutil.copytree( |
126 static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR) | 136 static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR) |
127 ) | 137 ) |
128 webbrowser.open(tmp_file) | 138 webbrowser.open(tmp_file) |
129 else: | 139 else: |