Skip to content

Commit

Permalink
Added more examples to examples folder
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jun 16, 2016
1 parent e2d3206 commit c0cdf7b
Show file tree
Hide file tree
Showing 24 changed files with 310 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ target/

/manage.yml
/.manage.yml
/.bck_manage.yml
31 changes: 19 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ e.g: A command to add users to your system::
$ manage create_user --name=Bruno --passwd=1234
Creating the user...

**manage** has two ways for you to define custom commands
**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 **general functions** defined anywhere in your project,
and if really needed can define new functions inside the **manage.yml** file

1. Using a custom command module (single 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

Expand Down Expand Up @@ -112,8 +115,8 @@ Now you run **manage --help**
shell Runs a Python shell with context
Using a commands package (multiple files)
-----------------------------------------
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.
Expand Down Expand Up @@ -160,8 +163,8 @@ Now you run **manage --help** and you have commands from both modules
init Initialize a manage shell in current...
shell Runs a Python shell with context
Custom command names
--------------------
Custom click_command names
--------------------------

Sometimes the name of commands differ from the name of the function so you can
customize it.
Expand All @@ -170,11 +173,15 @@ customize it.
commands:
- module: commands.system
names:
clear_cache: reset_cache
config:
clear_cache:
name: reset_cache
help_text: This resets the cache
- module: commands.user
names:
create_user: new_user
config:
create_user:
name: new_user
help_text: This creates new user
Having different namespaces
---------------------------
Expand All @@ -184,9 +191,9 @@ you can user namespaced commands.

.. code-block:: yaml
namespaced: true
commands:
- module: commands
namespaced: true
Now you run **manage --help** and you can see all the commands in the same module will be namespaced by **modulename_**

Expand Down
22 changes: 22 additions & 0 deletions examples/custom_command_names/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
click_commands:
- module: my_system.user
config:
create_user:
name: new_user
help_text: New user will be created
- module: my_system.group
config:
create_group:
name: new_group
help_text: New group will be created
help_text: This is the {project_name} interactive shell
project_name: Custom Named Commands
shell:
auto_import:
display: true
objects: {}
banner:
enabled: true
message: Manage Interactive Shell! edit your `manage.yml`
init_script: print("Starting interactive shell!")
readline_enabled: true
Empty file.
15 changes: 15 additions & 0 deletions examples/custom_command_names/my_system/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
def create_group(name):
"""Create a new group"""
click.echo('Creating the group %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
16 changes: 16 additions & 0 deletions examples/custom_command_names/my_system/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
20 changes: 20 additions & 0 deletions examples/custom_namespaces/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
click_commands:
- module: my_system.user
namespace: bar
- module: my_system.group
namespace: foo
config:
create_group:
name: new_group
help_text: New group will be created
help_text: This is the {project_name} interactive shell
project_name: Custom Named Commands
shell:
auto_import:
display: true
objects: {}
banner:
enabled: true
message: Manage Interactive Shell! edit your `manage.yml`
init_script: print("Starting interactive shell!")
readline_enabled: true
Empty file.
15 changes: 15 additions & 0 deletions examples/custom_namespaces/my_system/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
def create_group(name):
"""Create a new group"""
click.echo('Creating the group %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
16 changes: 16 additions & 0 deletions examples/custom_namespaces/my_system/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
13 changes: 13 additions & 0 deletions examples/multiple_commands/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
commands:
- module: my_system
help_text: This is the {project_name} interactive shell
project_name: Multiple With Commands
shell:
auto_import:
display: true
objects: {}
banner:
enabled: true
message: Manage Interactive Shell! edit your `manage.yml`
init_script: print("Starting interactive shell!")
readline_enabled: true
Empty file.
15 changes: 15 additions & 0 deletions examples/multiple_commands/my_system/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
def create_group(name):
"""Create a new group"""
click.echo('Creating the group %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
16 changes: 16 additions & 0 deletions examples/multiple_commands/my_system/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
19 changes: 19 additions & 0 deletions examples/namespaced_commands/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespaced: true
click_commands:
- module: my_system.user
- module: my_system.group
config:
create_group:
name: new_group
help_text: New group will be created
help_text: This is the {project_name} interactive shell
project_name: namespaced Commands
shell:
auto_import:
display: true
objects: {}
banner:
enabled: true
message: Manage Interactive Shell! edit your `manage.yml`
init_script: print("Starting interactive shell!")
readline_enabled: true
Empty file.
15 changes: 15 additions & 0 deletions examples/namespaced_commands/my_system/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
def create_group(name):
"""Create a new group"""
click.echo('Creating the group %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
16 changes: 16 additions & 0 deletions examples/namespaced_commands/my_system/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
13 changes: 13 additions & 0 deletions examples/simple_commands/manage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
commands:
- module: my_system.commands
help_text: This is the {project_name} interactive shell
project_name: Simple With Commands
shell:
auto_import:
display: true
objects: {}
banner:
enabled: true
message: Manage Interactive Shell! edit your `manage.yml`
init_script: print("Starting interactive shell!")
readline_enabled: true
Empty file.
23 changes: 23 additions & 0 deletions examples/simple_commands/my_system/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8

import click


@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user %s' % name)


@click.command()
@click.option('--name')
def create_group(name):
"""Create a new group"""
click.echo('Creating the group %s' % name)


def this_is_not_command(foo, bar):
"""Will not be added"""
pass
19 changes: 7 additions & 12 deletions manage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import os
import sys
import copy
import code
import json
import yaml
Expand All @@ -10,6 +11,7 @@
import rlcompleter
from manage.template import default_manage_dict
from manage.auto_import import import_objects, exec_init, exec_init_script
from manage.commands_collector import load_commands

MANAGE_FILE = 'manage.yml'
HIDDEN_MANAGE_FILE = '.{0}'.format(MANAGE_FILE)
Expand All @@ -26,15 +28,14 @@ def load_manage_dict(filename=None):
elif os.path.exists(HIDDEN_MANAGE_FILE):
manage_filename = HIDDEN_MANAGE_FILE
else:
default_manage_dict['shell']['banner']['message'] = (
MANAGE_DICT.update(copy.deepcopy(default_manage_dict))
MANAGE_DICT['shell']['banner']['message'] = (
"WARNING: This is not a managed project\n"
"\tPlease `exit()` and \n"
"\trun `$ manage init`\n"
"\tand edit `manage.yml` file with desired options"
)
default_manage_dict['shell']['auto_import']['display'] = False
MANAGE_DICT.update(default_manage_dict)

MANAGE_DICT['shell']['auto_import']['display'] = False
if manage_filename:
with open(manage_filename) as manage_file:
MANAGE_DICT.update(yaml.load(manage_file))
Expand Down Expand Up @@ -155,12 +156,6 @@ def shell(config, ipython, ptpython):
shell.interact(banner=banner_msg)


@click.command()
def foo():
"""do nothing"""
print("foo")


def load_manage_dict_from_sys_args():
single_option = [item for item in sys.argv if '--filename=' in item]
if single_option:
Expand All @@ -173,12 +168,12 @@ def load_manage_dict_from_sys_args():


def main():
sys.path.insert(0, '.')
load_manage_dict_from_sys_args()
cli.help = MANAGE_DICT.get(
'help_text', '{project_name} Interactive shell!'
).format(**MANAGE_DICT)
# TODO: Load commands
# cli.add_command(foo, name='bla')
load_commands(cli, MANAGE_DICT)
return cli()


Expand Down

0 comments on commit c0cdf7b

Please sign in to comment.