Using Python Functionality in Flow Scripts
You can use almost all standard Python functionality in flow scripts by importing the standard Python module of your choice. There are several third-party modules available. See available third-party Python modules for more information. On this page we will give you some examples of common usage patterns.
Available Third-Party Python Modules
The following third-party Python modules are available to be used in your flow scripts:
- async-timeout
- html5lib
- lxml
- passlib
- pdftotext
- pycryptodome
- pyjwt
- pyquery
- pystache
- python-dateutil
- pyyaml
- ujson
Contact us at info@cloudomation.com to request additional Python modules.
Examples
Parse a PDF File
You can access the content of PDF files directly in your flow script.
Example: Access a date written in a PDF file
import re
import base64
import io
import pdftotext
def handler(system, this):
pdf_name = 'example.pdf'
# Open the Cloudomation file object
pdf_file = system.file(pdf_name)
# Per default Cloudomation tries to parse files using UTF-8 and returns a Python string.
# Here we want to access the binary content of the file,
# so we need to set `convert_binary` to `False`
file_base64 = pdf_file.get('content', convert_binary=False)
file_bytes = base64.b64decode(file_base64)
# Construct a `PDF` object from the bytes
pdf = pdftotext.PDF(io.BytesIO(file_bytes))
# Access the text of the first page of the PDF file
pdf_text = pdf[0]
# Clear the PDF object, so that the file object is closed
pdf = None
# Use a Regex to find the date
the_date = re.search(r'Date:\s*(\d{4}-[01]\d-[0-3]\d)', pdf_text).group(1)
this.log(the_date)
return this.success('all done')
Work with CSV Files
You can use the
csv
module to read CSV files into Python objects or write Python objects into CSV files.Example: Create a CSV file and send it by email.
import base64
import csv
import io
def handler(system, this):
fake_data_list = [
{'name': 'spam', 'number': 1},
{'name': 'eggs', 'number': 2},
{'name': 'foo', 'number': 3},
{'name': 'bar', 'number': 4},
]
result_file = io.StringIO()
csv_writer = csv.DictWriter(
result_file,
fieldnames=['name', 'number'],
)
csv_writer.writeheader()
for data in fake_data_list:
csv_writer.writerow(data)
csv_content = result_file.getvalue()
result_file.close()
result_file = None
csv_writer = None
system.file('result.csv').save(content=csv_content)
this.task(
'SMTP',
inputs={
'from': 'me@example.com',
'to': 'you@example.com',
'subject': 'Result',
'text': 'See the attached file',
'login': 'me@example.com',
'password': '****',
'smtp_host': 'SMTP.example.com',
'smtp_port': 587,
'use_tls': True,
'attachments': [
'cloudomation:result.csv',
]
}
)
return this.success('all done')