-
Notifications
You must be signed in to change notification settings - Fork 54
/
jpterm.py
239 lines (205 loc) · 8.38 KB
/
jpterm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""JMESPath text terminal."""
import os
import sys
import json
import argparse
import urwid
import jmespath
import pygments.lexers
import collections
__version__ = '0.2.2'
SAMPLE_JSON = {
'a': 'foo',
'b': 2,
'c': {
'd': 'baz',
'e': [1, 2, 3]
},
"d": True,
"e": None,
"f": 1.1
}
OUTPUT_MODES = [
'result',
'expression',
'quiet',
]
class ConsoleJSONFormatter(object):
# We only need to worry about the tokens that can come
# from lexing JSON.
TOKEN_TYPES = {
# For the values of JSON strings.
'Token.Literal.String.Double': urwid.AttrSpec('dark green', 'default'),
'Token.Literal.Number.Integer': urwid.AttrSpec('dark blue', 'default'),
'Token.Literal.Number.Float': urwid.AttrSpec('dark blue', 'default'),
# null, true, false
'Token.Keyword.Constant': urwid.AttrSpec('light blue', 'default'),
'Token.Punctuation': urwid.AttrSpec('light blue', 'default'),
'Token.Text': urwid.AttrSpec('white', 'default'),
# Key names in a hash.
'Token.Name.Tag': urwid.AttrSpec('white', 'default'),
}
# Used when the token name is not in the list above.
DEFAULT_COLOR = urwid.AttrSpec('light blue', 'default'),
def generate_colors(self, tokens):
types = self.TOKEN_TYPES
default = self.DEFAULT_COLOR
for token_type, token_string in tokens:
yield types.get(str(token_type), default), token_string
class JMESPathDisplay(object):
PALETTE = [
('input expr', 'black,bold', 'light gray'),
('bigtext', 'white', 'black'),
]
def __init__(self, input_data, output_mode='result'):
self.view = None
self.parsed_json = input_data
self.lexer = pygments.lexers.get_lexer_by_name('json')
self.formatter = ConsoleJSONFormatter()
self.output_mode = output_mode
self.last_result = None
self.last_expression = None
def _create_colorized_json(self, json_string):
tokens = self.lexer.get_tokens(json_string)
markup = list(self.formatter.generate_colors(tokens))
return markup
def _get_font_instance(self):
return urwid.get_all_fonts()[-2][1]()
def _create_view(self):
self.input_expr = urwid.Edit(('input expr', "JMESPath Expression: "))
sb = urwid.BigText("JMESPath", self._get_font_instance())
sb = urwid.Padding(sb, 'center', None)
sb = urwid.AttrWrap(sb, 'bigtext')
sb = urwid.Filler(sb, 'top', None, 5)
self.status_bar = urwid.BoxAdapter(sb, 5)
div = urwid.Divider()
self.header = urwid.Pile(
[self.status_bar, div,
urwid.AttrMap(self.input_expr, 'input expr'), div],
focus_item=2)
urwid.connect_signal(self.input_expr, 'change', self._on_edit)
self.input_json = urwid.Text(
self._create_colorized_json(json.dumps(self.parsed_json,
indent=2))
)
self.input_json_list = [div, self.input_json]
self.left_content = urwid.ListBox(self.input_json_list)
self.left_content = urwid.LineBox(self.left_content,
title='Input JSON')
self.jmespath_result = urwid.Text("")
self.jmespath_result_list = [div, self.jmespath_result]
self.right_content = urwid.ListBox(self.jmespath_result_list)
self.right_content = urwid.LineBox(self.right_content,
title='JMESPath Result')
self.content = urwid.Columns([self.left_content, self.right_content])
self.footer = urwid.Text("Status: ")
self.view = urwid.Frame(body=self.content, header=self.header,
footer=self.footer, focus_part='header')
def _on_edit(self, widget, text):
self.last_expression = text
if not text:
# If a user has hit backspace until there's no expression
# left, we can exit early and just clear the result text
# panel.
self.jmespath_result.set_text('')
return
try:
options = jmespath.Options(dict_cls=collections.OrderedDict)
result = jmespath.compile(text).search(self.parsed_json, options)
self.footer.set_text("Status: success")
except Exception:
pass
else:
if result is not None:
self.last_result = result
result_markup = self._create_colorized_json(
json.dumps(result, indent=2))
self.jmespath_result.set_text(result_markup)
def main(self, screen=None):
self._create_view()
self.loop = urwid.MainLoop(self.view, self.PALETTE,
unhandled_input=self.unhandled_input,
screen=screen)
self.loop.screen.set_terminal_properties(colors=256)
self.loop.run()
def unhandled_input(self, key):
if key == 'f5':
raise urwid.ExitMainLoop()
elif key == 'ctrl ]':
# Keystroke to quickly empty out the
# currently entered expression. Avoids
# having to hold backspace to delete
# the current expression current expression.
self.input_expr.edit_text = ''
self.jmespath_result.set_text('')
elif key == 'ctrl p':
new_mode = OUTPUT_MODES[
(OUTPUT_MODES.index(self.output_mode) + 1) % len(OUTPUT_MODES)]
self.output_mode = new_mode
self.footer.set_text("Status: output mode set to %s" % new_mode)
def display_output(self, filename):
if self.output_mode == 'result' and \
self.last_result is not None:
result = json.dumps(self.last_result, indent=2)
elif self.output_mode == 'expression' and \
self.last_expression is not None:
result = self.last_expression
else:
# If the output_mode is 'quiet' then we don't need to print anything.
return
if filename is not None:
with open(filename, 'w') as f:
f.write(result)
else:
sys.stdout.write(result)
def _load_input_json(filename):
if filename is not None:
with open(filename) as f:
input_json = json.load(f)
elif not os.isatty(sys.stdin.fileno()):
# If stdin is a pipe, we need read the JSON from
# stdin and then reset stdin this back to the controlling tty.
input_json = json.loads(sys.stdin.read())
sys.stdin = open(os.ctermid(), 'r')
else:
# If the user didn't provide a filename,
# we want to be helpful so we'll use a sample
# document so they can still try out the
# JMESPath Terminal.
input_json = SAMPLE_JSON
return input_json
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input-json', nargs='?',
help='The initial input JSON file to use. '
'If this value is not provided, a sample '
'JSON document will be provided.')
parser.add_argument('-m', '--output-mode',
choices=OUTPUT_MODES,
default='result',
help="Specify what's printed to stdout "
"when jpterm exits. This can also be changed "
"when jpterm is running using Ctrl-o")
parser.add_argument('-o', '--output-file',
help="By default, the output is printed "
"to stdout when jpterm exits. You can "
"instead direct the output to a file using "
"the -o/--ouput-file option.")
parser.add_argument('--version', action='version',
version='jmespath-term %s' % __version__)
args = parser.parse_args()
try:
input_json = _load_input_json(getattr(args, 'input-json', None))
except ValueError as e:
sys.stderr.write("Unable to load the input JSON: %s\n\n" % e)
return 1
screen = urwid.raw_display.Screen()
display = JMESPathDisplay(input_json, args.output_mode)
try:
display.main(screen=screen)
except KeyboardInterrupt:
pass
display.display_output(args.output_file)
return 0
if __name__ == '__main__':
sys.exit(main())