Exemple #1
0
 def __init__(self,
              loader,
              list_apps=False,
              app_cache_size=100,
              debug=False):
     self.loader = loader
     self.list_apps = list_apps
     self._app_init_lock = Lock()
     self.apps = LRU(app_cache_size)
     self.debug = debug
Exemple #2
0
    def test_setitem_overflow(self):
        lru = LRU(2)
        lru['foo1'] = 1
        lru['foo2'] = 2
        lru['foo3'] = 3

        assert 'foo1' not in lru
        assert 'foo2' in lru
        assert 'foo3' in lru
Exemple #3
0
    def test_length(self):
        lru = LRU(2)
        eq_(len(lru), 0)
        lru['foo1'] = 1
        eq_(len(lru), 1)
        lru['foo2'] = 2
        eq_(len(lru), 2)
        lru['foo3'] = 3
        eq_(len(lru), 2)

        del lru['foo3']
        eq_(len(lru), 1)
    def test_length(self):
        lru = LRU(2)
        assert len(lru) == 0
        lru['foo1'] = 1
        assert len(lru) == 1
        lru['foo2'] = 2
        assert len(lru) == 2
        lru['foo3'] = 3
        assert len(lru) == 2

        del lru['foo3']
        assert len(lru) == 1
Exemple #5
0
    def test_contains(self):
        lru = LRU(10)
        lru['foo1'] = 1

        assert 'foo1' in lru
        assert 'foo2' not in lru
 def test_get(self):
     lru = LRU(10)
     lru['foo1'] = 1
     assert lru.get('foo1') == 1
     assert lru.get('foo1', 2) == 1
Exemple #7
0
 def test_empty(self):
     lru = LRU(10)
     assert bool(lru) == False
     lru['foo1'] = '1'
     assert bool(lru) == True
Exemple #8
0
 def test_delitem(self):
     lru = LRU(10)
     lru['foo1'] = 1
     assert 'foo1' in lru
     del lru['foo1']
     assert 'foo1' not in lru
Exemple #9
0
 def test_get_default(self):
     lru = LRU(10)
     lru['foo1'] = 1
     eq_(lru.get('foo2'), None)
     eq_(lru.get('foo2', 2), 2)
Exemple #10
0
 def test_get(self):
     lru = LRU(10)
     lru['foo1'] = 1
     eq_(lru.get('foo1'), 1)
     eq_(lru.get('foo1', 2), 1)
Exemple #11
0
 def test_getitem(self):
     lru = LRU(10)
     lru['foo1'] = 1
     lru['foo2'] = 2
     assert lru['foo1'] == 1
     assert lru['foo2'] == 2
Exemple #12
0
class MultiMapProxy(object):
    def __init__(self,
                 loader,
                 list_apps=False,
                 app_cache_size=100,
                 debug=False):
        self.loader = loader
        self.list_apps = list_apps
        self._app_init_lock = Lock()
        self.apps = LRU(app_cache_size)
        self.debug = debug

    def __call__(self, environ, start_response):
        req = Request(environ)
        return self.handle(req)(environ, start_response)

    def handle(self, req):
        app_name = req.pop_path()
        # if not app_name:
        #     return self.index_list(req)

        if not app_name or (app_name not in self.apps
                            and not self.loader.app_available(app_name)):
            return Response('not found', status=404)

        # safe instance/app name for authorization
        req.environ['mapproxy.instance_name'] = app_name
        return self.proj_app(app_name)

    def index_list(self, req):
        """
        Return greeting response with a list of available apps (if enabled with list_apps).
        """
        import mapproxy.version
        html = "<html><body><h1>Welcome to MapProxy %s</h1>" % mapproxy.version.version

        url = req.script_url
        if self.list_apps:
            html += "<h2>available instances:</h2><ul>"
            html += '\n'.join(
                '<li><a href="%(url)s/%(name)s/">%(name)s</a></li>' % {
                    'url': url,
                    'name': app
                } for app in self.loader.available_apps())
            html += '</ul>'
        html += '</body></html>'
        return Response(html, content_type='text/html')

    def proj_app(self, proj_name):
        """
        Return the (cached) project app.
        """
        proj_app, timestamps = self.apps.get(proj_name, (None, None))

        if proj_app:
            if self.loader.needs_reload(proj_name, timestamps):
                # discard cached app
                proj_app = None

        if not proj_app:
            with self._app_init_lock:
                proj_app, timestamps = self.apps.get(proj_name, (None, None))
                if self.loader.needs_reload(proj_name, timestamps):
                    proj_app, timestamps = self.create_app(proj_name)
                    self.apps[proj_name] = proj_app, timestamps
                else:
                    proj_app, timestamps = self.apps[proj_name]

        return proj_app

    def create_app(self, proj_name):
        """
        Returns a new configured MapProxy app and a dict with the
        timestamps of all configuration files.
        """
        mapproxy_conf = self.loader.app_conf(proj_name)['mapproxy_conf']
        log.info('initializing project app %s with %s', proj_name,
                 mapproxy_conf)
        app = make_mapproxy_wsgi_app(mapproxy_conf, debug=self.debug)
        return app, app.config_files
Exemple #13
0
class MultiMapProxy(object):

    def __init__(self, loader, list_apps=False, app_cache_size=100, debug=False):
        self.loader = loader
        self.list_apps = list_apps
        self._app_init_lock = Lock()
        self.apps = LRU(app_cache_size)
        self.debug = debug

    def __call__(self, environ, start_response):
        req = Request(environ)
        return self.handle(req)(environ, start_response)

    def handle(self, req):
        app_name = req.pop_path()
        if not app_name:
            return self.index_list(req)

        if not app_name or (
                app_name not in self.apps and not self.loader.app_available(app_name)
            ):
            return Response('not found', status=404)

        # safe instance/app name for authorization
        req.environ['mapproxy.instance_name'] = app_name
        return self.proj_app(app_name)

    def index_list(self, req):
        """
        Return greeting response with a list of available apps (if enabled with list_apps).
        """
        import mapproxy.version
        html = "<html><body><h1>Welcome to MapProxy %s</h1>" % mapproxy.version.version

        url = req.script_url
        if self.list_apps:
            html += "<h2>available instances:</h2><ul>"
            html += '\n'.join('<li><a href="%(url)s/%(name)s/">%(name)s</a></li>' % {'url': url, 'name': app}
                              for app in self.loader.available_apps())
            html += '</ul>'
        html += '</body></html>'
        return Response(html, content_type='text/html')

    def proj_app(self, proj_name):
        """
        Return the (cached) project app.
        """
        proj_app, timestamps = self.apps.get(proj_name, (None, None))

        if proj_app:
            if self.loader.needs_reload(proj_name, timestamps):
                # discard cached app
                proj_app = None

        if not proj_app:
            with self._app_init_lock:
                proj_app, timestamps = self.apps.get(proj_name, (None, None))
                if self.loader.needs_reload(proj_name, timestamps):
                    proj_app, timestamps = self.create_app(proj_name)
                    self.apps[proj_name] = proj_app, timestamps
                else:
                    proj_app, timestamps = self.apps[proj_name]

        return proj_app

    def create_app(self, proj_name):
        """
        Returns a new configured MapProxy app and a dict with the
        timestamps of all configuration files.
        """
        mapproxy_conf = self.loader.app_conf(proj_name)['mapproxy_conf']
        log.info('initializing project app %s with %s', proj_name, mapproxy_conf)
        app = make_mapproxy_wsgi_app(mapproxy_conf, debug=self.debug)
        return app, app.config_files
Exemple #14
0
 def test_missing_key(self):
     lru = LRU(10)
     lru['foo']
 def test_get_default(self):
     lru = LRU(10)
     lru['foo1'] = 1
     eq_(lru.get('foo2'), None)
     eq_(lru.get('foo2', 2), 2)
 def test_get(self):
     lru = LRU(10)
     lru['foo1'] = 1
     eq_(lru.get('foo1'), 1)
     eq_(lru.get('foo1', 2), 1)
Exemple #17
0
 def test_get_default(self):
     lru = LRU(10)
     lru['foo1'] = 1
     assert lru.get('foo2') == None
     assert lru.get('foo2', 2) == 2
Exemple #18
0
 def __init__(self, loader, list_apps=False, app_cache_size=100, debug=False):
     self.loader = loader
     self.list_apps = list_apps
     self._app_init_lock = Lock()
     self.apps = LRU(app_cache_size)
     self.debug = debug
Exemple #19
0
 def test_repr(self):
     lru = LRU(10)
     lru['foo1'] = 1
     assert 'size=10' in repr(lru)
     assert 'foo1' in repr(lru)
Exemple #20
0
 def test_getitem(self):
     lru = LRU(10)
     lru['foo1'] = 1
     lru['foo2'] = 2
     eq_(lru['foo1'], 1)
     eq_(lru['foo2'], 2)
Exemple #21
0
 def test_missing_key(self):
     lru = LRU(10)
     with pytest.raises(KeyError):
         lru['foo']