Skip to content

Commit e03177a

Browse files
authored
Add get_text, get_bytes (#12)
* Add methods get_text and get_bytes - Remove path resolving in method open * Update version to 0.2.8
1 parent 0e5e8e8 commit e03177a

File tree

4 files changed

+202
-105
lines changed

4 files changed

+202
-105
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.2.8] - 2025-10-16
9+
10+
Release with minor features.
11+
12+
### Added
13+
14+
- Added [`get_text`](https://uhd-urz.github.io/properpath/apis/properpath/#properpath.ProperPath.get_text) and
15+
[`get_bytes`](https://uhd-urz.github.io/properpath/apis/properpath/#properpath.ProperPath.get_bytes) methods
16+
17+
### Changed
18+
19+
- Avoid resolving the path in `open` method
20+
21+
## [0.2.7] - 2025-10-09
22+
23+
This release adds Pydantic support and Rich pretty REPL printing support to ProperPath. 2 additional optional
24+
dependencies have been added: `properpath[pydantic]`,` properpath[rich]`.
25+
826
## [0.2.6] - 2025-09-29
927

1028
Release with minor improvements.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "properpath"
3-
version = "0.2.7"
3+
version = "0.2.8"
44
authors = [{ name = "Alexander Haller, Mahadi Xion", email = "[email protected]" }]
55
maintainers = [{ name = "Mahadi Xion", email = "[email protected]" }]
66
readme = "README.md"

src/properpath/properpath.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def open(self, mode="r", encoding=None, *args, **kwargs):
609609
ProperPath open first resolves the whole path and then simply returns pathlib.Path.open.
610610
This method is mainly overloaded to log exceptions.
611611
"""
612-
file = super().resolve()
612+
file = self._expanded
613613
try:
614614
return Path(file).open(
615615
mode=mode,
@@ -632,5 +632,65 @@ def open(self, mode="r", encoding=None, *args, **kwargs):
632632
self.PathException = os_exception
633633
raise e
634634

635+
# noinspection PyIncorrectDocstring
636+
def get_text(
637+
self,
638+
encoding: Optional[str] = None,
639+
errors: Optional[str] = None,
640+
newline: Optional[str] = None,
641+
default: Optional[str] = None,
642+
) -> Optional[str]:
643+
"""
644+
`get_text` method is basically [`read_text`](https://docs.python.org/3.13/library/pathlib.html#pathlib.Path.read_text)
645+
with support for extra `defaults` parameter. `get_text` passes optional arguments `encoding`, `errors`, `newline` to
646+
`read_text`.
647+
648+
Args:
649+
encoding: The same meaning as the `encoding` argument for method
650+
[`open`](https://docs.python.org/3.13/library/functions.html#open).
651+
errors: The same meaning as the `encoding` argument for method
652+
[`open`](https://docs.python.org/3.13/library/functions.html#open).
653+
newline: The same meaning as the `encoding` argument for method
654+
[`open`](https://docs.python.org/3.13/library/functions.html#open).
655+
`newline` is only supported in Python 3.13 and above.
656+
default:
657+
If the file does not exist, then `default` is returned. By default, `default` is `None`.
658+
659+
Example:
660+
```python
661+
import json
662+
from properpath import P
663+
664+
cache = json.loads(P("~/.cache/app/cache.json").get_text(encoding="utf-8", default='{}'))
665+
```
666+
667+
Returns:
668+
The decoded contents of the pointed-to file as a string or default (when file does not exist).
669+
"""
670+
try:
671+
if sys.version_info.minor >= 13:
672+
return super().read_text(encoding, errors, newline)
673+
return super().read_text(encoding, errors)
674+
except FileNotFoundError:
675+
return default
676+
677+
# noinspection PyIncorrectDocstring
678+
def get_bytes(self, default: Optional[bytes] = None) -> Optional[bytes]:
679+
"""
680+
`get_bytes` method is basically [`read_bytes`](https://docs.python.org/3.13/library/pathlib.html#pathlib.Path.read_bytes)
681+
with support for extra `defaults` parameter.
682+
683+
Args:
684+
default:
685+
If the file does not exist, then `default` is returned. By default, `default` is `None`.
686+
687+
Returns:
688+
The binary contents of the pointed-to file as a bytes object or default (when file does not exist).
689+
"""
690+
try:
691+
return super().read_bytes()
692+
except FileNotFoundError:
693+
return default
694+
635695

636696
P = ProperPath

0 commit comments

Comments
 (0)