Package: thelogrus
logrusTable of ContentsInstallationLicenseIncluded Commandshuman-timeIncluded functionsthelogrus.cliexec_subcommand(unfound)find_subcommand(args)is_program(name)run(command)thelogrus.logginggetCustomLogger(name, logLevel)thelogrus.timehumanTime(seconds)thelogrus.utilsmkdir_p(path)obfuscateString(snippet, showLength=5, smear='*')squashDicts(*dict_args)thelogrus.yamlreadYamlFile(path: str)writeYamlFile(path: str, data)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.Installationpip install thelogrusLicenseApache 2.0 license.Included Commandshuman-timeTakes a value in seconds either from stdin or as arg 1 and converts it to a more meat-friendly format using thehumanTimefunction.human-time 1234will print "20 minutes, 34 seconds"Included functionsthelogrus.cliexec_subcommand(unfound)Creates agit-style driver command. If your script is namedfoo, and is run asfoo bar bazand there is an executable in your$PATHnamedfoo-bar, it will callfoo-barwithbazas the command-line argument.unfoundis an optional argument that should be a function pointer and will be called ifexec_subcommandcan'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>importsysfromthelogrus.cliimportexec_subcommanddef_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%swith 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 formatfoo-bar-baz. If that command exists, we run it. If it doesn't, we check to see iffoo-barexists, in which case we runfoo-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 scriptsfoo-barandfoo-baz, and when the user typesfoo bar foobarwe find the foo-bar script and run it asfoo-bar foobarExample usage:#!/usr/bin/env python3importosimportsubprocessimportsysfromthelogrus.cliimportfind_subcommanddefsubcommander_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 wedo `foo blah foo bar` and the executable foo-blah-foo exists, we'll callit with the argument bar.We deliberately don't do anything with the arguments other than handthem off to the foo subcommand. Subcommands are responsible for theirown 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.ifos.path.basename(command)==sys.argv[0]:print("Could not find a subcommand for%s"%' '.join(sys.argv))sys.exit(1)exceptExceptionase: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 returnTrueif it exists and is executable.run(command)Runs a command (either a str or list) and returns itsstdout.thelogrus.logginggetCustomLogger(name, logLevel)Returns a custom logger with nicely formatted output.thelogrus.timehumanTime(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.utilsmkdir_p(path)os module doesn't have amkdir -pequivalent 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 thesmearcharacter. 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.yamlreadYamlFile(path: str)Return the data structure contained in a YAML fileArgs:
path (str): Path to read fromReturns:
Data decoded from YAML file contentwriteYamlFile(path: str, data)Writes a data structure or object into a YAML fileArgs:
path (str): Path to data file
data (any): Data to convert and write