Mercurial > hg > TextShaper
diff 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 |
line wrap: on
line diff
--- a/textshaper/commands.py Sun Feb 23 16:48:09 2014 -0800 +++ b/textshaper/commands.py Sun Mar 02 15:33:07 2014 -0800 @@ -1,23 +1,53 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- """ CLI commands for textshaper """ +import inspect + class Shaper(object): - """individual text shaper component""" + """ + individual text shaper component + (function wrapper) + """ def __init__(self, function): self.function = function + self.func_name = function.func_name + + def __call__(self, text, **kwargs): + return self.function(text, **kwargs) + + def __str__(self): + return self.func_name + class Commands(object): def __init__(self): self.shapers = [] self.keys = {} + self.display_keys = [] def add(self, function, key=None): self.shapers.append(Shaper(function)) - if key: - self.keys[key] = self.shapers[-1] + if not key: + key = str(self.shapers[-1]) + key = key.lower() + self.keys[key] = self.shapers[-1] + name = str(self.shapers[-1]).lower() + if name.startswith(key): + display_name = '{}{}'.format(key, name[len(key):].upper()) + else: + display_name = '{}:{}'.format(key, name.upper()) + self.display_keys.append(display_name) + + def call(self, key, text, **kwargs): + if key in self.keys: + return self.keys[key](text, **kwargs) + + __call__ = call + + def display(self): + return ' '.join(self.display_keys)