def __init__(self, mongrel2_pair=None, handler_tuples=None, pool=None, no_handler=None, base_handler=None, template_loader=None, log_level=logging.INFO, login_url=None, db_conn=None, cookie_secret=None, *args, **kwargs): """Brubeck is a class for managing connections to Mongrel2 servers while providing an asynchronous system for managing message handling. mongrel2_pair should be a 2-tuple consisting of the pull socket address and the pub socket address for communicating with Mongrel2. Brubeck creates and manages a Mongrel2Connection instance from there. handler_tuples is a list of two-tuples. The first item is a regex for matching the URL requested. The second is the class instantiated to handle the message. """ # All output is sent via logging # (while i figure out how to do a good abstraction via zmq) logging.basicConfig(level=log_level) # Log whether we're using eventlet or gevent. logging.info('Using coroutine library: %s' % CORO_LIBRARY) # A Mongrel2Connection is currently just a way to manage # the sockets we need to open with a Mongrel2 instance and # identify this particular Brubeck instance as the sender if mongrel2_pair is not None: (pull_addr, pub_addr) = mongrel2_pair self.m2conn = Mongrel2Connection(pull_addr, pub_addr) else: raise ValueError('No mongrel2 connection possible.') # Class based route lists should be handled this way. # It is also possible to use `add_route`, a decorator provided by a # brubeck instance, that can extend routing tables. self.handler_tuples = handler_tuples if self.handler_tuples is not None: self.init_routes(handler_tuples) # We can accept an existing pool or initialize a new pool if pool is None: self.pool = coro_pool() elif callable(pool): self.pool = pool() else: raise ValueError('Unable to initialize coroutine pool') # Set a base_handler for handling errors (eg. 404 handler) self.base_handler = base_handler if self.base_handler is None: self.base_handler = WebMessageHandler # A database connection is optional. The var name is now in place self.db_conn = db_conn # Login url is optional self.login_url = login_url # This must be set to use secure cookies self.cookie_secret = cookie_secret # Any template engine can be used. Brubeck just needs a function that # loads the environment without arguments. if callable(template_loader): loaded_env = template_loader() if loaded_env: self.template_env = loaded_env else: raise ValueError('template_env failed to load.')
def __init__(self, msg_conn=None, handler_tuples=None, pool=None, no_handler=None, base_handler=None, template_loader=None, log_level=logging.INFO, login_url=None, db_conn=None, cookie_secret=None, api_base_url=None, *args, **kwargs): """Brubeck is a class for managing connections to webservers. It supports Mongrel2 and WSGI while providing an asynchronous system for managing message handling. `msg_conn` should be a `connections.Connection` instance. `handler_tuples` is a list of two-tuples. The first item is a regex for matching the URL requested. The second is the class instantiated to handle the message. `pool` can be an existing coroutine pool, but one will be generated if one isn't provided. `base_handler` is a class that Brubeck can rely on for implementing error handling functions. `template_loader` is a function that builds the template loading environment. `log_level` is a log level mapping to Python's `logging` module's levels. `login_url` is the default URL for a login screen. `db_conn` is a database connection to be shared in this process `cookie_secret` is a string to use for signing secure cookies. """ # All output is sent via logging # (while i figure out how to do a good abstraction via zmq) logging.basicConfig(level=log_level) # Log whether we're using eventlet or gevent. logging.info('Using coroutine library: %s' % CORO_LIBRARY) # Attach the web server connection if msg_conn is not None: self.msg_conn = msg_conn else: raise ValueError('No web server connection provided.') # Class based route lists should be handled this way. # It is also possible to use `add_route`, a decorator provided by a # brubeck instance, that can extend routing tables. self.handler_tuples = handler_tuples if self.handler_tuples is not None: self.init_routes(handler_tuples) # We can accept an existing pool or initialize a new pool if pool is None: self.pool = coro_pool() elif callable(pool): self.pool = pool() else: raise ValueError('Unable to initialize coroutine pool') # Set a base_handler for handling errors (eg. 404 handler) self.base_handler = base_handler if self.base_handler is None: self.base_handler = WebMessageHandler # A database connection is optional. The var name is now in place self.db_conn = db_conn # Login url is optional self.login_url = login_url # API base url is optional if api_base_url is None: self.api_base_url = '/' else: self.api_base_url = api_base_url # This must be set to use secure cookies self.cookie_secret = cookie_secret # Any template engine can be used. Brubeck just needs a function that # loads the environment without arguments. # # It then creates a function that renders templates with the given # environment and attaches it to self. if callable(template_loader): loaded_env = template_loader() if loaded_env: self.template_env = loaded_env # Create template rendering function def render_template(template_file, **context): """Renders template using provided template environment. """ if hasattr(self, 'template_env'): t_env = self.template_env template = t_env.get_template(template_file) body = template.render(**context or {}) return body # Attach it to brubeck app (self) setattr(self, 'render_template', render_template) else: raise ValueError('template_env failed to load.')