From 11e6e5b4e5273daba0879b6049e7a22a69e503d9 Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Mon, 11 Mar 2024 14:35:29 -0400 Subject: [PATCH] Add foxden module to CHAP --- CHAP/foxden/__init__.py | 6 ++++ CHAP/foxden/processor.py | 42 ++++++++++++++++++++++++ CHAP/foxden/writer.py | 65 ++++++++++++++++++++++++++++++++++++++ CHAP/test/foxden/test.yaml | 5 +++ setup.py | 3 ++ 5 files changed, 121 insertions(+) create mode 100755 CHAP/foxden/__init__.py create mode 100755 CHAP/foxden/processor.py create mode 100755 CHAP/foxden/writer.py create mode 100644 CHAP/test/foxden/test.yaml diff --git a/CHAP/foxden/__init__.py b/CHAP/foxden/__init__.py new file mode 100755 index 0000000..6fc29d7 --- /dev/null +++ b/CHAP/foxden/__init__.py @@ -0,0 +1,6 @@ +"""This subpackage contains pieces for communication with FOXDEN services. +""" + +from CHAP.foxden.processor import ( + FoxdenProvenanceProcessor, +) diff --git a/CHAP/foxden/processor.py b/CHAP/foxden/processor.py new file mode 100755 index 0000000..b71aa67 --- /dev/null +++ b/CHAP/foxden/processor.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- +#pylint: disable= +""" +File : processor.py +Author : Valentin Kuznetsov +Description: Processor module for FOXDEN services +""" + +# system modules +from time import time + +# local modules +from CHAP import Processor +from CHAP.foxden.writer import FoxdenWriter + + +class FoxdenProvenanceProcessor(Processor): + """A Processor to communicate with FOXDEN provenance server.""" +# def __init__(self): +# self.writer = FoxdenWriter() + + def process(self, data, url, dryRun=False, verbose=False): + """process data API""" + + t0 = time() + self.logger.info(f'Executing "process" with url {url} data {data} dryrun {dryRun}') + writer = FoxdenWriter() + +# data = self.writer.write(data, url, dryRun) + data = writer.write(data, url, dryRun=dryRun) + + self.logger.info(f'Finished "process" in {time()-t0:.3f} seconds\n') + + return data + + +if __name__ == '__main__': + # local modules + from CHAP.processor import main + + main() diff --git a/CHAP/foxden/writer.py b/CHAP/foxden/writer.py new file mode 100755 index 0000000..81a498e --- /dev/null +++ b/CHAP/foxden/writer.py @@ -0,0 +1,65 @@ +"""FOXDE command line writer.""" + +# system modules +import os + +# Local modules +from CHAP.writer import main + +class FoxdenWriter(): + """FOXDEN writer writes data to specific FOXDEN service + """ + + def write(self, data, url, method="POST", headers={}, timeout=10, dryRun=False): + """Write the input data as text to a file. + + :param data: input data + :type data: list[PipelineData] + :param url: url of service + :type url: str + :param method: HTTP method to use, POST for creation and PUT for update + :type method: str + :param headers: HTTP headers to use + :type headers: dictionary + :param timeout: timeout of HTTP request + :type timeout: str + :param dryRun: dryRun option to verify HTTP workflow + :type dryRun: boolean + :return: contents of the input data + :rtype: object + """ + import requests + if 'Content-Type' not in headers: + headers['Content-type'] = 'application/json' + if 'Accept' not in headers: + headers['Accept'] = 'application/json' + if dryRun: + print("### HTTP writer call", url, headers, data) + return [] + token = "" + fname = os.getenv("CHESS_WRITE_TOKEN") + if not fname: + msg = f'CHESS_WRITE_TOKEN env variable is not set' + raise Exception(msg) + with open(fname, 'r') as istream: + token = istream.read() + if token: + headers["Authorization"] = f"Bearer {token}" + else: + msg = f'No valid write token found in CHESS_WRITE_TOKEN env variable' + raise Exception(msg) + + # make actual HTTP request to FOXDEN service + if method.lower() == 'post': + resp = requests.post(url, headers=headers, timeout=timeout, data=data) + elif method.lower() == 'put': + resp = requests.put(url, headers=headers, timeout=timeout, data=data) + else: + msg = f"unsupporteed method {method}" + raise Exception(msg) + data = resp.content + return data + + +if __name__ == '__main__': + main() diff --git a/CHAP/test/foxden/test.yaml b/CHAP/test/foxden/test.yaml new file mode 100644 index 0000000..e99c3cc --- /dev/null +++ b/CHAP/test/foxden/test.yaml @@ -0,0 +1,5 @@ +pipeline: + - common.PrintProcessor: {} + - foxden.FoxdenProvenanceProcessor: + url: localhost + dryRun: True diff --git a/setup.py b/setup.py index f219b19..04eaa76 100755 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ # [set version] version = 'PACKAGE_VERSION' +version = 'v0.0.12' # [version set] def datafiles(idir, pattern=None): @@ -54,6 +55,7 @@ def datafiles(idir, pattern=None): 'CHAP.sin2psi', 'CHAP.tomo', 'CHAP.utils', + 'CHAP.foxden', 'MLaaS' ], package_dir={ @@ -65,6 +67,7 @@ def datafiles(idir, pattern=None): 'CHAP.saxswaxs': 'CHAP/saxswaxs', 'CHAP.sin2psi': 'CHAP/sin2psi', 'CHAP.tomo': 'CHAP/tomo', + 'CHAP.foxden': 'CHAP/foxden', 'CHAP.utils': 'CHAP/utils', 'MLaaS': 'MLaaS' },