-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.py
More file actions
75 lines (61 loc) · 2.16 KB
/
io.py
File metadata and controls
75 lines (61 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import contextlib
import io
import typing as t
from pathlib import Path
from pathlibfs import Path as PathPlus
from yarl import URL
@contextlib.contextmanager
def to_io(source: t.Union[str, Path, t.IO]) -> t.Generator[t.IO, None, None]:
"""
Main context manager for accessing resources.
Before accessing / opening, it converges a path string, object, or IO handle, to an IO handle.
"""
fp: t.IO
if isinstance(source, io.TextIOWrapper):
fp = source
elif isinstance(source, (str, Path, PathPlus)):
source = str(source)
path = open_url(source)
fp = path.open(mode="rt")
else:
raise TypeError(f"Unable to converge to IO handle. type={type(source)}, value={source}")
yield fp
fp.close()
def open_url(url: str) -> PathPlus:
"""
Access URL, with specific handling for GitHub URLs.
When approached using a GitHub HTTP URL, converge it to a pathlibfs / fsspec URL,
and open it.
Input URLs
----------
github+https://foobar:ghp_lalala@github.com/acme/sweet-camino/path/to/document.md
github+https://foobar:ghp_lalala@github.com/acme/sweet-camino/blob/main/path/to/document.md
Output Path
-----------
fs = Path("github://path/to/document.md", username="foobar", token="ghp_lalala", org="acme", repo="sweet-camino")
"""
uri = URL(url)
if uri.scheme.startswith("github+https"):
path_fragments = uri.path.split("/")[1:]
path_kwargs = {
"username": uri.user,
"token": uri.password,
"org": path_fragments[0],
"repo": path_fragments[1],
}
real_path_fragments = path_fragments[2:]
if path_fragments[2] in ["blob", "raw"]:
real_path_fragments = path_fragments[4:]
downstream_url = "github://" + "/".join(real_path_fragments)
path = PathPlus(downstream_url, **path_kwargs)
else:
path = PathPlus(url)
return path
def path_without_scheme(url_like: str) -> PathPlus:
"""
Return a pathlibfs Path, without the scheme.
"""
url = URL(str(url_like))
if url.is_absolute():
url = url.with_scheme("")
return PathPlus(str(url))