-
Notifications
You must be signed in to change notification settings - Fork 26
/
script.py
127 lines (99 loc) · 4.32 KB
/
script.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
import port.api.props as props
from port.api.commands import (CommandSystemDonate, CommandSystemExit, CommandUIRender)
import pandas as pd
import zipfile
def process(sessionId):
yield donate(f"{sessionId}-tracking", '[{ "message": "user entered script" }]')
key = "zip-contents-example"
meta_data = []
meta_data.append(("debug", f"{key}: start"))
# STEP 1: select the file
data = None
while True:
meta_data.append(("debug", f"{key}: prompt file"))
promptFile = prompt_file("application/zip, text/plain")
fileResult = yield render_donation_page(promptFile)
if fileResult.__type__ == 'PayloadString':
meta_data.append(("debug", f"{key}: extracting file"))
extractionResult = doSomethingWithTheFile(fileResult.value)
if extractionResult != 'invalid':
meta_data.append(("debug", f"{key}: extraction successful, go to consent form"))
data = extractionResult
break
else:
meta_data.append(("debug", f"{key}: prompt confirmation to retry file selection"))
retry_result = yield render_donation_page(retry_confirmation())
if retry_result.__type__ == 'PayloadTrue':
meta_data.append(("debug", f"{key}: skip due to invalid file"))
continue
else:
meta_data.append(("debug", f"{key}: retry prompt file"))
break
# STEP 2: ask for consent
if data is not None:
meta_data.append(("debug", f"{key}: prompt consent"))
prompt = prompt_consent(data, meta_data)
consent_result = yield render_donation_page(prompt)
if consent_result.__type__ == "PayloadJSON":
meta_data.append(("debug", f"{key}: donate consent data"))
yield donate(f"{sessionId}-{key}", consent_result.value)
yield exit(0, "Success")
def render_donation_page(body):
header = props.PropsUIHeader(props.Translatable({
"en": "Port flow example",
"nl": "Port voorbeeld flow"
}))
page = props.PropsUIPageDonation("Zip", header, body, None)
return CommandUIRender(page)
def retry_confirmation():
text = props.Translatable({
"en": "Unfortunately, we cannot process your file. Continue, if you are sure that you selected the right file. Try again to select a different file.",
"nl": "Helaas, kunnen we uw bestand niet verwerken. Weet u zeker dat u het juiste bestand heeft gekozen? Ga dan verder. Probeer opnieuw als u een ander bestand wilt kiezen."
})
ok = props.Translatable({
"en": "Try again",
"nl": "Probeer opnieuw"
})
cancel = props.Translatable({
"en": "Continue",
"nl": "Verder"
})
return props.PropsUIPromptConfirm(text, ok, cancel)
def prompt_file(extensions):
description = props.Translatable({
"en": "Please select any zip file stored on your device.",
"nl": "Selecteer een willekeurige zip file die u heeft opgeslagen op uw apparaat."
})
return props.PropsUIPromptFileInput(description, extensions)
def doSomethingWithTheFile(filename):
return extract_zip_contents(filename)
def extract_zip_contents(filename):
names = []
try:
file = zipfile.ZipFile(filename)
data = []
for name in file.namelist():
names.append(name)
info = file.getinfo(name)
data.append((name, info.compress_size, info.file_size))
return data
except zipfile.error:
return "invalid"
def prompt_consent(data, meta_data):
table_title = props.Translatable({
"en": "Zip file contents",
"nl": "Inhoud zip bestand"
})
log_title = props.Translatable({
"en": "Log messages",
"nl": "Log berichten"
})
data_frame = pd.DataFrame(data, columns=["filename", "compressed size", "size"])
table = props.PropsUIPromptConsentFormTable("zip_content", table_title, data_frame)
meta_frame = pd.DataFrame(meta_data, columns=["type", "message"])
meta_table = props.PropsUIPromptConsentFormTable("log_messages", log_title, meta_frame)
return props.PropsUIPromptConsentForm([table], [meta_table])
def donate(key, json_string):
return CommandSystemDonate(key, json_string)
def exit(code, info):
return CommandSystemExit(code, info)