comparison frontends/src/jp/output_template.py @ 2187:4ec72927a222

jp (outputs): moved output options parsing and checking to base methods
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 23:31:06 +0100
parents f472179305a1
children a81261cee29b
comparison
equal deleted inserted replaced
2186:9061c7247964 2187:4ec72927a222
37 def __init__(self, jp): 37 def __init__(self, jp):
38 self.host = jp 38 self.host = jp
39 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render) 39 jp.register_output(C.OUTPUT_COMPLEX, TEMPLATE, self.render)
40 self.renderer = template.Renderer(jp) 40 self.renderer = template.Renderer(jp)
41 41
42 def parse_options(self, options):
43 options_dict = {}
44 for option in options:
45 try:
46 key, value = option.split(u'=', 1)
47 except ValueError:
48 key, value = option, None
49 options_dict[key.strip()] = value.strip() if value is not None else None
50 return options_dict
51
52 def render(self, data): 42 def render(self, data):
53 """render output data using requested template 43 """render output data using requested template
54 44
55 template to render the data can be either command's TEMPLATE or 45 template to render the data can be either command's TEMPLATE or
56 template output_option requested by user. 46 template output_option requested by user.
58 to the variable itself. 48 to the variable itself.
59 command's template_data_mapping attribute will be used if it exists to convert 49 command's template_data_mapping attribute will be used if it exists to convert
60 data to a dict usable by the template. 50 data to a dict usable by the template.
61 """ 51 """
62 cmd = self.host.command 52 cmd = self.host.command
63 options = self.parse_options(cmd.args.output_opts) 53 options = self.host.parse_output_options()
64 if not OPTIONS.issuperset(options): 54 self.host.check_output_options(OPTIONS, options)
65 self.host.disp(u"The following output options are invalid: {invalid_options}".format(
66 invalid_options = u', '.join(set(options).difference(OPTIONS))),
67 error=True)
68 self.host.quit(C.EXIT_BAD_ARG)
69 try: 55 try:
70 template_path = cmd.TEMPLATE 56 template_path = cmd.TEMPLATE
71 except AttributeError: 57 except AttributeError:
72 if not 'template' in cmd.args.output_opts: 58 if not 'template' in cmd.args.output_opts:
73 self.host.disp(u'no default template set for this command, ' 59 self.host.disp(u'no default template set for this command, '