CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xattr

Python wrapper for extended filesystem attributes with dict-like interface

Overview
Eval results
Files

pyxattr-compatibility.mddocs/

pyxattr Compatibility

Compatibility layer providing pyxattr-compatible API for easy migration from the pyxattr package. This module handles different calling conventions, namespace support, and byte string operations to match pyxattr behavior.

Import

from xattr import pyxattr_compat

Capabilities

Namespace Constants

Predefined namespace prefixes for extended attribute organization, matching pyxattr conventions.

NS_SECURITY: bytes   # b"security"
NS_USER: bytes       # b"user"  
NS_SYSTEM: bytes     # b"system"
NS_TRUSTED: bytes    # b"trusted"

Usage Examples:

from xattr import pyxattr_compat

# Use namespace constants
user_ns = pyxattr_compat.NS_USER        # b"user"
security_ns = pyxattr_compat.NS_SECURITY  # b"security"

Get Attribute

Retrieve extended attribute with optional namespace support, matching pyxattr calling conventions.

def getxattr(item, attribute, nofollow=False):
    """
    Get extended attribute (pyxattr compatible).
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - attribute: str/bytes, attribute name
    - nofollow: bool, don't follow symbolic links
    
    Returns:
    bytes: attribute value
    
    Raises:
    IOError: filesystem error or attribute not found
    """

def get(item, name, nofollow=False, namespace=None):
    """
    Get extended attribute with namespace support.
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - name: bytes, attribute name (without namespace prefix)
    - nofollow: bool, don't follow symbolic links
    - namespace: bytes, namespace prefix (NS_USER, NS_SECURITY, etc.)
    
    Returns:
    bytes: attribute value
    
    Raises:
    IOError: filesystem error or attribute not found
    TypeError: namespace is None
    """

Usage Examples:

from xattr import pyxattr_compat

# Direct attribute access (pyxattr style)
value = pyxattr_compat.getxattr('/path/to/file', b'user.description')
print(value)  # b'File description'

# With namespace support
value = pyxattr_compat.get('/path/to/file', b'description', 
                          namespace=pyxattr_compat.NS_USER)
print(value)  # b'File description'

# Don't follow symlinks
link_value = pyxattr_compat.get('/path/to/symlink', b'link_info',
                               nofollow=True, namespace=pyxattr_compat.NS_USER)

Set Attribute

Set extended attribute with flags and namespace support.

def setxattr(item, name, value, flags=0, nofollow=False):
    """
    Set extended attribute (pyxattr compatible).
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - name: str/bytes, attribute name
    - value: bytes, attribute value
    - flags: int, operation flags (XATTR_CREATE, XATTR_REPLACE)
    - nofollow: bool, don't follow symbolic links
    
    Raises:
    IOError: filesystem error or flag constraints
    """

def set(item, name, value, nofollow=False, flags=0, namespace=None):
    """
    Set extended attribute with namespace support.
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - name: bytes, attribute name (without namespace prefix)
    - value: bytes, attribute value
    - nofollow: bool, don't follow symbolic links
    - flags: int, operation flags (XATTR_CREATE, XATTR_REPLACE)
    - namespace: bytes, namespace prefix
    
    Raises:
    IOError: filesystem error or flag constraints
    TypeError: namespace is None
    """

Usage Examples:

from xattr import pyxattr_compat
import xattr

# Direct set (pyxattr style)
pyxattr_compat.setxattr('/path/to/file', b'user.title', b'My Document')

# With namespace
pyxattr_compat.set('/path/to/file', b'title', b'My Document',
                   namespace=pyxattr_compat.NS_USER)

# With flags
pyxattr_compat.set('/path/to/file', b'id', b'12345',
                   flags=xattr.XATTR_CREATE,
                   namespace=pyxattr_compat.NS_USER)

# On symlink itself
pyxattr_compat.setxattr('/path/to/symlink', b'user.type', b'link', nofollow=True)

List Attributes

List extended attributes with optional namespace filtering.

def listxattr(item, nofollow=False):
    """
    List extended attributes (pyxattr compatible).
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - nofollow: bool, don't follow symbolic links
    
    Returns:
    list[bytes]: attribute names as bytes
    
    Raises:
    IOError: filesystem error
    """

def list(item, nofollow=False, namespace=None):
    """
    List extended attributes with namespace filtering.
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - nofollow: bool, don't follow symbolic links
    - namespace: bytes/None, namespace to filter by
    
    Returns:
    list[bytes]: attribute names (without namespace prefix if namespace specified)
    
    Raises:
    IOError: filesystem error
    """

Usage Examples:

from xattr import pyxattr_compat

# List all attributes (returns bytes)
attrs = pyxattr_compat.listxattr('/path/to/file')
print(attrs)  # [b'user.description', b'user.title']

# List attributes in specific namespace
user_attrs = pyxattr_compat.list('/path/to/file', 
                                 namespace=pyxattr_compat.NS_USER)
print(user_attrs)  # [b'description', b'title'] (no 'user.' prefix)

# List all namespaces
all_attrs = pyxattr_compat.list('/path/to/file')  # No namespace filtering
print(all_attrs)  # [b'user.description', b'user.title', b'security.selinux']

Remove Attribute

Remove extended attributes with namespace support.

def removexattr(item, name, nofollow=False):
    """
    Remove extended attribute (pyxattr compatible).
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - name: str/bytes, attribute name
    - nofollow: bool, don't follow symbolic links
    
    Raises:
    IOError: filesystem error or attribute not found
    """

def remove(item, name, nofollow=False, namespace=None):
    """
    Remove extended attribute with namespace support.
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - name: bytes, attribute name (without namespace prefix)
    - nofollow: bool, don't follow symbolic links
    - namespace: bytes, namespace prefix
    
    Raises:
    IOError: filesystem error or attribute not found
    TypeError: namespace is None
    """

Usage Examples:

from xattr import pyxattr_compat

# Remove attribute directly
pyxattr_compat.removexattr('/path/to/file', b'user.old_description')

# Remove with namespace
pyxattr_compat.remove('/path/to/file', b'old_description',
                      namespace=pyxattr_compat.NS_USER)

# Remove from symlink
pyxattr_compat.remove('/path/to/symlink', b'temp_attr',
                      nofollow=True, namespace=pyxattr_compat.NS_USER)

Get All Attributes

Convenience function to retrieve all attributes and their values, with optional namespace filtering.

def get_all(item, nofollow=False, namespace=None):
    """
    Get all extended attributes and their values.
    
    Parameters:
    - item: str/bytes path, int fd, or file-like object
    - nofollow: bool, don't follow symbolic links
    - namespace: bytes/None, namespace to filter by
    
    Returns:
    list[tuple[bytes, bytes]]: list of (name, value) tuples
    
    Raises:
    IOError: filesystem error
    """

Usage Examples:

from xattr import pyxattr_compat

# Get all attributes and values
all_attrs = pyxattr_compat.get_all('/path/to/file')
for name, value in all_attrs:
    print(f"{name}: {value}")

# Get only user namespace attributes
user_attrs = pyxattr_compat.get_all('/path/to/file', 
                                   namespace=pyxattr_compat.NS_USER)
for name, value in user_attrs:
    print(f"user.{name}: {value}")  # name doesn't include 'user.' prefix

Migration Example

Example showing migration from pyxattr to xattr using compatibility layer:

# Original pyxattr code
try:
    import pyxattr
    
    # Set attribute
    pyxattr.setxattr('/path/to/file', 'user.title', b'Document')
    
    # Get attribute
    title = pyxattr.getxattr('/path/to/file', 'user.title')
    
    # List attributes
    attrs = pyxattr.listxattr('/path/to/file')
    
except ImportError:
    # Fallback to xattr compatibility layer
    from xattr import pyxattr_compat as pyxattr
    
    # Same interface works
    pyxattr.setxattr('/path/to/file', b'user.title', b'Document')
    title = pyxattr.getxattr('/path/to/file', b'user.title')
    attrs = pyxattr.listxattr('/path/to/file')

Key Differences from Main xattr API

  • Byte strings: pyxattr compatibility uses bytes for attribute names and values
  • Different parameters: nofollow instead of symlink parameter (inverted logic)
  • Namespace handling: Built-in namespace prefix handling
  • Return types: Functions return bytes/lists instead of strings where applicable
  • Error handling: Maintains pyxattr exception behavior

Install with Tessl CLI

npx tessl i tessl/pypi-xattr

docs

command-line-tool.md

convenience-functions.md

dict-interface.md

index.md

pyxattr-compatibility.md

tile.json