Package: pydevkit
pydevkitWelcome to Python Development Kit.It provides functionality, frequently needed in a python development:logging4 predefined configurations (incl json logs)control via command-linecontrol via environment variablescolorsANSI coloration with command-line controlshellprintf-like shell command wrapperargument parsingenvironment-aware options with lookup order
command line>environment>defaultuse module doc string as a help messagehelp formatter with switchable default and source var sectionsTo usepydevkitin your project, follow these stepsInstall itpip3installpydevkitModify entry point to look like thisimportpydevkit.log.config# noqa: F401frompydevkit.argparseimportArgumentParserdefget_args():p=ArgumentParser(help=__doc__,version='1.2.3')# FIXME: add your args herep.add_argument("--file",help="file arg")returnp.parse_known_args()defmain():Args,UnknownArgs=get_args()No need to modify any other code. Instead, continue to use standardloggingmodule.importlogginglog=logging.getLogger(__name__)We're done. Now your script has working logging configuration.LoggingRun-time optionsTo control logging, you can use bothPYDEVKIT_LOG_environment variables and--log-command-line options. For example:PYDEVKIT_LOG_HANDLER=app./run.py--log-level=debugHere is the list of all logging options:level -debug,info,warning,error,criticalhandler -app,app_mini,json,json_minidate -datetime,date,timeorstrftimeformat e.g.%Y-%m-%dcolor -auto,yes,no. Ifautois selected, module will enable coloration
for terminals and disable when output is redirected to pipe or file.threads -yes,no. Include thread name in a logDeveloper infoYou can pass extra arguments to the logger withlog.info("main text",extra={"extra":{"more":"info"}})External configurationIfPYTHON_LOGGING_CONFIGvariable is defined and points to a file, PDK will
use it to configure standardloggingmodule, usingdictConfig(for json files)
orfileConfigmethods.Argument parsingCustomArgumentParserwrapper featuringderiving help (description + epilogue) from single doc stringautomaticversionoptionshow / hide option's default valueshow / hide option's environment variablebuilt-in logging optionsclassArgumentParser(help=None,version=None,usage="full",show_default=True,show_envvar=True,**kwargs)All standard keywords work as well. New parameters are:help- gets single string, usually module's doc string,
if\nEPILOG:\nis present, splits it intodescriptionandepilog,
otherwise justdescriptionversion- if supplied, PDK adds--versioncommand-line optionusage- when "full" or None, PDK builds standard long usageusage: run.py [-h] [--log-level arg] [--log-color arg] [--log-handler arg],when "short"usage: run.py [options] [logging]show_default- add(default: XXX)to helpshow_envvar- add(envvar: XXX)to helpDerive help from documentationThis feature allows you to use single string, usually main__doc__string, to
initializeArgParser. Add documentation string to the entry point#!/usr/bin/env python'''Main help messageand another lineEPILOG:usage examples, notes etc'''# main code herethen pass it to the parserp=ArgumentParser(help=__doc__)ANSI colorsANSI colors controlled by--log-coloroption.frompydevkit.termimportterm_getdefmain():term=term_get()log.warning("try%sred%sstring",term.red,term.normal)Shell commandsSimpleprintf-like shell wrapperfrompydevkit.shellimportShellsh=Shell()sh['a1']='long string'sh['a2']='long string'cmd='echo %(a1)s %(a2)s'sh(cmd)# print stdouttxt=sh.inp(cmd)# grab stdoutMiscTBDDevelopmentSeedevelopmentdoc