Skip to main content

A framework for building beautiful shells.

Project description

pynubia


Nubia is a lightweight framework for building command-line applications with Python. It was originally designed for the “logdevice interactive shell (aka. ldshell)” at Facebook. Since then it was factored out to be a reusable component and several internal Facebook projects now rely on it as a quick and easy way to get an intuitive shell/cli application without too much boilerplate.

Nubia is built on top of python-prompt-toolkit which is a fantastic toolkit for building interactive command-line applications.

Disclaimer: Nubia is beta for non-ldshell use-cases. Some of the design decisions might sound odd but they fit the ldshell usecase perfectly. We are continuously making changes to make it more consistent and generic outside of the ldshell use-case. Until a fully stable release is published, use it on your own risk.

See the CONTRIBUTING file for how to help out.

If you are curious on the origins of the name, checkout Nubia on Wikipedia with its unique and colourful architecture.

Key Features

  • Interactive mode that offers fish-style auto-completion
  • CLI mode that gets generated from your functions and classes.
  • Optional bash/zsh completions via an external utility ‘nubia-complete’ (experimental)
  • A customisable status-bar in interactive mode.
  • An optional IPython-based interactive shell
  • Arguments with underscores are automatically hyphenated
  • Python3 type annotations are used for input type validation

Interactive mode

The interactive mode in Nubia is what makes it unique. It is very easy to build a unique shell for your program with zero overhead. The interactive shell in its simplistic form offers automatic completions for commands, sub-commands, arguments, and values. It also offers a great deal of control for developers to take control over auto-completions, even for commands that do not fall under the typical format. An example is the “select” command in ldshell which is expressed as a SQL-query. We expect that most use cases of Nubia will not need such control and the AutoCommand will be enough without further customisation.

If you start a nubia-based program without a command, it automatically starts an interactive shell. The interactive mode looks like this:

Interactive Demo

Non-interactive mode

The CLI mode works exactly like any traditional unix-based command line utility. Non-interactive Demo

Have your @command decorated function return an int to send that value as the Unix return code for your non interactive CLI.

Examples

It starts with a function like this:

import socket
import typing

from termcolor import cprint
from nubia import argument, command, context

@command
@argument("hosts", description="Hostnames to resolve", aliases=["i"])
@argument("bad_name", name="nice", description="testing")
async def lookup(hosts: typing.List[str], bad_name: int) -> int:
    """
    This will lookup the hostnames and print the corresponding IP addresses
    """
    ctx = context.get_context()

    if not hosts:
        cprint("No hosts supplied via --hosts")
        return 1

    print(f"hosts: {hosts}")
    cprint(f"Verbose? {ctx.verbose}")

    for host in hosts:
        cprint(f"{host} is {socket.gethostbyname(host)}")

    return 0

Requirements

Nubia-based applications require Python 3.9+ and works with both Mac OS X or Linux. While in theory it should work on Windows, it has never been tried.

Installing Nubia

If you are installing nubia for your next project, you should be able to easily use pip for that:

pip install pynubia

Building Nubia from source

poetry build

Running example in virtualenv:

We recommend setting up a separate Python environment using a tool like virtualenv, pyenv-virtualenv, or poetry shell.

If you would like to run the example, install the dependencies and run the example module as a script.

poetry install
cd example
python -m nubia_example

To run the unit tests:

poetry run nosetests

Getting Started

See the getting started guide to learn how to build a simple application with Nubia.

License

Nubia is BSD licensed, as found in the LICENSE file.

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

pynubia-0.3.1.tar.gz (44.9 kB view details)

Uploaded Source

Built Distribution

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

pynubia-0.3.1-py3-none-any.whl (61.0 kB view details)

Uploaded Python 3

File details

Details for the file pynubia-0.3.1.tar.gz.

File metadata

  • Download URL: pynubia-0.3.1.tar.gz
  • Upload date:
  • Size: 44.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.3 Linux/6.14.5-300.fc42.x86_64

File hashes

Hashes for pynubia-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a1e0c153a5d543afafdb97538ef5a43b05c6ccb9b2df1894272e1d2090b0efa5
MD5 60489732f0f95624779f6d4739d89fd1
BLAKE2b-256 de98097ce985c54f5993fe89bdb5cca4b787809a179d82ecff010cb3bcfd78ba

See more details on using hashes here.

File details

Details for the file pynubia-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pynubia-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 61.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.3 Linux/6.14.5-300.fc42.x86_64

File hashes

Hashes for pynubia-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c36f03d0b10821808a6ed8c8742d2dddf6781fcddbd7dc6d878550062a18f3ca
MD5 f2bd2487b1ebabc584454be0587b71a9
BLAKE2b-256 62507523bad67307ae35ca06fe72bf7afbc33a725ef34dd8c300f17fd76bdeda

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