|
| 1 | +# |
| 2 | +# sambacc: a samba container configuration tool |
| 3 | +# Copyright (C) 2022 John Mulligan |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +# |
| 18 | +"""xattr shim module |
| 19 | +
|
| 20 | +This module exists to insulate sambacc from the platform xattr module. |
| 21 | +Currently it only support pyxattr. This module can be imported without |
| 22 | +pyxattr (xattr) present. The functions will import the required module |
| 23 | +and raise an ImportError if xattr is not available. |
| 24 | +
|
| 25 | +This shim also provides a typed functions for xattr management. This |
| 26 | +could have been accomplished by writing a pyi file for xattr but since |
| 27 | +we need the runtime support we just add new functions. |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +import pathlib |
| 32 | +import typing |
| 33 | + |
| 34 | +XAttrItem = typing.Union[ |
| 35 | + int, # an open file descriptor, not wrapped by an object |
| 36 | + pathlib.Path, # pathlib path object |
| 37 | + str, # basic path string |
| 38 | + typing.IO, # an open file descriptor, wrapped by an object |
| 39 | +] |
| 40 | +Namespace = typing.Optional[bytes] |
| 41 | + |
| 42 | + |
| 43 | +def get( |
| 44 | + item: XAttrItem, |
| 45 | + name: str, |
| 46 | + *, |
| 47 | + nofollow: bool = False, |
| 48 | + namespace: Namespace = None |
| 49 | +) -> bytes: |
| 50 | + """Get an xattr from the target item and name. |
| 51 | + See docs for PyXattr module for details. |
| 52 | + """ |
| 53 | + import xattr # type: ignore |
| 54 | + |
| 55 | + kwargs: dict[str, typing.Any] = {"nofollow": nofollow} |
| 56 | + if namespace is not None: |
| 57 | + kwargs["namespace"] = namespace |
| 58 | + return xattr.get(item, name, **kwargs) |
| 59 | + |
| 60 | + |
| 61 | +def set( |
| 62 | + item: XAttrItem, |
| 63 | + name: str, |
| 64 | + value: str, |
| 65 | + *, |
| 66 | + flags: typing.Optional[int] = None, |
| 67 | + nofollow: bool = False, |
| 68 | + namespace: Namespace = None |
| 69 | +) -> None: |
| 70 | + """Set an xattr. See docs for PyXattr module for details.""" |
| 71 | + import xattr # type: ignore |
| 72 | + |
| 73 | + kwargs: dict[str, typing.Any] = {"nofollow": nofollow} |
| 74 | + if flags is not None: |
| 75 | + kwargs["flags"] = flags |
| 76 | + if namespace is not None: |
| 77 | + kwargs["namespace"] = namespace |
| 78 | + return xattr.set(item, name, value, **kwargs) |
0 commit comments