Exemplo n.º 1
0
 def test_it(self):
     from repoze.depinj import inject
     from repoze.depinj import lookup
     inject('ob1', Dummy)
     inject('ob2', Dummy)
     self.assertEqual(lookup(Dummy), 'ob1')
     self.assertEqual(lookup(Dummy), 'ob2')
     self.assertEqual(lookup(Dummy), Dummy)
Exemplo n.º 2
0
 def pipeline(self):
     pipeline = self._pipeline
     if pipeline is None:
         if self.mode == 'MAINTENANCE':
             pipeline = lookup(maintenance)(None)
         else:
             instance = self.instance()
             pipeline = lookup(make_karl_pipeline)(instance)
         self._pipeline = pipeline
     return pipeline
Exemplo n.º 3
0
 def test_it_with_a_dict_injected(self):
     from repoze.depinj import lookup
     from repoze.depinj import injector
     dummy = {'foo': 'bar'}
     injector.inject('foo', dummy)
     dummy['foo'] = 'baz'  # prove lookup is done on object itself
     self.assertEqual(lookup(dummy), 'foo')
Exemplo n.º 4
0
    def _spin_up(self):
        config = self.config
        name = self.name

        # Write postoffice zodb config
        po_uri = config.get('zodbconn.uri.postoffice')
        if po_uri is None:
            # Backwards compatible
            po_uri = config.get('postoffice.zodb_uri')
            if po_uri:
                config['zodbconn.uri.postoffice'] = po_uri
        if po_uri is None:
            if 'postoffice.dsn' in config:
                if 'postoffice.blob_cache' not in config:
                    raise ValueError("If postoffice.dsn is in config, then "
                                     "postoffice.blob_cache is required.")
                po_uri = self._write_zconfig(
                    'postoffice.conf', config['postoffice.dsn'],
                    config['postoffice.blob_cache'], name='postoffice')
                config['zodbconn.uri.postoffice'] = po_uri
        if po_uri:
            config['postoffice.queue'] = name

        pg_dsn = self.config.get('pgtextindex.dsn')
        if pg_dsn is None:
            pg_dsn = self.config.get('dsn')
        if pg_dsn is not None:
            config['pgtextindex.dsn'] = pg_dsn

        instance = lookup(make_karl_instance)(name, config, self.uri)
        self._instance = instance
        return instance
Exemplo n.º 5
0
    def root_factory(request, name='site'):
        def finished(request):
            # closing the primary also closes any secondaries opened
            now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            elapsed = time.time() - before
            if elapsed > connstats_threshhold:
                loads_after, stores_after = connection.getTransferCounts()
                loads = loads_after - loads_before
                stores = stores_after - stores_before
                with open(connstats_file, 'a', 0) as f:
                    f.write('"%s", "%s", "%s", %f, %d, %d\n' %
                               (now,
                                request.method,
                                request.path_url,
                                elapsed,
                                loads,
                                stores,
                               )
                           )
                    f.flush()

        if connstats_file is not None:
            request.add_finished_callback(finished)

        # NB: Finished callbacks are executed in the order they've been added
        # to the request.  pyramid_zodbconn's ``get_connection`` registers a
        # finished callback which closes the ZODB database.  Because the
        # finished callback it registers closes the database, we need it to
        # execute after the "finished" function above.  As a result, the above
        # call to ``request.add_finished_callback`` *must* be executed before
        # we call ``get_connection`` below.

        # Rationale: we want the call to getTransferCounts() above to happen
        # before the ZODB database is closed, because closing the ZODB database
        # has the side effect of clearing the transfer counts (the ZODB
        # activity monitor clears the transfer counts when the database is
        # closed).  Having the finished callbacks called in the "wrong" order
        # will result in the transfer counts being cleared before the above
        # "finished" function has a chance to read their per-request values,
        # and they will appear to always be zero.
            
        connection = get_connection(request)
        if connstats_file is not None:
            before = time.time()
            loads_before, stores_before = connection.getTransferCounts()
        folder = connection.root()
        if name not in folder:
            bootstrapper = queryUtility(IBootstrapper, default=populate)
            bootstrapper(folder, name, request)

            # Use pgtextindex
            if 'pgtextindex.dsn' in settings:
                site = folder.get(name)
                index = lookup(KarlPGTextIndex)(get_weighted_textrepr)
                site.catalog['texts'] = index

            transaction.commit()

        return folder[name]
Exemplo n.º 6
0
def make_app(global_config, **local_config):
    settings = global_config.copy()
    settings.update(local_config)

    _require_settings(settings,
        'instances_config',
        'who_secret',
        'who_cookie',
        'var',
    )

    _require_externals(
        'aspell',
        'pdftotext',
        'ppthtml',
        'ps2ascii',
        'rtf2xml',
        'xls2csv',
        'wvWare',
    )

    var = os.path.abspath(settings['var'])
    if 'mail_queue_path' not in settings:
        settings['mail_queue_path'] = os.path.join(var, 'mail_queue')
    if 'error_monitor_dir' not in settings:
        settings['error_monitor_dir'] = os.path.join(var, 'errors')
    if 'blob_cache' not in settings:
        settings['blob_cache'] = os.path.join(var, 'blob_cache')
    if 'var_instance' not in settings:
        settings['var_instance'] = os.path.join(var, 'instance')
    if 'var_tmp' not in settings:
        settings['var_tmp'] = os.path.join(var, 'tmp')

    # Configure timezone
    tz = settings.get('timezone')
    if tz is not None:
        os.environ['TZ'] = tz
        time.tzset()

    # Set up logging
    log_config = settings.copy()
    log_config['get_current_instance'] = get_current_instance
    configure_log(**log_config)

    # Configure repoze.bfg application
    config = lookup(Configurator)(settings=settings)
    config.add_route('sites', '/*subpath')
    config.add_view(site_dispatch, route_name='sites')

    app = config.make_wsgi_app()
    return app
Exemplo n.º 7
0
    def appmaker(folder, name='site'):
        if name not in folder:
            bootstrapper = queryUtility(IBootstrapper, default=populate)
            bootstrapper(folder, name)

            # Use pgtextindex
            if 'pgtextindex.dsn' in settings:
                site = folder.get(name)
                index = lookup(KarlPGTextIndex)(get_weighted_textrepr)
                site.catalog['texts'] = index

            transaction.commit()

        return folder[name]
Exemplo n.º 8
0
def make_app(global_config, **local_config):
    settings = global_config.copy()
    settings.update(local_config)

    _require_settings(settings, "instances_config", "who_secret", "who_cookie", "var")

    _require_externals("aspell", "pdftotext", "ppthtml", "ps2ascii", "rtf2xml", "xls2csv", "doctotext")

    var = os.path.abspath(settings["var"])
    if "mail_queue_path" not in settings:
        settings["mail_queue_path"] = os.path.join(var, "mail_queue")
    if "error_monitor_dir" not in settings:
        settings["error_monitor_dir"] = os.path.join(var, "errors")
    if "blob_cache" not in settings:
        settings["blob_cache"] = os.path.join(var, "blob_cache")
    if "var_instance" not in settings:
        settings["var_instance"] = os.path.join(var, "instance")
    if "var_tmp" not in settings:
        settings["var_tmp"] = os.path.join(var, "tmp")

    # Configure timezone
    tz = settings.get("timezone")
    if tz is not None:
        os.environ["TZ"] = tz
        time.tzset()

    # Set up logging
    log_config = settings.copy()
    log_config["get_current_instance"] = get_current_instance
    configure_log(**log_config)

    # Configure repoze.bfg application
    config = lookup(Configurator)(settings=settings)
    config.add_route("sites", "/*subpath")
    config.add_view(site_dispatch, route_name="sites")

    app = config.make_wsgi_app()
    return app
Exemplo n.º 9
0
 def test_it_not_injected(self):
     from repoze.depinj import lookup
     dummy = {'foo': 'bar'}
     self.assertEqual(lookup(dummy), dummy)
Exemplo n.º 10
0
 def test_it_not_injected(self):
     from repoze.depinj import lookup
     self.assertEqual(lookup('whatever'), 'whatever')
Exemplo n.º 11
0
 def test_it_injected(self):
     from repoze.depinj import lookup
     from repoze.depinj import injector
     injector.inject('123', 'whatever')
     self.assertEqual(lookup('whatever'), '123')
Exemplo n.º 12
0
def root_factory(request, name='site'):
    connstats_file = request.registry.settings.get('connection_stats_filename')
    connstats_threshhold = float(
        request.registry.settings.get('connection_stats_threshhold', 0))

    def finished(request):
        # closing the primary also closes any secondaries opened
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        elapsed = time.time() - before
        if elapsed > connstats_threshhold:
            loads_after, stores_after = connection.getTransferCounts()
            loads = loads_after - loads_before
            stores = stores_after - stores_before
            with open(connstats_file, 'a', 0) as f:
                f.write('"%s", "%s", "%s", %f, %d, %d\n' % (
                    now,
                    request.method,
                    request.path_url,
                    elapsed,
                    loads,
                    stores,
                ))
                f.flush()

    # NB: Finished callbacks are executed in the order they've been added
    # to the request.  pyramid_zodbconn's ``get_connection`` registers a
    # finished callback which closes the ZODB database.  Because the
    # finished callback it registers closes the database, we need it to
    # execute after the "finished" function above.  As a result, the above
    # call to ``request.add_finished_callback`` *must* be executed before
    # we call ``get_connection`` below.

    # Rationale: we want the call to getTransferCounts() above to happen
    # before the ZODB database is closed, because closing the ZODB database
    # has the side effect of clearing the transfer counts (the ZODB
    # activity monitor clears the transfer counts when the database is
    # closed).  Having the finished callbacks called in the "wrong" order
    # will result in the transfer counts being cleared before the above
    # "finished" function has a chance to read their per-request values,
    # and they will appear to always be zero.

    if connstats_file is not None:
        request.add_finished_callback(finished)

    connection = get_connection(request)

    if connstats_file is not None:
        before = time.time()
        loads_before, stores_before = connection.getTransferCounts()

    folder = connection.root()
    if name not in folder:
        from karl.bootstrap.bootstrap import populate  # avoid circdep
        bootstrapper = queryUtility(IBootstrapper, default=populate)
        bootstrapper(folder, name, request)

        # Use pgtextindex
        if 'pgtextindex.dsn' in request.registry.settings:
            site = folder.get(name)
            index = lookup(KarlPGTextIndex)(get_weighted_textrepr,
                                            drop_and_create=True)
            site.catalog['texts'] = index

        transaction.commit()

    request.registry.notify(RootCreated(request, folder))

    return folder[name]
Exemplo n.º 13
0
def get_instances(settings):
    instances = settings.get('instances')
    if instances is None:
        instances = lookup(Instances)(settings)
        settings['instances'] = instances
    return instances