Small modules

argparser_to_readme

jflib.argparser_to_readme.argparser_to_readme(argparser, template='README-template.md', destination='README.md', indentation=0, placeholder='{{ argparse }}')[source]

Add the formatted help output of a command line utility using the Python module argparse to a README file.

Parameters
  • argparser (object) – The argparse parser object.

  • template (str) – The path of a template text file containing the placeholder. Default: README-template.md

  • destination (str) – The path of the destination file. Default: README.me

  • indentation (int) – Indent the formatted help output by X spaces. Default: 0

  • placeholder (str) – Placeholder string that gets replaced by the formatted help output. Default: {{ argparse }}

Return type

None

capturing

Capture the stdout or stderr output.

Capture stdout:

with Capturing() as output:
    print('line 1')

is equivalent to

with Capturing(stream='stdout') as output:
    print('line 1')

Capture stderr:

with Capturing(stream='stderr') as output:
    print('line 1', file=sys.stderr)
class jflib.capturing.Capturing(stream='stdout', clean_ansi=False)[source]

Bases: List[str]

Capture the stdout or stderr output. This class is designed for unit tests.

Parameters
  • stream (Union[Literal[‘stdout’], Literal[‘stderr’]]) – stdout or stderr.

  • clean_ansi (bool) – Clean out ANSI colors from the captured output.

tostring()[source]

Convert the output into an string. By default a list of output lines is returned.

Return type

str

send_email

jflib.send_email.send_email(from_addr, to_addr, subject, body, smtp_login, smtp_password, smtp_server)[source]

Send a email.

Parameters
  • from_addr (str) – The email address of the sender.

  • to_addr (str) – The email address of the recipient.

  • subject (str) – The email subject.

  • body (str) – The email body.

  • smtp_login (str) – The SMTP login name.

  • smtp_password (str) – The SMTP password.

  • smtp_server (str) – The URL of the SMTP server, for example: smtp.example.com:587.

Returns

Problems

termcolor

ANSII Color formatting for output in terminal.

jflib.termcolor.colored(text, color=None, on_color=None, attrs=None)[source]

Colorize text.

Available text colors:

red, green, yellow, blue, magenta, cyan, white.

Available text highlights:

on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

Available attributes:

bold, dark, underline, blink, reverse, concealed.

Example:

colored(‘Hello, World!’, ‘red’, ‘on_grey’, [‘blue’, ‘blink’]) colored(‘Hello, World!’, ‘green’)

Return type

str

jflib.termcolor.cprint(text, color=None, on_color=None, attrs=None)[source]

Print colorize text.

jflib.termcolor.remove_color(text)[source]

https://stackoverflow.com/a/14693789/10193818

Return type

str

utils

Small utility functions.

jflib.utils.download(url, dest)[source]

Download a file and save it under a destination path.

Parameters
  • url (str) – The URL of the file to download.

  • dest (str) – The path of the destination file.

Return type

None

jflib.utils.make_executable(path)[source]

Make a file executable.

Parameters

path (str) – The path of the file

Return type

None