Skip to content

Commit

Permalink
split os.path from os
Browse files Browse the repository at this point in the history
  • Loading branch information
marian-code committed Nov 19, 2021
1 parent 123b18b commit db69cdc
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 275 deletions.
3 changes: 2 additions & 1 deletion ssh_utilities/abstract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from ._builtins import BuiltinsABC
from ._connection import ConnectionABC
from ._os import OsABC, OsPathABC, DirEntryABC
from ._os import OsABC, DirEntryABC
from ._os_path import OsPathABC
from ._pathlib import PathlibABC
from ._shutil import ShutilABC
from ._subprocess import SubprocessABC
Expand Down
162 changes: 3 additions & 159 deletions ssh_utilities/abstract/_os.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Template module for all os classes."""
"""Template module for all os classes and methods."""

import logging
from abc import ABC, abstractmethod
Expand All @@ -8,7 +8,7 @@
from ..typeshed import _ONERROR, _SPATH
from . import _ATTRIBUTES

__all__ = ["OsPathABC", "OsABC", "DirEntryABC"]
__all__ = ["OsABC", "DirEntryABC"]

logging.getLogger(__name__)

Expand Down Expand Up @@ -117,162 +117,6 @@ def stat(self, *, follow_symlinks: bool = True) -> "_ATTRIBUTES":
raise NotImplementedError


class OsPathABC(ABC):
"""`os.path` module drop-in replacement base."""

__name__: str
__abstractmethods__: FrozenSet[str]

@abstractmethod
def isfile(self, path: "_SPATH") -> bool:
"""Check if path points to a file.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if file could not be accessed
"""
raise NotImplementedError

@abstractmethod
def isdir(self, path: "_SPATH") -> bool:
"""Check if path points to directory.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if dir could not be accessed
"""
raise NotImplementedError

@abstractmethod
def exists(self, path: "_SPATH") -> bool:
"""Check if path exists in filesystem.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
"""
raise NotImplementedError

@abstractmethod
def islink(self, path: "_SPATH") -> bool:
"""Check if path points to symbolic link.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if dir could not be accessed
"""
raise NotImplementedError

@abstractmethod
def realpath(self, path: "_SPATH") -> str:
"""Return the canonical path of the specified filename.
Eliminates any symbolic links encountered in the path.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
path to resolve
Returns
-------
str
string representation of the resolved path
"""
raise NotImplementedError

@abstractmethod
def getsize(self, path: "_SPATH") -> int:
"""Return the size of path in bytes.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
path to file/directory
Returns
-------
int
size in bytes
Raises
------
OsError
if the file does not exist or is inaccessible
"""
raise NotImplementedError

@abstractmethod
def join(self, path: "_SPATH", *paths: "_SPATH") -> str:
"""Join one or more path components intelligently.
The return value is the concatenation of path and any members of
*paths with exactly one directory separator following each non-empty
part except the last, meaning that the result will only end
in a separator if the last part is empty. If a component is
an absolute path, all previous components are thrown away and
joining continues from the absolute path component. On Windows,
the drive letter is not reset when an absolute path component
(e.g., 'foo') is encountered. If a component contains a drive letter,
all previous components are thrown away and the drive letter is reset.
Note that since there is a current directory for each drive,
os.path.join("c:", "foo") represents a path relative to the current
directory on drive C: (c:foo), not c:/foo.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
the starting path part
*paths : :const:`ssh_utilities.typeshed._SPATH`
path parts to join to the first one
Returns
-------
str
joined path parts
"""
raise NotImplementedError


class OsABC(ABC, Generic[_Os1, _Os2, _Os3, _Os4, _Os5, _Os6]):
"""`os` module drop-in replacement base."""

Expand Down Expand Up @@ -454,7 +298,7 @@ def replace(self, src: "_SPATH", dst: "_SPATH", *,
------
OsError
If dst is a directory, the operation will fail with an OSError,
Warnings
--------
If dst exists and is a file, it will be replaced silently
Expand Down
168 changes: 168 additions & 0 deletions ssh_utilities/abstract/_os_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
"""Template module for all os.path classes and methods."""

import logging
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, FrozenSet

if TYPE_CHECKING:
from ..typeshed import _SPATH

__all__ = ["OsPathABC"]

logging.getLogger(__name__)


class OsPathABC(ABC):
"""`os.path` module drop-in replacement base."""

__name__: str
__abstractmethods__: FrozenSet[str]

@abstractmethod
def isfile(self, path: "_SPATH") -> bool:
"""Check if path points to a file.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if file could not be accessed
"""
raise NotImplementedError

@abstractmethod
def isdir(self, path: "_SPATH") -> bool:
"""Check if path points to directory.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if dir could not be accessed
"""
raise NotImplementedError

@abstractmethod
def exists(self, path: "_SPATH") -> bool:
"""Check if path exists in filesystem.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
"""
raise NotImplementedError

@abstractmethod
def islink(self, path: "_SPATH") -> bool:
"""Check if path points to symbolic link.
Parameters
----------
path: :const:`ssh_utilities.typeshed._SPATH`
path to check
Returns
-------
bool
check result
Raises
------
IOError
if dir could not be accessed
"""
raise NotImplementedError

@abstractmethod
def realpath(self, path: "_SPATH") -> str:
"""Return the canonical path of the specified filename.
Eliminates any symbolic links encountered in the path.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
path to resolve
Returns
-------
str
string representation of the resolved path
"""
raise NotImplementedError

@abstractmethod
def getsize(self, path: "_SPATH") -> int:
"""Return the size of path in bytes.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
path to file/directory
Returns
-------
int
size in bytes
Raises
------
OsError
if the file does not exist or is inaccessible
"""
raise NotImplementedError

@abstractmethod
def join(self, path: "_SPATH", *paths: "_SPATH") -> str:
"""Join one or more path components intelligently.
The return value is the concatenation of path and any members of
*paths with exactly one directory separator following each non-empty
part except the last, meaning that the result will only end
in a separator if the last part is empty. If a component is
an absolute path, all previous components are thrown away and
joining continues from the absolute path component. On Windows,
the drive letter is not reset when an absolute path component
(e.g., 'foo') is encountered. If a component contains a drive letter,
all previous components are thrown away and the drive letter is reset.
Note that since there is a current directory for each drive,
os.path.join("c:", "foo") represents a path relative to the current
directory on drive C: (c:foo), not c:/foo.
Parameters
----------
path : :const:`ssh_utilities.typeshed._SPATH`
the starting path part
*paths : :const:`ssh_utilities.typeshed._SPATH`
path parts to join to the first one
Returns
-------
str
joined path parts
"""
raise NotImplementedError
3 changes: 2 additions & 1 deletion ssh_utilities/local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

# ! preserve this order otherwise import fail
from ._os import Os
from ._os_path import OsPath
from ._builtins import Builtins
from ._pathlib import Pathlib
from ._shutil import Shutil
from ._subprocess import Subprocess
from .local import LocalConnection

__all__ = ["LocalConnection", "Builtins", "Os", "Pathlib", "Shutil",
"Subprocess"]
"Subprocess", "OsPath"]
Loading

0 comments on commit db69cdc

Please sign in to comment.