pathlibutil 0.1.7__tar.gz → 0.1.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pathlibutil
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: inherits from pathlib.Path with methods for hashing, copying, deleting and more
5
5
  Home-page: https://d-chris.github.io
6
6
  License: MIT
@@ -51,6 +51,7 @@ Description-Content-Type: text/markdown
51
51
  - `Path.stat()` returns a `StatResult` object to get file or directory information containing
52
52
  - `TimeInt` objects for `atime`, `ctime`, `mtime` and `birthtime`
53
53
  - `ByteInt` object for `size`
54
+ - `Path.relative_to()` to get relative path from a file or directory, `walk_up` to walk up the directory tree.
54
55
 
55
56
  ## Installation
56
57
 
@@ -27,6 +27,7 @@
27
27
  - `Path.stat()` returns a `StatResult` object to get file or directory information containing
28
28
  - `TimeInt` objects for `atime`, `ctime`, `mtime` and `birthtime`
29
29
  - `ByteInt` object for `size`
30
+ - `Path.relative_to()` to get relative path from a file or directory, `walk_up` to walk up the directory tree.
30
31
 
31
32
  ## Installation
32
33
 
@@ -401,15 +401,15 @@ class Path(BasePath):
401
401
  Return a new `Path` with changed suffix or remove it when its an empty
402
402
  string.
403
403
 
404
+ Multiple suffixes can be changed at once by passing a list of suffixes.
405
+ With a empty list all suffixes will be removed.
406
+
404
407
  >>> Path('test.a.b').with_suffix('.c')
405
408
  Path('test.a.c')
406
409
 
407
410
  >>> Path('test.a.b').with_suffix('')
408
411
  Path('test.a')
409
412
 
410
- Multiple suffixes can be changed at once by passing a list of suffixes.
411
- With a empty list all suffixes will be removed.
412
-
413
413
  >>> Path('test.a.b').with_suffix(['.c', '.d'])
414
414
  Path('test.c.d')
415
415
 
@@ -432,7 +432,48 @@ class Path(BasePath):
432
432
  end = -1 * len(self.suffixes) or None
433
433
  name = self.name.split(".")[0:end]
434
434
  stem = self.parent.joinpath("".join(name))
435
- return super(Path, stem).with_suffix(suffix)
435
+ return super(self.__class__, stem).with_suffix(suffix)
436
+
437
+ def relative_to(
438
+ self, *other: Union[str, _Path], walk_up: Union[bool, int] = False
439
+ ) -> _Path:
440
+ """
441
+ Return the relative path to another path identified by the passed
442
+ arguments. If the operation is not possible (because this is not
443
+ related to the other path), raise `ValueError`.
444
+
445
+ The `walk_up` parameter controls whether `..` may be used to resolve
446
+ the path.
447
+
448
+ If `walk_up` is a integer it specifies the maximum number of `..` to resolve, if
449
+ max is reached a `ValueError` is raised.
450
+
451
+ >>> Path('a/b/c/d').relative_to('a/b')
452
+ Path('c/d')
453
+
454
+ >>> Path('a/b/c/d').relative_to('a/b/e/f/g', walk_up=True)
455
+ Path('../../../c/d')
456
+
457
+ >>> Path('a/b/c/d').relative_to('a/b/e/f/g', walk_up=2)
458
+ ValueError: '../../../c/d' is outside of the relative deepth of '2'
459
+ """
460
+ if not walk_up:
461
+ return super().relative_to(*other)
462
+
463
+ try:
464
+ relative = super().relative_to(*other, walk_up=walk_up)
465
+ except TypeError:
466
+ relative = self.__class__(os.path.relpath(self, Path(*other)))
467
+
468
+ if type(walk_up) is not int:
469
+ return relative
470
+
471
+ if relative.parts.count("..") > walk_up:
472
+ raise ValueError(
473
+ f"'{relative}' is outside of the relative deepth of '{walk_up}'"
474
+ )
475
+
476
+ return relative
436
477
 
437
478
 
438
479
  class Register7zFormat(Path, archive="7z"):
@@ -2,7 +2,7 @@ import functools
2
2
  import os
3
3
  import re
4
4
  from datetime import datetime, tzinfo
5
- from typing import Set, Tuple, TypeVar
5
+ from typing import Set, Tuple, TypeVar, Iterable
6
6
 
7
7
  _ByteInt = TypeVar("_ByteInt", bound="ByteInt")
8
8
  _stat_result = TypeVar("_stat_result", bound="os.stat_result")
@@ -332,3 +332,22 @@ class StatResult:
332
332
  Return representation of `os.stat_result` object.
333
333
  """
334
334
  return repr(self._obj)
335
+
336
+ def __dir__(self) -> Iterable[str]:
337
+ """
338
+ Return a list of attributes of `os.stat_result` object.
339
+ """
340
+ return dir(self._obj)
341
+
342
+ def __len__(self) -> int:
343
+ """
344
+ Return length of `os.stat_result` object.
345
+ """
346
+ return len(self._obj)
347
+
348
+ @property
349
+ def stat_result(self) -> os.stat_result:
350
+ """
351
+ Return the wrapped `os.stat_result` object.
352
+ """
353
+ return self._obj
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pathlibutil"
3
- version = "v0.1.7"
3
+ version = "v0.1.8"
4
4
  description = "inherits from pathlib.Path with methods for hashing, copying, deleting and more"
5
5
  authors = ["Christoph Dörrer <d-chris@web.de>"]
6
6
  readme = "README.md"
File without changes