forked from Bioconductor-notebooks/BiocImageBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIDockerfileEditor.py
67 lines (56 loc) · 2.62 KB
/
UIDockerfileEditor.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
from PyQt5.QtGui import (
QBrush, QFont, QSyntaxHighlighter, QTextCharFormat
)
from PyQt5.QtCore import Qt, QRegExp
keyword_list = ['ADD', 'COPY', 'RUN', 'FROM', 'CMD', 'ENTRYPOINT',
'VOLUME', 'LABEL', 'MAINTAINER', 'EXPOSE', 'ENV',
'USER', 'WORKDIR', 'ARG', 'STOPSIGNAL', 'HEALTHCHECK', 'SHELL']
class DockerSyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, parent=None):
self.keywordFormat = self._text_format(Qt.blue)
self.stringFormat = self._text_format(Qt.darkRed)
self.commentFormat = self._text_format(Qt.darkGreen)
self.decoratorFormat = self._text_format(Qt.darkGray)
self.keywords = list(keyword_list)
self.rules = [(QRegExp(r"\b%s\b" % kwd), self.keywordFormat)
for kwd in self.keywords] + \
[(QRegExp(r"'.*'"), self.stringFormat),
(QRegExp(r'".*"'), self.stringFormat),
(QRegExp(r"#.*"), self.commentFormat),
(QRegExp(r"@[A-Za-z_]+[A-Za-z0-9_]+"),
self.decoratorFormat)]
self.multilineStart = QRegExp(r"(''')|" + r'(""")')
self.multilineEnd = QRegExp(r"(''')|" + r'(""")')
super().__init__(parent)
def highlightBlock(self, text):
for pattern, format in self.rules:
exp = QRegExp(pattern)
index = exp.indexIn(text)
while index >= 0:
length = exp.matchedLength()
if exp.captureCount() > 0:
self.setFormat(exp.pos(1), len(str(exp.cap(1))), format)
else:
self.setFormat(exp.pos(0), len(str(exp.cap(0))), format)
index = exp.indexIn(text, index + length)
# Multi line strings
start = self.multilineStart
end = self.multilineEnd
self.setCurrentBlockState(0)
startIndex, skip = 0, 0
if self.previousBlockState() != 1:
startIndex, skip = start.indexIn(text), 3
while startIndex >= 0:
endIndex = end.indexIn(text, startIndex + skip)
if endIndex == -1:
self.setCurrentBlockState(1)
commentLen = len(text) - startIndex
else:
commentLen = endIndex - startIndex + 3
self.setFormat(startIndex, commentLen, self.stringFormat)
startIndex, skip = (start.indexIn(text,startIndex + commentLen + 3), 3)
def _text_format(self, foreground=Qt.black, weight=QFont.Normal):
fmt = QTextCharFormat()
fmt.setForeground(QBrush(foreground))
fmt.setFontWeight(weight)
return fmt