pygnuutils

0.1.1 · active · verified Thu Apr 16

pygnuutils is a pure Python implementation for various GNU core utilities, aiming to provide similar functionality in a Pythonic way. It is currently at version 0.1.1 and has a moderate release cadence, with the last update over a year ago.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and use the `Ls` utility to emulate `ls` commands with Pythonic arguments. It also includes an advanced example of dependency injection, a core feature of `pygnuutils`, to customize behavior like symlink resolution.

from pygnuutils.ls import Ls

# Instantiate the Ls utility
ls = Ls()

# List contents of the current directory, similar to 'ls -a'
print("Current directory (ls -a):")
ls('.', all_=True) # Note: 'all_' for '-a' argument

# List contents of a specific directory in long format, similar to 'ls -l /tmp'
print("\n/tmp directory (ls -l):")
ls('/tmp', long=True)

# Example with dependency injection (a key feature mentioned in README)
import os
class ReadlinkWatch(Ls.stub):
    def readlink(self, path, dir_fd=None):
        print(f'Resolving symlink: {path}...')
        return os.readlink(path, dir_fd=dir_fd)

ls_with_watch = Ls(stub=ReadlinkWatch())
print("\nListing with symlink watcher:")
# This might require creating a symlink in /tmp for demonstration
# Example: os.symlink('/etc/passwd', '/tmp/mylink')
# For safe execution, we won't create it here, but demonstrate the call.
try:
    ls_with_watch('/tmp', long=True)
except OSError as e:
    print(f"Could not complete symlink watch example: {e}")
    print("To run, ensure you have symlinks in /tmp or create one, e.g., 'os.symlink('/etc/passwd', '/tmp/mylink')'")

view raw JSON →