[Pytest-commit] commit/py: pmoore: Add {read, write}_{binary, text} methods to LocalPath instances
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Wed Sep 3 15:15:24 CEST 2014
1 new commit in py:
https://bitbucket.org/hpk42/py/commits/764221efcef5/
Changeset: 764221efcef5
User: pmoore
Date: 2014-09-02 15:53:51
Summary: Add {read,write}_{binary,text} methods to LocalPath instances
Affected #: 3 files
diff -r 4927b441e6e93ffa70404cf836d80ccafbb215a7 -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc py/_path/common.py
--- a/py/_path/common.py
+++ b/py/_path/common.py
@@ -115,6 +115,22 @@
"""
return self.new(basename='').join(*args, **kwargs)
+ def read_binary(self):
+ """ read and return a bytestring from reading the path. """
+ f = self.open('rb')
+ try:
+ return f.read()
+ finally:
+ f.close()
+
+ def read_text(self, encoding=None):
+ """ read and return a Unicode string from reading the path. """
+ f = self.open('r', encoding=encoding)
+ try:
+ return f.read()
+ finally:
+ f.close()
+
def read(self, mode='r'):
""" read and return a bytestring from reading the path. """
if sys.version_info < (2,3):
diff -r 4927b441e6e93ffa70404cf836d80ccafbb215a7 -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc py/_path/local.py
--- a/py/_path/local.py
+++ b/py/_path/local.py
@@ -2,7 +2,7 @@
local path implementation.
"""
from contextlib import contextmanager
-import sys, os, re, atexit
+import sys, os, re, atexit, io
import py
from py._path import common
from stat import S_ISLNK, S_ISDIR, S_ISREG
@@ -330,13 +330,15 @@
obj.strpath = normpath(strpath)
return obj
- def open(self, mode='r', ensure=False):
+ def open(self, mode='r', ensure=False, encoding=None):
""" return an opened file with the given mode.
If ensure is True, create parent directories if needed.
"""
if ensure:
self.dirpath().ensure(dir=1)
+ if encoding:
+ return py.error.checked_call(io.open, self.strpath, mode, encoding=encoding)
return py.error.checked_call(open, self.strpath, mode)
def _fastjoin(self, name):
@@ -434,6 +436,30 @@
py.error.checked_call(os.mkdir, getattr(p, "strpath", p))
return p
+ def write_binary(self, data, ensure=False):
+ """ write binary data into path. If ensure is True create
+ missing parent directories.
+ """
+ if ensure:
+ self.dirpath().ensure(dir=1)
+ f = self.open('wb')
+ try:
+ f.write(data)
+ finally:
+ f.close()
+
+ def write_text(self, data, ensure=False, encoding=None):
+ """ write binary data into path. If ensure is True create
+ missing parent directories.
+ """
+ if ensure:
+ self.dirpath().ensure(dir=1)
+ f = self.open('w', encoding=encoding)
+ try:
+ f.write(data)
+ finally:
+ f.close()
+
def write(self, data, mode='w', ensure=False):
""" write data into path. If ensure is True create
missing parent directories.
diff -r 4927b441e6e93ffa70404cf836d80ccafbb215a7 -r 764221efcef5bbc004e0e7f68ee5230dfa7168dc testing/path/test_local.py
--- a/testing/path/test_local.py
+++ b/testing/path/test_local.py
@@ -801,3 +801,24 @@
x.write(part.encode(sys.getdefaultencoding()))
assert x.read() == part.encode(sys.getdefaultencoding())
+class TestBinaryAndTextMethods:
+ def test_read_binwrite(self, tmpdir):
+ x = tmpdir.join("hello")
+ part = py.builtin._totext("hällo", "utf8")
+ part_utf8 = part.encode("utf8")
+ x.write_binary(part_utf8)
+ assert x.read_binary() == part_utf8
+ assert x.read_text(encoding="utf8") == part
+ def test_read_textwrite(self, tmpdir):
+ x = tmpdir.join("hello")
+ part = py.builtin._totext("hällo", "utf8")
+ part_utf8 = part.encode("utf8")
+ x.write_text(part, encoding="utf8")
+ assert x.read_binary() == part_utf8
+ assert x.read_text(encoding="utf8") == part
+ def test_default_encoding(self, tmpdir):
+ x = tmpdir.join("hello")
+ # Can't use UTF8 as the default encoding (ASCII) doesn't support it
+ part = py.builtin._totext("hello", "ascii")
+ x.write_text(part)
+ assert x.read_text() == part
Repository URL: https://bitbucket.org/hpk42/py/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the pytest-commit
mailing list