Esempio n. 1
0
def non_existing_repo():
    """
    A fixture that makes sure a repository does not exist before the test
    and also deletes it after the test.
    The value returned by this fixture is the name of the store.
    """
    server = AllegroGraphServer(AG_HOST, AG_PORT, USER, PASSWORD, proxy=AG_PROXY)
    catalog = server.openCatalog(CATALOG)
    store = 'temp-store'
    while store in catalog.listRepositories():
        store = 'temp-store-' + ''.join(random.choice('0123456789') for _ in range(16))
    yield store
    if store in catalog.listRepositories():
        catalog.deleteRepository(store)
Esempio n. 2
0
def test_server_url_in_env(clean_env):
    clean_env['AGRAPH_HOST'] = 'somehost'
    clean_env['AGRAPH_PORT'] = 12345
    clean_env['AGRAPH_USER'] = '******'
    clean_env['AGRAPH_PASSWORD'] = '******'
    server = AllegroGraphServer()
    assert server.url == 'http://somehost:12345'
    assert server._client.user == 'luser'
    assert server._client.password == '1234'
Esempio n. 3
0
def conn():
    """
    Provides a connection to the test repository. The repository is cleared
    before the test, but not after it.
    """
    server = AllegroGraphServer(AG_HOST, AG_PORT, USER, PASSWORD, proxy=AG_PROXY)
    catalog = server.openCatalog(CATALOG)
    stores = catalog.listRepositories()

    # Instead of renewing the database, clear it.
    mode = Repository.CREATE if STORE not in stores else Repository.OPEN

    repo = catalog.getRepository(STORE, mode)
    repo.initialize()
    connection = repo.getConnection()

    if STORE in stores:
        connection.clear()
        connection.clearNamespaces()
        connection.disableDuplicateSuppression()

    with connection:
        yield connection
Esempio n. 4
0
def ag_connect(repo,
               catalog=None,
               create=True,
               fail_if_exists=False,
               clear=False,
               session=False,
               autocommit=False,
               lifetime=None,
               loadinitfile=False,
               host=None,
               port=None,
               protocol=None,
               user=None,
               password=None,
               cainfo=None,
               sslcert=None,
               verifyhost=None,
               verifypeer=None,
               indices=None,
               proxy=os.environ.get('AGRAPH_PROXY')):
    """
    Create a connection to an AllegroGraph repository.

    When closed the connection will take care of releasing all intermediate resources
    that were created in order to open it.

    :param repo: Repository name.
    :type repo: string
    :param catalog: Catalog name (optional, root catalog is the default).
    :type catalog: string
    :param create: if `True` (default) create the repository if it does not exist.
    :type create: bool
    :param fail_if_exists: if `True` and the repository exists raise an exception.
                           This applies only if `create` is `True`.
                           The default value is `False`.
    :type fail_if_exists: bool
    :param clear: if `True` delete all data after creating the connection.
                  The default is `False`.
    :type clear: bool
    :param session: If ``True`` start a session after creating the connection.
                    The default is ``False``.
    :type session: bool
    :param autocommit: When opening a session: if ``True``, commits are done on each
                       request, otherwise you will need to call :meth:`.commit` or
                       :meth:`.rollback` as appropriate for your application.
                       The default value is ``False``.
    :type autocommit: bool
    :param lifetime: Time (in seconds) before the session expires when idle.
                     Note that the client maintains a thread that ping the
                     session before this happens.
                     Ignored if not starting a session.
    :type lifetime: int
    :param loadinitfile: if ``True`` then the current initfile will be loaded
                         for you when the session starts. The default is ``False``.
                         Ignored if not starting a session.
    :type loadinitfile: bool
    :param host: AllegroGraph server host (default: ``'127.0.0.1'`` or the value
                 of the AGRAPH_HOST environment variable if that is defined.`
                 Can also be used to supply protocol and port number
                 (e.g. ``https://localhost:10036``).
    :type host: string
    :param protocol: Either ``"http"`` or ``"https"``.
                     The default is ``"http"``.
                     Overrides the protocol specified in ``host``.
    :type protocol: string
    :param port: AllegroGraph server port (default: `10035` for http and `10036`
                 for https, or the AGRAPH_PORT environment variable if that is defined).
                 Overrides the port number provided in ``host``.
    :type port: int
    :param user: Username for authentication (default: value of the ``AGRAPH_USER``
                 environment variable).
    :type user: string
    :param password:  Password for authentication (default: value of the ``AGRAPH_PASSWORD``
                      environment variable).
    :type password: string
    :param cainfo: Path to file or directory with CA certificates.
    :type cainfo: string
    :param sslcert: Path to a client certificate to use for
                    authentication instead of username and password.
    :type sslcert: string
    :param verifyhost: See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html
    :type verifyhost: int
    :param verifypeer: See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html
    :type verifypeer: int
    :param indices: List of indices to create if creating a new repository.
    :type indices: list[string]
    :param proxy: Proxy specification string. The format is SCHEME://HOST:PORT.
                  Supported schemes are 'http', 'socks4' and 'socks5'.
                  Note that for SOCKS proxies DNS requests are performed by the
                  proxy server.
                  The default value is taken from the AGRAPH_PROXY environment
                  variable.
    :type proxy: string
    :return: A :class:`.RepositoryConnection` object.
    :rtype: franz.openrdf.repositoryconnection.RepositoryConnection
    """
    server = AllegroGraphServer(host=host,
                                port=port,
                                protcol=protocol,
                                user=user,
                                password=password,
                                sslcert=sslcert,
                                cainfo=cainfo,
                                verifyhost=verifyhost,
                                verifypeer=verifypeer,
                                proxy=proxy)
    cat_handle = server.openCatalog(catalog)
    repo_exists = repo in cat_handle.listRepositories()
    if not repo_exists:
        if create:
            repo_handle = cat_handle.createRepository(repo, indices)
        else:
            raise Exception('Store %s does not exist.' % repo)
    else:
        if fail_if_exists and create:
            raise Exception('Store %s already exists.' % repo)
        mode = Repository.RENEW if clear else Repository.OPEN
        repo_handle = cat_handle.getRepository(repo, mode)
    conn = RepositoryConnection(repo_handle, close_repo=True)
    if session:
        # conn.close will close it if necessary
        conn.openSession(autocommit=autocommit,
                         lifetime=lifetime,
                         loadinitfile=loadinitfile)
    return conn
Esempio n. 5
0
def test_server_https_if_cainfo(clean_env):
    server = AllegroGraphServer('somehost', cainfo='/path/to/ca/bundle')
    assert server.url == 'https://somehost:10036'
Esempio n. 6
0
def test_server_port_as_arg(clean_env):
    server = AllegroGraphServer('somehost', port=4321)
    assert server.url == 'http://somehost:4321'
Esempio n. 7
0
def test_server_override_port(clean_env):
    server = AllegroGraphServer('somehost:1234', port=4321)
    assert server.url == 'http://somehost:4321'
Esempio n. 8
0
def test_server_override_protocol(clean_env):
    server = AllegroGraphServer('http://somehost', protocol='https')
    assert server.url == 'https://somehost:10036'
Esempio n. 9
0
def test_server_protocol_as_arg(clean_env):
    server = AllegroGraphServer('somehost', protocol='https')
    assert server.url == 'https://somehost:10036'
Esempio n. 10
0
def test_server_all_defaults(clean_env):
    server = AllegroGraphServer('somehost')
    assert server.url == 'http://somehost:10035'
Esempio n. 11
0
def test_server_all_data_in_host(clean_env):
    server = AllegroGraphServer('https://somehost:4321/and/then/some')
    assert server.url == 'https://somehost:4321/and/then/some'
Esempio n. 12
0
def ag_connect(repo, catalog=None,
               create=True, fail_if_exists=False, clear=False,
               session=False, autocommit=False, lifetime=None, loadinitfile=False,
               host=None, port=None, protocol=None,
               user=None, password=None, cainfo=None, sslcert=None,
               verifyhost=None, verifypeer=None, indices=None,
               proxy=os.environ.get('AGRAPH_PROXY')):
    """
    Create a connection to an AllegroGraph repository.

    When closed the connection will take care of releasing all intermediate resources
    that were created in order to open it.

    :param repo: Repository name.
    :type repo: string
    :param catalog: Catalog name (optional, root catalog is the default).
    :type catalog: string
    :param create: if `True` (default) create the repository if it does not exist.
    :type create: bool
    :param fail_if_exists: if `True` and the repository exists raise an exception.
                           This applies only if `create` is `True`.
                           The default value is `False`.
    :type fail_if_exists: bool
    :param clear: if `True` delete all data after creating the connection.
                  The default is `False`.
    :type clear: bool
    :param session: If ``True`` start a session after creating the connection.
                    The default is ``False``.
    :type session: bool
    :param autocommit: When opening a session: if ``True``, commits are done on each
                       request, otherwise you will need to call :meth:`.commit` or
                       :meth:`.rollback` as appropriate for your application.
                       The default value is ``False``.
    :type autocommit: bool
    :param lifetime: Time (in seconds) before the session expires when idle.
                     Note that the client maintains a thread that ping the
                     session before this happens.
                     Ignored if not starting a session.
    :type lifetime: int
    :param loadinitfile: if ``True`` then the current initfile will be loaded
                         for you when the session starts. The default is ``False``.
                         Ignored if not starting a session.
    :type loadinitfile: bool
    :param host: AllegroGraph server host (default: ``'127.0.0.1'`` or the value
                 of the AGRAPH_HOST environment variable if that is defined.`
                 Can also be used to supply protocol and port number
                 (e.g. ``https://localhost:10036``).
    :type host: string
    :param protocol: Either ``"http"`` or ``"https"``.
                     The default is ``"http"``.
                     Overrides the protocol specified in ``host``.
    :type protocol: string
    :param port: AllegroGraph server port (default: `10035` for http and `10036`
                 for https, or the AGRAPH_PORT environment variable if that is defined).
                 Overrides the port number provided in ``host``.
    :type port: int
    :param user: Username for authentication (default: value of the ``AGRAPH_USER``
                 environment variable).
    :type user: string
    :param password:  Password for authentication (default: value of the ``AGRAPH_PASSWORD``
                      environment variable).
    :type password: string
    :param cainfo: Path to file or directory with CA certificates.
    :type cainfo: string
    :param sslcert: Path to a client certificate to use for
                    authentication instead of username and password.
    :type sslcert: string
    :param verifyhost: See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html
    :type verifyhost: int
    :param verifypeer: See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html
    :type verifypeer: int
    :param indices: List of indices to create if creating a new repository.
    :type indices: list[string]
    :param proxy: Proxy specification string. The format is SCHEME://HOST:PORT.
                  Supported schemes are 'http', 'socks4' and 'socks5'.
                  Note that for SOCKS proxies DNS requests are performed by the
                  proxy server.
                  The default value is taken from the AGRAPH_PROXY environment
                  variable.
    :type proxy: string
    :return: A :class:`.RepositoryConnection` object.
    :rtype: franz.openrdf.repositoryconnection.RepositoryConnection
    """
    server = AllegroGraphServer(host=host, port=port, protcol=protocol,
                                user=user, password=password, sslcert=sslcert,
                                cainfo=cainfo, verifyhost=verifyhost, verifypeer=verifypeer,
                                proxy=proxy)
    cat_handle = server.openCatalog(catalog)
    repo_exists = repo in cat_handle.listRepositories()
    if not repo_exists:
        if create:
            repo_handle = cat_handle.createRepository(repo, indices)
        else:
            raise Exception('Store %s does not exist.' % repo)
    else:
        if fail_if_exists and create:
            raise Exception('Store %s already exists.' % repo)
        mode = Repository.RENEW if clear else Repository.OPEN
        repo_handle = cat_handle.getRepository(repo, mode)
    conn = RepositoryConnection(repo_handle, close_repo=True)
    if session:
        # conn.close will close it if necessary
        conn.openSession(autocommit=autocommit, lifetime=lifetime, loadinitfile=loadinitfile)
    return conn