From 60cbdbfef56e6b469e5899d0205c967727b0f764 Mon Sep 17 00:00:00 2001 From: Alex Wernle <awernle@usgs.gov> Date: Tue, 18 Oct 2022 15:36:50 -0600 Subject: [PATCH 1/4] Imported Enum to define a CLI parameter with a predefined set of values to choose from. Created a spreadsheets_dir option and factory option. --- geomagio/processing/copy_absolutes.py | 37 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/geomagio/processing/copy_absolutes.py b/geomagio/processing/copy_absolutes.py index f60c214f4..6d45442f4 100644 --- a/geomagio/processing/copy_absolutes.py +++ b/geomagio/processing/copy_absolutes.py @@ -10,14 +10,20 @@ from datetime import date, datetime, time, timedelta from typing import List import typer +from enum import Enum from obspy import UTCDateTime -from ..metadata import Metadata, MetadataCategory, MetadataFactory -from ..residual import Reading, WebAbsolutesFactory +from ..metadata import Metadata, MetadataFactory, MetadataCategory +from ..residual import Reading, WebAbsolutesFactory, SpreadsheetAbsolutesFactory TODAY = datetime.combine(date.today(), time(0, 0, 0)) +class factories(str, Enum): + web_absolutes_factory = "web_absolutes_factory" + spreadsheet_factory = "spreadsheet_factory" + + def copy_absolutes( observatory: str = typer.Option(..., help="Observatory code"), starttime: datetime = typer.Option( @@ -40,12 +46,22 @@ def copy_absolutes( help="URL to web absolutes service", metavar="URL", ), + spreadsheets_dir: str = typer.Option( + default="---", help="Directory to residual spreadsheets", metavar="DIR" + ), + # I am not sure what to specify as the default directory, I was using a local directory as a test + factory: factories = factories.web_absolutes_factory, ): - """Copy absolutes from the web absolutes service into the metadata service.""" - # read readings from web absolutes - web_absolutes_factory = WebAbsolutesFactory(url=web_absolutes_url) + """Copy absolutes from the web absolutes service OR residual spreadsheets into the metadata service.""" + print(f"Using factory: {factory.value}") + + if factory.value == factories.web_absolutes_factory: + factory = WebAbsolutesFactory(url=web_absolutes_url) + else: + factory = SpreadsheetAbsolutesFactory(base_directory=spreadsheets_dir) + readings = get_readings( - factory=web_absolutes_factory, + factory=factory, observatory=observatory, starttime=UTCDateTime(starttime), endtime=UTCDateTime(endtime), @@ -88,17 +104,14 @@ def create_reading_metadata(reading: Reading) -> Metadata: def get_readings( - factory: WebAbsolutesFactory, - observatory: str, - starttime: UTCDateTime, - endtime: UTCDateTime, + factory: factories, observatory: str, starttime: UTCDateTime, endtime: UTCDateTime ) -> List[Reading]: - """Get readings from web absolutes. + """Get readings from web absolutes or residual spreadsheets. Parameters ---------- factory - configured WebAbsolutesFactory + configured WebAbsolutesFactory or SpreadsheetAbsolutesFactory observatory search observatory starttime -- GitLab From a60191e13ade43fdb1d532b2319c426918005a3f Mon Sep 17 00:00:00 2001 From: Alex Wernle <awernle@usgs.gov> Date: Fri, 21 Oct 2022 11:03:54 -0600 Subject: [PATCH 2/4] Working on Jeremy's comments --- geomagio/processing/copy_absolutes.py | 34 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/geomagio/processing/copy_absolutes.py b/geomagio/processing/copy_absolutes.py index 6d45442f4..ffbfbcb2b 100644 --- a/geomagio/processing/copy_absolutes.py +++ b/geomagio/processing/copy_absolutes.py @@ -7,7 +7,8 @@ Created on Thu Jul 7 11:05:19 2022 import json import os from datetime import date, datetime, time, timedelta -from typing import List +from typing import List, Optional +from pathlib import Path import typer from enum import Enum @@ -19,9 +20,9 @@ from ..residual import Reading, WebAbsolutesFactory, SpreadsheetAbsolutesFactory TODAY = datetime.combine(date.today(), time(0, 0, 0)) -class factories(str, Enum): - web_absolutes_factory = "web_absolutes_factory" - spreadsheet_factory = "spreadsheet_factory" +class ResidualFactory(str, Enum): + SPREADSHEET = "spreadsheet" + WEB_ABSOLUTES = "webabsolutes" def copy_absolutes( @@ -46,19 +47,23 @@ def copy_absolutes( help="URL to web absolutes service", metavar="URL", ), - spreadsheets_dir: str = typer.Option( - default="---", help="Directory to residual spreadsheets", metavar="DIR" + directory: Optional[Path] = typer.Option( + None, + exists=True, + file_okay=False, + dir_okay=True, + writable=False, + readable=False, + resolve_path=True, + help="Residual spreadsheet directory", ), - # I am not sure what to specify as the default directory, I was using a local directory as a test - factory: factories = factories.web_absolutes_factory, + factory: ResidualFactory = ResidualFactory.WEB_ABSOLUTES, ): """Copy absolutes from the web absolutes service OR residual spreadsheets into the metadata service.""" - print(f"Using factory: {factory.value}") - - if factory.value == factories.web_absolutes_factory: + if factory.value == ResidualFactory.WEB_ABSOLUTES: factory = WebAbsolutesFactory(url=web_absolutes_url) else: - factory = SpreadsheetAbsolutesFactory(base_directory=spreadsheets_dir) + factory = SpreadsheetAbsolutesFactory(base_directory=directory) readings = get_readings( factory=factory, @@ -104,7 +109,10 @@ def create_reading_metadata(reading: Reading) -> Metadata: def get_readings( - factory: factories, observatory: str, starttime: UTCDateTime, endtime: UTCDateTime + factory: ResidualFactory, + observatory: str, + starttime: UTCDateTime, + endtime: UTCDateTime, ) -> List[Reading]: """Get readings from web absolutes or residual spreadsheets. -- GitLab From 79319cf86c86b676f29ab48ad26712bdb20fd131 Mon Sep 17 00:00:00 2001 From: Alex Wernle <awernle@usgs.gov> Date: Tue, 15 Nov 2022 14:51:06 -0700 Subject: [PATCH 3/4] Added confirm method as a check to user --- geomagio/processing/copy_absolutes.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/geomagio/processing/copy_absolutes.py b/geomagio/processing/copy_absolutes.py index ffbfbcb2b..b2398b027 100644 --- a/geomagio/processing/copy_absolutes.py +++ b/geomagio/processing/copy_absolutes.py @@ -71,7 +71,15 @@ def copy_absolutes( starttime=UTCDateTime(starttime), endtime=UTCDateTime(endtime), ) + + # confirm whether or not to copy absolutes print(f"Found {len(readings)} absolutes") + copy = typer.confirm(f"Are you sure you want to copy {len(readings)} absolutes?") + if not copy: + print("Not copying absolutes") + raise typer.Abort() + print("Copying over absolutes") + # write readings to metadata service metadata_factory = MetadataFactory(token=metadata_token, url=metadata_url) with typer.progressbar( -- GitLab From 60c003b202465ffe3f0faeb612f4eec1b1ba478a Mon Sep 17 00:00:00 2001 From: Alex Wernle <awernle@usgs.gov> Date: Wed, 16 Nov 2022 12:27:19 -0700 Subject: [PATCH 4/4] Added force option to force skip the typer.confirm check --- geomagio/processing/copy_absolutes.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/geomagio/processing/copy_absolutes.py b/geomagio/processing/copy_absolutes.py index b2398b027..86d0748c5 100644 --- a/geomagio/processing/copy_absolutes.py +++ b/geomagio/processing/copy_absolutes.py @@ -58,6 +58,10 @@ def copy_absolutes( help="Residual spreadsheet directory", ), factory: ResidualFactory = ResidualFactory.WEB_ABSOLUTES, + force: bool = typer.Option( + default=False, + help="Force skip the check to copy absolutes", + ), ): """Copy absolutes from the web absolutes service OR residual spreadsheets into the metadata service.""" if factory.value == ResidualFactory.WEB_ABSOLUTES: @@ -73,12 +77,11 @@ def copy_absolutes( ) # confirm whether or not to copy absolutes - print(f"Found {len(readings)} absolutes") - copy = typer.confirm(f"Are you sure you want to copy {len(readings)} absolutes?") - if not copy: - print("Not copying absolutes") - raise typer.Abort() - print("Copying over absolutes") + if not force: + typer.confirm( + f"Are you sure you want to copy {len(readings)} absolutes?", abort=True + ) + print("Copying over absolutes") # write readings to metadata service metadata_factory = MetadataFactory(token=metadata_token, url=metadata_url) -- GitLab