@@ -45,6 +45,27 @@ def is_url(path: str) -> bool:
4545 return path .lower ().startswith (URL_PROTOCOLS )
4646
4747
48+ def _path_to_file_url (resolved_path : Path | PurePosixPath ) -> str :
49+ """
50+ Convert an already-resolved absolute path to a ``file://`` URL.
51+
52+ Uses ``as_posix()`` so the same logic handles both POSIX paths (which
53+ already start with ``/``) and Windows paths (``C:/...``, no leading
54+ slash) without OS-specific branching.
55+
56+ Parameters
57+ ----------
58+ resolved_path : Path or PurePosixPath
59+ Absolute, already-resolved path.
60+
61+ Returns
62+ -------
63+ str
64+ ``file://`` URL.
65+ """
66+ return f"file:///{ resolved_path .as_posix ().lstrip ('/' )} "
67+
68+
4869def normalize_to_url (path : str ) -> str :
4970 """
5071 Normalize a path to URL form.
@@ -72,15 +93,7 @@ def normalize_to_url(path: str) -> str:
7293 """
7394 if is_url (path ):
7495 return path
75- # Convert local path to file:// URL
76- # Ensure absolute path and proper format
77- abs_path = str (Path (path ).resolve ())
78- # Handle Windows paths (C:\...) vs Unix paths (/...)
79- if abs_path .startswith ("/" ):
80- return f"file://{ abs_path } "
81- else :
82- # Windows: file:///C:/path
83- return f"file:///{ abs_path .replace (chr (92 ), '/' )} "
96+ return _path_to_file_url (Path (path ).resolve ())
8497
8598
8699def parse_url (url : str ) -> tuple [str , str ]:
@@ -418,7 +431,7 @@ def _full_path(self, path: str | PurePosixPath) -> str:
418431 elif self .protocol == "file" :
419432 location = self .spec .get ("location" , "" )
420433 if location :
421- return str (Path (location ) / path )
434+ return (Path (location ) / path ). as_posix ( )
422435 return path
423436 else :
424437 return self ._require_adapter ().full_path (self .spec , path )
@@ -453,13 +466,7 @@ def get_url(self, path: str | PurePosixPath) -> str:
453466 full_path = self ._full_path (path )
454467
455468 if self .protocol == "file" :
456- # Ensure absolute path for file:// URL
457- abs_path = str (Path (full_path ).resolve ())
458- if abs_path .startswith ("/" ):
459- return f"file://{ abs_path } "
460- else :
461- # Windows path
462- return f"file:///{ abs_path .replace (chr (92 ), '/' )} "
469+ return _path_to_file_url (Path (full_path ).resolve ())
463470 elif self .protocol == "s3" :
464471 return f"s3://{ full_path } "
465472 elif self .protocol == "gcs" :
0 commit comments