annotate textshaper/commands.py @ 57:a2bbe406f570 default tip

which is no longer maintained; roll our mediocre own
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 10:40:34 -0800
parents 88a69d587326
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 CLI commands for textshaper
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
7 import inspect
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
8
33
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 class Shaper(object):
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
10 """
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
11 individual text shaper component
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
12 (function wrapper)
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
13 """
33
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def __init__(self, function):
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 self.function = function
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
17 self.func_name = function.func_name
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
18
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
19 def __call__(self, text, **kwargs):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
20 return self.function(text, **kwargs)
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
21
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
22 def __str__(self):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
23 return self.func_name
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
24
33
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 class Commands(object):
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 def __init__(self):
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 self.shapers = []
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 self.keys = {}
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
31 self.display_keys = []
33
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 def add(self, function, key=None):
de3148412191 STUB: textshaper/commands.py
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 self.shapers.append(Shaper(function))
34
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
35 if not key:
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
36 key = str(self.shapers[-1])
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
37 key = key.lower()
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
38 self.keys[key] = self.shapers[-1]
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
39 name = str(self.shapers[-1]).lower()
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
40 if name.startswith(key):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
41 display_name = '{}{}'.format(key, name[len(key):].upper())
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
42 else:
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
43 display_name = '{}:{}'.format(key, name.upper())
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
44 self.display_keys.append(display_name)
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
45
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
46 def call(self, key, text, **kwargs):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
47 if key in self.keys:
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
48 return self.keys[key](text, **kwargs)
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
49
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
50 __call__ = call
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
51
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
52 def display(self):
88a69d587326 round1 of commands
Jeff Hammel <k0scist@gmail.com>
parents: 33
diff changeset
53 return ' '.join(self.display_keys)