1 |
commit: 0a9a567ec6672e2ad541dfbf5ad6f6484bcbcf77 |
2 |
Author: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Dec 31 21:06:57 2022 +0000 |
4 |
Commit: Arthur Zamarin <arthurzam <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 31 21:06:57 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=0a9a567e |
7 |
|
8 |
fileutils: small cleanup and modernization |
9 |
|
10 |
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org> |
11 |
|
12 |
src/snakeoil/_fileutils.py | 15 --------------- |
13 |
src/snakeoil/fileutils.py | 14 +++++--------- |
14 |
tests/test_fileutils.py | 7 ------- |
15 |
tests/test_osutils.py | 4 +--- |
16 |
4 files changed, 6 insertions(+), 34 deletions(-) |
17 |
|
18 |
diff --git a/src/snakeoil/_fileutils.py b/src/snakeoil/_fileutils.py |
19 |
index 4a226486..cd0bb645 100644 |
20 |
--- a/src/snakeoil/_fileutils.py |
21 |
+++ b/src/snakeoil/_fileutils.py |
22 |
@@ -64,10 +64,6 @@ class readlines_iter: |
23 |
return self.iterable |
24 |
|
25 |
|
26 |
-def _native_readlines_shim(*args, **kwds): |
27 |
- return native_readlines("r", *args, **kwds) |
28 |
- |
29 |
- |
30 |
def native_readlines( |
31 |
mode, |
32 |
mypath, |
33 |
@@ -107,17 +103,6 @@ def _strip_whitespace_filter(iterable): |
34 |
yield line.strip() |
35 |
|
36 |
|
37 |
-def _py2k_ascii_strict_filter(source): |
38 |
- for line in source: |
39 |
- if any((0x80 & ord(char)) for char in line): |
40 |
- raise ValueError("character ordinal over 127") |
41 |
- yield line |
42 |
- |
43 |
- |
44 |
-def _native_readfile_shim(*args, **kwds): |
45 |
- return native_readfile("r", *args, **kwds) |
46 |
- |
47 |
- |
48 |
def native_readfile(mode, mypath, none_on_missing=False, encoding=None): |
49 |
"""Read a file, returning the contents. |
50 |
|
51 |
|
52 |
diff --git a/src/snakeoil/fileutils.py b/src/snakeoil/fileutils.py |
53 |
index b0aa7247..743ef2d1 100644 |
54 |
--- a/src/snakeoil/fileutils.py |
55 |
+++ b/src/snakeoil/fileutils.py |
56 |
@@ -12,21 +12,17 @@ from .currying import pretty_docs |
57 |
from .klass import GetAttrProxy |
58 |
|
59 |
|
60 |
-def touch(fname, mode=0o644, **kwargs): |
61 |
+def touch(fname: str, mode: int = 0o644, dir_fd=None, **kwargs): |
62 |
"""touch(1) equivalent |
63 |
|
64 |
:param fname: file path |
65 |
- :type fname: str |
66 |
:param mode: file mode |
67 |
- :type mode: octal |
68 |
|
69 |
See os.utime for other supported arguments. |
70 |
""" |
71 |
flags = os.O_CREAT | os.O_APPEND |
72 |
- dir_fd = kwargs.get("dir_fd", None) |
73 |
- os_open = partial(os.open, dir_fd=dir_fd) |
74 |
|
75 |
- with os.fdopen(os_open(fname, flags, mode)) as f: |
76 |
+ with os.fdopen(os.open(fname, flags, mode, dir_fd=dir_fd)) as f: |
77 |
os.utime( |
78 |
f.fileno() if os.utime in os.supports_fd else fname, |
79 |
dir_fd=None if os.supports_fd else dir_fd, |
80 |
@@ -34,7 +30,7 @@ def touch(fname, mode=0o644, **kwargs): |
81 |
) |
82 |
|
83 |
|
84 |
-def mmap_or_open_for_read(path): |
85 |
+def mmap_or_open_for_read(path: str): |
86 |
size = os.stat(path).st_size |
87 |
if size == 0: |
88 |
return (None, data_source.bytes_ro_StringIO(b"")) |
89 |
@@ -153,10 +149,10 @@ class AtomicWriteFile(AtomicWriteFile_mixin): |
90 |
__getattr__ = GetAttrProxy("raw") |
91 |
|
92 |
|
93 |
-def _mk_pretty_derived_func(func, name_base, name, *args, **kwds): |
94 |
+def _mk_pretty_derived_func(func, name_base: str, name: str, *args, **kwds): |
95 |
if name: |
96 |
name = "_" + name |
97 |
- return pretty_docs(partial(func, *args, **kwds), name="%s%s" % (name_base, name)) |
98 |
+ return pretty_docs(partial(func, *args, **kwds), name=name_base + name) |
99 |
|
100 |
|
101 |
_mk_readfile = partial(_mk_pretty_derived_func, _fileutils.native_readfile, "readfile") |
102 |
|
103 |
diff --git a/tests/test_fileutils.py b/tests/test_fileutils.py |
104 |
index 356eb74d..de8a1f7d 100644 |
105 |
--- a/tests/test_fileutils.py |
106 |
+++ b/tests/test_fileutils.py |
107 |
@@ -122,13 +122,6 @@ class TestAtomicWriteFile: |
108 |
af.close() |
109 |
|
110 |
|
111 |
-def cpy_setup_class(scope, func_name): |
112 |
- if getattr(fileutils, "native_%s" % func_name) is getattr(fileutils, func_name): |
113 |
- scope["skip"] = "extensions disabled" |
114 |
- else: |
115 |
- scope["func"] = staticmethod(getattr(fileutils, func_name)) |
116 |
- |
117 |
- |
118 |
class Test_readfile: |
119 |
func = staticmethod(fileutils.readfile) |
120 |
|
121 |
|
122 |
diff --git a/tests/test_osutils.py b/tests/test_osutils.py |
123 |
index fac98532..a60bb15f 100644 |
124 |
--- a/tests/test_osutils.py |
125 |
+++ b/tests/test_osutils.py |
126 |
@@ -10,7 +10,6 @@ from unittest import mock |
127 |
import pytest |
128 |
from snakeoil import osutils |
129 |
from snakeoil.contexts import Namespace |
130 |
-from snakeoil.fileutils import touch |
131 |
from snakeoil.osutils import native_readdir, supported_systems, sizeof_fmt |
132 |
from snakeoil.osutils.mount import MNT_DETACH, MS_BIND, mount, umount |
133 |
|
134 |
@@ -114,8 +113,7 @@ class TestEnsureDirs: |
135 |
|
136 |
def test_path_is_a_file(self, tmp_path): |
137 |
# fail if passed a path to an existing file |
138 |
- path = tmp_path / "file" |
139 |
- touch(path) |
140 |
+ (path := tmp_path / "file").touch() |
141 |
assert path.is_file() |
142 |
assert not osutils.ensure_dirs(path, mode=0o700) |