@@ -16,8 +16,6 @@ def __init__(self, parsed_url, *args, **kwargs):
1616 )
1717 url_kwargs .update (kwargs )
1818 self ._fs = cls (** url_kwargs )
19- if self ._url .scheme in ["hdfs" ]:
20- self ._fs .root_marker = "/"
2119
2220 def argument_upath_self_to_filepath (self , func ):
2321 """if arguments are passed to the wrapped function, and if the first
@@ -26,33 +24,33 @@ def argument_upath_self_to_filepath(self, func):
2624 """
2725
2826 def wrapper (* args , ** kwargs ):
29- if args :
30- args = list (args )
31- first_arg = args .pop (0 )
32- if not kwargs .get ("path" ):
33- if isinstance (first_arg , UniversalPath ):
34- first_arg = first_arg .path
35- if not self ._fs .root_marker and first_arg .startswith (
36- "/"
37- ):
38- first_arg = first_arg [1 :]
39- args .insert (0 , first_arg )
40- args = tuple (args )
41- else :
42- if not self ._fs .root_marker and kwargs ["path" ].startswith (
43- "/"
44- ):
45- kwargs ["path" ] = kwargs ["path" ][1 :]
46- if self ._url .scheme == "hdfs" :
47- if "trunicate" in kwargs :
48- kwargs .pop ("trunicate" )
49- if func .__name__ == "mkdir" :
50- args = args [:1 ]
51-
27+ args , kwargs = self ._format_path (args , kwargs )
5228 return func (* args , ** kwargs )
5329
5430 return wrapper
5531
32+ def _format_path (self , args , kwargs ):
33+ """formats the path properly for the filesystem backend."""
34+ args = list (args )
35+ first_arg = args .pop (0 )
36+ if not kwargs .get ("path" ):
37+ if isinstance (first_arg , UniversalPath ):
38+ first_arg = self ._remove_root_slash (first_arg .path )
39+ args .insert (0 , first_arg )
40+ args = tuple (args )
41+ else :
42+ kwargs ["path" ] = self ._remove_root_slash (kwargs ["path" ])
43+ return args , kwargs
44+
45+ def _remove_root_slash (self , s ):
46+ """If the filesystem backend doesn't have a root_marker, strip the
47+ leading slash of a path
48+ """
49+ if not self ._fs .root_marker and s .startswith ("/" ):
50+ return s [1 :]
51+ else :
52+ return s
53+
5654 def __getattribute__ (self , item ):
5755 class_attrs = ["_url" , "_fs" , "__class__" ]
5856 if item in class_attrs :
@@ -62,6 +60,8 @@ def __getattribute__(self, item):
6260 "__init__" ,
6361 "__getattribute__" ,
6462 "argument_upath_self_to_filepath" ,
63+ "_format_path" ,
64+ "_remove_root_slash" ,
6565 ]
6666 if item in class_methods :
6767 return lambda * args , ** kwargs : getattr (self .__class__ , item )(
@@ -134,8 +134,8 @@ def _init(self, *args, template=None, **kwargs):
134134
135135 def __getattribute__ (self , item ):
136136 if item == "__class__" :
137- return UniversalPath
138- if item in getattr (UniversalPath , "not_implemented" ):
137+ return super (). __getattribute__ ( "__class__" )
138+ if item in getattr (self . __class__ , "not_implemented" ):
139139 raise NotImplementedError (f"UniversalPath has no attribute { item } " )
140140 else :
141141 return super ().__getattribute__ (item )
@@ -254,7 +254,7 @@ def _from_parts_init(cls, args, init=False):
254254 def _from_parts (self , args , init = True ):
255255 # We need to call _parse_args on the instance, so as to get the
256256 # right flavour.
257- obj = object .__new__ (UniversalPath )
257+ obj = object .__new__ (self . __class__ )
258258 drv , root , parts = self ._parse_args (args )
259259 obj ._drv = drv
260260 obj ._root = root
@@ -264,7 +264,7 @@ def _from_parts(self, args, init=True):
264264 return obj
265265
266266 def _from_parsed_parts (self , drv , root , parts , init = True ):
267- obj = object .__new__ (UniversalPath )
267+ obj = object .__new__ (self . __class__ )
268268 obj ._drv = drv
269269 obj ._root = root
270270 obj ._parts = parts
0 commit comments