-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformers.py
80 lines (61 loc) · 2.2 KB
/
transformers.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import api
try:
api.message.debug("Importing external modules...")
import loremipsum # pip install loremipsum
except ImportError:
api.message.warn("Failed to import one or more modules. Some transform handlers may not work.")
else:
api.message.debug("External modules imported")
def isnumber(value):
try:
int(value)
return True
except ValueError:
return False
@api.transformer("Help")
def t(text):
readmepath = os.path.join(api.execdir, "README.md")
with open(readmepath, encoding="utf8") as f:
return f.read()
@api.transformer("Lorem Ipsum Generator")
def t(text):
label = "Count of sentences"
sdd = api.SimpleDataDialog(
"Lorem Ipsum Generator",
api.DataEntry(label, 100, validator=isnumber))
if not sdd.canceled:
count = int(sdd.result[label])
return " ".join(
data[2]
# Workaround for a bug in the loremipsum library
.replace("b'", "")
.replace("'", "")
for data in loremipsum.generate_sentences(count, start_with_lorem=True))
@api.transformer('Generate "Hello" transformers')
def t(text):
label = "Count"
template = 'api.transformer("Hello {{index}}")(lambda text: "Hello {{index}}")'
sdd = api.SimpleDataDialog('Generate "Hello" tranformers', api.DataEntry(label, validator=isnumber))
if not sdd.canceled:
count = int(sdd.result[label])
return "\n\n\n".join(
template.replace("{{index}}", str(i))
for i in range(1, count + 1))
@api.transformer("Say Hello")
def t(text):
entries = [
api.DataEntry("Forename", validator=lambda value: value == "Tom"),
api.DataEntry("Surname", "Jones", validator=bool),
api.DataEntry("No meaning"),
]
sdd = api.SimpleDataDialog("Hello", entries)
if not sdd.canceled:
result = sdd.result
return f"Hello {result['Forename']} {result['Surname']}"
@api.transformer("Raise exception")
def t(text):
raise Exception(":-P")
api.transformer("Return None")(lambda text: None)