Skip to content

Commit

Permalink
New Command module
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Jan 10, 2015
1 parent 0a7f73b commit e392604
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
5 changes: 3 additions & 2 deletions code/python/echomesh/util/Importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ def importer(classpath):
except ImportError:
# Try to import a function out of a module.
parts = classpath.split('.')
function = classpath.pop()
return getattr(importlib.import_module(classpath), function)
function = parts.pop()

return getattr(importlib.import_module('.'.join(parts)), function)
53 changes: 53 additions & 0 deletions code/python/echomesh/util/command/Command.py
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', '')
23 changes: 23 additions & 0 deletions code/python/echomesh/util/command/Command_test.py
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.
4 changes: 4 additions & 0 deletions code/python/echomesh/util/command/test/Bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def foo():
return 'bar'

COMMAND = foo
10 changes: 10 additions & 0 deletions code/python/echomesh/util/command/test/Baz.py
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'
4 changes: 4 additions & 0 deletions code/python/echomesh/util/command/test/Foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def foo():
return 'foo'

HELP = 'Some help text'
1 change: 1 addition & 0 deletions code/python/echomesh/util/command/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = 'Foo', 'Bar'

0 comments on commit e392604

Please sign in to comment.