Skip to content

Commit

Permalink
remove join_paths and add _format_path
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzaida committed Sep 28, 2021
1 parent 2409531 commit d4b6d96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
25 changes: 15 additions & 10 deletions mmcv/fileio/file_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import os
import os.path as osp
import re
from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Optional, Union
Expand Down Expand Up @@ -92,8 +93,19 @@ def _path_mapping(self, filepath: str) -> str:
filepath = filepath.replace(k, v)
return filepath

def _format_path(self, filepath: str) -> str:
"""Convert filepath to standard format of petrel oss.
Since the filepath is concatenated by `os.path.join`, in a windows
environment, the filepath will be the format of
's3://bucket_name\\image.jpg'. By invoking `_format_path`, the above
filepath will be converted to 's3://bucket_name/image.jpn'.
"""
return re.sub(r'\\+', '/', filepath)

def get(self, filepath: Union[str, Path]) -> memoryview:
filepath = self._path_mapping(str(filepath))
filepath = self._format_path(filepath)
value = self._client.Get(filepath)
value_buf = memoryview(value)
return value_buf
Expand All @@ -105,25 +117,24 @@ def get_text(self,

def put(self, obj: bytes, filepath: Union[str, Path]) -> None:
filepath = self._path_mapping(str(filepath))
filepath = self._format_path(filepath)
self._client.put(filepath, obj)

def put_text(self,
obj: str,
filepath: Union[str, Path],
encoding: str = 'utf-8') -> None:
self.put(bytes(obj, encoding=encoding), str(filepath))
self.put(bytes(obj, encoding=encoding), filepath)

def remove(self, filepath: Union[str, Path]) -> None:
filepath = self._path_mapping(str(filepath))
filepath = self._format_path(filepath)
self._client.delete(filepath)

def check_exist(self, filepath: Union[str, Path]) -> bool:
# TODO, need other team to support the feature
return True

def join_paths(self, path, *paths) -> str:
return f'{path}/{"/".join(paths)}'


class MemcachedBackend(BaseStorageBackend):
"""Memcached storage backend.
Expand Down Expand Up @@ -248,9 +259,6 @@ def remove(self, filepath: Union[str, Path]) -> None:
def check_exist(self, filepath: Union[str, Path]) -> bool:
return osp.exists(str(filepath))

def join_paths(self, path, *paths) -> str:
return osp.join(path, *paths)


class HTTPBackend(BaseStorageBackend):
"""HTTP and HTTPS storage bachend."""
Expand Down Expand Up @@ -485,6 +493,3 @@ def remove(self, filepath):

def check_exist(self, filepath):
return self.client.check_exist(filepath)

def join_paths(self, path, *paths) -> str:
return self.client.join_paths(path, *paths)
18 changes: 0 additions & 18 deletions tests/test_fileclient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import platform
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -265,23 +264,6 @@ def test_infer_client(self):
client = FileClient.infer_client(uri=uri)
assert client.backend_name == 'petrel'

def test_join_paths(self):
# HardDiskBackend
file_client_args = {'backend': 'disk'}
client = FileClient(**file_client_args)
dir1 = '/path/of/your/directory'
dir2 = 'c:\\windows\\path\\of\\your\\directory'
filename1 = 'filename'
if platform.system() == 'Windows':
assert client.join_paths(dir2, filename1) == f'{dir2}\\{filename1}'
else:
assert client.join_paths(dir1, filename1) == f'{dir1}/{filename1}'

# PetrelBackend
file_client_args = {'backend': 'petrel'}
client = FileClient(**file_client_args)
assert client.join_paths(dir1, filename1) == f'{dir1}/{filename1}'

def test_register_backend(self):

# name must be a string
Expand Down

0 comments on commit d4b6d96

Please sign in to comment.