|
23 | 23 | import argparse |
24 | 24 | import ast |
25 | 25 | import cmd |
| 26 | +from collections.abc import Callable |
26 | 27 | import datetime |
27 | 28 | import json |
28 | 29 | import logging |
|
32 | 33 | import re |
33 | 34 | import stat as os_stat |
34 | 35 | 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 |
36 | 37 |
|
37 | 38 | try: |
38 | 39 | import tomllib # type: ignore [import-not-found, unused-ignore] |
@@ -282,23 +283,31 @@ def stat(path: Union[Path, str]) -> Optional[celtypes.MapType]: |
282 | 283 | data["r"] = celtypes.BoolType(os.access(path, os.R_OK)) |
283 | 284 | data["w"] = celtypes.BoolType(os.access(path, os.W_OK)) |
284 | 285 | 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 | + |
302 | 311 | return celtypes.MapType(data | extra) |
303 | 312 | except FileNotFoundError: |
304 | 313 | return None |
|
0 commit comments