def create_app(debug=False): """Returns the Mushi API application instance.""" app = factory.create_app(__name__, __path__, debug) # set the default json encoder app.json_encoder = JSONDatetimeEncoder # set the error handlers app.errorhandler(ApiError)(handle_api_exception) app.errorhandler(AuthenticationError)(handle_403) app.errorhandler(404)(handle_404) @app.route('/') def version(): return jsonify({'version': '0.1'}) return app
def __call__(self): # Create an application context. app = create_app(__name__, []) ctx = app.test_request_context() ctx.push() parser = argparse.ArgumentParser( prog=self.argv[0], description="Manage the user's account.") subparsers = parser.add_subparsers(dest='subcommand') subparsers.required = True sub = subparsers.add_parser('add', help='add a user') sub.add_argument('email', action='store', help="the email of the new user's account") sub.add_argument( '-n', '--name', dest='name', action='store', help='the full name of the user (default: email address)') sub.add_argument( '-p', '--password', dest='password', action='store', help='the full name of the user (will be asked if not provided)') sub = subparsers.add_parser('list', help='list users') args = parser.parse_args(self.argv[1:]) if args.subcommand == 'add': new_user = User() new_user.email = args.email new_user.name = args.name or args.email if args.password: password = args.password else: password = getpass('password: '******'confirm: ') != password: raise InvalidArgumentError('Password do not match.') new_user.password = md5(password.encode()).hexdigest() db_session.add(new_user) db_session.commit() elif args.subcommand == 'list': for user in db_session.query(User): print('name: {:>15}, email: {:>15}'.format(user.name, user.email)) ctx.pop()
def create_app(debug=False): """Returns the mikata API application instance.""" app = factory.create_app(__name__, __path__, debug) def inject_auth_user(): token = parse_auth_token() if token is None: return {} return {"auth_user": token.owner} app.context_processor(inject_auth_user) app.errorhandler(403)(handle_403) app.errorhandler(InvalidTokenError)(handle_403) app.errorhandler(404)(handle_404) return app
def __call__(self): # create an application context app = create_app(__name__, []) ctx = app.test_request_context() ctx.push() parser = argparse.ArgumentParser( prog=self.argv[0], description='Manage the database of Mikata.') subparsers = parser.add_subparsers(dest='subcommand') subparsers.required = True subparsers.add_parser('sync', help='synchronize the database') args = parser.parse_args(self.argv[1:]) if args.subcommand == 'sync': db_sync() ctx.pop()
def __call__(self): # create an application context app = create_app(__name__, []) ctx = app.test_request_context() ctx.push() # set up a dictionary to serve as the environment for the shell imported_objects = {} # try activating rlcompleter try: import readline except ImportError: pass else: import rlcompleter readline.set_completer(rlcompleter.Completer(imported_objects).complete) readline.parse_and_bind("tab:complete") code.interact(local=imported_objects)