Skip to content

Commit

Permalink
Merge pull request #114 from vkuznet/main
Browse files Browse the repository at this point in the history
Add foxden module
  • Loading branch information
keara-soloway authored Mar 11, 2024
2 parents a368ee0 + 0691db0 commit 78e8b0e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHAP/foxden/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""This subpackage contains pieces for communication with FOXDEN services.
"""

from CHAP.foxden.processor import (
FoxdenProvenanceProcessor,
)
42 changes: 42 additions & 0 deletions CHAP/foxden/processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#pylint: disable=
"""
File : processor.py
Author : Valentin Kuznetsov <vkuznet AT gmail dot com>
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()
65 changes: 65 additions & 0 deletions CHAP/foxden/writer.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions CHAP/test/foxden/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pipeline:
- common.PrintProcessor: {}
- foxden.FoxdenProvenanceProcessor:
url: localhost
dryRun: True
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# [set version]
version = 'PACKAGE_VERSION'
version = 'v0.0.12'
# [version set]

def datafiles(idir, pattern=None):
Expand Down Expand Up @@ -54,6 +55,7 @@ def datafiles(idir, pattern=None):
'CHAP.sin2psi',
'CHAP.tomo',
'CHAP.utils',
'CHAP.foxden',
'MLaaS'
],
package_dir={
Expand All @@ -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'
},
Expand Down

0 comments on commit 78e8b0e

Please sign in to comment.