Skip to content

Instantly share code, notes, and snippets.

@derks
Created April 27, 2012 05:01
Show Gist options
  • Save derks/2506009 to your computer and use it in GitHub Desktop.
Save derks/2506009 to your computer and use it in GitHub Desktop.
cement2 + drest
import drest
from cement2.core import foundation, controller
class VTController(controller.CementBaseController):
class Meta:
label = 'base'
arguments = [
( ['-f', '--foo'], dict(help='foo option', dest='foo'))
]
config_section = 'base'
config_defaults = dict(
foo='bar',
api_endpoint='http://localhost:8000/api/v1/',
)
def __init__(self, *args, **kw):
super(VTController, self).__init__(*args, **kw)
self.api = None
def _setup(self, app_obj):
super(VTController, self)._setup(app_obj)
# create the api using your config [base] -> api_endpoint setting
self.api = drest.api.TastyPieAPI(
self.app.config.get('base', 'api_endpoint')
)
@controller.expose(help='this is the default command', hide=True)
def default(self):
print('Inside base.default.')
# prints foo config option, overridden by --foo
print self.app.config.get('base', 'foo')
# checks that --foo was passed
if self.app.pargs.foo:
print('--foo option was passed')
@controller.expose(help='some other command')
def my_command(self):
pass
class VTApp(foundation.CementApp):
class Meta:
label = 'version_tracker'
base_controller = VTController
try:
vt = VTApp()
vt.setup()
vt.run()
except drest.exc.dRestAPIError as e:
print(e.msg)
finally:
vt.close()
@derks
Copy link
Author

derks commented Apr 27, 2012

Config at ~/.version_tracker.conf

[base]
api_endpoint = http://localhost:8000/api/v1/

Run it:

$ python test.py 
Inside base.default.
bar


$ python test.py --foo asdfasfasdfa
Inside base.default.
asdfasfasdfa
--foo option was passed


$ python test.py --help
usage: test.py  -opt1 --opt2=VAL [arg1] [arg2] ...

Application Base Controller

commands:

  my-command
    some other command

optional arguments:
  -h, --help         show this help message and exit
  --debug            toggle debug output
  --quiet            suppress all output
  -f FOO, --foo FOO  foo option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment