comparison sat_frontends/jp/output_std.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/output_std.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """Standard outputs"""
20
21
22 from sat_frontends.jp.constants import Const as C
23 from sat.tools.common.ansi import ANSI as A
24 import json
25
26 __outputs__ = ["Simple", "Json"]
27 SIMPLE = u'simple'
28 JSON = u'json'
29 JSON_RAW = u'json_raw'
30
31
32 class Simple(object):
33 """Default outputs"""
34
35 def __init__(self, host):
36 self.host = host
37 host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
38 host.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
39 host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
40 host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict)
41 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict)
42 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
43
44 def simple_print(self, data):
45 self.host.disp(unicode(data))
46
47 def list(self, data):
48 self.host.disp(u'\n'.join(data))
49
50 def dict(self, data, indent=0, header_color=C.A_HEADER):
51 options = self.host.parse_output_options()
52 self.host.check_output_options({u'no-header'}, options)
53 show_header = not u'no-header' in options
54 for k, v in data.iteritems():
55 if show_header:
56 header = A.color(header_color, k) + u': '
57 else:
58 header = u''
59
60 self.host.disp((u'{indent}{header}{value}'.format(
61 indent=indent*u' ',
62 header=header,
63 value=v)))
64
65 def list_dict(self, data):
66 for idx, datum in enumerate(data):
67 if idx:
68 self.host.disp(u'\n')
69 self.dict(datum)
70
71 def dict_dict(self, data):
72 for key, sub_dict in data.iteritems():
73 self.host.disp(A.color(C.A_HEADER, key))
74 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER)
75
76
77 class Json(object):
78 """outputs in json format"""
79
80 def __init__(self, host):
81 self.host = host
82 host.register_output(C.OUTPUT_TEXT, JSON, self.dump)
83 host.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty)
84 host.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
85 host.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
86 host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
87 host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty)
88 host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump)
89 host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty)
90 host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump)
91 host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
92 host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
93
94 def dump(self, data):
95 self.host.disp(json.dumps(data, default=str))
96
97 def dump_pretty(self, data):
98 self.host.disp(json.dumps(data, indent=4, default=str))