Skip to main content

logrus

License GitHub stars Code Climate Issue Count

Table of Contents

The logrus is a collection of random utility functions. Nothing in here is all that special, they're just yet another implementation of functions I've rewritten at every job to use in various utility scripts. By open sourcing them now, I'm hoping to not have to write them yet again.

And yes, the name is from Zelazny's Amber series.

Installation

pip install thelogrus

License

Apache 2.0 license.

Included Commands

human-time

Takes a value in seconds either from stdin or as arg 1 and converts it to a more meat-friendly format using the humanTime function.

human-time 1234 will print "20 minutes, 34 seconds"

Included functions

thelogrus.cli

exec_subcommand(unfound)

Creates a git-style driver command. If your script is named foo, and is run as foo bar baz and there is an executable in your $PATH named foo-bar, it will call foo-bar with baz as the command-line argument.

unfound is an optional argument that should be a function pointer and will be called if exec_subcommand can't find a suitable subcommand. Mainly useful for you to have a custom usage message.

Example usage:

#!/usr/bin/env python3
#
# Test script for thelogrus
#
# Confirms that thelogrus.cli.exec_subcommand works as expected
#
# Copyright 2019, Joe Block <jpb@unixorn.net>

import sys
from thelogrus.cli import exec_subcommand


def _usage(message):
  '''
  Custom usage printer
  '''
  print("%s" % sys.argv[0])
  print("Called as %s" % (' '.join(sys.argv)))
  print("Oh look, a custom usage message.")
  print("Attempted to find an executable using all the permutations of %s with no luck." % '-'.join(sys.argv))
  print("%s" % message)


if __name__ == '__main__':
  exec_subcommand(unfound=_usage)

find_subcommand(args)

Given a list ['foo','bar', 'baz'], attempts to create a command name in the format foo-bar-baz. If that command exists, we run it. If it doesn't, we check to see if foo-bar exists, in which case we run foo-bar baz. We keep slicing chunks off the end of the command name and adding them to the argument list until we find a valid command name we can run.

This allows us to easily make git-style command drivers where for example we have a driver script, foo, and subcommand scripts foo-bar and foo-baz, and when the user types foo bar foobar we find the foo-bar script and run it as foo-bar foobar

Example usage:

#!/usr/bin/env python3

import os
import subprocess
import sys
from thelogrus.cli import find_subcommand


def subcommander_driver():
  '''
  Process the command line arguments and run the appropriate subcommand.

  We want to be able to do git-style handoffs to subcommands where if we
  do `foo blah foo bar` and the executable foo-blah-foo exists, we'll call
  it with the argument bar.

  We deliberately don't do anything with the arguments other than hand
  them off to the foo subcommand. Subcommands are responsible for their
  own argument parsing.
  '''
  try:
    (command, args) = find_subcommand(sys.argv)

    # If we can't construct a subcommand from sys.argv, it'll still be able
    # to find this driver script, and re-running ourself isn't useful.
    if os.path.basename(command) == sys.argv[0]:
      print("Could not find a subcommand for %s" % ' '.join(sys.argv))
      sys.exit(1)
  except Exception as e:
    print(str(e))
    sys.exit(1)
  subprocess.check_call([command] + args)


if __name__ == '__main__':
  subcommander_driver()

is_program(name)

Search for a given program in $PATH, and return True if it exists and is executable.

run(command)

Runs a command (either a str or list) and returns its stdout.

thelogrus.logging

getCustomLogger(name, logLevel)

Returns a custom logger with nicely formatted output.

thelogrus.time

humanTime(seconds)

Takes a value in seconds, returns it in meat-friendly format. humanFriendlyTime(8675309) would return "100 days 9 hours 48 minutes 29 seconds".

thelogrus.utils

mkdir_p(path)

os module doesn't have a mkdir -p equivalent so added one.

obfuscateString(snippet, showLength=5, smear='*')

Takes a string, chops off showLength characters from the beginning and end and replaces everything else with the smear character. Really only useful for displaying just enough of a credential in a log to show it's using the correct credential, but not revealing the actual credential.

squashDicts(*dict_args)

Return a dict that is all the dict_args squashed together.

thelogrus.yaml

readYamlFile(path: str)

Return the data structure contained in a YAML file

Args: path (str): Path to read from

Returns: Data decoded from YAML file content

writeYamlFile(path: str, data)

Writes a data structure or object into a YAML file

Args: path (str): Path to data file data (any): Data to convert and write

Metadata

Release files for thelogrus 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thelogrus 0.7.0
File Size Uploaded Provenance
thelogrus-0.7.0.tar.gz 13.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for thelogrus 0.7.0
File Interpreter ABI Platform
thelogrus-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.9 kB

Release files / thelogrus-0.7.0.tar.gz

Download URL thelogrus-0.7.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fd7171e065bb739ab54ac3c5098e601037ca7ca91366b8e96140e078b1fe4e04
BLAKE2b-256 checksum
How to use checksums
9227370f9b7eda763ee2f2a5228bab066b15c86d551b750f3841f6afb8c33892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.6.0

Release files / thelogrus-0.7.0-py3-none-any.whl

Download URL thelogrus-0.7.0-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6b3e16e9a6c7ef8d9b9e5823434aab2720d5d7bb83450f69aaa589b9c68eda3
BLAKE2b-256 checksum
How to use checksums
aa7ef502e2f438b697060def938837c067c500186254dd3ff8621b5860e00320
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.13 Darwin/21.6.0

Security for thelogrus 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page