-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete rework for Path using decorators and mutable contexts
- Loading branch information
Showing
8 changed files
with
677 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import functools | ||
import inspect | ||
|
||
import path | ||
|
||
import mutapath | ||
|
||
__EXCLUDE_FROM_WRAPPING = ["__dir__", "__eq__", "__format__", "__repr__", "__str__", "__sizeof__", "__init__", | ||
"__post_init__", "__getattribute__", "__delattr__", "__setattr__", "__getattr__", | ||
"__fspath__", "_norm"] | ||
|
||
__MUTABLE_FUNCTIONS = {"rename", "renames", "copy", "copy2", "copyfile", "copymode", "copystat", "copytree", "move", | ||
"basename", "abspath", "join", "joinpath", "normpath", "relpath", "realpath", "relpathto"} | ||
|
||
|
||
def __is_def(member): | ||
while isinstance(member, functools.partial): | ||
member = member.func | ||
if inspect.isbuiltin(member): | ||
return False | ||
return inspect.isroutine(member) | ||
|
||
|
||
def __path_func(orig_func): | ||
def wrap_decorator(*args, **kwargs): | ||
result = orig_func(*args, **kwargs) | ||
if isinstance(result, path.Path): | ||
return mutapath.Path(result) | ||
else: | ||
return result | ||
|
||
return wrap_decorator | ||
|
||
|
||
def path_wrap(cls): | ||
for name, method in inspect.getmembers(path.Path, __is_def): | ||
if not name.startswith("__"): | ||
setattr(path.Path, name, __path_func(method)) | ||
for name, method in inspect.getmembers(cls, __is_def): | ||
if name not in __EXCLUDE_FROM_WRAPPING: | ||
setattr(cls, name, __path_func(method)) | ||
return cls | ||
|
||
|
||
def __mutate_func(cls, method_name): | ||
from mutapath import Path | ||
|
||
def mutation_decorator(self, *args, **kwargs): | ||
orig_func = getattr(path.Path, method_name) | ||
if isinstance(self, Path): | ||
result = orig_func(self._contained, *args, **kwargs) | ||
if isinstance(result, mutapath.Path): | ||
self._contained = result._contained | ||
return self | ||
else: | ||
return result | ||
else: | ||
result = orig_func(self, *args, **kwargs) | ||
return cls(result) | ||
|
||
return mutation_decorator | ||
|
||
|
||
def path_mutable(cls): | ||
names, _ = zip(*inspect.getmembers(path.Path, __is_def)) | ||
names = __MUTABLE_FUNCTIONS.intersection(names) | ||
for method_name in names: | ||
setattr(cls, method_name, __mutate_func(cls, method_name)) | ||
return cls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class PathException(BaseException): | ||
""" | ||
Exception about inconsistencies between the virtual path and the real file system. | ||
""" | ||
pass |
Oops, something went wrong.