forked from openannotation/annotator-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cli][s]: give info about available commands (copy and paste datautil…
…/clitools.py code).
- Loading branch information
rgrp
committed
Mar 25, 2011
1 parent
7f5de6c
commit 6d5499e
Showing
1 changed file
with
46 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import os | ||
import sys | ||
import optparse | ||
import inspect | ||
|
||
from annotator.app import app, setup_app | ||
from annotator.app import setup_app as _setup_app | ||
import annotator.model as model | ||
|
||
|
||
def fixtures(): | ||
'''Create some fixtures (e.g. for demoing).''' | ||
acc = model.Account.get('tester') | ||
if acc is None: | ||
acc = model.Account( | ||
|
@@ -23,10 +27,46 @@ def fixtures(): | |
print 'Fixtures created ([email protected] / pass)' | ||
|
||
|
||
if __name__ == '__main__': | ||
setup_app() | ||
action = sys.argv[1] | ||
if action == 'fixtures': | ||
fixtures() | ||
def _module_functions(functions): | ||
local_functions = dict(functions) | ||
for k,v in local_functions.items(): | ||
if not inspect.isfunction(v) or k.startswith('_'): | ||
del local_functions[k] | ||
return local_functions | ||
|
||
def _main(functions_or_object): | ||
isobject = inspect.isclass(functions_or_object) | ||
if isobject: | ||
_methods = _object_methods(functions_or_object) | ||
else: | ||
_methods = _module_functions(functions_or_object) | ||
|
||
usage = '''%prog {action} | ||
Actions: | ||
''' | ||
usage += '\n '.join( | ||
[ '%s: %s' % (name, m.__doc__.split('\n')[0] if m.__doc__ else '') for (name,m) | ||
in sorted(_methods.items()) ]) | ||
parser = optparse.OptionParser(usage) | ||
# Optional: for a config file | ||
# parser.add_option('-c', '--config', dest='config', | ||
# help='Config file to use.') | ||
options, args = parser.parse_args() | ||
|
||
if not args or not args[0] in _methods: | ||
parser.print_help() | ||
sys.exit(1) | ||
|
||
method = args[0] | ||
if isobject: | ||
getattr(functions_or_object(), method)(*args[1:]) | ||
else: | ||
_methods[method](*args[1:]) | ||
|
||
__all__ = [ '_main' ] | ||
|
||
if __name__ == '__main__': | ||
_setup_app() | ||
_main(locals()) | ||
|