mirror of
https://github.com/Kozea/Radicale.git
synced 2025-09-15 20:36:55 +00:00
Type hints for multifilesystem
This commit is contained in:
parent
cecb17df03
commit
698ae875ce
14 changed files with 428 additions and 246 deletions
|
@ -16,18 +16,31 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import posixpath
|
||||
from typing import Callable, ContextManager, Iterator, Optional, cast
|
||||
|
||||
from radicale import pathutils
|
||||
from radicale import pathutils, types
|
||||
from radicale.log import logger
|
||||
from radicale.storage import multifilesystem
|
||||
from radicale.storage.multifilesystem.base import StorageBase
|
||||
|
||||
|
||||
class StorageDiscoverMixin:
|
||||
@types.contextmanager
|
||||
def _null_child_context_manager(path: str,
|
||||
href: Optional[str]) -> Iterator[None]:
|
||||
yield
|
||||
|
||||
def discover(self, path, depth="0", child_context_manager=(
|
||||
lambda path, href=None: contextlib.ExitStack())):
|
||||
|
||||
class StoragePartDiscover(StorageBase):
|
||||
|
||||
def discover(
|
||||
self, path: str, depth: str = "0", child_context_manager: Optional[
|
||||
Callable[[str, Optional[str]], ContextManager[None]]] = None
|
||||
) -> Iterator[types.CollectionOrItem]:
|
||||
# assert isinstance(self, multifilesystem.Storage)
|
||||
if child_context_manager is None:
|
||||
child_context_manager = _null_child_context_manager
|
||||
# Path should already be sanitized
|
||||
sane_path = pathutils.strip_path(path)
|
||||
attributes = sane_path.split("/") if sane_path else []
|
||||
|
@ -44,6 +57,7 @@ class StorageDiscoverMixin:
|
|||
return
|
||||
|
||||
# Check if the path exists and if it leads to a collection or an item
|
||||
href: Optional[str]
|
||||
if not os.path.isdir(filesystem_path):
|
||||
if attributes and os.path.isfile(filesystem_path):
|
||||
href = attributes.pop()
|
||||
|
@ -54,10 +68,13 @@ class StorageDiscoverMixin:
|
|||
|
||||
sane_path = "/".join(attributes)
|
||||
collection = self._collection_class(
|
||||
self, pathutils.unstrip_path(sane_path, True))
|
||||
cast(multifilesystem.Storage, self),
|
||||
pathutils.unstrip_path(sane_path, True))
|
||||
|
||||
if href:
|
||||
yield collection._get(href)
|
||||
item = collection._get(href)
|
||||
if item is not None:
|
||||
yield item
|
||||
return
|
||||
|
||||
yield collection
|
||||
|
@ -67,7 +84,9 @@ class StorageDiscoverMixin:
|
|||
|
||||
for href in collection._list():
|
||||
with child_context_manager(sane_path, href):
|
||||
yield collection._get(href)
|
||||
item = collection._get(href)
|
||||
if item is not None:
|
||||
yield item
|
||||
|
||||
for entry in os.scandir(filesystem_path):
|
||||
if not entry.is_dir():
|
||||
|
@ -80,5 +99,6 @@ class StorageDiscoverMixin:
|
|||
continue
|
||||
sane_child_path = posixpath.join(sane_path, href)
|
||||
child_path = pathutils.unstrip_path(sane_child_path, True)
|
||||
with child_context_manager(sane_child_path):
|
||||
yield self._collection_class(self, child_path)
|
||||
with child_context_manager(sane_child_path, None):
|
||||
yield self._collection_class(
|
||||
cast(multifilesystem.Storage, self), child_path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue