-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
137 lines (108 loc) · 3.14 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
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
# -*- coding: utf-8 -*-
from . import core as html5
################################################################
# NOTE: This part of the html5 library is superseded by flare. #
# It is not improved anymore, and just remains here for #
# existing projects. #
# #
# Visit https://github.com/mausbrand/flare for details. #
################################################################
def unescape(val, maxLength = 0):
"""
Unquotes several HTML-quoted characters in a string.
:param val: The value to be unescaped.
:type val: str
:param maxLength: Cut-off after maxLength characters.
A value of 0 means "unlimited". (default)
:type maxLength: int
:returns: The unquoted string.
:rtype: str
"""
val = val \
.replace("<", "<") \
.replace(">", ">") \
.replace(""", "\"") \
.replace("'", "'")
if maxLength > 0:
return val[0:maxLength]
return val
def doesEventHitWidgetOrParents(event, widget):
"""
Test if event 'event' hits widget 'widget' (or *any* of its parents)
"""
while widget:
if event.target == widget.element:
return widget
widget = widget.parent()
return None
def doesEventHitWidgetOrChildren(event, widget):
"""
Test if event 'event' hits widget 'widget' (or *any* of its children)
"""
if event.target == widget.element:
return widget
for child in widget.children():
if doesEventHitWidgetOrChildren(event, child):
return child
return None
def textToHtml(node, text, clear=False):
"""
Generates html nodes from text by splitting text into content and into
line breaks html5.Br.
:param node: The node where the nodes are appended to.
:param text: The text to be inserted.
:param clear: Clear node before inserting text
"""
if clear:
node.removeAllChildren()
for (i, part) in enumerate(text.split("\n")):
if i > 0:
node.appendChild(html5.Br())
node.appendChild(html5.TextNode(part))
def parseInt(s, ret = 0):
"""
Parses a value as int
"""
if not isinstance(s, str):
return int(s)
elif s:
if s[0] in "+-":
ts = s[1:]
else:
ts = s
if ts and all([_ in "0123456789" for _ in ts]):
return int(s)
return ret
def parseFloat(s, ret = 0.0):
"""
Parses a value as float.
"""
if not isinstance(s, str):
return float(s)
elif s:
if s[0] in "+-":
ts = s[1:]
else:
ts = s
if ts and ts.count(".") <= 1 and all([_ in ".0123456789" for _ in ts]):
return float(s)
return ret
def importJS(*args, **kwargs):
"""
Dynamically imports the provided JavaScript files sequentially.
This has to be done when multiple scripts are bootstrapped, and cause errors in case they're all loaded
simultaneously.
:param callback: Allows to specify a callback function to be called when all scripts have been loaded.
:type callback: callable
"""
if not args:
callback = kwargs.get("callback")
if callable(callback):
callback()
return
script = html5.Script()
script["src"] = args[0]
#print(script["src"])
script.onLoad = lambda *xargs, **xkwargs: importJS(*args[1:], **kwargs)
script.sinkEvent("onLoad")
html5.Head().appendChild(script)