-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from shubhbapna/use-url
allow use of url for req, constraint and graph files
- Loading branch information
Showing
13 changed files
with
98 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import io | ||
import pathlib | ||
import typing | ||
from contextlib import contextmanager | ||
from urllib.parse import urlparse | ||
|
||
from .request_session import session | ||
|
||
|
||
@contextmanager | ||
def open_file_or_url( | ||
path_or_url: str | pathlib.Path, | ||
) -> typing.Generator[io.TextIOWrapper, typing.Any, None]: | ||
location = str(path_or_url) | ||
if location.startswith("file://"): | ||
location = urlparse(location).path | ||
|
||
if location.startswith(("https://", "http://")): | ||
response = session.get(location) | ||
yield io.StringIO(response.text) | ||
else: | ||
f = open(location, "r") | ||
try: | ||
yield f | ||
finally: | ||
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pathlib | ||
|
||
import requests_mock | ||
|
||
from fromager import read | ||
|
||
|
||
def test_read_from_file(tmp_path: pathlib.Path): | ||
file = tmp_path / "test" | ||
text = ["hello", "world"] | ||
file.write_text("\n".join(text)) | ||
with read.open_file_or_url(file) as f: | ||
for index, line in enumerate(f): | ||
assert line.strip() == text[index] | ||
|
||
|
||
def test_read_from_url(): | ||
url = "https://someurl.com" | ||
text = ["hello", "world"] | ||
with requests_mock.Mocker() as r: | ||
r.get(url, text="\n".join(text)) | ||
with read.open_file_or_url(url) as f: | ||
for index, line in enumerate(f): | ||
assert line.strip() == text[index] |