Skip to content

Commit

Permalink
packaging of python grpc seems to work now
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Aug 30, 2018
1 parent 2084bd6 commit f5d5a77
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 14 deletions.
14 changes: 7 additions & 7 deletions chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from concurrent import futures
import json
import os
import pkg_resources
import platform
try:
import Queue as queue
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions pbx/py_fix.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions py_grpc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The code in this folder is licensed under Apache 2.0
http://www.apache.org/licenses/LICENSE-2.0
24 changes: 24 additions & 0 deletions py_grpc/README.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion py_grpc/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions py_grpc/tinode_grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion py_grpc/tinode_grpc/model_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
9 changes: 6 additions & 3 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f5d5a77

Please sign in to comment.