Created
April 27, 2012 05:01
-
-
Save derks/2506009 to your computer and use it in GitHub Desktop.
cement2 + drest
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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Config at ~/.version_tracker.conf
Run it: