From f5d5a77c2050073012a1eed4c15e0cc80201ff29 Mon Sep 17 00:00:00 2001 From: or-else Date: Thu, 30 Aug 2018 17:24:53 +0300 Subject: [PATCH] packaging of python grpc seems to work now --- chatbot/chatbot.py | 14 +++++++------- pbx/py_fix.py | 11 +++++++++++ py_grpc/LICENSE | 2 ++ py_grpc/README.md | 24 ++++++++++++++++++++++++ py_grpc/setup.py | 8 +++++++- py_grpc/tinode_grpc/__init__.py | 4 ++-- py_grpc/tinode_grpc/model_pb2_grpc.py | 2 +- server/utils.go | 9 ++++++--- 8 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 pbx/py_fix.py create mode 100644 py_grpc/LICENSE create mode 100644 py_grpc/README.md diff --git a/chatbot/chatbot.py b/chatbot/chatbot.py index e6a8bcb38..6e7f64bfc 100644 --- a/chatbot/chatbot.py +++ b/chatbot/chatbot.py @@ -5,6 +5,7 @@ from concurrent import futures import json import os +import pkg_resources import platform try: import Queue as queue @@ -17,14 +18,13 @@ import grpc -# Import generated modules from ../pbx/ -sys.path.append('../py_grpc/tinode_grpc') -import model_pb2 as pb -import model_pb2_grpc as pbx +# Import generated grpc modules +from tinode_grpc import pb +from tinode_grpc import pbx APP_NAME = "Tino-chatbot" APP_VERSION = "1.0" -LIB_VERSION = "0.15" +LIB_VERSION = pkg_resources.get_distribution("tinode_grpc").version # Dictionary wich contains lambdas to be executed when server response is received onCompletion = {} @@ -72,6 +72,8 @@ def Account(self, acc_event, context): action = None if acc_event.action == pb.CREATE: action = "created" + # TODO: subscribe to the new user. + elif acc_event.action == pb.UPDATE: action = "updated" elif acc_event.action == pb.DELETE: @@ -81,8 +83,6 @@ def Account(self, acc_event, context): print("Account", action, ":", acc_event.user_id, acc_event.public) - # TODO: subscribe to the new user. - return pb.Unused() queue_out = queue.Queue() diff --git a/pbx/py_fix.py b/pbx/py_fix.py new file mode 100644 index 000000000..274209cb3 --- /dev/null +++ b/pbx/py_fix.py @@ -0,0 +1,11 @@ +# grpc-tools generates python 2 file which does not work with +# python3 packaging system. This is a fix. + +model_pb2_grpc = "../py_grpc/tinode_grpc/model_pb2_grpc.py" + +with open(model_pb2_grpc, "r") as fh: + content = fh.read().replace("import model_pb2 as model__pb2", + "from . import model_pb2 as model__pb2") + + with open(model_pb2_grpc,"w") as fh: + fh.write(content) diff --git a/py_grpc/LICENSE b/py_grpc/LICENSE new file mode 100644 index 000000000..3331e7bbc --- /dev/null +++ b/py_grpc/LICENSE @@ -0,0 +1,2 @@ +The code in this folder is licensed under Apache 2.0 +http://www.apache.org/licenses/LICENSE-2.0 diff --git a/py_grpc/README.md b/py_grpc/README.md new file mode 100644 index 000000000..12e43568d --- /dev/null +++ b/py_grpc/README.md @@ -0,0 +1,24 @@ +# Generated Protocol Buffer and gRPC files for [Tinode](https://github.com/tinode) + +Generated Python code for [gRPC](https://grpc.io/) client and plugins. + +gRPC clients must implement rpc service `Node`, plugins must implement `Plugin`. + +For a sample implementation of a command line client see [tn-cli](../tn-cli/). +For a partial plugin implementation see [chatbot](../chatbot/). + +## Installing + +Install the package by executing +``` +pip install tinode_grpc +``` + + +## Generating files + +Don't modify included files directly. If you want to make changes, you have to install protobuffers tool chain and gRPC the generate the Python bindings from [`pbx/model.proto`](../pbx/model.proto) (your path to `model.proto` may be different): +``` +python -m grpc_tools.protoc -I../pbx --python_out=. --grpc_python_out=. ../pbx/model.proto +``` +The generated `model_pb2_grpc.py` imports `model_pb2.py` as a module instead of a package which is incompatible with python3 packaging system. Use `../pbx/py_fix.py` to apply a fix. This is only needed if you want to repackage the generated files. diff --git a/py_grpc/setup.py b/py_grpc/setup.py index c9ffeecd1..e2796e363 100644 --- a/py_grpc/setup.py +++ b/py_grpc/setup.py @@ -6,12 +6,18 @@ with open("README.md", "r") as fh: long_description = fh.read() +# Convert git tag like "v0.15.5-rc5-3-g2084bd63" to PEP 440 version like "0.15.5rc5.post3" def git_version(): p = Popen(['git', 'describe', '--tags'], stdout=PIPE, stderr=PIPE) p.stderr.close() line = p.stdout.readlines()[0].decode("ascii").strip() - if line.startswith("v"): #strip "v" prefix from git tag "v0.15.5" -> "0.15.5". + if line.startswith("v"): line = line[1:] + if "-rc" in line: + line = line.replace("-rc", "rc") + if "-" in line: + parts = line.split("-") + line = parts[0] + ".post" + parts[1] return line setuptools.setup( diff --git a/py_grpc/tinode_grpc/__init__.py b/py_grpc/tinode_grpc/__init__.py index 74dbeee6e..9bb17e7fa 100644 --- a/py_grpc/tinode_grpc/__init__.py +++ b/py_grpc/tinode_grpc/__init__.py @@ -1,2 +1,2 @@ -from .model_pb2 import * -from .model_pb2_grpc import * +from . import model_pb2 as pb +from . import model_pb2_grpc as pbx diff --git a/py_grpc/tinode_grpc/model_pb2_grpc.py b/py_grpc/tinode_grpc/model_pb2_grpc.py index 1d5962058..a6e4a4b49 100644 --- a/py_grpc/tinode_grpc/model_pb2_grpc.py +++ b/py_grpc/tinode_grpc/model_pb2_grpc.py @@ -1,7 +1,7 @@ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! import grpc -import model_pb2 as model__pb2 +from . import model_pb2 as model__pb2 class NodeStub(object): diff --git a/server/utils.go b/server/utils.go index 9cdea760b..ab5302842 100644 --- a/server/utils.go +++ b/server/utils.go @@ -337,8 +337,9 @@ func parseTopicAccess(acs *MsgDefaultAcsMode, defAuth, defAnon types.AccessMode) return } -// Parses version of format 0.13.xx or 0.13-xx or 0.13 -// The major and minor parts must be valid, the last part is ignored if missing or unparceable. +// Parses version in the following formats: +// 1.2 | 1.2abc | 1.2.3 | 1.2.3abc +// The major and minor parts must be valid, the trailer is ignored if missing or unparceable. func parseVersion(vers string) int { var major, minor, trailer int var err error @@ -353,7 +354,9 @@ func parseVersion(vers string) int { return 0 } - dot2 := strings.IndexAny(vers[dot+1:], ".-") + dot2 := strings.IndexFunc(vers[dot+1:], func(r rune) bool { + return !unicode.IsDigit(r) + }) if dot2 > 0 { minor, err = strconv.Atoi(vers[dot+1 : dot2]) // Ignoring the error here