-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathscript.py
139 lines (111 loc) · 4.91 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
128
129
130
131
132
133
134
135
136
137
138
139
import port.api.props as props
from port.api.assets import *
from port.api.commands import (CommandSystemDonate, CommandSystemExit, CommandUIRender)
import pandas as pd
import zipfile
import json
def process(sessionId):
print(read_asset("hello_world.txt"))
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
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)
if consent_result.__type__ == "PayloadFalse":
value = json.dumps('{"status" : "donation declined"}')
yield donate(f"{sessionId}-{key}", value)
def render_donation_page(body):
header = props.PropsUIHeader(props.Translatable({
"en": "Port flow example",
"de": "Port beispiel",
"nl": "Port voorbeeld flow"
}))
page = props.PropsUIPageDonation("Zip", header, body)
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.",
"de": "Leider können wir Ihre Datei nicht bearbeiten. Fahren Sie fort, wenn Sie sicher sind, dass Sie die richtige Datei ausgewählt haben. Versuchen Sie erneut, eine andere Datei auszuwählen.",
"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",
"de": "Versuchen Sie es noch einmal",
"nl": "Probeer opnieuw"
})
cancel = props.Translatable({
"en": "Continue",
"de": "Weiter",
"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.",
"en": "Wählen Sie eine beliebige Zip-Datei aus, die Sie auf Ihrem Gerät gespeichert haben.",
"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",
"de": "Inhalt der Zip-Datei",
"nl": "Inhoud zip bestand"
})
log_title = props.Translatable({
"en": "Log messages",
"en": "Logmeldungen",
"nl": "Log berichten"
})
tables=[]
if data is not None:
data_frame = pd.DataFrame(data, columns=["filename", "compressed size", "size"])
tables = [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(tables, [meta_table])
def donate(key, json_string):
return CommandSystemDonate(key, json_string)
def exit(code, info):
return CommandSystemExit(code, info)