Command Line Manager + Interactive Shell for Python Projects

Overview

Manage

Command Line Manager + Interactive Shell for Python Projects

Documentation Status

Features

With manage you add a command line manager to your Python project and also it comes with an interactive shell with iPython support.

All you have to do is init your project directory (creating the manage.yml file)

$ pip install manage
$ cd /my_project_root_folder
$ manage init
creating manage.yml....

The file manage.yml describes how manage command should discover your app modules and custom commands and also it defines which objects should be loaded in to the shell

Note

Windows users may need to install proper version of PyYAML depending on the version of that thing you call an operating system, installable available in: https://pypi.python.org/pypi/PyYAML or consider using Linux and don't worry about this as everything works well in Linux except games, photoshop and solitary game :)

The Shell

By default the command manage shell is included, it is a simple Python REPL console with some configurable options.

You can change the banner message to say anything you want, e.g: "Welcome to my shell!" and you can also specify some objects to be automatically imported in to the shell context so when you enter in to the shell you already have your project's common objects available.

Also you can specify a custom function to run or a string based code block to run, useful to init and configure the objects.

Consoles

manage shell can start different consoles by passing the options

  • manage shell --ipython - This is the default (if ipython installed)
  • manage shell --ptpython
  • manage shell --bpython
  • manage shell --python - This is the default Python console including support for autocomplete. (will be default when no other is installed)

The first thing you can do with manage is customizing the objects that will be automatically loaded in to shell, saving you from importing and initializing a lot of stuff every time you need to play with your app via console.

Edit manage.yml with:

project_name: My Awesome Project
help_text: |
  This is the {project_name} interactive shell!
shell:
  console: bpython
  readline_enabled: false  # MacOS has no readline completion support
  banner:
    enabled: true
    message: 'Welcome to {project_name} shell!'
  auto_import:
    display: true
    objects:
      my_system.config.settings:
      my_system.my_module.MyClass:
      my_system.my_module.OtherClass:
        as: NiceClass
      sys.path:
        as: sp
        init:
          insert:
            args:
              - 0
              - /path/to/be/added/automatically/to/sys/path
  init_script: |
    from my_system.config import settings
    print("Initializing settings...")
    settings.configure()

Then the above manage.yaml will give you a shell like this:

$ manage shell
Initializing settings...
Welcome to My Awesome Project shell!
    Auto imported: ['sp', 'settings', 'MyClass', 'NiceCLass']
>>>  NiceClass. 
   
   
    
     # autocomplete enabled
   
   

Watch the demo:

asciicast

Check more examples in:

https://github.com/rochacbruno/manage/tree/master/examples/

The famous naval fate example (used in docopt and click) is in:

https://github.com/rochacbruno/manage/tree/master/examples/naval/

Projects using manage

  • Quokka CMS (A Flask based CMS) is using manage
  • Red Hat Satellite QE tesitng framework (robottelo) is using manage

Custom Commands

Sometimes you need to add custom commands in to your project e.g: A command to add users to your system:

$ manage create_user --name=Bruno --passwd=1234
Creating the user...

manage has some different ways for you to define custom commands, you can use click commands defined in your project modules, you can also use function_commands defined anywhere in your project, and if really needed can define inline_commands inside the manage.yml file

1. Using a custom click_commands module (single file)

Lets say you have a commands module in your application, you write your custom command there and manage will load it

# myproject/commands.py
import click
@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
    """Create a new user"""
    click.echo('Creating the user...')
    mysystem.User.create(name, password)

Now you go to your manage.yml or .manage.yml and specify your custom command module.

click_commands:
  - module: commands

Now you run manage --help

$ manage --help
...
Commands:
  create_user  Create a new user
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

Using a click_commands package (multiple files)

It is common to have different files to hold your commands so you may prefer having a commands/ package and some python modules inside it to hold commands.

# myproject/commands/user.py
import click
@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
    """Create a new user"""
    click.echo('Creating the user...')
    mysystem.User.create(name, password)
# myproject/commands/system.py
import click
@click.command()
def clear_cache():
    """Clear the system cache"""
    click.echo('The cache will be erased...')
    mysystem.cache.clear()

So now you want to add all those commands to your manage editing your manage file with.

click_commands:
  - module: commands

Now you run manage --help and you have commands from both modules

$ manage --help
...
Commands:
  create_user  Create a new user
  clear_cache  Clear the system cache
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

Custom click_command names

Sometimes the name of commands differ from the name of the function so you can customize it.

click_commands:
  - module: commands.system
    config:
      clear_cache:
        name: reset_cache
        help_text: This resets the cache
  - module: commands.user
    config:
      create_user:
        name: new_user
        help_text: This creates new user

Having different namespaces

If customizing the name looks too much work for you, and you are only trying to handle naming conflicts you can user namespaced commands.

namespaced: true
click_commands:
  - module: commands

Now you run manage --help and you can see all the commands in the same module will be namespaced by modulename_

$ manage --help
...
Commands:
  user_create_user    Create a new user
  system_clear_cache  Clear the system cache
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

And you can even customize namespace for each module separately

Note

If namespaced is true all commands will be namespaced, set it to false in order to define separately

click_commands:
  - module: commands.system
    namespace: sys
  - module: commands.user
    namespace: user

Now you run manage --help and you can see all the commands in the same module will be namespaced.

$ manage --help
...
Commands:
  user_create_user  Create a new user
  sys_clear_cache  Clear the system cache
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

2. Defining your inline commands in manage file directly

Sometimes your command is so simple that you do not want (or can't) have a custom module, so you can put all your commands in yaml file directly.

inline_commands:
  - name: clear_cache
    help_text: Executes inline code to clear the cache
    context:
      - sys
      - pprint
    options:
      --days:
        default: 100
    code: |
      pprint.pprint({'clean_days': days, 'path': sys.path})

Now running manage --help

$ manage --help
...
Commands:
  clear_cache  Executes inline code to clear the cache
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

And you can run using

$ manage clear_cache --days 15

3. Using general functions as commands

And if you already has some defined function (any callable works).

# my_system.functions.py
def create_user(name, password):
    print("Creating user %s" % name)
function_commands:
  - function: my_system.functions.create_user
    name: new_user
    help_text: Create new user
    options:
      --name:
        required: true
      --password:
        required: true

Now running manage --help

$ manage --help
...
Commands:
  new_user     Create new user
  debug        Shows the parsed manage file
  init         Initialize a manage shell in current...
  shell        Runs a Python shell with context

$ manage new_user --name=Bruno --password=1234
Creating user Bruno

Further Explanations

  • You can say, how this is useful?, There's no need to get a separate package and configure everything in yaml, just use iPython to do it. Besides, IPython configuration has a lot more options and capabilities.
  • So I say: Nice! If you don't like it, dont't use it!

Credits

Similar projects

Owner
Python Manage
Management Tool for Python
Python Manage
Rdwcli - Car list cli app with python

Rdwcli - Car list cli app with python

Arie Twigt 1 Feb 02, 2022
A simple python implementation of a reverse shell

llehs A python implementation of a reverse shell. Note for contributors The project is open for contributions and is hacktoberfest registered! llehs u

Archisman Ghosh 2 Jul 05, 2022
topalias - Linux alias generator from bash/zsh command history with statistics, written on Python.

topalias topalias - Linux alias generator from bash/zsh command history with statistics, written on Python. Features Generate short alias for popular

Sergey Chudakov 38 May 26, 2022
The Pythone Script will generate a (.)sh file with reverse shell codes then you can execute the script on the target

Pythone Script will generate a (.)sh file with reverse shell codes then you can execute the script on the targetPythone Script will generate a (.)sh file with reverse shell codes then you can execute

Boy From Future 15 Sep 16, 2022
Get latest astronomy job and rumor news in your command line

astrojobs Tired of checking the AAS job register and astro rumor mill for job news? Get the latest updates in the command line! astrojobs automaticall

Philip Mocz 19 Jul 20, 2022
A super simple wallet application for the NANO cryptocurrency that runs in the terminal

Nano Terminal Wallet A super simple wallet application for the NANO cryptocurrency that runs in the terminal Written in 2021 by NinjaSnail1080 (Discor

9 Jul 22, 2022
🦎 A NeoVim plugin for highlighting visual selections like in a normal document editor!

🦎 HighStr.nvim A NeoVim plugin for highlighting visual selections like in a normal document editor! Demo TL;DR HighStr.nvim is a NeoVim plugin writte

Pocco81 222 Jan 03, 2023
frogtrade9000 - a command-line Rich client for the freqtrade REST API

frogtrade9000 - a command-line Rich client for the freqtrade REST API I found FreqUI too cumbersome and slow on my Raspberry Pi 400 when running multi

Robert Davey 79 Dec 02, 2022
liquidctl – liquid cooler control Cross-platform tool and drivers for liquid coolers and other devices

Cross-platform CLI and Python drivers for AIO liquid coolers and other devices

1.7k Jan 08, 2023
Tools crack instagram + fb ayok dicoba keburu premium 😁

FITUR INSTALLASI [1] pkg update && pkg upgrade [2] pkg install git [3] pkg install python [4] pkg install python2 [5] pkg install nano [6]

Jeeck 1 Dec 11, 2021
CmdTube is a Python CLI library for searching, downloading, and watching YouTube tutorials

CmdTube is a Python CLI library for searching, downloading, and watching YouTube tutorials. This library was made with programmers in mind and it's dedicated to every programmer who watches YouTube v

Samuel Ayomide Ogunleke 2 Aug 22, 2022
Objexplore is an interactive Python object explorer for the terminal.

Objexplore is an interactive Python object explorer for the terminal. Use it while debugging, or exploring a new library, or whatever! 9D1FAC73-B2A5-4

kylepollina 249 Dec 23, 2022
Dynamically Generate GitHub Stats as like Terminal Interface

GitHub Stats Terminal Style Dynamically Generate GitHub Stats as like Terminal Interface Usage Create a New Repository using this Template or click he

YOGESHWARAN R 63 Jan 03, 2023
keep your machine's shell history synchronize

SyncShell Yet another tool for laziness Keep your machine's shell history synchronize Get SyncShell Currently, SyncShell is just available on PyPi and

Masoud Ghorbani 53 Dec 12, 2022
A simple cli tool to commit Conventional Commits

convmoji A simple cli tool to commit Conventional Commits. Requirements Install pip install convmoji convmoji --help Examples A conventianal commit co

3 Jul 04, 2022
TermPair lets developers securely share and control terminals in real timeπŸ”’

View and control terminals from your browser with end-to-end encryption πŸ”’

Chad Smith 1.5k Jan 05, 2023
CLI client for RFC 4226's HOTP and RFC 6238's TOTP.

One Time Password (OTP, TOTP/HOTP) OTP serves as additional protection in case of password leaks. onetimepass allows you to manage OTP codes and gener

Apptension 4 Jan 05, 2022
A simple note taker CLI program written in python

note-taker A simple note taker program written in python This allows you to snip your todo's, notes, and your tasks easily without extra charges Requi

marcusz 4 Nov 02, 2021
instant coding answers via the command line

howdoi instant coding answers via the command line Sherlock, your neighborhood command-line sloth sleuth. Are you a hack programmer? Do you find yours

Benjamin Gleitzman 9.8k Jan 08, 2023
Task-manager-CLI with Priority Modification

Task-manager-CLI with Priority Modification The functions for the app have been written in task.py file. 1. Install Node.js This project requires Node

1 Jan 21, 2022