-
Notifications
You must be signed in to change notification settings - Fork 0
/
456.py
453 lines (369 loc) · 14.2 KB
/
456.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from __future__ import print_function
from __future__ import absolute_import
from searcher import settings_data
from searcher import util
from searcher import platformselect
from searcher import ptime as ptime
from searcher import language_en as la
from peewee import *
from peewee import SQL
from playhouse.sqlite_ext import SqliteExtDatabase, RowIDField, FTS5Model, SearchField
# from playhouse.apsw_ext import APSWDatabase
import inspect
import threading
import time
import hou
import hdefereval as hd
import os
import sys
# info
__author__ = "instance.id"
__copyright__ = "2020 All rights reserved."
__status__ = "Prototype"
current_file_path = os.path.abspath(
inspect.getsourcefile(lambda: 0)
)
def get_platform():
return getattr(hou.session, "PLATFORM", None)
def get_settings():
return getattr(hou.session, "SETTINGS", None)
def get_dbconnection():
return getattr(hou.session, "DBCONNECTION", None)
scriptpath = os.path.dirname(current_file_path)
dbfile = "searcher.db"
dbpath = os.path.join(
hou.homeHoudiniDirectory(), 'Searcher', dbfile
)
hou.session.SETTINGS = {}
hou.session.DBCONNECTION = DatabaseProxy()
db = DatabaseProxy()
# --------------------------------------------------------- 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)
class Meta:
# table_name = 'hotkeysindex'
database = db
options = {'prefix': [2, 3], 'tokenize': 'porter'}
# !SECTION DatabaseModels
def create_tables(dbc):
dbc.create_tables([Settings, HContext, Hotkeys, HotkeysIndex])
def worker():
hd.executeInMainThreadWithResult(updatecontext)
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
# -------------------------------------------- initialsetup
# NOTE initialsetup ---------------------------------------
def initialsetup(cur):
currentidx = hou.hotkeys.changeIndex()
chindex = getchangeindex(cur)
if len(chindex) == 0:
chindex = int(currentidx)
updatechangeindex(chindex, True)
updatedataasync()
if hou.isUIAvailable():
hou.ui.setStatusMessage(
la.MESSAGES['initialsetup1'], severity=hou.severityType.Message)
else:
print(la.MESSAGES['initialsetup1'])
else:
chindex = int(chindex[0][0])
if int(currentidx) != chindex:
getlastusedhk(cur)
updatedataasync()
updatechangeindex(int(currentidx))
if hou.isUIAvailable():
hou.ui.setStatusMessage(
la.MESSAGES['initialsetup2'], severity=hou.severityType.Message)
# --------------------------------------------------------------- Retrieve
# SECTION Retrieve -------------------------------------------------------
# ------------------------------------------ getchangeindex
# NOTE getchangeindex -------------------------------------
def getchangeindex(cur):
try:
cur.execute("SELECT indexvalue FROM settings")
result = cur.fetchall()
return result
except(AttributeError, TypeError) as e:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
(la.DBERRORMSG['getchangeindex'] + str(e)), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['getchangeindex'] + str(e))
# ------------------------------------------- getlastusedhk
# NOTE getlastusedhk --------------------------------------
def getlastusedhk(cur):
settingdata = get_settings()
lastkey = settingdata[util.SETTINGS_KEYS[11]]
try:
if str(lastkey) != "":
lasthk = str(lastkey).split(' ')
hkcheck = hou.hotkeys.assignments(str(lasthk[0]))
if len(hkcheck) is 0:
settingdata[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:
settingdata[util.SETTINGS_KEYS[11]] = ""
settings_data.savesettings(settingdata)
currentidx = hou.hotkeys.changeIndex()
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:
settingdata[util.SETTINGS_KEYS[11]] = ""
settings_data.savesettings(settingdata)
currentidx = hou.hotkeys.changeIndex()
updatechangeindex(int(currentidx))
else:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
(la.DBERRORMSG['getlastusedhk3']), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['getlastusedhk3'])
else:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
(la.DBERRORMSG['getlastusedhk2']), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['getlastusedhk2'])
except(AttributeError, TypeError) as e:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
(la.DBERRORMSG['getlastusedhk1'] + str(e)), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['getlastusedhk1'] + str(e))
# !SECTION
# ----------------------------------------------------------------- Update
# SECTION Update ---------------------------------------------------------
# ------------------------------------------------ dbupdate
# NOTE dbupdate -------------------------------------------
def dbupdate(cur):
currentidx = hou.hotkeys.changeIndex()
chindex = getchangeindex(cur)
if int(currentidx) != chindex:
getlastusedhk(cur)
updatedataasync()
updatechangeindex(int(currentidx))
# ----------------------------------------- updatedataasync
# NOTE updatedataasync ------------------------------------
def updatedataasync():
thread = threading.Thread(target=worker)
thread.daemon = True
thread.start()
# --------------------------------------- updatechangeindex
# NOTE updatechangeindex ----------------------------------
def updatechangeindex(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(
(la.DBERRORMSG['updatechangeindex'] + str(e)), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['updatechangeindex'] + str(e))
# ------------------------------------------- updatecontext
# NOTE updatecontext --------------------------------------
def updatecontext(debug=False):
try:
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()
except(AttributeError, TypeError) as e:
hou.ui.setStatusMessage(
(la.DBERRORMSG['updatecontext'] + str(e)), severity=hou.severityType.Warning)
# endregion
# ------------------------------------------- cleardatabase
# NOTE cleardatabase --------------------------------------
def cleardatabase():
try:
delhk = "DELETE FROM hotkeys"
delctx = "DELETE FROM hcontext"
delhkindex = "DELETE FROM hotkeysindex"
db.cursor().execute(delhk)
db.cursor().execute(delctx)
db.cursor().execute(delhkindex)
result = db.cursor().fetchall()
return result
except(AttributeError, TypeError) as e:
if hou.isUIAvailable():
hou.ui.setStatusMessage(
(la.DBERRORMSG['cleardatabase'] + str(e)), severity=hou.severityType.Warning)
else:
print(la.DBERRORMSG['cleardatabase'] + str(e))
# !SECTION
def deferaction(action, val):
hd.executeDeferred(action, val)
def checklasthk(cur):
getlastusedhk(cur)
def main():
platform = get_platform()
platform = platformselect.get_platform()
if not os.path.exists(settings_data.searcher_path):
os.mkdir(settings_data.searcher_path)
if not os.path.isfile(settings_data.searcher_settings):
if platform == "unix":
os.system(("touch %s" % settings_data.searcher_settings))
else:
os.mknod(settings_data.searcher_settings)
settings_data.createdefaults(platform)
hou.session.SETTINGS = settings_data.loadsettings()
settingdata = get_settings()
isdebug = util.Dbug(
settingdata[util.SETTINGS_KEYS[4]],
str(settingdata[util.SETTINGS_KEYS[10]]),
settingdata[util.SETTINGS_KEYS[12]],
settingdata[util.SETTINGS_KEYS[13]],
)
if "linux" in hou.applicationPlatformInfo():
print("Platform is Linux")
elif "windows" in hou.applicationPlatformInfo():
print("Platform is Windows")
inmemory = (settingdata[util.SETTINGS_KEYS[0]])
if inmemory:
val = ':memory:'
else:
val = str(settingdata[util.SETTINGS_KEYS[1]])
db.initialize(
SqliteExtDatabase(
val,
pragmas=(
("cache_size", -1024 * 64),
("journal_mode", "off"),
("temp_store", "memory"),
("synchronous", 0)
)))
hou.session.DBCONNECTION = db
dbc = get_dbconnection()
time1 = ptime.time()
if inmemory:
create_tables(dbc)
cur = dbc.cursor()
initialsetup(cur)
else:
if not os.path.isfile(settingdata[util.SETTINGS_KEYS[1]]):
create_tables(dbc)
cur = dbc.cursor()
deferaction(initialsetup, cur)
else:
cur = dbc.cursor()
deferaction(dbupdate, cur)
time2 = ptime.time()
if isdebug and isdebug.level in {"TIMER", "ALL"}:
res = ((time2 - time1) * 1000.0)
if hou.isUIAvailable():
hou.ui.setStatusMessage(
('Startup took %0.4f ms' % res), severity=hou.severityType.Message)
else:
print('Startup took %0.4f ms' % res)
if __name__ == '__main__':
main()