-
Notifications
You must be signed in to change notification settings - Fork 0
I26 use urls #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I26 use urls #27
Changes from 7 commits
1857448
5436a19
1807593
15b748d
d2a66bb
13fe89d
4283862
dba1546
d6772ea
d7ee5c3
9eb95c2
8f71878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |
| import logging | ||
| import os | ||
| import json | ||
| import requests | ||
| from datetime import datetime, timedelta | ||
| from tempfile import NamedTemporaryFile | ||
|
|
||
| logger = logging.getLogger("PYWPS") | ||
| logger.setLevel(logging.NOTSET) | ||
|
|
@@ -34,6 +36,38 @@ def replace_filenames(config, temp_config): | |
| temp_config.writelines(newdata) | ||
|
|
||
|
|
||
| def replace_urls(config, outdir): | ||
| """ | ||
| Copy https URLs to local storage and replace URLs | ||
| with local paths in config file. | ||
| Parameters: | ||
| config (str): Config file | ||
| outdir (str): Output directory | ||
| """ | ||
| read_config = open(config, "r") | ||
| filedata = read_config.readlines() | ||
| read_config.close() | ||
|
|
||
| for i in range(len(filedata)): | ||
| if "https" in filedata[i]: | ||
| url = filedata[i].split(" ")[-1] # https url is last word in line | ||
| url = url.rstrip() # remove \n character at end | ||
| r = requests.get(url) | ||
| filename = url.split("/")[-1] | ||
| prefix, suffix = filename.split(".") | ||
| suffix = "." + suffix | ||
| local_file = NamedTemporaryFile( | ||
| suffix=suffix, prefix=prefix, dir=outdir, delete=False | ||
| ) | ||
|
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of tempfile here 👍 |
||
| local_file.write(r.content) | ||
| filedata[i] = filedata[i].replace(url, local_file.name) | ||
|
Comment on lines
+49
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this is fine, we generally don't want to use indices if we can help it. It is more understandable to iterate through the iterable object :
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the index because I wasn't sure if the assignment in line 63 would modify the |
||
|
|
||
| write_config = open(config, "w") | ||
| for line in filedata: | ||
| write_config.write(f"{line}\n") | ||
| write_config.close() | ||
|
|
||
|
|
||
| def config_hander(workdir, unprocessed, config_template): | ||
| """ | ||
| This function enables users to provide dictionary-like string for Configuration input. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| pytest | ||
| flake8 | ||
| pytest-flake8 | ||
| requests-mock | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know this package existed. I wonder if we can apply to our notebooks to make truly offline tests. |
||
| ipython | ||
| pytest-notebook | ||
| nbsphinx | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ SEARCH_FOR_CHANNEL: False | |
| #-- Path to Pour Points File (char) --# | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to use this file in the jupyter lab demo as well |
||
| # A comma separated file of outlets to route to [lons, lats] - one coordinate pair per line (order not important) | ||
| # May optionally include a column [names] - which will (if not aggregating) be included in param file | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/dodsC/datasets/RVIC/sample_pour.txt | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/fileServer/datasets/RVIC/sample_pour.txt | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this change from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so that |
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the ideal use of |
||
| #-- ====================================== --# | ||
|
|
||
|
|
@@ -83,7 +83,7 @@ FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/dodsC/da | |
| # This defines the unit hydrograph to rout flow to the edge of each grid cell. | ||
| # A comma separated file of [time in seconds, unit hydrograph ordinate] - one timestep per line | ||
| # The timestep should be 1hr (3600 sec) or less. | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/dodsC/datasets/RVIC/uhbox.csv | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/fileServer/datasets/RVIC/uhbox.csv | ||
|
|
||
| #-- Number of Header lines to ignore in [UH_BOX]FILE_NAME (INT) --# | ||
| HEADER_LINES = 1 | ||
|
|
@@ -92,7 +92,7 @@ HEADER_LINES = 1 | |
| [ROUTING] | ||
| #-- ====================================== --# | ||
| #-- Path to routing inputs netcdf (char) --# | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/dodsC/datasets/RVIC/sample_flow_parameters.nc | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/fileServer/datasets/RVIC/sample_flow_parameters.nc | ||
|
|
||
| #-- netCDF Variable Names --# | ||
| LONGITUDE_VAR: lon | ||
|
|
@@ -124,7 +124,7 @@ CELL_FLOWDAYS: 4 | |
| [DOMAIN] | ||
| #-- ====================================== --# | ||
| #-- Path to cesm compliant domain file (char) --# | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/dodsC/datasets/RVIC/sample_routing_domain.nc | ||
| FILE_NAME: https://docker-dev03.pcic.uvic.ca/twitcher/ows/proxy/thredds/fileServer/datasets/RVIC/sample_routing_domain.nc | ||
|
|
||
| #-- netCDF Variable Names --# | ||
| LONGITUDE_VAR: lon | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import pytest | ||
| from pkg_resources import resource_filename | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def make_mock_urls(config, requests_mock): | ||
| read_config = open(config, "r") | ||
| config_data = read_config.readlines() | ||
| read_config.close() | ||
| for line in config_data: | ||
| if "https" in line: | ||
| url = line.split(" ")[-1] # https url is last word in line | ||
| url = url.rstrip() # remove \n character at end | ||
| filename = url.split("/")[-1] | ||
| f = open(resource_filename(__name__, f"data/samples/{filename}"), "rb") | ||
| filedata = f.read() | ||
| f.close() | ||
| requests_mock.get(url, content=filedata) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,19 @@ def test_parameters_local(config): | |
| temp_config.read() | ||
| params = f"config={temp_config.name};" | ||
| run_wps_process(Parameters(), params) | ||
|
|
||
|
|
||
| @pytest.mark.online | ||
| @pytest.mark.parametrize( | ||
| ("config"), [resource_filename(__name__, "configs/parameter_https.cfg")], | ||
| ) | ||
| def test_parameters_https(config, make_mock_urls): | ||
|
||
| config_name = os.path.splitext(config)[0] # Remove .cfg extension | ||
| with NamedTemporaryFile( | ||
| suffix=".cfg", prefix=os.path.basename(config_name), mode="w+t" | ||
| ) as temp_config: # Avoid permanent replacement of https URLs | ||
| read_config = open(config, "r") | ||
| temp_config.writelines(read_config.read()) | ||
| temp_config.read() | ||
|
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we aren't just directly reading into the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean here. |
||
| params = f"config={temp_config.name};" | ||
| run_wps_process(Parameters(), params) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use a context manager here.