Skip to main content

Python Common Client Library - A collection of useful utilities for command-line applications

Project description

clicommon

A lightweight Python library providing common utilities for command-line applications. It simplifies logging, variable checking, and subprocess execution in CLI tools and scripts.

Features

  • bcheck: Check if a variable exists in the caller's scope and is truthy
  • mlog: Flexible logging with colored output, timestamps, and exit codes
  • rcmd: Simple wrapper for running shell commands with error handling

Installation

pip install clicommon

Or using uv:

uv add clicommon

Usage

bcheck

Checks if a variable exists in the caller's scope and evaluates to True.

from clicommon import bcheck

my_var = True
if bcheck("my_var"):
    print("Variable exists and is truthy")

if not bcheck("undefined_var"):
    print("Variable does not exist or is falsy")

mlog

A versatile logging function with support for colors, timestamps, and exit codes.

from clicommon import mlog

# Simple message
mlog("INFO", "Starting process")

# Use colors (COLORS must be set in scope)
COLORS = True
mlog("SUCCESS", "Operation completed successfully")

# Add timestamps (DATELOG must be set in scope)
DATELOG = True
mlog("WARN", "This is a warning")

# Enable debug output (DEBUG must be set in scope)
DEBUG = True
mlog("DEBUG", "Debug information")

# Exit with code
mlog("ERROR", "Fatal error occurred", exit_code=1)

Message Types and Default Colors

Type Color
INFO Green
SUCCESS Green
WARN/WARNING Yellow
FATAL/ERROR/CRITICAL Red
TEST Gray
DEBUG Magenta
VERBOSE Bright Cyan
BUILD_DEBUG/CODE_DEBUG Bright Green

Function Signature

def mlog(msg_type, msg_string=None, exit_code=None, datelog=None, colors=None)
  • msg_type: Type of message (determines color and verbosity filtering)
  • msg_string: Message content (if None, msg_type becomes the message)
  • exit_code: If provided, exit with this code after logging
  • datelog: If True, add ISO8601 timestamp (or set DATELOG=True in scope)
  • colors: If True, use ANSI colors (or set COLORS=True in scope)

rcmd

Run shell commands with automatic error handling and logging.

from clicommon import rcmd

# Simple command execution
output = rcmd("ls -la")
print(output)

# Commands that fail will automatically log an error and exit

Development

Install development dependencies:

uv sync --dev

Run tests:

uv run pytest

Build the package:

uv build

Publishing to PyPI

To publish a new release to PyPI:

  1. Update the version in pyproject.toml
  2. Commit your changes
  3. Create and push a version tag:
    git tag v0.3.0
    git push origin v0.3.0
    
  4. The GitHub workflow will automatically:
    • Run all tests
    • Build the package
    • Publish to PyPI (requires PYPI_API_TOKEN secret in GitHub)

Important: Before tagging, ensure you have added a PYPI_API_TOKEN secret to your GitHub repository settings.

To test the build locally:

uv build

License

Apache License 2.0

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clicommon-0.3.0.tar.gz (66.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clicommon-0.3.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file clicommon-0.3.0.tar.gz.

File metadata

  • Download URL: clicommon-0.3.0.tar.gz
  • Upload date:
  • Size: 66.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clicommon-0.3.0.tar.gz
Algorithm Hash digest
SHA256 157bcac00e9728cc87ba9fe0ee7205e07f9cc7052e72a7c252e532d050e369cf
MD5 7c3a8518453180141c7c451cb572d075
BLAKE2b-256 d4c4150f73e2e73ae09c164c4e949eb52c1f52ad4ee1258e97afb46c57953599

See more details on using hashes here.

File details

Details for the file clicommon-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: clicommon-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clicommon-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54b107f24e190a8f6c86bd1a62b287e4c88a44e109ab52ac80b33b86479ecb5a
MD5 fc818d9e2534f271efad218e995c0975
BLAKE2b-256 d8cfc39cbcbf7f6a55f398e8549c106f811a8d02126b7435377324c9affec077

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page