Skip to content

Commit da95ee4

Browse files
feat: add bulk processing in python template
1 parent 3496e2c commit da95ee4

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

cli/src/utils/initIAppWorkspace.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ async function copyChosenTemplateFiles({
150150
''
151151
);
152152
}
153+
if (!useBulkProcessing && !useProtectedData) {
154+
modifiedCode = modifiedCode.replaceAll(
155+
/ *(\/\/|#) <<protectedData\|bulkProcessing>>\n((.*)\n)*? *(\/\/|#) <<\/protectedData\|bulkProcessing>>(\n)?/g,
156+
''
157+
);
158+
}
153159
if (!useInputFile) {
154160
modifiedCode = modifiedCode.replaceAll(
155161
/ *(\/\/|#) <<inputFile>>\n((.*)\n)*? *(\/\/|#) <<\/inputFile>>(\n)?/g,

cli/templates/JavaScript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"main": "src/app.js",
66
"type": "module",
77
"dependencies": {
8-
// <<protectedData>>
8+
// <<protectedData|bulkProcessing>>
99
"@iexec/dataprotector-deserializer": "^0.1.1",
10-
// <</protectedData>>
10+
// <</protectedData|bulkProcessing>>
1111
"figlet": "^1.8.1"
1212
}
1313
}

cli/templates/JavaScript/src/app.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import fs from 'node:fs/promises';
33
import path from 'node:path';
44
// <</bulkProcessing>>
55
import figlet from 'figlet';
6-
// <<protectedData>>
6+
// <<protectedData|bulkProcessing>>
77
import { IExecDataProtectorDeserializer } from '@iexec/dataprotector-deserializer';
8-
// <</protectedData>>
9-
// <<bulkProcessing>>
10-
import { IExecDataProtectorDeserializer } from '@iexec/dataprotector-deserializer';
11-
// <</bulkProcessing>>
8+
// <</protectedData|bulkProcessing>>
129

1310
const main = async () => {
1411
const { IEXEC_OUT } = process.env;
@@ -31,9 +28,9 @@ const main = async () => {
3128
const deserializer = new IExecDataProtectorDeserializer();
3229
// The protected data mock created for the purpose of this Hello World journey
3330
// contains an object with a key "secretText" which is a string
34-
const protectedName = await deserializer.getValue('secretText', 'string');
31+
const protectedText = await deserializer.getValue('secretText', 'string');
3532
console.log('Found a protected data');
36-
messages.push(protectedName);
33+
messages.push(protectedText);
3734
} catch (e) {
3835
console.log('It seems there is an issue with your protected data:', e);
3936
}
@@ -53,12 +50,12 @@ const main = async () => {
5350
});
5451
// The protected data mock created for the purpose of this Hello World journey
5552
// contains an object with a key "secretText" which is a string
56-
const protectedName = await deserializer.getValue(
53+
const protectedText = await deserializer.getValue(
5754
'secretText',
5855
'string'
5956
);
6057
console.log(`Found protected data ${i} of bulk`);
61-
messages.push(protectedName);
58+
messages.push(protectedText);
6259
} catch (e) {
6360
console.log(
6461
`It seems there is an issue with protected data ${i}:`,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# requirements must be compatible with python 3.13.3
22
pyfiglet==1.0.2
3-
# <<protectedData>>
3+
# <<protectedData|bulkProcessing>>
44
borsh-construct==0.1.0
5-
# <</protectedData>>
5+
# <</protectedData|bulkProcessing>>

cli/templates/Python3.13/src/app.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import shutil
88
# <</inputFile>>
99
from pyfiglet import Figlet
10-
# <<protectedData>>
10+
# <<protectedData|bulkProcessing>>
1111
import protected_data
12-
# <</protectedData>>
12+
# <</protectedData|bulkProcessing>>
1313

1414
# ⚠️ Your Python code will be run in a python v3.8.3 environment
1515

@@ -32,10 +32,28 @@
3232
# The protected data mock created for the purpose of this Hello World journey
3333
# contains an object with a key "secretText" which is a string
3434
protected_text = protected_data.getValue('secretText', 'string')
35+
print('Found a protected data')
3536
messages.append(protected_text)
3637
except Exception as e:
3738
print('It seems there is an issue with your protected data:', e)
3839
# <</protectedData>>
40+
# <<bulkProcessing>>
41+
42+
bulkSize = int(os.getenv("IEXEC_BULK_SLICE_SIZE", "0"))
43+
if bulkSize > 0:
44+
print(f"Got {bulkSize} protected data to process in bulk!")
45+
for i in range(1, bulkSize + 1):
46+
try:
47+
# The protected data mock created for the purpose of this Hello World journey
48+
# contains an object with a key "secretText" which is a string
49+
protected_text = protected_data.getValue(
50+
'secretText', 'string', i)
51+
print(f"Found protected data {i} of bulk")
52+
messages.append(protected_text)
53+
except Exception as e:
54+
print(
55+
f"It seems there is an issue with your protected data {i}:", e)
56+
# <</bulkProcessing>>
3957
# <<inputFile>>
4058

4159
IEXEC_INPUT_FILES_NUMBER = int(os.getenv("IEXEC_INPUT_FILES_NUMBER", 0))

cli/templates/Python3.13/src/protected_data.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
# <<protectedData>>
1+
# <<protectedData|bulkProcessing>>
22
# dataprotector deserializer module
33
import os
44
import zipfile
55
from borsh_construct import String, I128, F64, Bool
66

77

8-
def getValue(path: str, schema: str):
8+
def getValue(path: str, schema: str, bulkIndex: int = None) -> any:
99
file_path = path.replace('.', '/')
1010
IEXEC_IN = os.getenv('IEXEC_IN')
11-
IEXEC_DATASET_FILENAME = os.getenv('IEXEC_DATASET_FILENAME')
11+
file_name: str
1212

13-
if IEXEC_DATASET_FILENAME == None:
13+
if bulkIndex != None:
14+
file_name = os.getenv(
15+
f'IEXEC_DATASET_{bulkIndex}_FILENAME')
16+
else:
17+
file_name = os.getenv('IEXEC_DATASET_FILENAME')
18+
19+
if file_name == None:
1420
raise Exception('Missing protected data')
1521

16-
dataset_file_path = os.path.join(IEXEC_IN, IEXEC_DATASET_FILENAME)
22+
dataset_file_path = os.path.join(IEXEC_IN, file_name)
1723

1824
file_bytes: bytes
1925
try:
@@ -38,4 +44,4 @@ def getValue(path: str, schema: str):
3844
raise Exception(f"Failed to deserialize \"{path}\" as \"{schema}\"")
3945

4046
return file_bytes
41-
# <</protectedData>>
47+
# <</protectedData|bulkProcessing>>

0 commit comments

Comments
 (0)