Skip to content

Commit

Permalink
make mars_request work
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHodson committed Jan 24, 2024
1 parent 41c157f commit fcc8c7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/ionbeam/writers/construct_mars_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
}


def construct_mars_request(message):
request = {"database": "fdbdev", "class": "rd", "source": str(message.metadata.filepath)}
request |= message.metadata.mars_request.as_strings()
def construct_mars_request(message, override=dict(database="fdbdev")):
source = {"source": message.metadata.filepath}
odb_keys = {k.key: k.value for k in message.metadata.mars_keys if not k.reason == "Skipped"}
request = odb_keys | source | override # rightmost dict takes precedence here
request = {k: mars_value_formatters.get(k, str)(v) for k, v in request.items()}
return request
34 changes: 18 additions & 16 deletions src/ionbeam/writers/mars_client_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from ..core.bases import Writer, Message, FileMessage, FinishMessage
from ..core.aviso import send_aviso_notification
from .construct_mars_request import construct_mars_request


import logging
Expand All @@ -33,14 +34,14 @@ def write_temp_mars_request(
request,
file,
verb="archive",
quote_keys = {"source", "target", "filter"}
# quote_keys = {"source", "target", "filter"}
):
keys = []
for key, value in request.items():
if key in quote_keys:
keys.append(f'{key}="{value}"')
else:
keys.append(f'{key}={value}')
# if key in quote_keys:
keys.append(f'{key}="{value}"')
# else:
# keys.append(f'{key}={value}')

keys = ",\n ".join(keys)
rendered = f"""{verb},
Expand All @@ -58,8 +59,8 @@ def run_temp_mars_request(file):
except sb.CalledProcessError as exc:
print("Status : FAIL", exc.returncode, exc.output.decode())
raise exc
else:
logger.debug("MARS Archive Output: \n{}\n".format(output))
# else:
# print("Output: \n{}\n".format(output))


@dataclasses.dataclass
Expand All @@ -75,22 +76,23 @@ def process(self, message: FileMessage | FinishMessage) -> Iterable[Message]:
if isinstance(message, FinishMessage):
return

assert message.metadata.mars_request is not None
assert message.metadata.mars_keys is not None
assert message.metadata.filepath is not None

request = {"database": "fdbdev", "class": "rd", "source": str(message.metadata.filepath)}
mars_request = request | message.metadata.mars_request.as_strings()

logger.info(f"mars_request: {mars_request}")

mars_request = construct_mars_request(message)

with tempfile.NamedTemporaryFile() as fp:
mars_request_string = write_temp_mars_request(mars_request, file=fp.name)
logger.debug(f"mars_request_string: {mars_request_string}")
logger.debug(mars_request_string)
run_temp_mars_request(file=fp.name)

# Send a notification to AVISO that we put this data into the DB
response = send_aviso_notification(mars_request)
# logger.debug("Aviso respose {response}")

# TODO: the explicit mars_keys should not be necessary here.
metadata = self.generate_metadata(message, mars_request=message.metadata.mars_request)
metadata = self.generate_metadata(message, mars_keys=message.metadata.mars_keys)
output_msg = FileMessage(metadata=metadata)

assert output_msg.metadata.mars_request is not None
assert output_msg.metadata.mars_keys is not None
yield self.tag_message(output_msg, message)

0 comments on commit fcc8c7b

Please sign in to comment.