The program generate-from-proto.py will fetch the Google Cloudevents proto
files from a Github repository and generate Python libraries for each type of
event defined there.
Before running the generator, create a virtual Python environment (recommended) and install the libraries it needs with:
pip install -r requirements.txt
You will also need an Internet connection as the generator fetches the event definitions from Github.
Arguments are:
-hor--help- Print help and then exit without other action.
-qor--quiet- If specified, do not prompt for confirmation of any operation, including ones that would delete files or directories.
-o [dirname]or--out [dirname]-
Required. Write generated library source code to
dirname. That directory, and all its contents, will be deleted first. Program will prompt user for confirmation before deleting any contents, unless-qor--quiteis specified. -r [repo-name]or--repo [repo-name]-
Optional. Fetch proto definitions from Github repo
repo-name. Default, if not specified, isgoogleapis/google-cloudevents. -b [branch-name]or--out [branch-name]-
Optional. Fetch proto definitions from Github branch
branch-name. Default, if not specified, ismain.
The generated code can be packaged and installed using standard Python packaging tools. Or, the library can be installed directly from source by changing to the generated code library and running:
pip install .
Print the name of the bucket and object that triggered the event:
from google.events.cloud import storage
with open("file-containing-storage-json-example", "r") as f:
example = f.read()
event = storage.StorageObjectData.from_json(example)
print(f"Bucket is {event.bucket}")
print(f"Name is {event.name}")