Created
January 11, 2011 16:39
-
-
Save chuangbo/774683 to your computer and use it in GitHub Desktop.
chuangbo's decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf8 -*- | |
_actions = [] | |
def action(f): | |
"""Decorator to register an action.""" | |
_actions.append(f) | |
return f | |
@action | |
def install(): | |
"""Setup everything.""" | |
pass | |
@action | |
def help(name=None): | |
"""Show this help.""" | |
a = name and find_action(name) | |
print "Project xxx Help" | |
print "" | |
if a: | |
print " %s\t%s" % (a.__name__, a.__doc__) | |
else: | |
print "Available actions" | |
for a in _actions: | |
print " %s\t%s" % (a.__name__, a.__doc__) | |
def find_action(name): | |
for action in _actions: | |
if action.__name__ == name: | |
return action | |
def run_action(name, args): | |
a = find_action(name) | |
if a: | |
a(*args) | |
else: | |
print >> sys.stderr, "unknown command", name | |
help() | |
if __name__ == '__main__': | |
import sys | |
run_action(sys.argv[1], sys.argv[2:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment