-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
import six | ||
|
||
from echomesh.base.GetPrefix import PrefixDict | ||
from echomesh.util.Importer import importer | ||
from echomesh.util.string.Formatted import Formatted | ||
|
||
class Command(Formatted): | ||
FORMAT_MEMBERS = 'name', 'help' | ||
|
||
def __init__(self, classpath, name, importer=importer): | ||
self.classpath = classpath | ||
self.name = name | ||
self.importer = importer | ||
self._load() | ||
|
||
def _load(self): | ||
mod = self.importer('%s.%s' % (self.classpath, self.name)) | ||
get = lambda key: getattr(mod, key, None) | ||
|
||
self.call = get('COMMAND') or get(self.name.lower()) | ||
assert (self.call) | ||
self.help = get('HELP') | ||
self._loaded = True | ||
|
||
def __call__(self, *args, **kwds): | ||
return self.call(*args, **kwds) | ||
|
||
|
||
class Registry(PrefixDict): | ||
def __init__(self, name, base_path, more_paths, | ||
allow_prefixes=True, importer=importer): | ||
super(Registry, self).__init__() | ||
self.allow_prefixes = allow_prefixes | ||
self.name = name | ||
if isinstance(more_paths, six.string_types): | ||
classpaths = (c.strip() for c in more_paths.split(':')) | ||
classpaths = [c for c in classpaths if c] | ||
else: | ||
classpaths = list(more_paths) | ||
classpaths.append(base_path) | ||
|
||
self.help_table = PrefixDict(name=name, allow_prefixes=allow_prefixes) | ||
for cp in reversed(classpaths): | ||
module = importer(cp) | ||
for name in getattr(module, '__all__'): | ||
command = Command(cp, name) | ||
self[name.lower()] = command | ||
self.help_table.update(getattr(module, 'HELP', {})) | ||
|
||
def help(self, key): | ||
return self.help_table.get(key) or getattr(self[key], 'HELP', '') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from echomesh.util.command import Command | ||
from echomesh.util.TestCase import TestCase | ||
|
||
class Command_test(TestCase): | ||
def test_simple(self): | ||
registry = Command.Registry('test', 'echomesh.util.command.test', '') | ||
foo = registry['f'] | ||
self.assertEquals(foo(), 'foo') | ||
self.assertEquals(registry.help('foo'), '') | ||
|
||
def test_two(self): | ||
registry = Command.Registry('test', 'echomesh.util.command.test', | ||
'echomesh.util.command.test.Baz') | ||
foo = registry['f'] | ||
self.assertEquals(foo(), 'foo') | ||
with self.assertRaises(KeyError) as cm: | ||
b = registry['ba'] | ||
self.assertEquals( | ||
str(cm.exception), | ||
'In table test, "ba" matches more than one: bar and baz') | ||
self.assertEquals(registry['baz'](), 'baz') |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def foo(): | ||
return 'bar' | ||
|
||
COMMAND = foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
__all__ = 'Baz', 'Bing' | ||
|
||
class Class(object): pass | ||
|
||
Baz = Class() | ||
Baz.baz = lambda: 'baz' | ||
Baz.HELP = 'Baz help' | ||
|
||
Bing = Class() | ||
Bing.COMMAND = lambda: 'bing' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def foo(): | ||
return 'foo' | ||
|
||
HELP = 'Some help text' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__all__ = 'Foo', 'Bar' |