Skip to content

Commit

Permalink
fix: apply Alastair's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mfacchinelli committed Aug 5, 2024
1 parent ee70625 commit 7dd1d47
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
4 changes: 0 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"configurations": [
{
"console": "integratedTerminal",
"env": {
"TESTCONTAINERS_HOST_OVERRIDE": "host.docker.internal",
"WIREMOCK_DIND": "1"
},
"justMyCode": false,
"name": "Python: Debug Tests",
"program": "${file}",
Expand Down
3 changes: 2 additions & 1 deletion src/imap_mag/appConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class AppConfig(BaseModel):
api: Optional[API] = None

def __init__(self, **kwargs):
# replace hypens with underscores so that you can build config from constructor args, and still have them mapped to the hyphen split property names in the YAML files
# Replace hypens with underscores so that you can build config from constructor args,
# and still have them mapped to the hyphen split property names in the YAML files.
kwargs = dict((key.replace("_", "-"), value) for (key, value) in kwargs.items())
super().__init__(**kwargs)

Expand Down
29 changes: 20 additions & 9 deletions src/imap_mag/cli/fetchScience.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Program to retrieve and process MAG CDF files."""

import typing
from enum import Enum
from pathlib import Path

import pandas as pd
Expand All @@ -10,6 +11,16 @@
from ..client.sdcDataAccess import ISDCDataAccess


class MAGMode(str, Enum):
Normal = "norm"
Burst = "burst"


class MAGSensor(str, Enum):
IBS = "magi"
OBS = "mago"


class FetchScienceOptions(typing.TypedDict):
"""Options for SOC interactions."""

Expand All @@ -22,22 +33,22 @@ class FetchScienceOptions(typing.TypedDict):
class FetchScience:
"""Manage SOC data."""

__descriptors: list[str]
__flavors: list[str]
__modes: list[MAGMode]
__sensor: list[MAGSensor]

__data_access: ISDCDataAccess

def __init__(
self,
data_access: ISDCDataAccess,
descriptors: list[str] = ["norm", "burst"],
flavors: list[str] = ["-magi", "-mago"],
modes: list[MAGMode] = ["norm", "burst"],
sensors: list[MAGSensor] = ["magi", "mago"],
) -> None:
"""Initialize SDC interface."""

self.__data_access = data_access
self.__descriptors = descriptors
self.__flavors = flavors
self.__modes = modes
self.__sensor = sensors

def download_latest_science(
self, **options: typing_extensions.Unpack[FetchScienceOptions]
Expand All @@ -46,7 +57,7 @@ def download_latest_science(

downloaded = []

for descriptor in self.__descriptors:
for mode in self.__modes:
date_range: pd.DatetimeIndex = pd.date_range(
start=appUtils.convertToDatetime(options["start_date"]),
end=appUtils.convertToDatetime(options["end_date"]),
Expand All @@ -55,10 +66,10 @@ def download_latest_science(
)

for date in date_range.to_pydatetime():
for flavor in self.__flavors:
for sensor in self.__sensor:
file_details = self.__data_access.get_filename(
level=options["level"],
descriptor=str(descriptor) + str(flavor),
descriptor=str(mode) + "-" + str(sensor),
start_date=date,
end_date=None,
version="latest",
Expand Down
3 changes: 2 additions & 1 deletion src/imap_mag/client/sdcDataAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def upload(self, file_name: str) -> None:
try:
imap_data_access.upload(file_name)
except imap_data_access.io.IMAPDataAccessError as e:
logging.warn(f"Upload failed: {e}")
logging.error(f"Upload failed: {e}")
raise e

def query(
self, **options: typing_extensions.Unpack[QueryOptions]
Expand Down
16 changes: 7 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import os
import re
from pathlib import Path

import pytest
Expand Down Expand Up @@ -144,22 +145,19 @@ def test_fetch_science_downloads_cdf_from_sdc(wiremock_manager): # noqa: F811
wiremock_manager.add_string_mapping(
"/query?instrument=mag&data_level=l1b&descriptor=norm-magi&start_date=20250502&version=latest&extension=cdf",
json.dumps(query_response),
priority=1,
)
wiremock_manager.add_file_mapping(
"/download/imap/mag/l1b/2025/05/imap_mag_l1b_norm-magi_20250502_v000.cdf",
cdf_file,
)
wiremock_manager.add_string_mapping(
"/query?instrument=mag&data_level=l1b&descriptor=norm-mago&start_date=20250502&version=latest&extension=cdf",
json.dumps({}),
)
wiremock_manager.add_string_mapping(
"/query?instrument=mag&data_level=l1b&descriptor=burst-magi&start_date=20250502&version=latest&extension=cdf",
json.dumps({}),
)
wiremock_manager.add_string_mapping(
"/query?instrument=mag&data_level=l1b&descriptor=burst-mago&start_date=20250502&version=latest&extension=cdf",
re.escape("/query?instrument=mag&data_level=l1b&descriptor=")
+ ".*"
+ re.escape("&start_date=20250502&version=latest&extension=cdf"),
json.dumps({}),
pattern=True,
priority=2,
)

(_, config_file) = create_serialize_config(
Expand Down
30 changes: 21 additions & 9 deletions tests/wiremockUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,30 @@ def add_string_mapping(
self,
url: str,
body: str,
*,
pattern: bool = False,
priority: int | None = None,
) -> None:
Mappings.create_mapping(
Mapping(
request=MappingRequest(
method=HttpMethods.GET,
url=url,
),
response=MappingResponse(status=200, body=body),
persistent=False,
)
request = MappingRequest(
method=HttpMethods.GET,
)

if pattern:
request.url_pattern = url
else:
request.url = url

mapping = Mapping(
request=request,
response=MappingResponse(status=200, body=body),
persistent=False,
)

if priority:
mapping.priority = priority

Mappings.create_mapping(mapping)

def add_file_mapping(
self,
url: str,
Expand Down

0 comments on commit 7dd1d47

Please sign in to comment.