-
Notifications
You must be signed in to change notification settings - Fork 25
/
script.py
167 lines (134 loc) · 6.06 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
import time
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':
# Extracting the zipfile
meta_data.append(("debug", f"{key}: extracting file"))
extraction_result = []
zipfile_ref = get_zipfile(fileResult.value)
print(zipfile_ref, fileResult.value)
files = get_files(zipfile_ref)
fileCount = len(files)
for index, filename in enumerate(files):
percentage = ((index+1)/fileCount)*100
promptMessage = prompt_extraction_message(f"Extracting file: {filename}", percentage)
yield render_donation_page(promptMessage)
file_extraction_result = extract_file(zipfile_ref, filename)
extraction_result.append(file_extraction_result)
if len(extraction_result) >= 0:
meta_data.append(("debug", f"{key}: extraction successful, go to consent form"))
data = extraction_result
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, 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.",
"de": "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 prompt_extraction_message(message, percentage):
description = props.Translatable({
"en": "One moment please. Information is now being extracted from the selected file.",
"de": "Einen Moment bitte. Es werden nun Informationen aus der ausgewählten Datei extrahiert.",
"nl": "Een moment geduld. Informatie wordt op dit moment uit het geselecteerde bestaand gehaald."
})
return props.PropsUIPromptProgress(description, message, percentage)
def get_zipfile(filename):
try:
return zipfile.ZipFile(filename)
except zipfile.error:
return "invalid"
def get_files(zipfile_ref):
try:
return zipfile_ref.namelist()
except zipfile.error:
return []
def extract_file(zipfile_ref, filename):
try:
# make it slow for demo reasons only
time.sleep(1)
info = zipfile_ref.getinfo(filename)
return (filename, info.compress_size, info.file_size)
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",
"de": "Log Nachrichten",
"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)