Mercurial > libervia-backend
comparison sat_frontends/jp/output_template.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 0fa217fafabf |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
25 import webbrowser | 25 import webbrowser |
26 import tempfile | 26 import tempfile |
27 import os.path | 27 import os.path |
28 | 28 |
29 __outputs__ = ["Template"] | 29 __outputs__ = ["Template"] |
30 TEMPLATE = u'template' | 30 TEMPLATE = u"template" |
31 OPTIONS = {u'template', u'browser', u'inline-css'} | 31 OPTIONS = {u"template", u"browser", u"inline-css"} |
32 | 32 |
33 | 33 |
34 class Template(object): | 34 class Template(object): |
35 """outputs data using SàT templates""" | 35 """outputs data using SàT templates""" |
36 | 36 |
47 to the variable itself. | 47 to the variable itself. |
48 command's template_data_mapping attribute will be used if it exists to convert | 48 command's template_data_mapping attribute will be used if it exists to convert |
49 data to a dict usable by the template. | 49 data to a dict usable by the template. |
50 """ | 50 """ |
51 # media_dir is needed for the template | 51 # media_dir is needed for the template |
52 self.host.media_dir = self.host.bridge.getConfig('', 'media_dir') | 52 self.host.media_dir = self.host.bridge.getConfig("", "media_dir") |
53 cmd = self.host.command | 53 cmd = self.host.command |
54 try: | 54 try: |
55 template_path = cmd.TEMPLATE | 55 template_path = cmd.TEMPLATE |
56 except AttributeError: | 56 except AttributeError: |
57 if not 'template' in cmd.args.output_opts: | 57 if not "template" in cmd.args.output_opts: |
58 self.host.disp(u'no default template set for this command, ' | 58 self.host.disp( |
59 u'you need to specify a template using --oo template=[path/to/template.html]', | 59 u"no default template set for this command, " |
60 error=True) | 60 u"you need to specify a template using --oo template=[path/to/template.html]", |
61 error=True, | |
62 ) | |
61 self.host.quit(C.EXIT_BAD_ARG) | 63 self.host.quit(C.EXIT_BAD_ARG) |
62 | 64 |
63 options = self.host.parse_output_options() | 65 options = self.host.parse_output_options() |
64 self.host.check_output_options(OPTIONS, options) | 66 self.host.check_output_options(OPTIONS, options) |
65 self.renderer = template.Renderer(self.host) | 67 self.renderer = template.Renderer(self.host) |
66 try: | 68 try: |
67 template_path = options['template'] | 69 template_path = options["template"] |
68 except KeyError: | 70 except KeyError: |
69 # template is not specified, we use default one | 71 # template is not specified, we use default one |
70 pass | 72 pass |
71 if template_path is None: | 73 if template_path is None: |
72 self.host.disp(u"Can't parse template, please check its syntax", | 74 self.host.disp(u"Can't parse template, please check its syntax", error=True) |
73 error=True) | |
74 self.host.quit(C.EXIT_BAD_ARG) | 75 self.host.quit(C.EXIT_BAD_ARG) |
75 | 76 |
76 try: | 77 try: |
77 mapping_cb = cmd.template_data_mapping | 78 mapping_cb = cmd.template_data_mapping |
78 except AttributeError: | 79 except AttributeError: |
79 kwargs = data | 80 kwargs = data |
80 else: | 81 else: |
81 kwargs = mapping_cb(data) | 82 kwargs = mapping_cb(data) |
82 | 83 |
83 css_inline = u'inline-css' in options | 84 css_inline = u"inline-css" in options |
84 rendered = self.renderer.render(template_path, css_inline=css_inline, **kwargs) | 85 rendered = self.renderer.render(template_path, css_inline=css_inline, **kwargs) |
85 | 86 |
86 if 'browser' in options: | 87 if "browser" in options: |
87 template_name = os.path.basename(template_path) | 88 template_name = os.path.basename(template_path) |
88 tmp_dir = tempfile.mkdtemp() | 89 tmp_dir = tempfile.mkdtemp() |
89 self.host.disp(_(u"Browser opening requested.\nTemporary files are put in the following directory, " | 90 self.host.disp( |
90 u"you'll have to delete it yourself once finished viewing: {}").format(tmp_dir)) | 91 _( |
92 u"Browser opening requested.\nTemporary files are put in the following directory, " | |
93 u"you'll have to delete it yourself once finished viewing: {}" | |
94 ).format(tmp_dir) | |
95 ) | |
91 tmp_file = os.path.join(tmp_dir, template_name) | 96 tmp_file = os.path.join(tmp_dir, template_name) |
92 with open(tmp_file, 'w') as f: | 97 with open(tmp_file, "w") as f: |
93 f.write(rendered.encode('utf-8')) | 98 f.write(rendered.encode("utf-8")) |
94 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) | 99 theme, theme_root_path = self.renderer.getThemeAndRoot(template_path) |
95 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) | 100 static_dir = os.path.join(theme_root_path, C.TEMPLATE_STATIC_DIR) |
96 if os.path.exists(static_dir): | 101 if os.path.exists(static_dir): |
97 import shutil | 102 import shutil |
98 shutil.copytree(static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR)) | 103 |
104 shutil.copytree( | |
105 static_dir, os.path.join(tmp_dir, theme, C.TEMPLATE_STATIC_DIR) | |
106 ) | |
99 webbrowser.open(tmp_file) | 107 webbrowser.open(tmp_file) |
100 else: | 108 else: |
101 self.host.disp(rendered) | 109 self.host.disp(rendered) |