mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
110: Started fleshing out more in cli.py
This commit is contained in:
parent
5313e5a491
commit
9282418291
6
ipa
6
ipa
@ -27,6 +27,12 @@ Just proof of concept stuff in here right now.
|
||||
|
||||
import sys
|
||||
from ipalib.startup import api
|
||||
from ipalib.cli import CLI
|
||||
|
||||
cli = CLI(api)
|
||||
cli.run()
|
||||
|
||||
sys.exit()
|
||||
|
||||
TAB_WIDTH = 2
|
||||
|
||||
|
@ -21,6 +21,8 @@
|
||||
Functionality for Command Line Inteface.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
def to_cli(name):
|
||||
"""
|
||||
@ -38,3 +40,32 @@ def from_cli(cli_name):
|
||||
"""
|
||||
assert isinstance(cli_name, basestring)
|
||||
return cli_name.replace('-', '_')
|
||||
|
||||
|
||||
class CLI(object):
|
||||
def __init__(self, api):
|
||||
self.__api = api
|
||||
|
||||
def __get_api(self):
|
||||
return self.__api
|
||||
api = property(__get_api)
|
||||
|
||||
def print_commands(self):
|
||||
for cmd in self.api.cmd:
|
||||
print to_cli(cmd.name)
|
||||
|
||||
def run(self):
|
||||
if len(sys.argv) < 2:
|
||||
self.print_commands()
|
||||
print 'Usage: ipa COMMAND [OPTIONS]'
|
||||
sys.exit(2)
|
||||
return
|
||||
name= sys.argv[1]
|
||||
if name == '_api_':
|
||||
print_api()
|
||||
sys.exit()
|
||||
elif name not in api.cmd:
|
||||
print_commands()
|
||||
print 'ipa: ERROR: unknown command %r' % name
|
||||
sys.exit(2)
|
||||
api.cmd[name]()
|
||||
|
@ -199,7 +199,7 @@ class cmd(plugable.Plugin):
|
||||
if key in self.options:
|
||||
self.options[key].validate(value)
|
||||
|
||||
def execute(self, **kw)
|
||||
def execute(self, **kw):
|
||||
pass
|
||||
|
||||
def print_n_call(self, method, kw):
|
||||
@ -214,8 +214,7 @@ class cmd(plugable.Plugin):
|
||||
kw = self.normalize(**kw)
|
||||
kw.update(self.default(**kw))
|
||||
self.validate(**kw)
|
||||
self.execute(**kw)
|
||||
|
||||
return self.execute(**kw)
|
||||
|
||||
|
||||
class obj(plugable.Plugin):
|
||||
|
@ -21,6 +21,7 @@
|
||||
Unit tests for `ipalib.cli` module.
|
||||
"""
|
||||
|
||||
from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker
|
||||
from ipalib import cli
|
||||
|
||||
|
||||
@ -34,3 +35,21 @@ def test_from_cli():
|
||||
f = cli.from_cli
|
||||
assert f('initialize') == 'initialize'
|
||||
assert f('user-add') == 'user_add'
|
||||
|
||||
|
||||
class test_CLI(ClassChecker):
|
||||
"""
|
||||
Tests the `CLI` class.
|
||||
"""
|
||||
_cls = cli.CLI
|
||||
|
||||
def test_class(self):
|
||||
assert type(self.cls.api) is property
|
||||
|
||||
def test_api(self):
|
||||
"""
|
||||
Tests the `api` property.
|
||||
"""
|
||||
api = 'the plugable.API instance'
|
||||
o = self.cls(api)
|
||||
assert read_only(o, 'api') is api
|
||||
|
@ -267,6 +267,13 @@ class test_cmd(ClassChecker):
|
||||
sub.validate(**okay)
|
||||
raises(errors.RuleError, sub.validate, **fail)
|
||||
|
||||
def test_execute(self):
|
||||
"""
|
||||
Tests the `execute` method.
|
||||
"""
|
||||
assert 'execute' in self.cls.__public__ # Public
|
||||
|
||||
|
||||
|
||||
def test_obj():
|
||||
cls = public.obj
|
||||
|
Loading…
Reference in New Issue
Block a user