Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .grabl/automation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ build:
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
bazel build //...
bazel run @graknlabs_dependencies//tool/checkstyle:test-coverage
bazel test $(bazel query 'kind(checkstyle_test, //...)')
bazel test $(bazel query 'kind(checkstyle_test, //...)') --test_output=errors
# test-concept:
# image: graknlabs-ubuntu-20.04
# type: foreground
Expand Down
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ py_library(
graknlabs_client_python_requirement("grpcio"),
graknlabs_client_python_requirement("six"),
],
visibility =["//visibility:public"]
visibility = ["//visibility:public"]
)

checkstyle_test(
Expand Down
3 changes: 2 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ github_deps()
# Load @graknlabs dependencies #
################################

load("//dependencies/graknlabs:repositories.bzl", "graknlabs_common")
load("//dependencies/graknlabs:repositories.bzl", "graknlabs_common", "graknlabs_behaviour")
graknlabs_common()
graknlabs_behaviour()

# Load artifacts
load("//dependencies/graknlabs:artifacts.bzl", "graknlabs_grakn_core_artifacts")
Expand Down
7 changes: 7 additions & 0 deletions dependencies/graknlabs/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ def graknlabs_common():
remote = "https://github.com/graknlabs/common",
commit = "cfd261fd5412a0b45cb5494555e4491dd3ce5f64" # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_common
)

def graknlabs_behaviour():
git_repository(
name = "graknlabs_behaviour",
remote = "https://github.com/graknlabs/behaviour",
commit = "7088622b9f4a3bd99cf4fb278130ad76ff35af6b" # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_behaviour
)
10 changes: 8 additions & 2 deletions grakn/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
from grakn.rpc.database_manager import DatabaseManager as _DatabaseManager
from grakn.rpc.session import Session as _Session, SessionType

# Repackaging these enums allows users to import everything they (most likely) need from "grakn.client"
from grakn.rpc.transaction import TransactionType # noqa # pylint: disable=unused-import
# Repackaging these symbols allows users to import everything they (most likely) need from "grakn.client"
from grakn.common.exception import GraknClientException # noqa # pylint: disable=unused-import
from grakn.concept.type.attribute_type import ValueType # noqa # pylint: disable=unused-import
from grakn.rpc.transaction import TransactionType # noqa # pylint: disable=unused-import


class GraknClient(object):
Expand All @@ -34,6 +35,7 @@ class GraknClient(object):
def __init__(self, address=DEFAULT_URI):
self._channel = grpc.insecure_channel(address)
self._databases = _DatabaseManager(self._channel)
self._is_open = True

def session(self, database: str, session_type: SessionType, options=GraknOptions()):
return _Session(self, database, session_type, options)
Expand All @@ -43,6 +45,10 @@ def databases(self):

def close(self):
self._channel.close()
self._is_open = False

def is_open(self):
return self._is_open

def __enter__(self):
return self
Expand Down
8 changes: 4 additions & 4 deletions grakn/query/query_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ def delete(self, query: str, options=GraknOptions()):
delete_req = query_proto.Graql.Delete.Req()
delete_req.query = query
request.delete_req.CopyFrom(delete_req)
self._run_query(request, options)
return self._run_query(request, options)

def define(self, query: str, options=GraknOptions()):
request = query_proto.Query.Req()
define_req = query_proto.Graql.Define.Req()
define_req.query = query
request.define_req.CopyFrom(define_req)
self._run_query(request, options)
return self._run_query(request, options)

def undefine(self, query: str, options=GraknOptions()):
request = query_proto.Query.Req()
undefine_req = query_proto.Graql.Undefine.Req()
undefine_req.query = query
request.undefine_req.CopyFrom(undefine_req)
self._run_query(request, options)
return self._run_query(request, options)

def _run_query(self, query_req: query_proto.Query.Req, options: GraknOptions):
req = transaction_proto.Transaction.Req()
query_req.options.CopyFrom(grakn_proto_builder.options(options))
req.query_req.CopyFrom(query_req)
# Using stream makes this request asynchronous.
self._transaction._stream(req)
return self._transaction._stream(req)

def _iterate_query(self, query_req: query_proto.Query.Req, response_reader: Callable[[transaction_proto.Transaction.Res], List], options: GraknOptions):
req = transaction_proto.Transaction.Req()
Expand Down
10 changes: 6 additions & 4 deletions grakn/rpc/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def __init__(self, client, database: str, session_type: SessionType, options=Gra

self._session_id = self._grpc_stub.session_open(open_req).session_id
self._is_open = True
self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
self._pulse = self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
# TODO: This thread blocks the process from closing. We should try cancelling the scheduled task when the
# session closes. If that doesn't work, we need some other way of getting the thread to exit.
Thread(target=self._scheduler.run).start()
Thread(target=self._scheduler.run, daemon=True).start()

def transaction(self, transaction_type: TransactionType, options=GraknOptions()):
return Transaction(self._channel, self._session_id, transaction_type, options)
Expand All @@ -67,6 +67,8 @@ def is_open(self): return self._is_open
def close(self):
if self._is_open:
self._is_open = False
self._scheduler.cancel(self._pulse)
self._scheduler.empty()
req = session_proto.Session.Close.Req()
req.session_id = self._session_id
self._grpc_stub.session_close(req)
Expand All @@ -80,8 +82,8 @@ def _transmit_pulse(self):
pulse_req.session_id = self._session_id
res = self._grpc_stub.session_pulse(pulse_req)
if res.alive:
self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
Thread(target=self._scheduler.run).start()
self._pulse = self._scheduler.enter(delay=self._PULSE_FREQUENCY_SECONDS, priority=1, action=self._transmit_pulse, argument=())
Thread(target=self._scheduler.run, daemon=True).start()

def __enter__(self):
return self
Expand Down
2 changes: 1 addition & 1 deletion grakn/rpc/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Stream(six.Iterator):
_CONTINUE = "continue"
_DONE = "done"

def __init__(self, transaction, request_id: str, transform_response: Callable[[transaction_proto.Transaction.Res], List] = None):
def __init__(self, transaction, request_id: str, transform_response: Callable[[transaction_proto.Transaction.Res], List] = lambda res: None):
self._transaction = transaction
self._request_id = request_id
self._transform_response = transform_response
Expand Down
15 changes: 14 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@
# under the License.
#

# --extra-index-url https://repo.grakn.ai/repository/pypi-snapshot/simple

## Configuration options

# --extra-index-url https://repo.grakn.ai/repository/pypi-snapshot/simple # Allow importing of snapshots


## Dependencies

graknprotocol==2.0.0a4
grpcio==1.33.2
protobuf==3.6.1
six>=1.11.0


# Dev dependencies (not required to use the grakn-client package, but necessary for its development)

behave==1.2.6
PyHamcrest==2.0.2
3 changes: 2 additions & 1 deletion test/BUILD → tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
#

# Note: Do NOT rename this folder to 'test' as it conflicts with a built-in Python package!
load("@graknlabs_common//test/server:rules.bzl", "native_grakn_artifact")
load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
load("@rules_python//python:defs.bzl", "py_library", "py_test")
Expand All @@ -26,7 +27,7 @@ native_grakn_artifact(
mac_artifact = "@graknlabs_grakn_core_artifact_mac//file",
linux_artifact = "@graknlabs_grakn_core_artifact_linux//file",
windows_artifact = "@graknlabs_grakn_core_artifact_windows//file",
visibility = ["//test:__subpackages__"],
visibility = ["//tests:__subpackages__"],
)

py_test(
Expand Down
35 changes: 35 additions & 0 deletions tests/behaviour/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

package(default_visibility = ["//tests/behaviour:__subpackages__"])

load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")

py_library(
name = "background",
srcs = glob(["*.py"]),
deps = [],
)

checkstyle_test(
name = "checkstyle",
include = glob(["*"]),
license_type = "apache",
size = "small",
)
35 changes: 35 additions & 0 deletions tests/behaviour/config/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

package(default_visibility = ["//tests/behaviour:__subpackages__"])

load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")

py_library(
name = "parameters",
srcs = ["parameters.py"],
deps = [],
)

checkstyle_test(
name = "checkstyle",
include = glob(["*"]),
license_type = "apache",
size = "small",
)
35 changes: 35 additions & 0 deletions tests/behaviour/config/parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

from typing import List

from grakn.rpc.transaction import TransactionType


def parse_bool(value: str) -> bool:
return value == "true"


def parse_list(table) -> List[str]:
return [table.headings[0]] + list(map(lambda row: row[0], table.rows))


def parse_transaction_type(value: str) -> TransactionType:
return TransactionType.READ if value == "read" else TransactionType.WRITE

35 changes: 35 additions & 0 deletions tests/behaviour/connection/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

load("@graknlabs_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")

package(default_visibility = ["//tests/behaviour:__subpackages__"])

py_library(
name = "steps",
srcs = ["connection_steps.py"],
deps = [],
)

checkstyle_test(
name = "checkstyle",
include = glob(["*"]),
license_type = "apache",
size = "small",
)
30 changes: 30 additions & 0 deletions tests/behaviour/connection/connection_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

from behave import *


@step("connection has been opened")
def step_impl(context):
assert context.client and context.client.is_open()


@step("connection does not have any database")
def step_impl(context):
assert len(context.client.databases().all()) == 0
Loading