A general file client to access files in different backend. The client loads a file or text in a specified backend from its path and return it as a binary file. it can also register other backend accessor with a given name and backend class. Attributes: backend (str): The s
| 130 | |
| 131 | |
| 132 | class FileClient(object): |
| 133 | """A general file client to access files in different backend. |
| 134 | |
| 135 | The client loads a file or text in a specified backend from its path |
| 136 | and return it as a binary file. it can also register other backend |
| 137 | accessor with a given name and backend class. |
| 138 | |
| 139 | Attributes: |
| 140 | backend (str): The storage backend type. Options are "disk", |
| 141 | "memcached" and "lmdb". |
| 142 | client (:obj:`BaseStorageBackend`): The backend object. |
| 143 | """ |
| 144 | |
| 145 | _backends = { |
| 146 | 'disk': HardDiskBackend, |
| 147 | 'memcached': MemcachedBackend, |
| 148 | 'lmdb': LmdbBackend, |
| 149 | } |
| 150 | |
| 151 | def __init__(self, backend='disk', **kwargs): |
| 152 | if backend not in self._backends: |
| 153 | raise ValueError(f'Backend {backend} is not supported. Currently supported ones' |
| 154 | f' are {list(self._backends.keys())}') |
| 155 | self.backend = backend |
| 156 | self.client = self._backends[backend](**kwargs) |
| 157 | |
| 158 | def get(self, filepath, client_key='default'): |
| 159 | # client_key is used only for lmdb, where different fileclients have |
| 160 | # different lmdb environments. |
| 161 | if self.backend == 'lmdb': |
| 162 | return self.client.get(filepath, client_key) |
| 163 | else: |
| 164 | return self.client.get(filepath) |
| 165 | |
| 166 | def get_text(self, filepath): |
| 167 | return self.client.get_text(filepath) |