Skip to content

Commit a965fc7

Browse files
committed
Monkey-patch os.path to support gs:// URIs
This monkey-patches the private `os.path._get_sep` function used by `os.path.join` to handle paths that start with "gs://". If a path starts with "gs://", we ignore the OS separator and return "/" as the separator to use.
1 parent 319c4a4 commit a965fc7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

learned_optimization/py_utils.py

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
"""Common python utilities."""
17+
import os
1718
from concurrent import futures
1819
from typing import Any, Callable, Sequence
1920
import tqdm
@@ -26,3 +27,24 @@ def threaded_tqdm_map(threads: int, func: Callable[[Any], Any],
2627
for l in tqdm.tqdm(data):
2728
future_list.append(executor.submit(func, l))
2829
return [x.result() for x in tqdm.tqdm(future_list)]
30+
31+
32+
old_get_sep = os.path._get_sep
33+
34+
def new_get_sep(path):
35+
"""Return the OS separator for the given path.
36+
37+
If `path` starts with "gs://", "/" is used as the separator.
38+
"""
39+
if isinstance(path, bytes):
40+
gs_prefix = b'gs://'
41+
sep = b'/'
42+
else:
43+
gs_prefix = 'gs://'
44+
sep = '/'
45+
46+
if not path.startswith(gs_prefix):
47+
sep = old_get_sep(path)
48+
return sep
49+
50+
os.path._get_sep = new_get_sep

0 commit comments

Comments
 (0)