1. Sylvain Hellegouarch
  2. CherryPy Recipes

Source

CherryPy Recipes / testing / functional / robotframework_ / CherryPyLib.py

# -*- coding: utf-8 -*-
import imp
import os, os.path
 
import cherrypy

__all__ = ['CherryPyLib']
 
class CherryPyLib(object):
    def setup_cherrypy(self, conf_file=None):
        """ Configure the CherryPy engine and server using
        the built-in ``'embedded'`` environment mode.
 
        If provided, ``conf_file`` is a path to a CherryPy
        configuration file used in addition.

        Example:
        | Setup CherryPy |
        | Setup CherryPy | /path/to/server.conf |
        """
        cherrypy.config.update({"environment": "embedded"})
        if conf_file:
            cherrypy.config.update(conf_file)            
 
    def start_cherrypy(self):
        """ Start a CherryPy engine.

        Example:
        | Start CherryPy |
        """
        cherrypy.engine.start()
 
    def exit_cherrypy(self):
        """ Terminate a CherryPy engine.
        
        Example:
        | Exit CherryPy |
        """
        cherrypy.engine.exit()
 
    def mount_application(self, appmod, appcls, path='/', config=None, directory=None):
        """ Mount an application to be tested. ``appmod`` is 
        a Python module containing ``appcls``. The module is
        imported from the given directory. If not provided, we use
        the current directory instead.

        You may provide a ``config`` to the application either as
        a dictionary of a path to the config file.

        Returns an instance of ``cherypy.Application`` and
        raises ``ImportError`` if it failed importing it.
        
        Example:
        | ${app} | Mount Application | myapp | Root | / | /path/to/app.conf |
        """
        directory = directory or os.getcwd()
        file, filename, description = imp.find_module(appmod, [directory])
        mod = imp.load_module(appmod, file, filename, description)
        if hasattr(mod, appcls):
            cls = getattr(mod, appcls)
            app = cls()
            cherrypy.tree.mount(app, path, config)
        else:
            raise ImportError("cannot import name %s from %s" % (appcls, appmod))

        return app