comparison textshaper/commands.py @ 34:88a69d587326

round1 of commands
author Jeff Hammel <k0scist@gmail.com>
date Sun, 02 Mar 2014 15:33:07 -0800
parents de3148412191
children
comparison
equal deleted inserted replaced
33:de3148412191 34:88a69d587326
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
3 2
4 """ 3 """
5 CLI commands for textshaper 4 CLI commands for textshaper
6 """ 5 """
7 6
7 import inspect
8
8 class Shaper(object): 9 class Shaper(object):
9 """individual text shaper component""" 10 """
11 individual text shaper component
12 (function wrapper)
13 """
10 14
11 def __init__(self, function): 15 def __init__(self, function):
12 self.function = function 16 self.function = function
17 self.func_name = function.func_name
18
19 def __call__(self, text, **kwargs):
20 return self.function(text, **kwargs)
21
22 def __str__(self):
23 return self.func_name
24
13 25
14 class Commands(object): 26 class Commands(object):
15 27
16 def __init__(self): 28 def __init__(self):
17 self.shapers = [] 29 self.shapers = []
18 self.keys = {} 30 self.keys = {}
31 self.display_keys = []
19 32
20 def add(self, function, key=None): 33 def add(self, function, key=None):
21 self.shapers.append(Shaper(function)) 34 self.shapers.append(Shaper(function))
22 if key: 35 if not key:
23 self.keys[key] = self.shapers[-1] 36 key = str(self.shapers[-1])
37 key = key.lower()
38 self.keys[key] = self.shapers[-1]
39 name = str(self.shapers[-1]).lower()
40 if name.startswith(key):
41 display_name = '{}{}'.format(key, name[len(key):].upper())
42 else:
43 display_name = '{}:{}'.format(key, name.upper())
44 self.display_keys.append(display_name)
45
46 def call(self, key, text, **kwargs):
47 if key in self.keys:
48 return self.keys[key](text, **kwargs)
49
50 __call__ = call
51
52 def display(self):
53 return ' '.join(self.display_keys)