Skip to main content

A library jam for Python.

Project description

libjam

A library jam for Python.

Installing

libjam is available on PyPI, and can be installed using pip.

pip install libjam

To install the latest bleeding edge:

pip install git+https://github.com/philippkosarev/libjam.git

Modules

Captain

Makes creating command line interfaces easy.

Example CLI project:

example.py:

from libjam import Captain

class CLI:
  'An example CLI for the libjam library'
  def shout(self, *text):
    'Shouts the given text back'
    text = ' '.join(text)
    if options.get('world'):
      text += ' world'
    print(text + '!')

cli = CLI()
captain = Captain(cli)
captain.add_option(
  'world', ['world', 'w'],
  "Adds ' world' before the exclamation mark",
)
global options
function, args, options = captain.parse()
function(*args)

Usage:

$ ./example.py
example.py: No command specified.
Try 'example.py --help' for more information.

$ ./example.py shout Hello
Hello!

$ ./example.py shout Hello --world
Hello world!

$ ./example.py --help
Usage:
  example.py shout [TEXT]...
Synopsis:
  example.py [OPTION]... COMMAND [ARGS]...
Description:
  An example CLI for the libjam library.
Commands:
  shout - Shouts the given text back.
Options:
  -w, --world - Adds ' world' before the exclamation mark.
  -h, --help  - Prints this page.

Drawer

Responsible for file operations. Accepts the '/' as the file separator regardless the operating system.

Example CLI for calculating filesizes

mass.py:

#! /usr/bin/env python3

from libjam import Captain, drawer, typewriter
import sys

def print_progress(todo, done):
  typewriter.print_progress('Calculating file size', todo, done)

def mass(*paths):
  'Prints human-readable size of files'
  if len(paths) == 0:
    paths = ['./']
  n_paths = len(paths)
  results = {}
  for i, path in enumerate(paths):
    if n_paths > 1:
      print_progress(i+1, n_paths)
    if not drawer.exists(path):
      typewriter.clear_lines(0)
      print(f'{path}: No such file or directory.', file=sys.stderr)
      continue
    size = drawer.get_filesize(path)
    results[path] = size
  results = dict(sorted(results.items(), key=lambda item: item[1], reverse=True))
  typewriter.clear_lines(0)
  for file in results:
    size = results.get(file)
    size, units, _ = drawer.get_readable_filesize(size)
    size = round(size, 1)
    print(f'{file}: {size} {units.upper()}')

captain = Captain(mass)

def main():
  args = captain.parse()
  try:
    return mass(*args)
  except KeyboardInterrupt:
    print()
    sys.exit(1)

if __name__ == '__main__':
  sys.exit(main())

Usage:

$ ./mass.py
./: 100.9 MB

$ ./mass.py downloads pictures videos
videos: 34.7 GB
downloads: 12.6 GB
pictures: 2.2 GB

$ ./mass.py -h
Synopsis:
  mass.py [OPTION]... [PATHS]...
Description:
  Prints human-readable size of files.
Options:
  -h, --help - Prints this page.

Typewriter

Transforms text and prints to the terminal.

Notebook

Allows reading and writing .toml, .ini and .json files and provides an application configuration management system with the Notebook.Ledger class.

Example of an app configured through Notebook.Ledger:

# Imports
from libjam import notebook, drawer

# Defining default values
default_downloads_dir = drawer.absolute_path('~/Downloads')
if not drawer.is_folder(default_downloads_dir):
  default_downloads_dir = None
default_values = {
  'downloads-directory': default_downloads_dir,
}
# Config template
template = '''\
# An override for the default downloads directory
# downloads-directory = ''
'''

# Initialising config
ledger = notebook.Ledger('download-manager')
config_obj = ledger.init_config('config', default_values, template)
config_dict = config_obj.read()

# Checking values
downloads_dir = config_dict.get('downloads-directory')
if downloads_dir is None:
    config_obj.on_error(
      "Could not automatically find an existing Downloads directory. "
      "Please specify 'downloads-directory' in the configuration manually."
  )
if not drawer.exists(downloads_dir):
  config_obj.on_error("The specified 'downloads-directory' does not exist")

download_manager.py:

#! /usr/bin/env python3

# Imports
import requests

# Getting downloads_dir from config
from config import downloads_dir

# The rest of the program...

Example output:

$ ./download_manager.py
Configuration error(s):
/home/philipp/.config/download-manager/config.toml:
  - Could not automatically find an existing Downloads directory. Please specify 'downloads-directory' in the configuration manually.

Clipboard

Provides a few list operations such as deduplicate.

Flashcard

Provides the prompt_yn function.

Cloud

Provides the download function.

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

libjam-0.1.8.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

libjam-0.1.8-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file libjam-0.1.8.tar.gz.

File metadata

  • Download URL: libjam-0.1.8.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for libjam-0.1.8.tar.gz
Algorithm Hash digest
SHA256 3c2fc80e0b1150bbcab89510f8d9f5c97b51ddb9d3ac8575be87dec9a73353e2
MD5 3984e4ab6f9bc434aa9e94dd39bc9b22
BLAKE2b-256 2d3f6f00308b3f8a14000a0c1a85c300c7ab3e8ffe5d6e93024ddd1b001c1d4d

See more details on using hashes here.

File details

Details for the file libjam-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: libjam-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for libjam-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2a7e9a21ee7a67a5bd3cff3865d3faa7316b8ae909210ec3db5467aff102af22
MD5 937bc80eaaea3f5fb3b7b0d3bfe81936
BLAKE2b-256 e9ae14b76fbddbd3845eb03e61e93616d32eacbb29fe4d6a8e6600c58baed171

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