-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): Added a Python script sample (#5203)
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Filter text | ||
inputs: | ||
- {name: Text} | ||
- {name: Pattern, default: '.*'} | ||
outputs: | ||
- {name: Filtered text} | ||
metadata: | ||
annotations: | ||
author: Alexey Volkov <alexey.volkov@ark-kun.com> | ||
implementation: | ||
container: | ||
image: python:3.8 | ||
command: | ||
- sh | ||
- -ec | ||
- | | ||
# This is how additional packages can be installed dynamically | ||
python3 -m pip install pip six | ||
# Run the rest of the command after installing the packages. | ||
"$0" "$@" | ||
- python3 | ||
- -u # Auto-flush. We want the logs to appear in the console immediately. | ||
- -c # Inline scripts are easy, but have size limitaions and the error traces do not show source lines. | ||
- | | ||
import os | ||
import re | ||
import sys | ||
text_path = sys.argv[1] | ||
pattern = sys.argv[2] | ||
filtered_text_path = sys.argv[3] | ||
regex = re.compile(pattern) | ||
os.makedirs(os.path.dirname(filtered_text_path), exist_ok=True) | ||
with open(text_path, 'r') as reader: | ||
with open(filtered_text_path, 'w') as writer: | ||
for line in reader: | ||
if regex.search(line): | ||
writer.write(line) | ||
- {inputPath: Text} | ||
- {inputValue: Pattern} | ||
- {outputPath: Filtered text} |