From 84ac0705525813abbe4c9bbdf188e971b4a12c67 Mon Sep 17 00:00:00 2001 From: Matthew Price Date: Thu, 31 Aug 2023 09:29:24 +0100 Subject: [PATCH 1/5] Update write_text to include newline variable --- cloudpathlib/cloudpath.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 401f9775..11a1bfdb 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -2,6 +2,7 @@ from collections import defaultdict import collections.abc from contextlib import contextmanager +import io import os from pathlib import ( # type: ignore Path, @@ -626,15 +627,20 @@ def write_text( data: str, encoding: Optional[str] = None, errors: Optional[str] = None, + newline: Optional[str] = None, ) -> int: """Open the file in text mode, write to it, and close the file. NOTE: vendored from pathlib since we override open https://github.com/python/cpython/blob/3.8/Lib/pathlib.py#L1244-L1252 + https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L1146-L1155 """ if not isinstance(data, str): raise TypeError("data must be str, not %s" % data.__class__.__name__) - with self.open(mode="w", encoding=encoding, errors=errors) as f: + + encoding = io.text_encoding(encoding) + + with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f: return f.write(data) def read_bytes(self) -> bytes: From 15d222cabc0ded66ba968268299f28b6b06423c3 Mon Sep 17 00:00:00 2001 From: Matthew Price Date: Thu, 31 Aug 2023 09:31:55 +0100 Subject: [PATCH 2/5] Remove old docstring link --- cloudpathlib/cloudpath.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 11a1bfdb..de475c7c 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -632,7 +632,6 @@ def write_text( """Open the file in text mode, write to it, and close the file. NOTE: vendored from pathlib since we override open - https://github.com/python/cpython/blob/3.8/Lib/pathlib.py#L1244-L1252 https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L1146-L1155 """ if not isinstance(data, str): From 947ad2eaf9e8ed689a29da4d03212bb744a46f9f Mon Sep 17 00:00:00 2001 From: Matthew Price Date: Fri, 1 Sep 2023 13:51:15 +0100 Subject: [PATCH 3/5] Update cloudpathlib/cloudpath.py with black correction --- cloudpathlib/cloudpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index de475c7c..72348bc2 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -639,7 +639,7 @@ def write_text( encoding = io.text_encoding(encoding) - with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f: + with self.open(mode="w", encoding=encoding, errors=errors, newline=newline) as f: return f.write(data) def read_bytes(self) -> bytes: From b93d8d116b7f0986d48257211fd3e27aae407ce9 Mon Sep 17 00:00:00 2001 From: Matthew Price Date: Wed, 13 Sep 2023 13:57:55 +0100 Subject: [PATCH 4/5] Remove use of io module --- cloudpathlib/cloudpath.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 72348bc2..b2241b61 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -2,7 +2,6 @@ from collections import defaultdict import collections.abc from contextlib import contextmanager -import io import os from pathlib import ( # type: ignore Path, @@ -637,8 +636,6 @@ def write_text( if not isinstance(data, str): raise TypeError("data must be str, not %s" % data.__class__.__name__) - encoding = io.text_encoding(encoding) - with self.open(mode="w", encoding=encoding, errors=errors, newline=newline) as f: return f.write(data) From e2d8f4640f749e62383dda174b6372c0dd96fde6 Mon Sep 17 00:00:00 2001 From: Matthew Price Date: Thu, 14 Sep 2023 12:51:00 +0100 Subject: [PATCH 5/5] update HISTORY.md --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 9ec72101..25aee0e6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,7 @@ ## UNRELEASED - Add "CloudPath" as return type on `__init__` for mypy issues. ([Issue #179](https://github.com/drivendataorg/cloudpathlib/issues/179), [PR #342](https://github.com/drivendataorg/cloudpathlib/pull/342)) - Add `with_stem` to all path types when python version supports it (>=3.9). ([Issue #287](https://github.com/drivendataorg/cloudpathlib/issues/287), [PR #290](https://github.com/drivendataorg/cloudpathlib/pull/290), thanks to [@Gilthans](https://github.com/Gilthans)) + - Add `newline` parameter to the `write_text` method to align to `pathlib` functionality as of Python 3.10. [PR #362]https://github.com/drivendataorg/cloudpathlib/pull/362), thanks to [@pricemg](https://github.com/pricemg). ## v0.15.1 (2023-07-12)