33
|
1 # -*- coding: utf-8 -*-
|
|
2
|
|
3 """
|
|
4 CLI commands for textshaper
|
|
5 """
|
|
6
|
34
|
7 import inspect
|
|
8
|
33
|
9 class Shaper(object):
|
34
|
10 """
|
|
11 individual text shaper component
|
|
12 (function wrapper)
|
|
13 """
|
33
|
14
|
|
15 def __init__(self, function):
|
|
16 self.function = function
|
34
|
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
|
33
|
25
|
|
26 class Commands(object):
|
|
27
|
|
28 def __init__(self):
|
|
29 self.shapers = []
|
|
30 self.keys = {}
|
34
|
31 self.display_keys = []
|
33
|
32
|
|
33 def add(self, function, key=None):
|
|
34 self.shapers.append(Shaper(function))
|
34
|
35 if not key:
|
|
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)
|