TheThingsArchive / api

Generated code for accessing The Things Network v2 API
MIT License
12 stars 8 forks source link

Add python support #23

Closed rvolosatovs closed 6 years ago

rvolosatovs commented 6 years ago

@emmlejeail please test if this works for you Note that the protobuf and grpc packages should also be installed(i.e. pip install --user protobuf grpcio-tools)

packages should be imported relative to ./python directory. Example:

cd ./python
python -c 'from github_com.TheThingsNetwork.api.broker import *; s = BrokerServicer(); print s'

should output something similar to:

<github_com.TheThingsNetwork.api.broker.broker_pb2.BrokerServicer object at 0x7f128c657910>

I believe the SDK users don't see the "ugly" imports to github_com..., right? otherwise we should come up with a better solution.

Python (plugins) do not support dots in import paths, see also https://github.com/grpc/grpc/issues/10790 Currently everything in get-python.sh is hardcoded, we can make it work with the variables in Makefile later, if this turns out to be an issue. Generated dependencies are included in python directory, maybe there is a better solution?

htdvisser commented 6 years ago

I find it difficult to review this, as my python knowledge is pretty limited. If it works OK for @emmlejeail I think we can merge it, otherwise we should find someone in the community to try using it.

rvolosatovs commented 6 years ago

There's not much python happening, it's mostly just copying all protos from ${GOPATH}/src/github.com to a <temp dir>/github_com and generating the TTN protos and dependencies into <repo>/python. __init__.py files are required for (sub-)packages to be imported. Those also define the values exported - I made them export all the generated code, so once you import i.e. github_com.TheThingsNetwork.api.broker, all broker-related generated code is imported automatically. You can try all of this out using the example line I mentioned in the description. I believe the SDK __init.py__ can just import the github_com/TheThingsNetwork/api<...>, export the required values from (sub-)packages in this repo and effectively hide the github_com import paths and unnecessary stuff.