-
Notifications
You must be signed in to change notification settings - Fork 9
/
QCon.py
149 lines (128 loc) · 4.4 KB
/
QCon.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
import sublime, sublime_plugin
from .qpython import qconnection
from .qpython.qtype import QException
from socket import error as socket_error
from . import util
class QCon():
def __init__(self, host, port, username, password, name=None, hdb=False):
self.init = False
self.hdb = hdb
self.mem = 0
self.name = name
self.host = host
self.port = port
self.username = username
self.password = password
self.q = qconnection.QConnection(host = self.host, port = self.getPort(), username = self.username, password = self.password, encoding = 'UTF-8')
@classmethod
def fromH(cls, h):
if h is None: return None
p = h.split(':')
for i in range(len(p), 4):
p.append(None)
if not p[1].isdigit():
sublime.error_message('port must be digit: ' + p[1])
raise Exception('port must be digit: ' + p[1])
return cls(p[0], int(p[1]), p[2], p[3])
@classmethod
def new(cls, name, h):
con = QCon.fromH(h)
con.name = name
return con
@classmethod
def loadFromView(cls, view):
d = view.settings().get('q_handle')
return QCon.fromDict(d)
@classmethod
def loadFromViewWithPrompt(cls, view):
d = view.settings().get('q_handle')
#return QCon.fromDict(d)
con = QCon.fromDict(d)
if con:
return con
else:
#connect first
#sublime.message_dialog('Sublime-q: Choose your q connection first!')
view.window().run_command('q_show_connection_list')
return None
@classmethod
def fromDict(cls, d):
if d is None: return None
return cls(d["host"], d["port"], d["username"], d["password"], d["name"], d["hdb"] if "hdb" in d else None)
def clone(self):
return QCon.fromDict(self.toDict())
def toDict(self):
return {
"name": self.name,
"host": self.host,
"port": self.port,
"username": self.username,
"password": self.password,
"hdb": self.hdb
}
def equals(self, other):
if type(other) is not QCon:
other = QCon.fromDict(other)
return self.name == other.name \
and self.host == other.host \
and self.port == other.port \
and self.username == other.username \
and self.password == other.password
def h(self, h=None):
if h:
p = h.split(':')
for i in range(len(p), 4):
p.append(None)
if not p[1].isdigit():
sublime.error_message('port must be digit: ' + p[1])
raise Exception('port must be digit: ' + p[1])
self.host = p[0]
self.port = int(p[1])
self.username = p[2]
self.password = p[3]
else:
s = self.host + ':' + str(self.port)
if self.username: s+= ':' + self.username
if self.password: s+= ':' + self.password
return s
def hstatus(self):
s = self.host + ':' + str(self.getPort())
if self.username: s+= ':' + self.username
if self.password: s+= ':' + self.password
return s
def getPort(self):
return self.port + (1 if self.hdb else 0)
def isHdb(self):
return self.hdb
def useHdb(self, hdb):
self.hdb = hdb
print('use hdb: ' + str(self.hdb))
self.init = False
def status(self):
status = 'OK' if self.checkOk() else 'FAIL'
name = self.name if self.name else ''
hdb = ' (HDB)' if self.hdb else ''
try:
if self.mem:
mem = ' [' + util.format_mem(int(self.mem)) + ']'
else:
mem = ''
except:
mem = ''
return status + ': ' + name + hdb + '> ' + self.hstatus() + mem
def checkOk(self):
try:
self.q.open()
if not self.init:
#only call at first time
print('init ' + self.h())
#self.q('system "c 2000 2000"') #expand output to max 2000 chars
self.init = True
self.mem = self.q('@[{.Q.w[][`used]};();0]')
except QException as e:
return False
except socket_error as serr:
return False
finally:
self.q.close()
return True