A wrapper for the Gnu Privacy Guard (GPG or GnuPG)
—
Core GPG instance creation and configuration for establishing connections to the GPG binary and setting up the cryptographic environment.
Creates a GPG instance with comprehensive configuration options for binary location, keyring paths, and environment settings.
class GPG:
def __init__(self, gpgbinary='gpg', gnupghome=None, verbose=False,
use_agent=False, keyring=None, options=None,
secret_keyring=None, env=None):
"""
Initialize a GPG process wrapper.
Parameters:
- gpgbinary (str): Path to the GPG binary to use (default: 'gpg')
- gnupghome (str): Path to GPG home directory for keyrings
- verbose (bool): Enable verbose output
- use_agent (bool): Use GPG agent for passphrase handling
- keyring (str|list): Alternative keyring file(s) to use
- options (list): Additional command-line options for GPG
- secret_keyring (str|list): Alternative secret keyring file(s)
- env (dict): Environment variables for GPG subprocess
"""Properties and attributes available on GPG instances for configuration and introspection.
class GPG:
# Configuration properties
gpgbinary: str # Path to GPG binary
gnupghome: str # GPG home directory path
keyring: list # List of keyring files
secret_keyring: list # List of secret keyring files
verbose: bool # Verbose output flag
use_agent: bool # GPG agent usage flag
options: list # Additional GPG options
env: dict # Environment variables
encoding: str # Character encoding (default: 'latin-1')
# Runtime properties
version: tuple # GPG version information
buffer_size: int # I/O buffer size
decode_errors: str # Error handling strategy
on_data: callable # Data callback functionUtility methods for validation and argument construction.
def make_args(self, args, passphrase):
"""
Construct command line arguments for GPG operations.
Parameters:
- args (list): Base arguments for the operation
- passphrase (str): Passphrase to include if needed
Returns:
list: Complete argument list for GPG subprocess
"""
def is_valid_file(self, fileobj):
"""
Check if object is a valid file-like object.
Parameters:
- fileobj: Object to validate
Returns:
bool: True if object is file-like
"""
def is_valid_passphrase(self, passphrase):
"""
Validate passphrase format (no newlines allowed).
Parameters:
- passphrase (str): Passphrase to validate
Returns:
bool: True if passphrase is valid
"""
def set_output_without_confirmation(self, args, output):
"""
Configure output file handling to avoid confirmation prompts.
Parameters:
- args (list): Command arguments to modify
- output (str): Output file path
"""import gnupg
# Default configuration
gpg = gnupg.GPG()
# Custom GPG binary location
gpg = gnupg.GPG(gpgbinary='/usr/local/bin/gpg2')
# Custom GPG home directory
gpg = gnupg.GPG(gnupghome='/home/user/.gnupg')
# Multiple configuration options
gpg = gnupg.GPG(
gpgbinary='/usr/bin/gpg',
gnupghome='/path/to/gnupg/home',
verbose=True,
use_agent=True,
options=['--trust-model', 'always']
)# Single alternative keyring
gpg = gnupg.GPG(keyring='/path/to/keyring.gpg')
# Multiple keyrings
gpg = gnupg.GPG(keyring=[
'/path/to/public.gpg',
'/path/to/additional.gpg'
])
# Custom secret keyring
gpg = gnupg.GPG(
keyring='/path/to/public.gpg',
secret_keyring='/path/to/secret.gpg'
)# Custom environment variables
gpg = gnupg.GPG(env={
'GNUPGHOME': '/tmp/gnupg',
'GPG_TTY': '/dev/tty'
})
# Additional GPG options
gpg = gnupg.GPG(options=[
'--trust-model', 'always',
'--cipher-algo', 'AES256',
'--digest-algo', 'SHA256'
])
# Data callback for progress monitoring
def data_callback(data):
print(f"Processing: {len(data)} bytes")
gpg = gnupg.GPG()
gpg.on_data = data_callbackInstall with Tessl CLI
npx tessl i tessl/pypi-python-gnupg