Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
437 lines (387 loc) · 17.4 KB
/
Copy pathdatabase.py
File metadata and controls
437 lines (387 loc) · 17.4 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from __future__ import print_function
from __future__ import absolute_import
import weakref
import hou
import os
from . import util
from . import settings_data
from . import ptime as ptime
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase, RowIDField, FTS5Model, SearchField
import time
# --------------------------------------------- hou.session
# NOTE hou.session ----------------------------------------
def get_settings():
return getattr(hou.session, "SETTINGS", None)
def get_dbconnection():
return getattr(hou.session, "DBCONNECTION", None)
scriptpath = os.path.dirname(os.path.realpath(__file__))
db = get_dbconnection()
# --------------------------------------------------------- DatabaseModels
# SECTION DatabaseModels -------------------------------------------------
# ------------------------------------------------ Settings
# NOTE Settings -------------------------------------------
class Settings(Model):
id = IntegerField(unique=True)
indexvalue = IntegerField()
defaulthotkey = TextField()
searchdescription = IntegerField()
searchprefix = IntegerField()
searchcurrentcontext = IntegerField()
lastused = TextField()
class Meta:
table_name = 'settings'
database = db
# ------------------------------------------------ HContext
# NOTE HContext -------------------------------------------
class HContext(Model):
id = AutoField()
context = TextField(unique=True)
title = TextField()
description = TextField()
class Meta:
table_name = 'hcontext'
database = db
# # # ------------------------------------------- HContextIndex
# # # NOTE HContextIndex --------------------------------------
# class HContextIndex(FTS5Model):
# # rowid = RowIDField()
# context = SearchField()
# title = SearchField()
# description = SearchField()
# class Meta:
# database = db
# options = {'prefix': [2, 3], 'tokenize': 'porter'}
# ------------------------------------------------- Hotkeys
# NOTE Hotkeys --------------------------------------------
class Hotkeys(Model):
hotkey_symbol = CharField(unique=True)
label = CharField()
description = TextField()
assignments = TextField()
context = TextField()
class Meta:
table_name = 'hotkeys'
database = db
# -------------------------------------------- HotkeysIndex
# NOTE HotkeysIndex ---------------------------------------
class HotkeysIndex(FTS5Model):
# rowid = RowIDField()
hotkey_symbol = SearchField(unindexed=True)
label = SearchField()
description = SearchField()
assignments = SearchField(unindexed=True)
context = SearchField(unindexed=True)
def clear_index(self):
HotkeysIndex.delete().where(HotkeysIndex.rowid == self.id).execute()
class Meta:
# table_name = 'hotkeysindex'
database = db
options = {'prefix': [2, 3], 'tokenize': 'porter'}
# !SECTION
# -------------------------------------------------------------- Functions
# SECTION Functions ------------------------------------------------------
# ----------------------------------------------- py_unique
# NOTE py_unique ------------------------------------------
def py_unique(data):
return list(set(data))
# ------------------------------------------------- getdata
# NOTE getdata --------------------------------------------
def getdata():
rval = []
contextdata = []
hotkeydata = []
def getcontexts(r, context_symbol, root):
keys = None
branches = hou.hotkeys.contextsInContext(context_symbol)
for branch in branches:
branch_path = "%s/%s" % (r, branch['label'])
contextdata.append(
{'context': branch['symbol'],
'title': branch['label'],
'description': branch['help']}
)
commands = hou.hotkeys.commandsInContext(branch['symbol'])
for command in commands:
keys = hou.hotkeys.assignments(command['symbol'])
ctx = command['symbol'].rsplit('.', 1)
hotkeydata.append(
{'hotkey_symbol': command['symbol'],
'label': command['label'],
'description': command['help'],
'assignments': " ".join(keys),
'context': ctx[0]}
)
getcontexts(branch_path, branch['symbol'], root)
getcontexts("", "", rval)
return contextdata, hotkeydata
# !SECTION
# ----------------------------------------------------------- Database
# SECTION Database ---------------------------------------------------
class Databases(object):
def __init__(self):
self.settings = get_settings()
self.isdebug = util.bc(self.settings[util.SETTINGS_KEYS[4]])
inmemory = util.bc(self.settings[util.SETTINGS_KEYS[0]])
if inmemory:
val = ':memory:'
else:
val = (self.settings[util.SETTINGS_KEYS[1]])
self.db = db
if not self.db:
hou.session.DBCONNECTION = DatabaseProxy()
self.db.initialize(
SqliteExtDatabase(
val,
pragmas=(
("cache_size", -1024 * 64),
("journal_mode", "off"),
("temp_store", "memory"),
("synchronous", 0)
)))
if inmemory or not os.path.isfile(self.settings[util.SETTINGS_KEYS[1]]):
db.create_tables([
Settings,
HContext,
Hotkeys,
HotkeysIndex,]
)
self.initialsetup(self.cur)
self.cur = db.cursor()
self.isdebug = None
self.contexttime = 0
self.hotkeystime = 0
# ----------------------------------------------------------- Retrieve
# SECTION Retrieve ---------------------------------------------------
# -------------------------------------- getchangeindex
# NOTE getchangeindex ---------------------------------
def getchangeindex(self):
try:
self.cur.execute("SELECT indexvalue FROM settings")
result = self.cur.fetchall()
return result
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not get Searcher changeindex: " + str(e)), severity=hou.severityType.Error)
# ------------------------------------------- getlastusedhk
# NOTE getlastusedhk --------------------------------------
def getlastusedhk(self):
try:
lastkey = self.settings[util.SETTINGS_KEYS[11]]
if str(lastkey) != "":
lasthk = str(lastkey).split(' ')
hkcheck = hou.hotkeys.assignments(str(lasthk[0]))
if len(hkcheck) is 0:
self.settings[util.SETTINGS_KEYS[11]] = ""
settings_data.savesettings(settingdata)
return
rmresult = hou.hotkeys.removeAssignment(
str(lasthk[0]).strip(), str(lasthk[1]).strip())
if rmresult:
hkcheck = hou.hotkeys.assignments(str(lasthk[0]))
hou.hotkeys.saveOverrides()
if len(hkcheck) is 0:
self.settings[util.SETTINGS_KEYS[11]] = ""
settings_data.savesettings(settingdata)
self.updatechangeindex(int(currentidx))
else:
hou.hotkeys.clearAssignments(str(lasthk[0]))
hou.hotkeys.saveOverrides()
hkcheck = hou.hotkeys.assignments(str(lasthk[0]))
if len(hkcheck) is 0:
self.settings[util.SETTINGS_KEYS[11]] = ""
settings_data.savesettings(settingdata)
self.updatechangeindex(int(currentidx))
else:
if hou.isUIAvailable():
hou.ui.setStatusMessage(("Could not clear last assigned temp hotkey on last attempt:"), severity=hou.severityType.Warning)
else:
print("Could not clear last assigned temp hotkey on last attempt:")
else:
if hou.isUIAvailable():
hou.ui.setStatusMessage(("Could not clear last assigned temp hotkey:"), severity=hou.severityType.Warning)
else:
print("Could not clear last assigned temp hotkey:")
except(AttributeError, TypeError) as e:
if hou.isUIAvailable():
hou.ui.setStatusMessage(("Could not query last assigned temp hotkey:" + str(e)), severity=hou.severityType.Warning)
else:
print("Could not query last assigned temp hotkey: " + str(e))
# -------------------------------------------- getdefhotkey
# NOTE getdefhotkey ---------------------------------------
def getdefhotkey(self):
try:
self.cur.execute("SELECT defaulthotkey FROM settings")
result = self.cur.fetchall()
return result
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not get Searcher default hotkey: " + str(e)), severity=hou.severityType.Error)
# -------------------------------------------- gethcontexts
# NOTE gethcontexts ---------------------------------------
def gethcontexts(self):
try:
self.cur.execute("SELECT * FROM hcontext")
result = self.cur.fetchall()
return result
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not get Searcher hcontext: " + str(e)), severity=hou.severityType.Error)
# ------------------------------------------- gethcontextod
# NOTE gethcontextod --------------------------------------
def gethcontextod(self, inputlist):
try:
time1 = ptime.time()
result = []
query = (HContext
.select()
.where(HContext.context.in_(inputlist))).execute()
for hctx in query:
result.append((hctx.title, hctx.description, hctx.context))
uniqueresult = py_unique(result)
time2 = ptime.time()
self.contexttime = ((time2 - time1) * 1000.0)
return uniqueresult, self.contexttime
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not update Searcher context database: " + str(e)), severity=hou.severityType.Error)
# ---------------------------------------- ctxfilterresults
# NOTE ctxfilterresults -----------------------------------
def ctxfilterresults(self, inputTerm):
try:
result = []
query = (Hotkeys
.select()
.where(Hotkeys.context.in_(inputTerm))).execute()
for hctx in query:
result.append((hctx.label, hctx.description, hctx.assignments, hctx.hotkey_symbol, hctx.context))
uniqueresult = py_unique(result)
return uniqueresult
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not get Searcher context results: " + str(e)), severity=hou.severityType.Error)
# ------------------------------------------- searchresults
# NOTE searchresults --------------------------------------
def searchresults(self, inputTerm, debug, limit=0):
self.isdebug = debug
try:
time1 = ptime.time()
self.cur.execute(
"SELECT label, description, assignments, hotkey_symbol, context FROM hotkeysindex WHERE hotkeysindex MATCH '"
+ str(inputTerm)
+ "' ORDER BY rank"
+ " LIMIT "
+ str(limit)
)
result = self.cur.fetchall()
uniqueresult = py_unique(result)
time2 = ptime.time()
self.hotkeystime = ((time2 - time1) * 1000.0)
return uniqueresult, self.hotkeystime
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not get Searcher results: " + str(e)), severity=hou.severityType.Error)
# !SECTION
# ------------------------------------------------------------ Updates
# SECTION Updates ----------------------------------------------------
# --------------------------------------- updatechangeindex
# NOTE updatechangeindex ----------------------------------
def updatechangeindex(self, indexval, new=False):
try:
if new is True:
defaultkey = ""
for i in range(len(util.HOTKEYLIST)):
result = hou.hotkeys.findConflicts("h", util.HOTKEYLIST[i])
if not result:
defaultkey = util.HOTKEYLIST[i]
Settings.insert(indexvalue=indexval,
defaulthotkey=defaultkey, searchdescription=0, searchprefix=0, searchcurrentcontext=0, lastused="", id=1).execute()
else:
Settings.update(indexvalue=indexval).where(
Settings.id == 1).execute()
except(AttributeError, TypeError) as e:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
("Could not update Searcher context database: " + str(e)),
severity=hou.severityType.Warning
)
else:
print("Could not update Searcher context database: " + str(e))
# --------------------------------------------- updatetmphk
# NOTE updatetmphk ----------------------------------------
def updatetmphk(self, tmpkey):
try:
_ = Settings.update(
defaulthotkey=tmpkey).where(id == 1).execute()
return
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not update Searcher temp hotkey: " + str(e)), severity=hou.severityType.Error)
# ------------------------------------------- updatelastkey
# NOTE updatelastkey --------------------------------------
def updatelastkey(self, lastkey):
try:
_ = Settings.update(lastused=lastkey).where(id == 1).execute()
return
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not update Searcher temp hotkey: " + str(e)), severity=hou.severityType.Error)
# ------------------------------------------- updatecontext
# NOTE updatecontext --------------------------------------
def updatecontext(self, debug):
self.isdebug = debug
try:
time1 = ptime.time()
self.cleardatabase()
ctxdata, hkeydata = getdata()
with db.atomic():
for data_dict in ctxdata:
HContext.replace_many(data_dict).execute()
with db.atomic():
for idx in hkeydata:
Hotkeys.replace_many(idx).execute()
HotkeysIndex.replace_many(idx).execute()
time2 = ptime.time()
if self.isdebug and self.isdebug.level in {"TIMER", "ALL"}:
res = ((time2 - time1) * 1000.0)
if hou.isUIAvailable():
hou.ui.setStatusMessage(
('DB update took %0.4f ms' % res), severity=hou.severityType.Message)
else:
print('DB update took %0.4f ms' % res)
return res
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not update Searcher context database: " + str(e)), severity=hou.severityType.Error)
# !SECTION
# ------------------------------------------- cleardatabase
# NOTE cleardatabase --------------------------------------
def cleardatabase(self):
try:
delhk = "DELETE FROM hotkeys"
delctx = "DELETE FROM hcontext"
delhkindex = "DELETE FROM hotkeysindex"
# delhcindex = "DELETE FROM hcontextindex"
self.cur.execute(delhk)
self.cur.execute(delctx)
self.cur.execute(delhkindex)
result = self.cur.fetchall()
return result
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(("Could not update Searcher temp hotkey: " + str(e)),severity=hou.severityType.Error)
# -------------------------------------------- initialsetup
# NOTE initialsetup ---------------------------------------
def initialsetup(self):
currentidx = hou.hotkeys.changeIndex()
chindex = self.getchangeindex()
if len(chindex) == 0:
chindex = int(currentidx)
self.updatechangeindex(chindex, True)
self.updatecontext(self.isdebug)
if hou.isUIAvailable():
hou.ui.setStatusMessage(
"Searcher database created", severity=hou.severityType.Message)
else:
print("Searcher database created")
else:
chindex = int(chindex[0][0])
if int(currentidx) != chindex:
self.getlastusedhk()
self.updatecontext()
self.updatechangeindex(int(currentidx))
if hou.isUIAvailable():
hou.ui.setStatusMessage(
"Searcher database created and populated", severity=hou.severityType.Message)
# !SECTION
# !SECTION
You can’t perform that action at this time.
