-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
56 lines (49 loc) · 1.99 KB
/
utils.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
def set_environment():
# BEGIN virtualenv dynamic import
import os
import sys
env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'.env', 'lib')
if os.path.exists(env_path):
major, minor = sys.version_info.major, sys.version_info.minor
zip_path = os.path.join(env_path,
'python{major}{minor}.zip'.format(**locals()))
py_path = os.path.join(env_path,
'python{major}.{minor}'.format(**locals()))
LIBS = [zip_path, py_path] + \
[os.path.join(py_path, lib) for lib in ('plat-linux',
'lib-dynload',
'site-packages')]
for lib in LIBS:
sys.path.append(lib)
else:
raise RuntimeError('Virtualenv not found at {env_path}.'
''.format(**locals()))
# END virtualenv dynamic import
def load_ui_form(basedir, name):
# XXX We're assuming here max-depth of 1 package
from importlib import import_module
# Only 1 form per file!
module = import_module(basedir + '.' + name)
for key, value in module.__dict__.items():
if key.startswith('Ui'):
return value
raise Exception('Malformed UI file')
def compile_if_needed(fpath):
# XXX We're assuming here max-depth of 1 package
import os
from PyQt5.uic import compileUi
path = os.path.dirname(fpath)
package = os.path.basename(path)
name = os.path.splitext(os.path.basename(fpath))[0]
ui_name = name + '.ui'
module = 'ui_' + name
with open(os.path.join(path, module + '.py'), 'w') as fd:
compileUi(os.path.join(path, ui_name), fd)
return (package, module)
def pyqt_set_trace():
'''Set a tracepoint in the Python debugger that works with Qt'''
from PyQt5.QtCore import pyqtRemoveInputHook
from pdb import set_trace
pyqtRemoveInputHook()
set_trace()