|
| 1 | +from brewtils import parameter, Plugin, get_current_request_read_only, SystemClient |
| 2 | +import os |
| 3 | + |
| 4 | +__version__ = "3.0.0.dev0" |
| 5 | + |
| 6 | +class FileTriggerClient(object): |
| 7 | + """Client that runs off File Trigger Scheduler Logic""" |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + self.system_client = SystemClient() |
| 11 | + |
| 12 | + @parameter( |
| 13 | + key="upload_file", |
| 14 | + type="Boolean", |
| 15 | + description="Should File be uploaded to Beer Garden prior to processing?", |
| 16 | + default=False, |
| 17 | + ) |
| 18 | + @parameter( |
| 19 | + key="delete_file", |
| 20 | + type="Boolean", |
| 21 | + description="Should File be deleted after processing?", |
| 22 | + default=False, |
| 23 | + ) |
| 24 | + def file_create_trigger(self, upload_file: bool=False, delete_file: bool = True): |
| 25 | + """Used for File Trigger Jobs""" |
| 26 | + |
| 27 | + # Grab the file path from the metadata. Currently there isn't a clean way to |
| 28 | + # inject the path into the input parameters |
| 29 | + current_request = get_current_request_read_only() |
| 30 | + |
| 31 | + if "src_path" not in current_request.metadata: |
| 32 | + raise Exception("Missing 'src_path' metadata from file trigger") |
| 33 | + |
| 34 | + file_path = current_request.metadata["src_path"] |
| 35 | + |
| 36 | + if not os.path.exists(file_path): |
| 37 | + raise Exception(f"File path does not exist: {file_path}") |
| 38 | + |
| 39 | + if upload_file: |
| 40 | + # Uploading the file is good for scenarios where the command that processes the file does |
| 41 | + # not have access to the file path |
| 42 | + |
| 43 | + # Uploading to Base64 Parameters |
| 44 | + self.system_client.process_file_upload(uploaded_file=file_path) |
| 45 | + # The Base64 file can also be passed in via `open()`, if file path is provided |
| 46 | + # as a String, then Brewtils will invoke `open(path, 'rb')` |
| 47 | + # self.system_client.process_file_upload(uploaded_file=open(file_path, "r")) |
| 48 | + |
| 49 | + # Uploading to Bytes Parameters |
| 50 | + self.system_client.process_file_upload_bytes(uploaded_file=open(file_path, "rb").read()) |
| 51 | + else: |
| 52 | + self.system_client.process_file(file_path=file_path) |
| 53 | + |
| 54 | + if delete_file: |
| 55 | + os.remove(file_path) |
| 56 | + |
| 57 | + |
| 58 | + @parameter( |
| 59 | + key="file_path", |
| 60 | + type="String", |
| 61 | + description="Path to file to be processed", |
| 62 | + ) |
| 63 | + def process_file(self, file_path:str): |
| 64 | + """Returns contents of file path provided""" |
| 65 | + |
| 66 | + with open(file_path, 'r') as file: |
| 67 | + file_content = file.read() |
| 68 | + return file_content |
| 69 | + |
| 70 | + |
| 71 | + @parameter( |
| 72 | + key="uploaded_file", |
| 73 | + type="Base64", |
| 74 | + description="Path to file to be processed", |
| 75 | + ) |
| 76 | + def process_file_upload(self, uploaded_file): |
| 77 | + """Returns contents of file uploaded""" |
| 78 | + return uploaded_file.read() |
| 79 | + |
| 80 | + |
| 81 | + @parameter( |
| 82 | + key="uploaded_file", |
| 83 | + type="Bytes", |
| 84 | + description="Path to file to be processed", |
| 85 | + ) |
| 86 | + def process_file_upload_bytes(self, uploaded_file): |
| 87 | + """Returns contents of file uploaded""" |
| 88 | + return uploaded_file |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + plugin = Plugin( |
| 93 | + name="file_trigger", |
| 94 | + version=__version__, |
| 95 | + description="Example for how to handle file triggers", |
| 96 | + ) |
| 97 | + plugin.client = FileTriggerClient() |
| 98 | + plugin.run() |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments