-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.py
85 lines (70 loc) · 2.69 KB
/
util.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = (
"Copyright (C) 2020 Charlie Powell - Released under terms of the AGPLv3 License"
)
import time
def build_backup_name():
return "eeprom_backup-{}".format(time.strftime("%Y%m%d-%H%M%S"))
def construct_command(data):
command = data["command"]
for param, value in data["params"].items():
value = str(value) if command != "M145" and param != "S" else str(int(value))
command = command + " " + param + value
return command
def backup_json_to_list(eeprom_data):
eeprom_list = []
for key in eeprom_data.keys():
eeprom_list.append(
{
"name": key,
"command": eeprom_data[key]["command"],
"params": eeprom_data[key]["params"],
}
)
return eeprom_list
try:
# This is available in OP 1.6.0+
# TODO decide when to stop supporting 1.5.x and below
from octoprint.util.text import sanitize
except ImportError:
# We are below this version, use backported one instead
import re
from emoji import demojize
from octoprint.util import to_unicode
from octoprint.vendor.awesome_slugify import Slugify
_UNICODE_VARIATIONS = re.compile("[\uFE00-\uFE0F]", re.U)
_SLUGIFIES = {}
def sanitize(text, safe_chars="-_.", demoji=True):
"""
Sanitizes text by running it through slugify and optionally emoji translating.
Examples:
>>> sanitize("Hello World!") # doctest: +ALLOW_UNICODE
'Hello-World'
>>> sanitize("Hello World!", safe_chars="-_. ") # doctest: +ALLOW_UNICODE
'Hello World'
>>> sanitize("\u2764") # doctest: +ALLOW_UNICODE
'red_heart'
>>> sanitize("\u2764\ufe00") # doctest: +ALLOW_UNICODE
'red_heart'
>>> sanitize("\u2764", demoji=False) # doctest: +ALLOW_UNICODE
''
Args:
text: the text to sanitize
safe_chars: characters to consider safe and to keep after sanitization
emoji: whether to also convert emoji to text
Returns: the sanitized text
"""
slugify = _SLUGIFIES.get(safe_chars)
if slugify is None:
slugify = Slugify()
slugify.safe_chars = safe_chars
_SLUGIFIES[safe_chars] = slugify
text = to_unicode(text)
if demoji:
text = remove_unicode_variations(text)
text = demojize(text, delimiters=("", ""))
return slugify(text)
def remove_unicode_variations(text):
return _UNICODE_VARIATIONS.sub("", text)