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

Commit 8fb23b8

Browse files
authored
Merge pull request #23 from juanjux/add-libuast
Add libuast, upgrade libuast API, bugfixing
2 parents 60bf747 + 22d9675 commit 8fb23b8

File tree

13 files changed

+291
-32
lines changed

13 files changed

+291
-32
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ ENV/
101101

102102
# mypy
103103
.mypy_cache/
104+
105+
libuast

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ python:
1010
- "3.5"
1111
- "3.6"
1212
install:
13-
- pip install --upgrade pip
14-
- pip install grpcio
15-
- CC=gcc-5 CXX=g++-5 pip install -e .
13+
- pip3 install --upgrade pip
14+
- pip3 install grpcio
15+
- make bblfsh/libuast
16+
- pip3 install . --upgrade
1617
script:
18+
- python3 setup.py build_ext -i
1719
- python3 -m unittest discover .
1820
notifications:
1921
email: false

Makefile

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@ PYTHON ?= python3
22

33
makefile_dir := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
44

5-
all: bblfsh/github/com/gogo/protobuf/gogoproto/gogo_pb2.py \
6-
bblfsh/github/com/bblfsh/sdk/uast/generated_pb2.py \
7-
bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2_*.py \
8-
bblfsh/github/__init__.py \
9-
bblfsh/github/com/__init__.py \
10-
bblfsh/github/com/gogo/__init__.py \
11-
bblfsh/github/com/gogo/protobuf/__init__.py \
12-
bblfsh/github/com/gogo/protobuf/gogoproto/__init__.py \
13-
bblfsh/github/com/bblfsh/__init__.py \
14-
bblfsh/github/com/bblfsh/sdk/__init__.py \
15-
bblfsh/github/com/bblfsh/sdk/uast/__init__.py \
16-
bblfsh/github/com/bblfsh/sdk/protocol/__init__.py
5+
LIBUAST_VERSION = v0.2.0
6+
7+
.PHONY : all clean deps
8+
9+
all: deps \
10+
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 \
13+
bblfsh/github/__init__.py \
14+
bblfsh/github/com/__init__.py \
15+
bblfsh/github/com/gogo/__init__.py \
16+
bblfsh/github/com/gogo/protobuf/__init__.py \
17+
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
22+
23+
clean:
24+
rm -rf bblfsh/libuast
25+
rm -rf bblfsh/github
26+
27+
deps: bblfsh/libuast
28+
29+
bblfsh/libuast:
30+
curl -SL https://github.com/bblfsh/libuast/releases/download/$(LIBUAST_VERSION)/libuast-$(LIBUAST_VERSION).tar.gz | tar xz
31+
mv libuast-$(LIBUAST_VERSION) libuast
32+
cp -a libuast/src bblfsh/libuast
33+
rm -rf libuast
1734

1835
bblfsh/github/com/gogo/protobuf/gogoproto/gogo_pb2.py: github.com/gogo/protobuf/gogoproto/gogo.proto
1936
protoc --python_out bblfsh github.com/gogo/protobuf/gogoproto/gogo.proto
@@ -25,7 +42,7 @@ bblfsh/github/com/bblfsh/sdk/protocol:
2542
@mkdir -p $@
2643

2744
bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2_*.py: \
28-
bblfsh/github/com/bblfsh/sdk/protocol github.com/bblfsh/sdk/protocol/generated.proto
45+
bblfsh/github/com/bblfsh/sdk/protocol github.com/bblfsh/sdk/protocol/generated.proto
2946
$(PYTHON) -m grpc.tools.protoc --python_out=bblfsh/github/com/bblfsh/sdk/protocol \
3047
--grpc_python_out=bblfsh/github/com/bblfsh/sdk/protocol \
3148
-I github.com/bblfsh/sdk/protocol -I $(makefile_dir) \

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ python3 -m bblfsh -f file.py
1919

2020
### Installation
2121

22+
#### From the source code
23+
24+
```bash
25+
git clone https://github.com/bblfsh/client-python.git
26+
cd client-python
27+
make install
2228
```
29+
30+
#### Using pip3
31+
32+
```bash
2333
pip3 install bblfsh
2434
```
2535

bblfsh/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from bblfsh.client import BblfshClient
2+
from bblfsh.pyuast import filter

bblfsh/__main__.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import argparse
22
import sys
33

4+
from bblfsh.pyuast import filter
5+
46
from bblfsh.client import BblfshClient
57
from bblfsh.launcher import ensure_bblfsh_is_running
68

@@ -18,17 +20,46 @@ def setup():
1820
parser.add_argument("--disable-bblfsh-autorun", action="store_true",
1921
help="Do not automatically launch Babelfish server "
2022
"if it is not running.")
23+
24+
parser.add_argument("-q", "--query", default="", help="xpath query")
25+
parser.add_argument("-m", "--mapn", default="", help="transform function of the results (n)")
26+
parser.add_argument("-a", "--array", help='print results as an array', action='store_true')
27+
2128
args = parser.parse_args()
2229
return args
2330

31+
def run_query(root, query, mapn, as_array):
32+
result = filter(root, query)
33+
34+
if not result:
35+
print("Nothing found")
36+
37+
else:
38+
if mapn:
39+
result = [eval(mapn) for n in result]
40+
41+
if as_array:
42+
print("results[{}] = {}".format(len(result), result))
43+
else:
44+
print("Running xpath query: {}".format(query))
45+
print("FOUND {} roots".format(len(result)))
46+
47+
for i, node in enumerate(result):
48+
print("== {} ==================================".format(i+1))
49+
print(node)
2450

2551
def main():
2652
args = setup()
2753
if not args.disable_bblfsh_autorun:
2854
ensure_bblfsh_is_running()
29-
client = BblfshClient(args.endpoint)
30-
print(client.parse(args.file, args.language))
3155

56+
client = BblfshClient(args.endpoint)
57+
root = client.parse(args.file, args.language).uast
58+
query = args.query
59+
if query:
60+
run_query(root, query, args.mapn, args.array)
61+
else:
62+
print(root)
3263

3364
if __name__ == "__main__":
3465
sys.exit(main())

bblfsh/client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"github/com/bblfsh/sdk/protocol"))
99
sys.path.insert(0, os.path.dirname(__file__))
1010

11-
1211
class BblfshClient(object):
1312
"""
1413
Babelfish gRPC client. Currently it is only capable of fetching UASTs.
@@ -28,13 +27,13 @@ def __init__(self, endpoint):
2827
self._stub = ProtocolServiceStub(self._channel)
2928

3029
def parse(self, filename, language=None, contents=None, timeout=None,
31-
unicode_errors="ignore"):
30+
unicode_errors="ignore"):
3231
"""
33-
Queries the Babelfish server and receives the UAST for the specified
32+
Queries the Babelfish server and receives the UAST response for the specified
3433
file.
3534
3635
:param filename: The path to the file. Can be arbitrary if contents \
37-
is not None.
36+
is not None.
3837
:param language: The programming language of the file. Refer to \
3938
https://doc.bblf.sh/languages.html for the list of \
4039
currently supported languages. None means autodetect.
@@ -57,8 +56,8 @@ def parse(self, filename, language=None, contents=None, timeout=None,
5756
request = ParseRequest(filename=os.path.basename(filename),
5857
content=contents,
5958
language=self._scramble_language(language))
60-
response = self._stub.Parse(request, timeout=timeout)
61-
return response
59+
return self._stub.Parse(request, timeout=timeout)
60+
6261

6362
@staticmethod
6463
def _scramble_language(lang):

bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2.py

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bblfsh/github/com/bblfsh/sdk/protocol/generated_pb2_grpc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66

77
class ProtocolServiceStub(object):
8+
# missing associated documentation comment in .proto file
9+
pass
810

911
def __init__(self, channel):
1012
"""Constructor.
@@ -20,6 +22,8 @@ def __init__(self, channel):
2022

2123

2224
class ProtocolServiceServicer(object):
25+
# missing associated documentation comment in .proto file
26+
pass
2327

2428
def Parse(self, request, context):
2529
"""Parse uses DefaultParser to process the given parsing request to get the UAST.

bblfsh/github/com/gogo/protobuf/gogoproto/gogo_pb2.py

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)