Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 6b2a767

Browse files
authored
Merge pull request #26 from juanjux/bug/pypi_fix
Pypi fix, versioned SDK, improved README
2 parents 8fb23b8 + c88fbc6 commit 6b2a767

File tree

18 files changed

+158
-114
lines changed

18 files changed

+158
-114
lines changed

MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include *.md
2+
include *.txt
3+
include DCO
4+
include LICENSE
5+
include MAINTAINERS
6+
include Makefile

Makefile

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ PYTHON ?= python3
33
makefile_dir := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
44

55
LIBUAST_VERSION = v0.2.0
6+
SDK_VERSION = v0
67

78
.PHONY : all clean deps
89

910
all: deps \
1011
bblfsh/github/com/gogo/protobuf/gogoproto/gogo_pb2.py \
11-
bblfsh/github/com/bblfsh/sdk/uast/generated_pb2.py \
12-
bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2_*.py \
12+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/uast/generated_pb2.py \
13+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol/generated_pb2_*.py \
1314
bblfsh/github/__init__.py \
1415
bblfsh/github/com/__init__.py \
1516
bblfsh/github/com/gogo/__init__.py \
1617
bblfsh/github/com/gogo/protobuf/__init__.py \
1718
bblfsh/github/com/gogo/protobuf/gogoproto/__init__.py \
18-
bblfsh/github/com/bblfsh/__init__.py \
19-
bblfsh/github/com/bblfsh/sdk/__init__.py \
20-
bblfsh/github/com/bblfsh/sdk/uast/__init__.py \
21-
bblfsh/github/com/bblfsh/sdk/protocol/__init__.py
19+
bblfsh/gopkg/__init__.py \
20+
bblfsh/gopkg/in/__init__.py \
21+
bblfsh/gopkg/in/bblfsh/__init__.py \
22+
bblfsh/gopkg/in/bblfsh/sdk/__init__.py \
23+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/__init__.py \
24+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/uast/__init__.py \
25+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol/__init__.py
2226

2327
clean:
2428
rm -rf bblfsh/libuast
2529
rm -rf bblfsh/github
30+
rm -rf bblfsh/gopkg
2631

2732
deps: bblfsh/libuast
2833

@@ -35,18 +40,24 @@ bblfsh/libuast:
3540
bblfsh/github/com/gogo/protobuf/gogoproto/gogo_pb2.py: github.com/gogo/protobuf/gogoproto/gogo.proto
3641
protoc --python_out bblfsh github.com/gogo/protobuf/gogoproto/gogo.proto
3742

38-
bblfsh/github/com/bblfsh/sdk/uast/generated_pb2.py: github.com/bblfsh/sdk/uast/generated.proto
39-
protoc --python_out bblfsh github.com/bblfsh/sdk/uast/generated.proto
43+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/uast/generated_pb2.py: gopkg.in/bblfsh/sdk.$(SDK_VERSION)/uast/generated.proto
44+
protoc --python_out bblfsh gopkg.in/bblfsh/sdk.$(SDK_VERSION)/uast/generated.proto
4045

41-
bblfsh/github/com/bblfsh/sdk/protocol:
46+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol:
4247
@mkdir -p $@
4348

44-
bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2_*.py: \
45-
bblfsh/github/com/bblfsh/sdk/protocol github.com/bblfsh/sdk/protocol/generated.proto
46-
$(PYTHON) -m grpc.tools.protoc --python_out=bblfsh/github/com/bblfsh/sdk/protocol \
47-
--grpc_python_out=bblfsh/github/com/bblfsh/sdk/protocol \
48-
-I github.com/bblfsh/sdk/protocol -I $(makefile_dir) \
49-
github.com/bblfsh/sdk/protocol/generated.proto
49+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/uast:
50+
@mkdir -p $@
51+
52+
bblfsh/github/com/gogo/protobuf/gogoproto:
53+
@mkdir -p $@
54+
55+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol/generated_pb2_*.py: \
56+
bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol gopkg.in/bblfsh/sdk.$(SDK_VERSION)/protocol/generated.proto
57+
$(PYTHON) -m grpc.tools.protoc --python_out=bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol \
58+
--grpc_python_out=bblfsh/gopkg/in/bblfsh/sdk/$(SDK_VERSION)/protocol \
59+
-I gopkg.in/bblfsh/sdk.$(SDK_VERSION)/protocol -I $(makefile_dir) \
60+
gopkg.in/bblfsh/sdk.$(SDK_VERSION)/protocol/generated.proto
5061

5162
%/__init__.py:
5263
@touch $@

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,41 @@ This a pure Python implementation of querying [Babelfish](https://doc.bblf.sh/)
55
### Usage
66

77
API
8-
```
9-
from bblfsh import BblfshClient
8+
```python
9+
from bblfsh import BblfshClient, filter
1010

1111
client = BblfshClient("0.0.0.0:9432")
1212
print(client.parse("/path/to/file.py"))
13+
# "filter' allows you to use XPath queries to filter on result nodes:
14+
print(client.filter("//Import[@roleImportDeclaration]//alias")
1315
```
1416

17+
*TODO*: Link to `libuast` XPath documentation when available.
18+
1519
Command line
16-
```
20+
```bash
1721
python3 -m bblfsh -f file.py
1822
```
1923

24+
This will usually download and run a server Docker image automatically, but it'll
25+
also use an existing image if it's previously running. You could run a bblfsh
26+
server image with:
27+
28+
```bash
29+
docker run --privileged -d -p 9432:9432 --name bblfsh bblfsh/server
30+
```
31+
2032
### Installation
2133

34+
#### Dependencies
35+
36+
You need to install `libxml2` and its header files. The command for
37+
Debian and derived distributions would be:
38+
39+
```bash
40+
sudo apt install libxml2-dev
41+
```
42+
2243
#### From the source code
2344

2445
```bash

bblfsh/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import os
22
import sys
3+
import importlib
34

45
import grpc
56

7+
from bblfsh.sdkversion import VERSION
8+
69
# The following two insertions fix the broken pb import paths
710
sys.path.insert(0, os.path.join(os.path.dirname(__file__),
8-
"github/com/bblfsh/sdk/protocol"))
11+
"gopkg/in/bblfsh/sdk/%s/protocol" % VERSION))
912
sys.path.insert(0, os.path.dirname(__file__))
1013

14+
1115
class BblfshClient(object):
1216
"""
1317
Babelfish gRPC client. Currently it is only capable of fetching UASTs.
@@ -21,7 +25,9 @@ def __init__(self, endpoint):
2125
for example "0.0.0.0:9432"
2226
:type endpoint: str
2327
"""
24-
from bblfsh.github.com.bblfsh.sdk.protocol.generated_pb2_grpc import ProtocolServiceStub
28+
ProtocolServiceStub = importlib.import_module(
29+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2_grpc" % VERSION
30+
).ProtocolServiceStub
2531

2632
self._channel = grpc.insecure_channel(endpoint)
2733
self._stub = ProtocolServiceStub(self._channel)
@@ -48,7 +54,9 @@ def parse(self, filename, language=None, contents=None, timeout=None,
4854
:type timeout: float
4955
:return: UAST object.
5056
"""
51-
from bblfsh.github.com.bblfsh.sdk.protocol.generated_pb2 import ParseRequest
57+
ParseRequest = importlib.import_module(
58+
"bblfsh.gopkg.in.bblfsh.sdk.%s.protocol.generated_pb2" % VERSION
59+
).ParseRequest
5260

5361
if contents is None:
5462
with open(filename, errors=unicode_errors) as fin:
@@ -58,7 +66,6 @@ def parse(self, filename, language=None, contents=None, timeout=None,
5866
language=self._scramble_language(language))
5967
return self._stub.Parse(request, timeout=timeout)
6068

61-
6269
@staticmethod
6370
def _scramble_language(lang):
6471
if lang is None:
File renamed without changes.
File renamed without changes.

bblfsh/gopkg/in/bblfsh/sdk/v0/__init__.py

Whitespace-only changes.

bblfsh/gopkg/in/bblfsh/sdk/v0/protocol/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)