Python-Nubia – A Command-Line And Interactive Shell Framework

0
27
Python-Nubia - A Command-Line And Interactive Shell Framework

Nubia is a lightweight framework for constructing command-line purposes with Python. It was initially designed for the “logdevice interactive shell (aka. ldshell)” at Facebook. Since then it was factored out to be a reusable element and several other inside Facebook initiatives now depend on it as a fast and simple solution to get an intuitive shell/cli utility with out an excessive amount of boilerplate.

Disclaimer: Nubia is beta for non-ldshell use-instances. Some of the design selections would possibly sound odd however they match the ldshell usecase completely. We are repeatedly making modifications to make it extra constant and generic exterior of the ldshell use-case. Until a totally steady launch is revealed, use it by yourself danger.

If you’re curious on the origins of the title, checkout Nubia on Wikipedia with its distinctive and vibrant structure.


Key Features

  • Interactive mode that provides fish-fashion auto-completion
  • CLI mode that will get generated out of your capabilities and courses.
  • Optional bash/zsh completions by way of an exterior utility ‘nubia-complete’ (experimental)
  • A customisable standing-bar in interactive mode.
  • An elective IPython-based interactive shell
  • Arguments with underscores are routinely hyphenated
  • PythonThree kind annotations are used for enter kind validation

Interactive mode
The interactive mode in Nubia is what makes it distinctive. It may be very simple to construct a novel shell in your program with zero overhead. The interactive shell in its simplistic type presents automated completions for instructions, sub-instructions, arguments, and values. It additionally presents a substantial amount of management for builders to take management over auto-completions, even for instructions that don’t fall underneath the standard format. An instance is the “select” command in ldshell which is expressed as a SQL-question. We anticipate that almost all use instances of Nubia is not going to want such management and the AutoCommand will probably be sufficient with out additional customisation.
If you begin a nubia-based mostly program and not using a command, it routinely begins an interactive shell. The interactive mode seems to be like this:

Non-interactive mode
The CLI mode works precisely like every conventional unix-based mostly command line utility.

Examples
It begins with a operate 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", title="nice", description="testing")
def lookup(hosts: typing.List[str], bad_name: int):
    """
    This will lookup the hostnames and print the corresponding IP addresses
    """
    ctx = context.get_context()
    print(f"hosts: {hosts}")
    cprint(f"Verbose? {ctx.verbose}")

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

    # elective, by default it is 0
    return 0

Requirements
Nubia-based purposes require python 3.6+ and works with each Mac OS X or Linux. While in concept it ought to work on Windows, it has by no means been tried.

Installing Nubia
If you’re putting in nubia in your subsequent undertaking, you must be capable of simply use pip for that:

pip3 set up python-nubia

Building Nubia from supply
Ensure is pipenv put in:
You can both setup.py to construct a tarball, or use pipenv to setup a virtualenv with all of the dependencies put in.

Running instance in virtualenv:
If you want to run the instance, then you have to add the foundation of the supply tree into your PYTHONPATH.

pipenv replace --dev
pipenv shell

export PYTHONPATH="$(pwd)"
cd instance/
python nubia_example.py

To run the unit exams:

Getting Started
See the getting started information to learn to construct a easy utility with Nubia.

MoreTip.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.