Skip to content

Commit 8efc843

Browse files
committed
Change the design approach to the extras radically.
Instead of an overall "all-or-nothing" test each extra feature individually to see if it's part of the current OS.
1 parent 8e9a0cc commit 8efc843

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

‎src/celpy/__main__.py‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import argparse
2424
import ast
2525
import cmd
26+
from collections.abc import Callable
2627
import datetime
2728
import json
2829
import logging
@@ -32,7 +33,7 @@
3233
import re
3334
import stat as os_stat
3435
import sys
35-
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast
36+
from typing import Any, Dict, List, Optional, Tuple, Union, cast
3637

3738
try:
3839
import tomllib # type: ignore [import-not-found, unused-ignore]
@@ -282,23 +283,31 @@ def stat(path: Union[Path, str]) -> Optional[celtypes.MapType]:
282283
data["r"] = celtypes.BoolType(os.access(path, os.R_OK))
283284
data["w"] = celtypes.BoolType(os.access(path, os.W_OK))
284285
data["x"] = celtypes.BoolType(os.access(path, os.X_OK))
285-
try:
286-
extra = {
287-
"st_birthtime": celtypes.TimestampType(
288-
datetime.datetime.fromtimestamp(
289-
status.st_birthtime # type:ignore [attr-defined, unused-ignore]
290-
)
291-
),
292-
"st_blksize": celtypes.IntType(status.st_blksize),
293-
"st_blocks": celtypes.IntType(status.st_blocks),
294-
"st_flags": celtypes.IntType(status.st_flags), # type: ignore [attr-defined, unused-ignore]
295-
"st_rdev": celtypes.IntType(status.st_rdev),
296-
"st_gen": celtypes.IntType(status.st_gen), # type: ignore [attr-defined, unused-ignore]
297-
"group_access": celtypes.BoolType(status.st_gid == os.getegid()),
298-
"user_access": celtypes.BoolType(status.st_uid == os.geteuid()),
299-
}
300-
except AttributeError: # pragma: no cover
301-
extra = {}
286+
287+
# Optional features; any mixture may be present.
288+
extra_defs: dict[str, Callable[[Any], Any]] = {
289+
"st_birthtime": lambda status: celtypes.TimestampType(
290+
datetime.datetime.fromtimestamp(status.st_birthtime)
291+
),
292+
"st_blksize": lambda status: celtypes.IntType(status.st_blksize),
293+
"st_blocks": lambda status: celtypes.IntType(status.st_blocks),
294+
"st_flags": lambda status: celtypes.IntType(status.st_flags),
295+
"st_rdev": lambda status: celtypes.IntType(status.st_rdev),
296+
"st_gen": lambda status: celtypes.IntType(status.st_gen),
297+
"group_access": lambda status: celtypes.BoolType(
298+
status.st_gid == os.getegid()
299+
),
300+
"user_access": lambda status: celtypes.BoolType(
301+
status.st_uid == os.geteuid()
302+
),
303+
}
304+
extra: dict[str, Any] = {}
305+
for name, status_function in extra_defs.items():
306+
try:
307+
extra[name] = status_function(status)
308+
except AttributeError: # pragma: no cover
309+
pass
310+
302311
return celtypes.MapType(data | extra)
303312
except FileNotFoundError:
304313
return None

‎type_check/lineprecision.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Name Lines Precise Imprecise Any Empty Unanalyzed
22
--------------------------------------------------------------------
33
celpy 357 79 12 4 262 0
4-
celpy.__main__ 551 243 14 61 233 0
4+
celpy.__main__ 560 242 16 65 237 0
55
celpy.adapter 163 34 3 9 111 6
66
celpy.c7nlib 1663 344 16 152 1151 0
77
celpy.celparser 411 137 68 23 183 0

0 commit comments

Comments
 (0)