Skip to content

Commit 36939c7

Browse files
committed
Apply black code style
1 parent 1b4359c commit 36939c7

File tree

8 files changed

+234
-198
lines changed

8 files changed

+234
-198
lines changed

getdents/__init__.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,24 @@ def getdents(path: str, buff_size: int = 32768) -> Iterator[DirectoryEntry]:
4747
yield from (
4848
(inode, type, name)
4949
for inode, type, name in getdents_raw(fd, buff_size)
50-
if not(type == DT_UNKNOWN or inode == 0 or name in ('.', '..'))
50+
if not (type == DT_UNKNOWN or inode == 0 or name in (".", ".."))
5151
)
5252
finally:
5353
os.close(fd)
5454

5555

5656
__all__ = [
57-
'DT_BLK',
58-
'DT_CHR',
59-
'DT_DIR',
60-
'DT_FIFO',
61-
'DT_LNK',
62-
'DT_REG',
63-
'DT_SOCK',
64-
'DT_UNKNOWN',
65-
'MIN_GETDENTS_BUFF_SIZE',
66-
'O_GETDENTS',
67-
'getdents',
68-
'getdents_raw',
57+
"DT_BLK",
58+
"DT_CHR",
59+
"DT_DIR",
60+
"DT_FIFO",
61+
"DT_LNK",
62+
"DT_REG",
63+
"DT_SOCK",
64+
"DT_UNKNOWN",
65+
"MIN_GETDENTS_BUFF_SIZE",
66+
"O_GETDENTS",
67+
"DirectoryEntry",
68+
"getdents",
69+
"getdents_raw",
6970
]

getdents/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
from . import __name__ as prog
44
from .cli import main
55

6-
7-
if __name__ == '__main__': # pragma: no cover
6+
if __name__ == "__main__": # pragma: no cover
87
exit(main(prog=prog))

getdents/cli.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ def parse_args(
1212
) -> Tuple[str, int, Formatter]:
1313
parser = ArgumentParser(
1414
prog=prog,
15-
description='Print directory contents.',
15+
description="Print directory contents.",
1616
)
1717

18-
parser.add_argument('path', metavar='PATH')
18+
parser.add_argument("path", metavar="PATH")
1919
parser.add_argument(
20-
'-b', '--buffer-size',
21-
metavar='N',
20+
"-b",
21+
"--buffer-size",
22+
metavar="N",
2223
type=int,
2324
default=32768,
24-
help=(
25-
'Buffer size (in bytes) to allocate when iterating over directory'
26-
),
25+
help=("Buffer size (in bytes) to allocate when iterating over directory"),
2726
)
2827
parser.add_argument(
29-
'-o', '--output-format',
30-
metavar='NAME',
31-
default='plain',
28+
"-o",
29+
"--output-format",
30+
metavar="NAME",
31+
default="plain",
3232
choices=list(FORMATTERS),
33-
help='Output format: %s' % ', '.join(sorted(FORMATTERS)),
33+
help="Output format: %s" % ", ".join(sorted(FORMATTERS)),
3434
)
3535

3636
parsed_args = parser.parse_args(args)
3737
buff_size = parsed_args.buffer_size
3838

3939
if buff_size < MIN_GETDENTS_BUFF_SIZE:
40-
parser.error('Minimum buffer size is %s' % MIN_GETDENTS_BUFF_SIZE)
40+
parser.error("Minimum buffer size is %s" % MIN_GETDENTS_BUFF_SIZE)
4141

4242
return parsed_args.path, buff_size, FORMATTERS[parsed_args.output_format]
4343

@@ -49,7 +49,9 @@ def main(args: Optional[List[str]] = None, prog: Optional[str] = None) -> int:
4949
fmt(getdents(path, buff_size=buff_size), stdout)
5050
except MemoryError:
5151
print(
52-
'Not enough memory to allocate', buff_size, 'bytes of data',
52+
"Not enough memory to allocate",
53+
buff_size,
54+
"bytes of data",
5355
file=stderr,
5456
)
5557
return 3

getdents/formatters.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
DirectoryEntry,
1717
)
1818

19-
2019
Formatter = Callable[[Iterable[DirectoryEntry], TextIO], None]
21-
HEADER = ('inode', 'type', 'name')
22-
FORMATTERS: Dict[str, Formatter] = {}
20+
FormatterRegistry = Dict[str, Formatter]
21+
HEADER = ("inode", "type", "name")
22+
FORMATTERS: FormatterRegistry = {}
2323
TYPE_NAMES = {
24-
DT_BLK: 'blk',
25-
DT_CHR: 'chr',
26-
DT_DIR: 'dir',
27-
DT_FIFO: 'fifo',
28-
DT_LNK: 'lnk',
29-
DT_REG: 'reg',
30-
DT_SOCK: 'sock',
31-
DT_UNKNOWN: 'unknown',
24+
DT_BLK: "blk",
25+
DT_CHR: "chr",
26+
DT_DIR: "dir",
27+
DT_FIFO: "fifo",
28+
DT_LNK: "lnk",
29+
DT_REG: "reg",
30+
DT_SOCK: "sock",
31+
DT_UNKNOWN: "unknown",
3232
}
3333

3434

@@ -39,10 +39,11 @@ def formatter(
3939
def deco(fn: Formatter) -> Formatter:
4040
registry[name] = fn
4141
return fn
42+
4243
return deco
4344

4445

45-
@formatter('plain')
46+
@formatter("plain")
4647
def format_plain(
4748
directory_entries: Iterable[DirectoryEntry],
4849
file: TextIO = stdout,
@@ -65,24 +66,25 @@ def _format_csv(
6566

6667
for first in directory_entries:
6768
if headers:
68-
print(writer.writerow(HEADER), end='', file=file)
69+
print(writer.writerow(HEADER), end="", file=file)
6970

7071
for inode, type, name in chain((first,), directory_entries):
7172
print(
7273
writer.writerow((inode, TYPE_NAMES[type], name)),
73-
end='', file=file,
74+
end="",
75+
file=file,
7476
)
7577

7678

77-
@formatter('csv')
79+
@formatter("csv")
7880
def format_csv(
7981
directory_entries: Iterable[DirectoryEntry],
8082
file: TextIO = stdout,
8183
) -> None:
8284
return _format_csv(directory_entries, file, False)
8385

8486

85-
@formatter('csv-headers')
87+
@formatter("csv-headers")
8688
def format_csv_headers(
8789
directory_entries: Iterable[DirectoryEntry],
8890
file: TextIO = stdout,
@@ -91,34 +93,42 @@ def format_csv_headers(
9193

9294

9395
def json_encode(inode: int, type: int, name: str) -> str:
94-
return json_dumps({
95-
'inode': inode,
96-
'type': TYPE_NAMES[type],
97-
'name': name,
98-
})
96+
return json_dumps(
97+
{
98+
"inode": inode,
99+
"type": TYPE_NAMES[type],
100+
"name": name,
101+
}
102+
)
99103

100104

101-
@formatter('json')
105+
@formatter("json")
102106
def format_json(
103107
directory_entries: Iterable[DirectoryEntry],
104108
file: TextIO = stdout,
105109
) -> None:
106110
for inode, type, name in directory_entries:
107111
print(
108-
'[\n', json_encode(inode, type, name),
109-
sep='', end='', file=file,
112+
"[\n",
113+
json_encode(inode, type, name),
114+
sep="",
115+
end="",
116+
file=file,
110117
)
111118

112119
for inode, type, name in directory_entries:
113120
print(
114-
',\n', json_encode(inode, type, name),
115-
sep='', end='', file=file,
121+
",\n",
122+
json_encode(inode, type, name),
123+
sep="",
124+
end="",
125+
file=file,
116126
)
117127

118-
print('\n]', file=file)
128+
print("\n]", file=file)
119129

120130

121-
@formatter('json-stream')
131+
@formatter("json-stream")
122132
def format_json_stream(
123133
directory_entries: Iterable[DirectoryEntry],
124134
file: TextIO = stdout,

tests/test___init__.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
from unittest.mock import patch, sentinel
1+
from unittest.mock import Mock, patch, sentinel
22

33
from pytest import raises
44

55
from getdents import DT_DIR, DT_REG, DT_UNKNOWN, O_GETDENTS, getdents
66

77

8-
@patch('getdents.os')
9-
@patch('getdents.getdents_raw')
10-
def test_path(mock_getdents_raw, mock_os):
8+
@patch("getdents.os")
9+
@patch("getdents.getdents_raw")
10+
def test_path(mock_getdents_raw: Mock, mock_os: Mock) -> None:
1111
mock_os.open.return_value = sentinel.fd
1212

13-
list(getdents('/tmp', sentinel.size))
13+
list(getdents("/tmp", sentinel.size))
1414

15-
mock_os.open.assert_called_once_with('/tmp', O_GETDENTS)
15+
mock_os.open.assert_called_once_with("/tmp", O_GETDENTS)
1616
mock_getdents_raw.assert_called_once_with(sentinel.fd, sentinel.size)
1717
mock_os.close.assert_called_once_with(sentinel.fd)
1818

1919

20-
@patch('getdents.os')
21-
@patch('getdents.getdents_raw', side_effect=[Exception])
22-
def test_path_err(mock_getdents_raw, mock_os):
20+
@patch("getdents.os")
21+
@patch("getdents.getdents_raw", side_effect=[Exception])
22+
def test_path_err(mock_getdents_raw: Mock, mock_os: Mock) -> None:
2323
mock_os.open.return_value = sentinel.fd
2424

2525
with raises(Exception):
26-
list(getdents('/tmp', sentinel.size))
26+
list(getdents("/tmp", sentinel.size))
2727

28-
mock_os.open.assert_called_once_with('/tmp', O_GETDENTS)
28+
mock_os.open.assert_called_once_with("/tmp", O_GETDENTS)
2929
mock_getdents_raw.assert_called_once_with(sentinel.fd, sentinel.size)
3030
mock_os.close.assert_called_once_with(sentinel.fd)
3131

3232

33-
@patch('getdents.os')
34-
@patch('getdents.getdents_raw', return_value=iter([
35-
(1, DT_DIR, '.'),
36-
(2, DT_DIR, '..'),
37-
(3, DT_DIR, 'dir'),
38-
(4, DT_REG, 'file'),
39-
(5, DT_UNKNOWN, '???'),
40-
(0, DT_REG, 'deleted'),
41-
]))
42-
def test_filtering(mock_getdents_raw, mock_os):
43-
assert list(getdents(0)) == [
44-
(3, DT_DIR, 'dir'),
45-
(4, DT_REG, 'file'),
33+
@patch("getdents.os")
34+
@patch(
35+
"getdents.getdents_raw",
36+
return_value=iter(
37+
[
38+
(1, DT_DIR, "."),
39+
(2, DT_DIR, ".."),
40+
(3, DT_DIR, "dir"),
41+
(4, DT_REG, "file"),
42+
(5, DT_UNKNOWN, "???"),
43+
(0, DT_REG, "deleted"),
44+
]
45+
),
46+
)
47+
def test_filtering(mock_getdents_raw: Mock, mock_os: Mock) -> None:
48+
assert list(getdents(".")) == [
49+
(3, DT_DIR, "dir"),
50+
(4, DT_REG, "file"),
4651
]

tests/test__getdents.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
import os
22
import sys
3+
from pathlib import Path
4+
from typing import Iterable
35
from unittest.mock import ANY
46

57
from pytest import fixture, raises
68

7-
from getdents._getdents import (
8-
DT_DIR,
9-
MIN_GETDENTS_BUFF_SIZE,
10-
getdents_raw,
11-
)
9+
from getdents._getdents import DT_DIR, MIN_GETDENTS_BUFF_SIZE, getdents_raw
1210

1311

1412
@fixture
15-
def fixt_regular_file(tmpdir):
16-
f = tmpdir.join('test.txt')
17-
f.write('content')
13+
def fixt_regular_file(tmp_path: Path) -> Iterable[int]:
14+
f = tmp_path / "test.txt"
15+
f.write_text("content")
1816

19-
fd = os.open(str(f), os.O_RDONLY)
17+
fd = os.open(f, os.O_RDONLY)
2018

2119
yield fd
2220

2321
os.close(fd)
2422

2523

2624
@fixture
27-
def fixt_dir(tmpdir):
25+
def fixt_dir(tmp_path: Path) -> Iterable[int]:
2826
for i in range(10):
29-
tmpdir.mkdir('subdir%d' % i)
27+
(tmp_path / f"subdir{i}").mkdir()
3028

31-
fd = os.open(str(tmpdir), os.O_DIRECTORY | os.O_RDONLY)
29+
fd = os.open(tmp_path, os.O_DIRECTORY | os.O_RDONLY)
3230

3331
yield fd
3432

3533
os.close(fd)
3634

3735

38-
def test_not_a_directory(fixt_regular_file):
36+
def test_not_a_directory(fixt_regular_file: int) -> None:
3937
with raises(NotADirectoryError):
4038
getdents_raw(fixt_regular_file, MIN_GETDENTS_BUFF_SIZE)
4139

4240

43-
def test_small_buffer(fixt_dir):
41+
def test_small_buffer(fixt_dir: int) -> None:
4442
with raises(ValueError):
4543
getdents_raw(fixt_dir, MIN_GETDENTS_BUFF_SIZE - 1)
4644

4745

48-
def test_malloc_fail(fixt_dir):
46+
def test_malloc_fail(fixt_dir: int) -> None:
4947
with raises(MemoryError):
5048
getdents_raw(fixt_dir, sys.maxsize)
5149

5250

53-
def test_getdents_raw(fixt_dir):
54-
iterator = iter(sorted(
55-
getdents_raw(
56-
fixt_dir,
57-
MIN_GETDENTS_BUFF_SIZE,
58-
),
59-
key=lambda d: d[2],
60-
))
51+
def test_getdents_raw(fixt_dir: int) -> None:
52+
iterator = iter(
53+
sorted(
54+
getdents_raw(
55+
fixt_dir,
56+
MIN_GETDENTS_BUFF_SIZE,
57+
),
58+
key=lambda d: d[2],
59+
)
60+
)
6161

62-
assert next(iterator) == (ANY, DT_DIR, '.')
63-
assert next(iterator) == (ANY, DT_DIR, '..')
62+
assert next(iterator) == (ANY, DT_DIR, ".")
63+
assert next(iterator) == (ANY, DT_DIR, "..")
6464

6565
for i, entry in enumerate(iterator):
66-
assert entry == (ANY, DT_DIR, 'subdir%d' % i)
66+
assert entry == (ANY, DT_DIR, "subdir%d" % i)

0 commit comments

Comments
 (0)