-
Notifications
You must be signed in to change notification settings - Fork 50
/
quirks.py
91 lines (82 loc) · 3.2 KB
/
quirks.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
import os, sys, re, ostools
from PyQt5 import QtWidgets, QtCore
class ScriptQuirks(object):
def __init__(self):
self._datadir = ostools.getDataDir()
self.home = os.getcwd()
self.quirks = {}
self.last = {}
self.scripts = []
#self.load()
def loadModule(self, name, filename):
raise Exception
def modHas(self, module, attr):
return False
def loadAll(self):
self.last = self.quirks.copy()
self.quirks.clear()
for script in self.scripts:
print(script.getExtension())
script.load()
#print script.quirks
for q in script.quirks:
self.quirks.update(script.quirks)
for k in self.last:
if k in self.quirks:
if self.last[k] == self.quirks[k]:
del self.quirks[k]
#print self.quirks
if self.quirks:
print('Registered quirks:', '(), '.join(self.quirks) + "()")
else:
print("Warning: Couldn't find any script quirks")
def add(self, script):
self.scripts.append(script)
def load(self):
self.last = self.quirks.copy()
self.quirks.clear()
extension = self.getExtension()
filenames = []
if not os.path.exists(os.path.join(self.home, 'quirks')):
os.mkdir(os.path.join(self.home, 'quirks'))
for fn in os.listdir(os.path.join(self.home, 'quirks')):
if fn.endswith(extension) and not fn.startswith('_'):
filenames.append(os.path.join(self.home, 'quirks', fn))
if self._datadir:
if not os.path.exists(os.path.join(self._datadir, 'quirks')):
os.mkdir(os.path.join(self._datadir, 'quirks'))
for fn in os.listdir(os.path.join(self._datadir, 'quirks')):
if fn.endswith(extension) and not fn.startswith('_'):
filenames.append(os.path.join(self._datadir, 'quirks', fn))
modules = []
for filename in filenames:
extension_length = len(self.getExtension())
name = os.path.basename(filename)[:-extension_length]
try:
module = self.loadModule(name, filename)
if module is None:
continue
except Exception as e:
print("Error loading %s: %s (in quirks.py)" % (os.path.basename(name), e))
msgbox = QtWidgets.QMessageBox()
msgbox.setWindowTitle("Error!")
msgbox.setText("Error loading %s: %s (in quirks.py)" % (os.path.basename(filename), e))
msgbox.exec_()
else:
if self.modHas(module, 'setup'):
module.setup()
if self.modHas(module, 'commands'):
self.register(module)
modules.append(name)
for k in self.last:
if k in self.quirks:
if self.last[k] == self.quirks[k]:
del self.quirks[k]
def funcre(self):
if not self.quirks:
return r"\\[0-9]+"
f = r"("
for q in self.quirks:
f = f + q+r"\(|"
f = f + r"\)|\\[0-9]+)"
return f