Skip to content

Commit 162305c

Browse files
committed
make httppath implementation
1 parent 48103c4 commit 162305c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

upath/implementations/__init__.py

Whitespace-only changes.

upath/implementations/http.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import urllib
2+
3+
from upath.core import UniversalPath, _FSSpecAccessor
4+
5+
6+
class _HTTPAccessor(_FSSpecAccessor):
7+
def __init__(self, parsed_url, *args, **kwargs):
8+
super().__init__(parsed_url, *args, **kwargs)
9+
10+
def argument_upath_self_to_filepath(self, func):
11+
"""if arguments are passed to the wrapped function, and if the first
12+
argument is a UniversalPath instance, that argument is replaced with
13+
the UniversalPath's path attribute
14+
"""
15+
def wrapper(*args, **kwargs):
16+
if args:
17+
args = list(args)
18+
first_arg = args.pop(0)
19+
if not kwargs.get("path"):
20+
if isinstance(first_arg, UniversalPath):
21+
first_arg = str(first_arg)
22+
args.insert(0, first_arg)
23+
args = tuple(args)
24+
else:
25+
new_url = self._url.replace(path=kwargs["path"])
26+
unparsed = urllib.urlunparse(new_url)
27+
kwargs["path"] = unparsed
28+
return func(*args, **kwargs)
29+
return wrapper
30+
31+
32+
class HTTPPath(UniversalPath):
33+
_default_accessor = _HTTPAccessor

0 commit comments

Comments
 (0)