forked from kollhof/sublime-cypher
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cypher.py
133 lines (101 loc) · 3.27 KB
/
Cypher.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
import sublime
import sublime_plugin
import urllib2
import json
def item_to_str(item):
if isinstance(item, dict):
data = json.dumps(item['data'])
id_ = item['self'].rsplit('/', 1)[1]
if 'type' in item:
return "[%s:%s %s]" % (
id_, item['type'], data)
else:
return "(%s %s)" % (id_, data)
else:
try:
return str(item)
except ValueError:
return str(item.encode('utf-8'))
def print_table(headers, items):
column_widths = {}
rows = []
for col_idx, header in enumerate(headers):
column_widths[col_idx] = len(header)
for row in items:
row = tuple(item_to_str(item) for item in row)
for col_idx, data in enumerate(row):
col_width = column_widths[col_idx]
column_widths[col_idx] = max(col_width, len(data))
rows.append(row)
headers = [c.center(column_widths[i]) for i, c in enumerate(headers)]
lines = ['-' * column_widths[i] for i, c in enumerate(headers)]
lines = '+-' + '-+-'.join(lines) + '-+'
print lines
print '| ' + ' | '.join(headers) + ' |'
print lines
for row in rows:
fields = [c.ljust(column_widths[i]) for i, c in enumerate(row)]
print '| ' + ' | '.join(fields) + ' |'
print lines
print "%d items returned" % len(rows)
def print_error(file_name, query, err):
try:
ex = err['exception']
msg = err['message']
print '------------'
print msg
print '==============='
if ex == 'SyntaxException':
try:
_, match, col = msg.rsplit('\n', 2)
col = col.index('^')
match = match.strip('"')
line_num = 0
for line_num, lne in enumerate(query.splitlines()):
if match in lne:
break
line_num += 1
msg = 'File "%s:%s:%s": %s' % (file_name, line_num, col, msg)
print msg
return line_num, col
except Exception, e:
pass
msg = 'File "%s"\n%s: %s' % (file_name, ex, msg)
except Exception as e:
msg = e
from traceback import format_exc
msg = format_exc()
print msg
def cypher(query, **args):
data = {
"query": query,
"params": args
}
data = json.dumps(data)
req = urllib2.Request(
url="http://localhost:7474/db/data/cypher",
data=data)
req.add_header('Accept', 'application/json')
req.add_header('Content-Type', 'application/json')
try:
resp = urllib2.urlopen(req)
except urllib2.HTTPError as err:
if err.code == 400:
err = json.loads(err.read())
return print_error('', query, err)
else:
print err
return
else:
resp = json.loads(resp.read())
columns = resp['columns']
rows = resp['data']
print_table(columns, rows)
class CypherCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
if sel.size() == 0:
sel = sublime.Region(0, self.view.size())
query = self.view.substr(sel)
cypher(query)