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.4.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.4-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: clicommon-0.3.4.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.4.tar.gz
Algorithm Hash digest
SHA256 b8f5609c05d4bfeff88e4145534e239729b070e3918d05784f10e2da217fdbd2
MD5 019cf04456e47b40d3d4622263a23c5e
BLAKE2b-256 108055e55c305164e0fa9e91ad551eed2b79d81c587e7d9c683402f29616e788

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clicommon-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 14.9 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc5413331763e5ee5bbf69acdcc67fef3fa0c5901df57fef912cf8294152a626
MD5 464ca3f1ad51df0e38179b981cdb8ba2
BLAKE2b-256 bb179ac85f9ddd5cbbc32a076a9e9dcf4bd18a63b4b3e2c14380f9c3f19b265f

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