def test_call_with_literal_dict(self): from textwrap import dedent conf = dedent(""" [my] value = dict(**{'foo': 'bar'}) """) fp = compat.StringIO(conf) cherrypy.config.update(fp) self.assertEqual(cherrypy.config['my']['value'], {'foo': 'bar'})
def test_call_with_kwargs(self): from textwrap import dedent conf = dedent(""" [my] value = dict(foo="buzz", **cherrypy._test_dict) """) test_dict = {"foo": "bar", "bar": "foo", "fizz": "buzz"} cherrypy._test_dict = test_dict fp = compat.StringIO(conf) cherrypy.config.update(fp) test_dict['foo'] = 'buzz' self.assertEqual(cherrypy.config['my']['value']['foo'], 'buzz') self.assertEqual(cherrypy.config['my']['value'], test_dict) del cherrypy._test_dict
def test_config(self): from textwrap import dedent # variable substitution with [DEFAULT] conf = dedent(""" [DEFAULT] dir = "/some/dir" my.dir = %(dir)s + "/sub" [my] my.dir = %(dir)s + "/my/dir" my.dir2 = %(my.dir)s + '/dir2' """) fp = compat.StringIO(conf) cherrypy.config.update(fp) self.assertEqual(cherrypy.config["my"]["my.dir"], "/some/dir/my/dir") self.assertEqual(cherrypy.config["my"] ["my.dir2"], "/some/dir/my/dir/dir2")
def setup_server(): class Root: _cp_config = {'foo': 'this', 'bar': 'that'} def __init__(self): cherrypy.config.namespaces['db'] = self.db_namespace def db_namespace(self, k, v): if k == "scheme": self.db = v # @cherrypy.expose(alias=('global_', 'xyz')) def index(self, key): return cherrypy.request.config.get(key, "None") index = cherrypy.expose(index, alias=('global_', 'xyz')) def repr(self, key): return repr(cherrypy.request.config.get(key, None)) repr.exposed = True def dbscheme(self): return self.db dbscheme.exposed = True def plain(self, x): return x plain.exposed = True plain._cp_config = {'request.body.attempt_charsets': ['utf-16']} favicon_ico = cherrypy.tools.staticfile.handler( filename=os.path.join(localDir, '../favicon.ico')) class Foo: _cp_config = {'foo': 'this2', 'baz': 'that2'} def index(self, key): return cherrypy.request.config.get(key, "None") index.exposed = True nex = index def silly(self): return 'Hello world' silly.exposed = True silly._cp_config = {'response.headers.X-silly': 'sillyval'} # Test the expose and config decorators #@cherrypy.expose #@cherrypy.config(foo='this3', **{'bax': 'this4'}) def bar(self, key): return repr(cherrypy.request.config.get(key, None)) bar.exposed = True bar._cp_config = {'foo': 'this3', 'bax': 'this4'} class Another: def index(self, key): return str(cherrypy.request.config.get(key, "None")) index.exposed = True def raw_namespace(key, value): if key == 'input.map': handler = cherrypy.request.handler def wrapper(): params = cherrypy.request.params for name, coercer in list(value.items()): try: params[name] = coercer(params[name]) except KeyError: pass return handler() cherrypy.request.handler = wrapper elif key == 'output': handler = cherrypy.request.handler def wrapper(): # 'value' is a type (like int or str). return value(handler()) cherrypy.request.handler = wrapper class Raw: _cp_config = {'raw.output': repr} def incr(self, num): return num + 1 incr.exposed = True incr._cp_config = {'raw.input.map': {'num': int}} if not compat.py3k: thing3 = "thing3: unicode('test', errors='ignore')" else: thing3 = '' ioconf = compat.StringIO(""" [/] neg: -1234 filename: os.path.join(sys.prefix, "hello.py") thing1: cherrypy.lib.httputil.response_codes[404] thing2: __import__('cherrypy.tutorial', globals(), locals(), ['']).thing2 %s complex: 3+2j mul: 6*3 ones: "11" twos: "22" stradd: %%(ones)s + %%(twos)s + "33" [/favicon.ico] tools.staticfile.filename = %r """ % (thing3, os.path.join(localDir, 'static/dirback.jpg'))) root = Root() root.foo = Foo() root.raw = Raw() app = cherrypy.tree.mount(root, config=ioconf) app.request_class.namespaces['raw'] = raw_namespace cherrypy.tree.mount(Another(), "/another") cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove', 'db.scheme': r"sqlite///memory", })