aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2022-12-31 23:06:57 +0200
committerArthur Zamarin <arthurzam@gentoo.org>2022-12-31 23:06:57 +0200
commit0a9a567ec6672e2ad541dfbf5ad6f6484bcbcf77 (patch)
tree409bb788649df9b33a2f661636d1f3cc9720669f
parentprocess.spawn: add type annotations (diff)
downloadsnakeoil-0a9a567ec6672e2ad541dfbf5ad6f6484bcbcf77.tar.gz
snakeoil-0a9a567ec6672e2ad541dfbf5ad6f6484bcbcf77.tar.bz2
snakeoil-0a9a567ec6672e2ad541dfbf5ad6f6484bcbcf77.zip
fileutils: small cleanup and modernization
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r--src/snakeoil/_fileutils.py15
-rw-r--r--src/snakeoil/fileutils.py14
-rw-r--r--tests/test_fileutils.py7
-rw-r--r--tests/test_osutils.py4
4 files changed, 6 insertions, 34 deletions
diff --git a/src/snakeoil/_fileutils.py b/src/snakeoil/_fileutils.py
index 4a22648..cd0bb64 100644
--- a/src/snakeoil/_fileutils.py
+++ b/src/snakeoil/_fileutils.py
@@ -64,10 +64,6 @@ class readlines_iter:
return self.iterable
-def _native_readlines_shim(*args, **kwds):
- return native_readlines("r", *args, **kwds)
-
-
def native_readlines(
mode,
mypath,
@@ -107,17 +103,6 @@ def _strip_whitespace_filter(iterable):
yield line.strip()
-def _py2k_ascii_strict_filter(source):
- for line in source:
- if any((0x80 & ord(char)) for char in line):
- raise ValueError("character ordinal over 127")
- yield line
-
-
-def _native_readfile_shim(*args, **kwds):
- return native_readfile("r", *args, **kwds)
-
-
def native_readfile(mode, mypath, none_on_missing=False, encoding=None):
"""Read a file, returning the contents.
diff --git a/src/snakeoil/fileutils.py b/src/snakeoil/fileutils.py
index b0aa724..743ef2d 100644
--- a/src/snakeoil/fileutils.py
+++ b/src/snakeoil/fileutils.py
@@ -12,21 +12,17 @@ from .currying import pretty_docs
from .klass import GetAttrProxy
-def touch(fname, mode=0o644, **kwargs):
+def touch(fname: str, mode: int = 0o644, dir_fd=None, **kwargs):
"""touch(1) equivalent
:param fname: file path
- :type fname: str
:param mode: file mode
- :type mode: octal
See os.utime for other supported arguments.
"""
flags = os.O_CREAT | os.O_APPEND
- dir_fd = kwargs.get("dir_fd", None)
- os_open = partial(os.open, dir_fd=dir_fd)
- with os.fdopen(os_open(fname, flags, mode)) as f:
+ with os.fdopen(os.open(fname, flags, mode, dir_fd=dir_fd)) as f:
os.utime(
f.fileno() if os.utime in os.supports_fd else fname,
dir_fd=None if os.supports_fd else dir_fd,
@@ -34,7 +30,7 @@ def touch(fname, mode=0o644, **kwargs):
)
-def mmap_or_open_for_read(path):
+def mmap_or_open_for_read(path: str):
size = os.stat(path).st_size
if size == 0:
return (None, data_source.bytes_ro_StringIO(b""))
@@ -153,10 +149,10 @@ class AtomicWriteFile(AtomicWriteFile_mixin):
__getattr__ = GetAttrProxy("raw")
-def _mk_pretty_derived_func(func, name_base, name, *args, **kwds):
+def _mk_pretty_derived_func(func, name_base: str, name: str, *args, **kwds):
if name:
name = "_" + name
- return pretty_docs(partial(func, *args, **kwds), name="%s%s" % (name_base, name))
+ return pretty_docs(partial(func, *args, **kwds), name=name_base + name)
_mk_readfile = partial(_mk_pretty_derived_func, _fileutils.native_readfile, "readfile")
diff --git a/tests/test_fileutils.py b/tests/test_fileutils.py
index 356eb74..de8a1f7 100644
--- a/tests/test_fileutils.py
+++ b/tests/test_fileutils.py
@@ -122,13 +122,6 @@ class TestAtomicWriteFile:
af.close()
-def cpy_setup_class(scope, func_name):
- if getattr(fileutils, "native_%s" % func_name) is getattr(fileutils, func_name):
- scope["skip"] = "extensions disabled"
- else:
- scope["func"] = staticmethod(getattr(fileutils, func_name))
-
-
class Test_readfile:
func = staticmethod(fileutils.readfile)
diff --git a/tests/test_osutils.py b/tests/test_osutils.py
index fac9853..a60bb15 100644
--- a/tests/test_osutils.py
+++ b/tests/test_osutils.py
@@ -10,7 +10,6 @@ from unittest import mock
import pytest
from snakeoil import osutils
from snakeoil.contexts import Namespace
-from snakeoil.fileutils import touch
from snakeoil.osutils import native_readdir, supported_systems, sizeof_fmt
from snakeoil.osutils.mount import MNT_DETACH, MS_BIND, mount, umount
@@ -114,8 +113,7 @@ class TestEnsureDirs:
def test_path_is_a_file(self, tmp_path):
# fail if passed a path to an existing file
- path = tmp_path / "file"
- touch(path)
+ (path := tmp_path / "file").touch()
assert path.is_file()
assert not osutils.ensure_dirs(path, mode=0o700)