pathlibutil 0.2.0__tar.gz → 0.2.1__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.2.0
3
+ Version: 0.2.1
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
@@ -0,0 +1,48 @@
1
+ """
2
+ Wrapper around the `json` module that provides a custom JSON encoder for `pathlib.Path`
3
+ objects. This allows `pathlib.Path` objects to be serialized to JSON without having to
4
+ convert them to strings first.
5
+
6
+ ```python
7
+ from pathlib import Path
8
+ from pathlibutil.json import dump
9
+
10
+ data = {"file": Path(__file__)}
11
+
12
+ with open("data.json", "w") as f:
13
+ dump(data, f)
14
+ ```
15
+ """
16
+
17
+ import functools
18
+ import json
19
+ import pathlib
20
+ from json import load, loads
21
+
22
+
23
+ class PathEncoder(json.JSONEncoder):
24
+ """
25
+ JSON encoder that converts `pathlib.Path` objects to their string representation.
26
+ """
27
+
28
+ def default(self, obj):
29
+ """
30
+ Convert `pathlib.Path` objects to a string using `pathlib.Path.as_posix()`.
31
+ """
32
+ if isinstance(obj, pathlib.Path):
33
+ return obj.as_posix()
34
+
35
+ return super().default(obj)
36
+
37
+
38
+ @functools.wraps(json.dump)
39
+ def dump(obj, fp, *, cls=PathEncoder, **kwargs):
40
+ return json.dump(obj, fp, cls=cls, **kwargs)
41
+
42
+
43
+ @functools.wraps(json.dumps)
44
+ def dumps(obj, *, cls=PathEncoder, **kwargs):
45
+ return json.dumps(obj, cls=cls, **kwargs)
46
+
47
+
48
+ __all__ = ["load", "loads", "dump", "dumps", "PathEncoder"]
@@ -611,7 +611,8 @@ class Path(BasePath):
611
611
  be yielded if it is an `integer` files are yielded to this max. directory depth
612
612
  optional` **kwargs` are passed to `Path.walk()`.
613
613
 
614
- When recursing, folders can be excluded by passing a callable for `exclude_dirs`, e.g.
614
+ When recursing, folders can be excluded by passing a callable for
615
+ `exclude_dirs`, e.g.
615
616
 
616
617
  ```python
617
618
  def exclude_version_control(dirpath: _Path) -> bool:
@@ -622,7 +623,7 @@ class Path(BasePath):
622
623
  if exclude_dirs and not callable(exclude_dirs):
623
624
  raise TypeError("exclude_dirs must be a callable")
624
625
 
625
- depth = recursive if type(recursive) == int else None
626
+ depth = recursive if type(recursive) is int else None
626
627
 
627
628
  for root, dirs, files in self.walk(**kwargs):
628
629
  if depth is not None and len(root.relative_to(self).parts) >= depth:
@@ -7,7 +7,7 @@ requires = [
7
7
 
8
8
  [tool.poetry]
9
9
  name = "pathlibutil"
10
- version = "v0.2.0"
10
+ version = "v0.2.1"
11
11
  description = "inherits from pathlib.Path with methods for hashing, copying, deleting and more"
12
12
  authors = [ "Christoph Dörrer <d-chris@web.de>" ]
13
13
  readme = "README.md"
@@ -1,25 +0,0 @@
1
- import functools
2
- import importlib
3
- import pathlib
4
-
5
- json = importlib.import_module("json")
6
-
7
- from json import load, loads
8
-
9
-
10
- class PathEncoder(json.JSONEncoder):
11
- def default(self, obj):
12
- if isinstance(obj, pathlib.Path):
13
- return obj.as_posix()
14
-
15
- return super().default(obj)
16
-
17
-
18
- @functools.wraps(json.dump)
19
- def dump(obj, fp, *, cls=PathEncoder, **kwargs):
20
- return json.dump(obj, fp, cls=cls, **kwargs)
21
-
22
-
23
- @functools.wraps(json.dumps)
24
- def dumps(obj, *, cls=PathEncoder, **kwargs):
25
- return json.dumps(obj, cls=cls, **kwargs)
File without changes
File without changes