From ecd007fe4b125e620ef5117f86b4af94bc252679 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Tue, 25 Aug 2020 12:07:10 -0600 Subject: [PATCH 1/6] add CORS headers --- geomagio/api/secure/SessionMiddleware.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geomagio/api/secure/SessionMiddleware.py b/geomagio/api/secure/SessionMiddleware.py index 1138e079..252134e2 100644 --- a/geomagio/api/secure/SessionMiddleware.py +++ b/geomagio/api/secure/SessionMiddleware.py @@ -113,7 +113,8 @@ class SessionMiddleware: self, message: Message, value: str, max_age: int = None, ): headers = MutableHeaders(scope=message) - headers.append("Cache-Control", "no-cache") + headers.append("Cache-Control", "max-age=60") + headers.append("Access-Control-Allow-Origin", "*") headers.append( "Set-Cookie", f"{self.session_cookie}={value};" -- GitLab From 75b9bc744394fbf1ecb5171260f9a7cbbc954511 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Wed, 26 Aug 2020 17:37:18 -0600 Subject: [PATCH 2/6] add response headers to data endpoints --- geomagio/api/secure/SessionMiddleware.py | 3 +-- geomagio/api/ws/app.py | 2 -- geomagio/api/ws/data.py | 3 ++- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/geomagio/api/secure/SessionMiddleware.py b/geomagio/api/secure/SessionMiddleware.py index 252134e2..d4ef2090 100644 --- a/geomagio/api/secure/SessionMiddleware.py +++ b/geomagio/api/secure/SessionMiddleware.py @@ -113,8 +113,7 @@ class SessionMiddleware: self, message: Message, value: str, max_age: int = None, ): headers = MutableHeaders(scope=message) - headers.append("Cache-Control", "max-age=60") - headers.append("Access-Control-Allow-Origin", "*") + headers.append("Cache-Control") headers.append( "Set-Cookie", f"{self.session_cookie}={value};" diff --git a/geomagio/api/ws/app.py b/geomagio/api/ws/app.py index b8b96a49..1747490b 100644 --- a/geomagio/api/ws/app.py +++ b/geomagio/api/ws/app.py @@ -25,8 +25,6 @@ VERSION = os.getenv("GEOMAG_VERSION", "version") app = FastAPI(docs_url="/docs", root_path="/ws") -app.add_middleware(CORSMiddleware, allow_origins=["*"], max_age=86400) - app.include_router(algorithms.router) app.include_router(data.router) app.include_router(elements.router) diff --git a/geomagio/api/ws/data.py b/geomagio/api/ws/data.py index 053474c1..bbf67215 100644 --- a/geomagio/api/ws/data.py +++ b/geomagio/api/ws/data.py @@ -121,7 +121,8 @@ def format_timeseries( else: data = IAGA2002Writer.format(timeseries, elements) media_type = "text/plain" - return Response(data, media_type=media_type) + headers = {"allow_origins": "*", "max_age": "86400", "allow_headers": "*"} + return Response(data, media_type=media_type, headers=headers) def get_timeseries(data_factory: TimeseriesFactory, query: DataApiQuery) -> Stream: -- GitLab From 5ae1ca2c28e5e6a8132bacaa0ca7a24ebe65e5c8 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Thu, 27 Aug 2020 10:46:23 -0600 Subject: [PATCH 3/6] run black formatter --- bin/monitor.py | 4 ++-- geomagio/PlotTimeseriesFactory.py | 6 ++---- geomagio/StreamTimeseriesFactory.py | 6 ++---- geomagio/algorithm/FilterAlgorithm.py | 2 +- geomagio/algorithm/SqDistAlgorithm.py | 3 +-- geomagio/algorithm/XYZAlgorithm.py | 2 +- geomagio/api/db/create.py | 3 +-- geomagio/api/secure/app.py | 3 +-- geomagio/api/secure/login.py | 15 +++++-------- geomagio/api/ws/app.py | 9 +++----- geomagio/binlog/BinLogWriter.py | 3 +-- geomagio/edge/MiniSeedInputClient.py | 3 +-- geomagio/edge/RawInputClient.py | 7 +++---- geomagio/iaga2002/IAGA2002Writer.py | 3 +-- geomagio/imfjson/IMFJSONWriter.py | 3 +-- geomagio/imfv283/IMFV283Parser.py | 2 +- geomagio/pcdcp/PCDCPWriter.py | 3 +-- .../residual/SpreadsheetAbsolutesFactory.py | 12 ++++------- geomagio/residual/WebAbsolutesFactory.py | 9 +++----- geomagio/temperature/TEMPWriter.py | 3 +-- geomagio/vbf/VBFWriter.py | 3 +-- test/Controller_test.py | 6 +++--- test/ObservatoryMetadata_test.py | 3 +-- test/TimeseriesUtility_test.py | 18 ++++++---------- test/Util_test.py | 21 +++++++------------ test/algorithm_test/AdjustedAlgorithm_test.py | 3 +-- test/algorithm_test/SQDistAlgorithm_test.py | 8 +++---- test/edge_test/EdgeFactory_test.py | 18 ++++++---------- test/edge_test/MiniSeedFactory_test.py | 21 +++++++------------ test/edge_test/RawInputClient_test.py | 6 ++---- test/imfv122_test/IMFV122Parser_test.py | 12 ++++------- test/imfv283_test/IMFV283Parser_test.py | 3 +-- 32 files changed, 79 insertions(+), 144 deletions(-) diff --git a/bin/monitor.py b/bin/monitor.py index 7910bb09..ad57d77a 100755 --- a/bin/monitor.py +++ b/bin/monitor.py @@ -91,7 +91,7 @@ def get_gap_total(gaps, interval): def get_last_time(gaps, endtime): - """ Return the last time that a channel has in it. + """Return the last time that a channel has in it. Parameters ---------- gaps: array @@ -141,7 +141,7 @@ def get_table_header(): def has_gaps(gaps): - """ Returns True if gaps dictionary has gaps in it. + """Returns True if gaps dictionary has gaps in it. Parameters ---------- gaps: dictionary diff --git a/geomagio/PlotTimeseriesFactory.py b/geomagio/PlotTimeseriesFactory.py index db9574e4..a892b5e0 100644 --- a/geomagio/PlotTimeseriesFactory.py +++ b/geomagio/PlotTimeseriesFactory.py @@ -6,8 +6,7 @@ from .TimeseriesFactory import TimeseriesFactory class PlotTimeseriesFactory(TimeseriesFactory): - """TimeseriesFactory that generates a plot. - """ + """TimeseriesFactory that generates a plot.""" def __init__(self, *args, **kwargs): TimeseriesFactory.__init__(self, *args, **kwargs) @@ -21,8 +20,7 @@ class PlotTimeseriesFactory(TimeseriesFactory): type=None, interval=None, ): - """This factory does not support get_timeseries. - """ + """This factory does not support get_timeseries.""" raise NotImplementedError('"get_timeseries" not implemented') def put_timeseries( diff --git a/geomagio/StreamTimeseriesFactory.py b/geomagio/StreamTimeseriesFactory.py index 56ae0dcb..32837aca 100644 --- a/geomagio/StreamTimeseriesFactory.py +++ b/geomagio/StreamTimeseriesFactory.py @@ -34,8 +34,7 @@ class StreamTimeseriesFactory(TimeseriesFactory): type=None, interval=None, ): - """Get timeseries using stream as input. - """ + """Get timeseries using stream as input.""" if self.stream_data is None: # only read stream once self.stream_data = self.stream.read() @@ -55,6 +54,5 @@ class StreamTimeseriesFactory(TimeseriesFactory): type=None, interval=None, ): - """Put timeseries using stream as output. - """ + """Put timeseries using stream as output.""" self.factory.write_file(self.stream, timeseries, channels) diff --git a/geomagio/algorithm/FilterAlgorithm.py b/geomagio/algorithm/FilterAlgorithm.py index ee81bb47..99b7177b 100644 --- a/geomagio/algorithm/FilterAlgorithm.py +++ b/geomagio/algorithm/FilterAlgorithm.py @@ -35,7 +35,7 @@ STEPS = [ class FilterAlgorithm(Algorithm): """ - Filter Algorithm that filters and downsamples data + Filter Algorithm that filters and downsamples data """ def __init__( diff --git a/geomagio/algorithm/SqDistAlgorithm.py b/geomagio/algorithm/SqDistAlgorithm.py index d9aa1584..43445054 100644 --- a/geomagio/algorithm/SqDistAlgorithm.py +++ b/geomagio/algorithm/SqDistAlgorithm.py @@ -101,8 +101,7 @@ class SqDistAlgorithm(Algorithm): return (start - 3 * 30 * 24 * 60 * 60, end) def get_next_starttime(self): - """Return the next_starttime from the state, if it is set. - """ + """Return the next_starttime from the state, if it is set.""" return self.next_starttime def clear_state(self): diff --git a/geomagio/algorithm/XYZAlgorithm.py b/geomagio/algorithm/XYZAlgorithm.py index d87b4c09..1f43c878 100644 --- a/geomagio/algorithm/XYZAlgorithm.py +++ b/geomagio/algorithm/XYZAlgorithm.py @@ -58,7 +58,7 @@ class XYZAlgorithm(Algorithm): def get_required_channels(self): """Only the first two channels are required - for the XYZAlgorithm + for the XYZAlgorithm """ return self._inchannels[:2] diff --git a/geomagio/api/db/create.py b/geomagio/api/db/create.py index 80fc17e8..0ccc0a97 100644 --- a/geomagio/api/db/create.py +++ b/geomagio/api/db/create.py @@ -8,8 +8,7 @@ from .session_table import session def create_db(): - """Create the database using sqlalchemy. - """ + """Create the database using sqlalchemy.""" engine = sqlalchemy.create_engine(str(database.url)) sqlalchemy_metadata.create_all(engine) diff --git a/geomagio/api/secure/app.py b/geomagio/api/secure/app.py index 47dd3f61..753b9e3b 100644 --- a/geomagio/api/secure/app.py +++ b/geomagio/api/secure/app.py @@ -35,8 +35,7 @@ app.include_router(metadata_router) @app.get("/") async def index(request: Request, user: User = Depends(current_user)): - """Route to demo user login. - """ + """Route to demo user login.""" if user: link = f""" Logged in as {user.email}<br/> diff --git a/geomagio/api/secure/login.py b/geomagio/api/secure/login.py index 3bc77487..334264d2 100644 --- a/geomagio/api/secure/login.py +++ b/geomagio/api/secure/login.py @@ -45,8 +45,7 @@ from starlette.responses import RedirectResponse class User(BaseModel): - """Information about a logged in user. - """ + """Information about a logged in user.""" email: str sub: str # unique outh id @@ -113,8 +112,7 @@ router = APIRouter() @router.get("/authorize") async def authorize(request: Request): - """Authorize callback after authenticating using OpenID - """ + """Authorize callback after authenticating using OpenID""" # finish login token = await oauth.openid.authorize_access_token(request) @@ -135,8 +133,7 @@ async def authorize(request: Request): @router.get("/login") async def login(request: Request): - """Redirect to OpenID provider. - """ + """Redirect to OpenID provider.""" redirect_uri = request.url_for("authorize") # save original location if "Referer" in request.headers: @@ -147,8 +144,7 @@ async def login(request: Request): @router.get("/logout") async def logout(request: Request): - """Clear session and redirect to index page. - """ + """Clear session and redirect to index page.""" request.session.pop("token", None) request.session.pop("user", None) return RedirectResponse( @@ -162,6 +158,5 @@ async def logout(request: Request): @router.get("/user") async def user(request: Request, user: User = Depends(require_user())) -> User: - """Get currently logged in user. - """ + """Get currently logged in user.""" return user diff --git a/geomagio/api/ws/app.py b/geomagio/api/ws/app.py index 1747490b..9431fc44 100644 --- a/geomagio/api/ws/app.py +++ b/geomagio/api/ws/app.py @@ -38,8 +38,7 @@ async def redirect_to_docs(): @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): - """Value errors are user errors. - """ + """Value errors are user errors.""" data_format = ( "format" in request.query_params and str(request.query_params["format"]) @@ -50,8 +49,7 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE @app.exception_handler(Exception) async def server_exception_handler(request: Request, exc: Exception): - """Other exceptions are server errors. - """ + """Other exceptions are server errors.""" data_format = ( "format" in request.query_params and str(request.query_params["format"]) @@ -63,8 +61,7 @@ async def server_exception_handler(request: Request, exc: Exception): def format_error( status_code: int, exception: str, format: str, request: Request ) -> Response: - """Assign error_body value based on error format. - """ + """Assign error_body value based on error format.""" if format == "json": return json_error(status_code, exception, request.url) else: diff --git a/geomagio/binlog/BinLogWriter.py b/geomagio/binlog/BinLogWriter.py index f43ac161..7364628c 100644 --- a/geomagio/binlog/BinLogWriter.py +++ b/geomagio/binlog/BinLogWriter.py @@ -19,8 +19,7 @@ Zbuf = [] class BinLogWriter(object): - """BinLog writer. - """ + """BinLog writer.""" def __init__(self): return diff --git a/geomagio/edge/MiniSeedInputClient.py b/geomagio/edge/MiniSeedInputClient.py index dc66c17b..906c37e3 100644 --- a/geomagio/edge/MiniSeedInputClient.py +++ b/geomagio/edge/MiniSeedInputClient.py @@ -24,8 +24,7 @@ class MiniSeedInputClient(object): self.socket = None def close(self): - """Close socket if open. - """ + """Close socket if open.""" if self.socket is not None: try: self.socket.close() diff --git a/geomagio/edge/RawInputClient.py b/geomagio/edge/RawInputClient.py index 695b9368..20c29acf 100644 --- a/geomagio/edge/RawInputClient.py +++ b/geomagio/edge/RawInputClient.py @@ -109,8 +109,7 @@ class RawInputClient: raise TimeseriesFactoryException("Tag limited to 10 characters") def close(self): - """close the open sockets - """ + """close the open sockets""" if self.socket is not None: self.socket.close() self.socket = None @@ -144,7 +143,7 @@ class RawInputClient: return str(network + observatory.ljust(5) + channel + location).encode() def forceout(self): - """ force edge to recognize data + """force edge to recognize data NOTES ----- When sending data to edge it hangs on to the data, until either @@ -206,7 +205,7 @@ class RawInputClient: starttime += nsamp * timeoffset def _send(self, buf): - """ Send a block of data to the Edge/CWB combination. + """Send a block of data to the Edge/CWB combination. PARAMETERS ---------- diff --git a/geomagio/iaga2002/IAGA2002Writer.py b/geomagio/iaga2002/IAGA2002Writer.py index 14363bf8..2e9bbdfd 100644 --- a/geomagio/iaga2002/IAGA2002Writer.py +++ b/geomagio/iaga2002/IAGA2002Writer.py @@ -13,8 +13,7 @@ from . import IAGA2002Parser class IAGA2002Writer(object): - """IAGA2002 writer. - """ + """IAGA2002 writer.""" def __init__( self, diff --git a/geomagio/imfjson/IMFJSONWriter.py b/geomagio/imfjson/IMFJSONWriter.py index 81584224..fed776ce 100644 --- a/geomagio/imfjson/IMFJSONWriter.py +++ b/geomagio/imfjson/IMFJSONWriter.py @@ -10,8 +10,7 @@ from ..TimeseriesFactoryException import TimeseriesFactoryException class IMFJSONWriter(object): - """JSON writer. - """ + """JSON writer.""" def write(self, out, timeseries, channels, url=None): """Write timeseries to json file. diff --git a/geomagio/imfv283/IMFV283Parser.py b/geomagio/imfv283/IMFV283Parser.py index e9bcbfe5..ae23d5e7 100644 --- a/geomagio/imfv283/IMFV283Parser.py +++ b/geomagio/imfv283/IMFV283Parser.py @@ -218,7 +218,7 @@ class IMFV283Parser(object): return HEADER_SIZE def _parse_goes_header(self, data): - """ parse goes data header + """parse goes data header Parameters ---------- diff --git a/geomagio/pcdcp/PCDCPWriter.py b/geomagio/pcdcp/PCDCPWriter.py index 8409ab5e..f974a8b3 100644 --- a/geomagio/pcdcp/PCDCPWriter.py +++ b/geomagio/pcdcp/PCDCPWriter.py @@ -11,8 +11,7 @@ from obspy.core import Stream class PCDCPWriter(object): - """PCDCP writer. - """ + """PCDCP writer.""" def __init__(self, empty_value=PCDCPParser.NINES, temperatures=False): self.empty_value = empty_value diff --git a/geomagio/residual/SpreadsheetAbsolutesFactory.py b/geomagio/residual/SpreadsheetAbsolutesFactory.py index 1cf00785..48121677 100644 --- a/geomagio/residual/SpreadsheetAbsolutesFactory.py +++ b/geomagio/residual/SpreadsheetAbsolutesFactory.py @@ -246,8 +246,7 @@ class SpreadsheetAbsolutesFactory(object): endtime: UTCDateTime, include_measurements: bool = True, ) -> List[Reading]: - """Read spreadsheet files between starttime/endtime. - """ + """Read spreadsheet files between starttime/endtime.""" readings = [] start_filename = f"{observatory}-{starttime.datetime:%Y%j%H%M}.xlsm" end_filename = f"{observatory}-{endtime.datetime:%Y%j%H%M}.xlsm" @@ -301,8 +300,7 @@ class SpreadsheetAbsolutesFactory(object): def _parse_absolutes( self, sheet: openpyxl.worksheet, base_date: str ) -> List[Absolute]: - """Parse absolutes from a summary sheet. - """ + """Parse absolutes from a summary sheet.""" absolutes = [ Absolute( element="D", @@ -333,8 +331,7 @@ class SpreadsheetAbsolutesFactory(object): def _parse_measurements( self, sheet: openpyxl.worksheet, base_date: str, precision: str ) -> List[Measurement]: - """Parse measurements from a measurement sheet. - """ + """Parse measurements from a measurement sheet.""" measurements = [] for m in SPREADSHEET_MEASUREMENTS: measurement_type = m["type"] @@ -374,8 +371,7 @@ class SpreadsheetAbsolutesFactory(object): calculation_sheet: openpyxl.worksheet, summary_sheet: openpyxl.worksheet, ) -> Dict: - """Parse metadata from various sheets. - """ + """Parse metadata from various sheets.""" errors = [] mark_azimuth = None try: diff --git a/geomagio/residual/WebAbsolutesFactory.py b/geomagio/residual/WebAbsolutesFactory.py index 12073429..56998fb9 100644 --- a/geomagio/residual/WebAbsolutesFactory.py +++ b/geomagio/residual/WebAbsolutesFactory.py @@ -11,8 +11,7 @@ from .Reading import Reading class WebAbsolutesFactory(object): - """Read absolutes from web absolutes service. - """ + """Read absolutes from web absolutes service.""" def __init__( self, url: str = "https://geomag.usgs.gov/baselines/observation.json.php" @@ -39,8 +38,7 @@ class WebAbsolutesFactory(object): return self.parse_json(data) def parse_json(self, jsonstr: IO[str]) -> List[Reading]: - """Parse readings from the web absolutes JSON format. - """ + """Parse readings from the web absolutes JSON format.""" readings = [] response = json.load(jsonstr) for data in response["data"]: @@ -88,8 +86,7 @@ class WebAbsolutesFactory(object): } def _parse_reading(self, metadata: Mapping, data: Mapping) -> Reading: - """Parse absolutes and measurements from Reading json. - """ + """Parse absolutes and measurements from Reading json.""" absolutes = [ self._parse_absolute(element, data[element]) for element in ["D", "H", "Z"] diff --git a/geomagio/temperature/TEMPWriter.py b/geomagio/temperature/TEMPWriter.py index 57667d01..630f452d 100644 --- a/geomagio/temperature/TEMPWriter.py +++ b/geomagio/temperature/TEMPWriter.py @@ -9,8 +9,7 @@ from obspy.core import Stream class TEMPWriter(object): - """TEMP writer. - """ + """TEMP writer.""" def __init__(self, empty_value=numpy.int("9999")): self.empty_value = empty_value diff --git a/geomagio/vbf/VBFWriter.py b/geomagio/vbf/VBFWriter.py index 518e4a0b..bfb18f5e 100644 --- a/geomagio/vbf/VBFWriter.py +++ b/geomagio/vbf/VBFWriter.py @@ -9,8 +9,7 @@ from obspy.core import Stream class VBFWriter(object): - """VBF writer. - """ + """VBF writer.""" def __init__(self, empty_value=numpy.int("9999999")): self.empty_value = empty_value diff --git a/test/Controller_test.py b/test/Controller_test.py index c7d0509d..4a722ada 100644 --- a/test/Controller_test.py +++ b/test/Controller_test.py @@ -21,9 +21,9 @@ from obspy.core import UTCDateTime def test_controller(): """Controller_test.test_controller() - instantiate the controller, make certain the factories and algorithms - are set - """ + instantiate the controller, make certain the factories and algorithms + are set + """ inputfactory = TimeseriesFactory() outputfactory = TimeseriesFactory() algorithm = Algorithm() diff --git a/test/ObservatoryMetadata_test.py b/test/ObservatoryMetadata_test.py index 13554894..902abffb 100644 --- a/test/ObservatoryMetadata_test.py +++ b/test/ObservatoryMetadata_test.py @@ -46,8 +46,7 @@ DATA_INTERVAL_TYPE = { def test_set_metadata(): - """ObservatoryMetadata_test.test_set_metadata() - """ + """ObservatoryMetadata_test.test_set_metadata()""" # Test set_metadata by passing in a stats class, and looking # for parameters that are both passed in, and aquired from the default # metadata. diff --git a/test/TimeseriesUtility_test.py b/test/TimeseriesUtility_test.py index 6a0d4e68..827b0afe 100644 --- a/test/TimeseriesUtility_test.py +++ b/test/TimeseriesUtility_test.py @@ -12,8 +12,7 @@ assert_array_equal = numpy.testing.assert_array_equal def test_create_empty_trace(): - """TimeseriesUtility_test.test_create_empty_trace() - """ + """TimeseriesUtility_test.test_create_empty_trace()""" trace1 = _create_trace([1, 1, 1, 1, 1], "H", UTCDateTime("2018-01-01")) trace2 = _create_trace([2, 2], "E", UTCDateTime("2018-01-01")) observatory = "Test" @@ -171,8 +170,7 @@ def test_get_merged_gaps(): def test_get_trace_values(): - """TimeseriesUtility_test.test_get_trace_values() - """ + """TimeseriesUtility_test.test_get_trace_values()""" stream = Stream( [ __create_trace("H", [numpy.nan, 1, 1, numpy.nan, numpy.nan]), @@ -213,8 +211,7 @@ def test_get_trace_values(): def test_has_all_channels(): - """TimeseriesUtility_test.test_has_all_channels(): - """ + """TimeseriesUtility_test.test_has_all_channels():""" nan = numpy.nan stream = Stream( [ @@ -245,8 +242,7 @@ def test_has_all_channels(): def test_has_any_channels(): - """TimeseriesUtility_test.test_has_any_channels(): - """ + """TimeseriesUtility_test.test_has_any_channels():""" nan = numpy.nan stream = Stream( [ @@ -338,8 +334,7 @@ def test_merge_streams(): def test_pad_timeseries(): - """TimeseriesUtility_test.test_pad_timeseries() - """ + """TimeseriesUtility_test.test_pad_timeseries()""" trace1 = _create_trace([1, 1, 1, 1, 1], "H", UTCDateTime("2018-01-01")) trace2 = _create_trace([2, 2], "E", UTCDateTime("2018-01-01")) timeseries = Stream(traces=[trace1, trace2]) @@ -363,8 +358,7 @@ def test_pad_timeseries(): def test_pad_and_trim_trace(): - """TimeseriesUtility_test.test_pad_and_trim_trace() - """ + """TimeseriesUtility_test.test_pad_and_trim_trace()""" trace = _create_trace([0, 1, 2, 3, 4], "X", UTCDateTime("2018-01-01")) assert_equal(trace.stats.starttime, UTCDateTime("2018-01-01T00:00:00Z")) assert_equal(trace.stats.endtime, UTCDateTime("2018-01-01T00:04:00Z")) diff --git a/test/Util_test.py b/test/Util_test.py index 2c44beb7..3920bf0d 100644 --- a/test/Util_test.py +++ b/test/Util_test.py @@ -7,8 +7,7 @@ from obspy.core import UTCDateTime def test_get_file_for_url__throws_exception(): - """Util_test.test_get_file_for_url__throws_exception() - """ + """Util_test.test_get_file_for_url__throws_exception()""" # throws exception for non "file://" urls try: Util.get_file_from_url("http://someserver/path") @@ -18,16 +17,14 @@ def test_get_file_for_url__throws_exception(): def test_get_file_for_url__parses_file_urls(): - """Util_test.test_get_file_for_url__parses_file_urls() - """ + """Util_test.test_get_file_for_url__parses_file_urls()""" # parses file urls f = Util.get_file_from_url("file://./somefile") assert_equal(f, "./somefile") def test_get_file_for_url__creates_directories(): - """Util_test.test_get_file_for_url__creates_directories() - """ + """Util_test.test_get_file_for_url__creates_directories()""" # creates directories if requested if os.path.isdir("/tmp/_geomag_algorithms_test_"): shutil.rmtree("/tmp/_geomag_algorithms_test_") @@ -41,8 +38,7 @@ def test_get_file_for_url__creates_directories(): def test_get_interval__defaults(): - """Util_test.test_get_interval() - """ + """Util_test.test_get_interval()""" starttime = UTCDateTime("2015-01-01T00:00:00Z") endtime = UTCDateTime("2015-02-01T00:00:00Z") intervals = Util.get_intervals(starttime, endtime) @@ -50,8 +46,7 @@ def test_get_interval__defaults(): def test_get_interval__custom_size(): - """Util_test.test_get_interval__custom_size() - """ + """Util_test.test_get_interval__custom_size()""" starttime = UTCDateTime("2015-01-01T00:00:00Z") endtime = UTCDateTime("2015-01-02T00:00:00Z") intervals = Util.get_intervals(starttime, endtime, size=3600) @@ -59,8 +54,7 @@ def test_get_interval__custom_size(): def test_get_interval__negative_size(): - """Util_test.test_get_interval__negative_size() - """ + """Util_test.test_get_interval__negative_size()""" starttime = UTCDateTime("2015-01-01T00:00:00Z") endtime = UTCDateTime("2015-01-02T00:00:00Z") intervals = Util.get_intervals(starttime, endtime, size=-1) @@ -70,8 +64,7 @@ def test_get_interval__negative_size(): def test_get_interval__trim(): - """Util_test.test_get_interval__trim() - """ + """Util_test.test_get_interval__trim()""" starttime = UTCDateTime("2015-01-01T01:00:00Z") endtime = UTCDateTime("2015-01-02T00:00:00Z") intervals = Util.get_intervals(starttime, endtime, trim=True) diff --git a/test/algorithm_test/AdjustedAlgorithm_test.py b/test/algorithm_test/AdjustedAlgorithm_test.py index 532bb8c6..f37f93f7 100644 --- a/test/algorithm_test/AdjustedAlgorithm_test.py +++ b/test/algorithm_test/AdjustedAlgorithm_test.py @@ -4,8 +4,7 @@ from numpy.testing import assert_almost_equal, assert_equal def test_construct(): - """algorithm_test.AdjustedAlgorithm_test.test_construct() - """ + """algorithm_test.AdjustedAlgorithm_test.test_construct()""" # load adjusted data transform matrix and pier correction a = adj(statefile="etc/adjusted/adjbou_state_.json") diff --git a/test/algorithm_test/SQDistAlgorithm_test.py b/test/algorithm_test/SQDistAlgorithm_test.py index 49d15728..767f71c1 100644 --- a/test/algorithm_test/SQDistAlgorithm_test.py +++ b/test/algorithm_test/SQDistAlgorithm_test.py @@ -11,8 +11,8 @@ from numpy.testing import ( def test_sqdistalgorithm_additive1(): """SqDistAlgorithm_test.test_sqdistalgorithm_additive1() - Uses a simple 12 point data series to compare additive inputs with - corresponding outputs. + Uses a simple 12 point data series to compare additive inputs with + corresponding outputs. """ # configure to test zero-step predictions of 4 "season" cycles m = 4 @@ -314,8 +314,8 @@ def test_sqdistalgorithm_additive1(): def test_sqdistalgorithm_additive2(): """SqDistAlgorithm_test.test_sqdistalgorithm_additive2() - Uses synthetic data time series over 300 days to test additive method - outputs. + Uses synthetic data time series over 300 days to test additive method + outputs. """ # set up smoothing parameters m = 100 # length of "day" diff --git a/test/edge_test/EdgeFactory_test.py b/test/edge_test/EdgeFactory_test.py index 433ddd75..fca38a8c 100644 --- a/test/edge_test/EdgeFactory_test.py +++ b/test/edge_test/EdgeFactory_test.py @@ -6,22 +6,19 @@ from numpy.testing import assert_equal def test__get_edge_network(): - """edge_test.EdgeFactory_test.test__get_edge_network() - """ + """edge_test.EdgeFactory_test.test__get_edge_network()""" # _get_edge_network should always return NT for use by USGS geomag assert_equal(EdgeFactory()._get_edge_network(" ", " ", " ", " "), "NT") def test__get_edge_station(): - """edge_test.EdgeFactory_test.test__get_edge_station() - """ + """edge_test.EdgeFactory_test.test__get_edge_station()""" # _get_edge_station will return the observatory code passed in. assert_equal(EdgeFactory()._get_edge_station("BOU", " ", " ", " "), "BOU") def test__get_edge_channel(): - """edge_test.EdgeFactory_test.test__get_edge_channel() - """ + """edge_test.EdgeFactory_test.test__get_edge_channel()""" # Call private function _get_edge_channel, make certain # it gets back the appropriate 2 character code. assert_equal(EdgeFactory()._get_edge_channel("", "D", "", "minute"), "MVD") @@ -35,8 +32,7 @@ def test__get_edge_channel(): def test__get_edge_location(): - """edge_test.EdgeFactory_test.test__get_edge_location() - """ + """edge_test.EdgeFactory_test.test__get_edge_location()""" # Call _get_edge_location, make certain it returns the correct edge # location code. assert_equal(EdgeFactory()._get_edge_location("", "", "variation", ""), "R0") @@ -45,8 +41,7 @@ def test__get_edge_location(): def test__get_interval_code(): - """edge_test.EdgeFactory_test.test__get_interval_code() - """ + """edge_test.EdgeFactory_test.test__get_interval_code()""" assert_equal(EdgeFactory()._get_interval_code("day"), "D") assert_equal(EdgeFactory()._get_interval_code("hour"), "H") assert_equal(EdgeFactory()._get_interval_code("minute"), "M") @@ -54,8 +49,7 @@ def test__get_interval_code(): def test__set_metadata(): - """edge_test.EdgeFactory_test.test__set_metadata() - """ + """edge_test.EdgeFactory_test.test__set_metadata()""" # Call _set_metadata with 2 traces, and make certain the stats get # set for both traces. trace1 = Trace() diff --git a/test/edge_test/MiniSeedFactory_test.py b/test/edge_test/MiniSeedFactory_test.py index 18f2cee5..24397dbc 100644 --- a/test/edge_test/MiniSeedFactory_test.py +++ b/test/edge_test/MiniSeedFactory_test.py @@ -8,22 +8,19 @@ from geomagio.edge import MiniSeedFactory def test__get_edge_network(): - """edge_test.MiniSeedFactory_test.test__get_edge_network() - """ + """edge_test.MiniSeedFactory_test.test__get_edge_network()""" # _get_edge_network should always return NT for use by USGS geomag assert_equal(MiniSeedFactory()._get_edge_network(" ", " ", " ", " "), "NT") def test__get_edge_station(): - """edge_test.MiniSeedFactory_test.test__get_edge_station() - """ + """edge_test.MiniSeedFactory_test.test__get_edge_station()""" # _get_edge_station will return the observatory code passed in. assert_equal(MiniSeedFactory()._get_edge_station("BOU", " ", " ", " "), "BOU") def test__get_edge_channel(): - """edge_test.MiniSeedFactory_test.test__get_edge_channel() - """ + """edge_test.MiniSeedFactory_test.test__get_edge_channel()""" # Call private function _get_edge_channel, make certain # it gets back the appropriate 2 character code. factory = MiniSeedFactory() @@ -39,8 +36,7 @@ def test__get_edge_channel(): def test__get_edge_location(): - """edge_test.MiniSeedFactory_test.test__get_edge_location() - """ + """edge_test.MiniSeedFactory_test.test__get_edge_location()""" # Call _get_edge_location, make certain it returns the correct edge # location code. assert_equal(MiniSeedFactory()._get_edge_location("", "", "variation", ""), "R0") @@ -51,8 +47,7 @@ def test__get_edge_location(): def test__get_interval_code(): - """edge_test.MiniSeedFactory_test.test__get_interval_code() - """ + """edge_test.MiniSeedFactory_test.test__get_interval_code()""" assert_equal(MiniSeedFactory()._get_interval_code("day"), "P") assert_equal(MiniSeedFactory()._get_interval_code("hour"), "R") assert_equal(MiniSeedFactory()._get_interval_code("minute"), "U") @@ -73,8 +68,7 @@ class MockMiniSeedInputClient(object): def test__put_timeseries(): - """edge_test.MiniSeedFactory_test.test__put_timeseries() - """ + """edge_test.MiniSeedFactory_test.test__put_timeseries()""" trace1 = __create_trace([0, 1, 2, 3, numpy.nan, 5, 6, 7, 8, 9], channel="H") client = MockMiniSeedInputClient() factory = MiniSeedFactory() @@ -97,8 +91,7 @@ def test__put_timeseries(): def test__set_metadata(): - """edge_test.MiniSeedFactory_test.test__set_metadata() - """ + """edge_test.MiniSeedFactory_test.test__set_metadata()""" # Call _set_metadata with 2 traces, and make certain the stats get # set for both traces. trace1 = Trace() diff --git a/test/edge_test/RawInputClient_test.py b/test/edge_test/RawInputClient_test.py index d9676b52..850f1a7d 100644 --- a/test/edge_test/RawInputClient_test.py +++ b/test/edge_test/RawInputClient_test.py @@ -17,8 +17,7 @@ class MockRawInputClient(RawInputClient): def test_raw_input_client(): - """edge_test.RawInputClient_test.test_raw_input_client() - """ + """edge_test.RawInputClient_test.test_raw_input_client()""" network = "NT" station = "BOU" channel = "MVH" @@ -57,8 +56,7 @@ def test_raw_input_client(): def test__get_tag(): - """edge_test.RawInputClient_test.test_raw_input_client() - """ + """edge_test.RawInputClient_test.test_raw_input_client()""" network = "NT" station = "BOU" channel = "MVH" diff --git a/test/imfv122_test/IMFV122Parser_test.py b/test/imfv122_test/IMFV122Parser_test.py index 951d4160..2a3d87a1 100644 --- a/test/imfv122_test/IMFV122Parser_test.py +++ b/test/imfv122_test/IMFV122Parser_test.py @@ -6,8 +6,7 @@ from obspy.core import UTCDateTime def test_imfv122_parse_header__hour_of_day(): - """imfv122_test.test_imfv122_parse_header__minutes. - """ + """imfv122_test.test_imfv122_parse_header__minutes.""" parser = IMFV122Parser() parser._parse_header( "KAK MAY0216 123 03 HDZF A KYO 05381402 000000 RRRRRRRRRRRRRRRR" @@ -23,8 +22,7 @@ def test_imfv122_parse_header__hour_of_day(): def test_imfv122_parse_header__minute_of_day(): - """imfv122_test.test_imfv122_parse_header__seconds. - """ + """imfv122_test.test_imfv122_parse_header__seconds.""" parser = IMFV122Parser() parser._parse_header( "HER JAN0116 001 0123 HDZF R EDI 12440192 -14161 DRRRRRRRRRRRRRRR" @@ -40,8 +38,7 @@ def test_imfv122_parse_header__minute_of_day(): def test_imfv122_parse_data(): - """imfv122_test.test_imfv122_parse_data. - """ + """imfv122_test.test_imfv122_parse_data.""" parser = IMFV122Parser() parser._parse_header( "HER JAN0116 001 0123 HDZF R EDI 12440192 -14161 DRRRRRRRRRRRRRRR" @@ -63,8 +60,7 @@ def test_imfv122_parse_data(): def test_imfv122_post_process(): - """imfv122_test.test_imfv122_post_process. - """ + """imfv122_test.test_imfv122_post_process.""" parser = IMFV122Parser() parser._parse_header( "HER JAN0116 001 0123 HDZF R EDI 12440192 -14161 DRRRRRRRRRRRRRRR" diff --git a/test/imfv283_test/IMFV283Parser_test.py b/test/imfv283_test/IMFV283Parser_test.py index 70dcbf76..02ca2d3b 100644 --- a/test/imfv283_test/IMFV283Parser_test.py +++ b/test/imfv283_test/IMFV283Parser_test.py @@ -33,8 +33,7 @@ def test_parse_msg_header(): def test_parse_goes_header(): - """imfv283_test.IMFV283Parser_test.test_parse_goes_header() - """ + """imfv283_test.IMFV283Parser_test.test_parse_goes_header()""" goes_data = IMFV283Parser()._process_ness_block( IMFV283_EXAMPLE_VIC, imfv283_codes.OBSERVATORIES["VIC"], 191 ) -- GitLab From 4ad6f21a859d47c8acb5c69c2107624478548898 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Thu, 27 Aug 2020 10:59:58 -0600 Subject: [PATCH 4/6] resave headers in data.py --- geomagio/api/ws/data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geomagio/api/ws/data.py b/geomagio/api/ws/data.py index bbf67215..76107cb4 100644 --- a/geomagio/api/ws/data.py +++ b/geomagio/api/ws/data.py @@ -121,7 +121,7 @@ def format_timeseries( else: data = IAGA2002Writer.format(timeseries, elements) media_type = "text/plain" - headers = {"allow_origins": "*", "max_age": "86400", "allow_headers": "*"} + headers = {"allow_origins": "*", "max_age": "", "allow_headers": "*"} return Response(data, media_type=media_type, headers=headers) -- GitLab From 55595c27424d29bc8d503350e7d1e33e8f69a9ff Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Thu, 27 Aug 2020 11:57:11 -0600 Subject: [PATCH 5/6] change black formatter version --- .pre-commit-config.yaml | 6 +++--- geomagio/algorithm/AdjustedAlgorithm.py | 4 +++- geomagio/api/secure/SessionMiddleware.py | 5 ++++- geomagio/api/secure/login.py | 4 +++- geomagio/api/secure/metadata.py | 4 +++- geomagio/api/ws/Element.py | 5 ++++- geomagio/edge/EdgeFactory.py | 9 ++++++++- geomagio/pcdcp/PCDCPFactory.py | 4 +++- geomagio/processing/magproc.py | 5 ++++- geomagio/residual/SpreadsheetAbsolutesFactory.py | 3 ++- setup.py | 4 +++- 11 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 087d0de6..6b5dbc4f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ repos: -- repo: https://github.com/ambv/black - rev: stable + - repo: https://github.com/ambv/black + rev: 20.8b1 hooks: - - id: black + - id: black diff --git a/geomagio/algorithm/AdjustedAlgorithm.py b/geomagio/algorithm/AdjustedAlgorithm.py index 0c1bbf2f..601bad24 100644 --- a/geomagio/algorithm/AdjustedAlgorithm.py +++ b/geomagio/algorithm/AdjustedAlgorithm.py @@ -27,7 +27,9 @@ class AdjustedAlgorithm(Algorithm): inchannels = inchannels or ["H", "E", "Z", "F"] outchannels = outchannels or ["X", "Y", "Z", "F"] Algorithm.__init__( - self, inchannels=inchannels, outchannels=outchannels, + self, + inchannels=inchannels, + outchannels=outchannels, ) # state variables self.matrix = matrix diff --git a/geomagio/api/secure/SessionMiddleware.py b/geomagio/api/secure/SessionMiddleware.py index d4ef2090..c757c8b3 100644 --- a/geomagio/api/secure/SessionMiddleware.py +++ b/geomagio/api/secure/SessionMiddleware.py @@ -110,7 +110,10 @@ class SessionMiddleware: await self.save_session_callback(session_id, data) def set_cookie( - self, message: Message, value: str, max_age: int = None, + self, + message: Message, + value: str, + max_age: int = None, ): headers = MutableHeaders(scope=message) headers.append("Cache-Control") diff --git a/geomagio/api/secure/login.py b/geomagio/api/secure/login.py index 334264d2..c3407d93 100644 --- a/geomagio/api/secure/login.py +++ b/geomagio/api/secure/login.py @@ -67,7 +67,9 @@ async def current_user(request: Request) -> Optional[User]: return None -def require_user(allowed_groups: List[str] = None,) -> Callable[[Request, User], User]: +def require_user( + allowed_groups: List[str] = None, +) -> Callable[[Request, User], User]: """Create function to verifies user in allowed_groups Usage example: diff --git a/geomagio/api/secure/metadata.py b/geomagio/api/secure/metadata.py index 5e4f0846..fb0dfc4c 100644 --- a/geomagio/api/secure/metadata.py +++ b/geomagio/api/secure/metadata.py @@ -31,7 +31,9 @@ router = APIRouter() @router.post("/metadata", response_model=Metadata) async def create_metadata( - request: Request, metadata: Metadata, user: User = Depends(require_user()), + request: Request, + metadata: Metadata, + user: User = Depends(require_user()), ): metadata = await metadata_table.create_metadata(metadata) return Response(metadata, status_code=201, media_type="application/json") diff --git a/geomagio/api/ws/Element.py b/geomagio/api/ws/Element.py index eff80cca..952feae9 100644 --- a/geomagio/api/ws/Element.py +++ b/geomagio/api/ws/Element.py @@ -24,7 +24,10 @@ ELEMENTS = [ Element(id="SQ", name="Solar Quiet", units="nT"), Element(id="SV", name="Solar Variation", units="nT"), Element( - id="UK1", abbreviation="T-Electric", name="Electronics Temperature", units="°C", + id="UK1", + abbreviation="T-Electric", + name="Electronics Temperature", + units="°C", ), Element( id="UK2", diff --git a/geomagio/edge/EdgeFactory.py b/geomagio/edge/EdgeFactory.py index 83c278a6..a2251465 100644 --- a/geomagio/edge/EdgeFactory.py +++ b/geomagio/edge/EdgeFactory.py @@ -544,7 +544,14 @@ class EdgeFactory(TimeseriesFactory): TimeseriesUtility.pad_timeseries(timeseries, starttime, endtime) def _put_channel( - self, timeseries, observatory, channel, type, interval, starttime, endtime, + self, + timeseries, + observatory, + channel, + type, + interval, + starttime, + endtime, ): """Put a channel worth of data diff --git a/geomagio/pcdcp/PCDCPFactory.py b/geomagio/pcdcp/PCDCPFactory.py index 5941528d..f7633d2c 100644 --- a/geomagio/pcdcp/PCDCPFactory.py +++ b/geomagio/pcdcp/PCDCPFactory.py @@ -36,7 +36,9 @@ class PCDCPFactory(TimeseriesFactory): """ def __init__( - self, temperatures=False, **kwargs, + self, + temperatures=False, + **kwargs, ): TimeseriesFactory.__init__(self, **kwargs) self.temperatures = temperatures diff --git a/geomagio/processing/magproc.py b/geomagio/processing/magproc.py index 1522e311..14f2fc74 100644 --- a/geomagio/processing/magproc.py +++ b/geomagio/processing/magproc.py @@ -69,7 +69,10 @@ def prepfiles( def write_cal_file( - starttime: UTCDateTime, endtime: UTCDateTime, observatory: str, template: str, + starttime: UTCDateTime, + endtime: UTCDateTime, + observatory: str, + template: str, ): print( f"Loading calibration data for {observatory} [{starttime}, {endtime}]", diff --git a/geomagio/residual/SpreadsheetAbsolutesFactory.py b/geomagio/residual/SpreadsheetAbsolutesFactory.py index 48121677..d53b772b 100644 --- a/geomagio/residual/SpreadsheetAbsolutesFactory.py +++ b/geomagio/residual/SpreadsheetAbsolutesFactory.py @@ -288,7 +288,8 @@ class SpreadsheetAbsolutesFactory(object): return Reading( absolutes=absolutes, azimuth=Angle.from_dms( - degrees=int(mark_azimuth / 100.0), minutes=mark_azimuth % 100, + degrees=int(mark_azimuth / 100.0), + minutes=mark_azimuth % 100, ), hemisphere=metadata["hemisphere"], measurements=measurements, diff --git a/setup.py b/setup.py index b923235e..29212231 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,9 @@ setuptools.setup( }, python_requires=">=3.6, <4", scripts=["bin/geomag.py", "bin/geomag_webservice.py", "bin/make_cal.py"], - setup_requires=["setuptools-pipfile",], + setup_requires=[ + "setuptools-pipfile", + ], use_pipfile=True, entry_points={ "console_scripts": ["magproc-prepfiles=geomagio.processing.magproc:main"], -- GitLab From bc8947e3364c364e3388f1ebd8a404cbb80b61f6 Mon Sep 17 00:00:00 2001 From: Travis Rivers <trivers@contractor.usgs.gov> Date: Mon, 31 Aug 2020 10:52:33 -0600 Subject: [PATCH 6/6] format --- geomagio/api/secure/SessionMiddleware.py | 2 +- geomagio/api/ws/data.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/geomagio/api/secure/SessionMiddleware.py b/geomagio/api/secure/SessionMiddleware.py index c757c8b3..04c621c1 100644 --- a/geomagio/api/secure/SessionMiddleware.py +++ b/geomagio/api/secure/SessionMiddleware.py @@ -116,7 +116,7 @@ class SessionMiddleware: max_age: int = None, ): headers = MutableHeaders(scope=message) - headers.append("Cache-Control") + headers.append("Cache-Control", "no-cache") headers.append( "Set-Cookie", f"{self.session_cookie}={value};" diff --git a/geomagio/api/ws/data.py b/geomagio/api/ws/data.py index 76107cb4..053474c1 100644 --- a/geomagio/api/ws/data.py +++ b/geomagio/api/ws/data.py @@ -121,8 +121,7 @@ def format_timeseries( else: data = IAGA2002Writer.format(timeseries, elements) media_type = "text/plain" - headers = {"allow_origins": "*", "max_age": "", "allow_headers": "*"} - return Response(data, media_type=media_type, headers=headers) + return Response(data, media_type=media_type) def get_timeseries(data_factory: TimeseriesFactory, query: DataApiQuery) -> Stream: -- GitLab