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

Commit

Permalink
Merges python contexts and adds ResultContext encode
Browse files Browse the repository at this point in the history
Signed-off-by: ncordon <[email protected]>
  • Loading branch information
ncordon committed Sep 11, 2019
1 parent 05f7ac2 commit d1b0798
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bblfsh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from bblfsh.tree_order import TreeOrder
from bblfsh.aliases import *
from bblfsh.roles import role_id, role_name
from bblfsh.context import context
from bblfsh.result_context import context
23 changes: 0 additions & 23 deletions bblfsh/context.py

This file was deleted.

27 changes: 27 additions & 0 deletions bblfsh/result_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def iterate(self, order: int) -> NodeIterator:
TreeOrder.check_order(order)
return NodeIterator(iterator(self.ctx.root(), order), self.ctx)

# Encode in binary format by default
def encode(self, node: dict = None, fmt: int = 0):
encoded = self.ctx.encode(node, fmt)
return encoded

@property
def language(self) -> str:
return self._response.language
Expand All @@ -65,3 +70,25 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return repr(self.get_all())


# Python context
class Context:
def __init__(self, root: dict) -> None:
self.ctx = uast()
self.root = root

def filter(self, query: str) -> dict:
return self.ctx.filter(query, self.root)

def iterate(self, order: int) -> iterator:
TreeOrder.check_order(order)
return iterator(self.root, order)

def encode(self, fmt: int = 0):
encoded = self.ctx.encode(self.root, fmt)
return encoded


def context(root: dict) -> Context:
return Context(root)
18 changes: 13 additions & 5 deletions bblfsh/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,22 @@ def testSupportedLanguages(self) -> None:

def testEncode(self) -> None:
ctx = self._parse_fixture()
# This test is here for backward compatibility purposes,
# in case someone was relying on encoding contexts this way
self.assertEqual(ctx.ctx.encode(None, 0), ctx._response.uast)
self.assertEqual(ctx.encode(), ctx._response.uast)

def testEncodeWithEmptyContext(self) -> None:
ctx = ResultContext()
obj = {"k1": "v1", "k2": "v2"}
fmt = 1 # YAML

# This test is here for backward compatibility purposes,
# in case someone was relying on encoding contexts this way
data = ctx.ctx.encode(obj, fmt)
self.assertDictEqual(obj, decode(data, format=fmt).load())
other_data = ctx.encode(obj, fmt)
self.assertDictEqual(obj, decode(data, format = fmt).load())
self.assertDictEqual(obj, decode(other_data, format = fmt).load())

def testGetAll(self) -> None:
ctx = self._parse_fixture()
Expand Down Expand Up @@ -560,16 +567,17 @@ def testPythonContextFilter(self) -> None:
for nodeC, nodePy in zip(itC, itPy):
self.assertEqual(nodeC.get(), nodePy)

def testEncodeDecodePythonContext(self) -> None:
def testBinaryEncodeDecodePythonContext(self) -> None:
# Binary encoding should be invertible
# C++ memory context
ctxC = self._parse_fixture()
# Python memory context
pyDict = ctxC.root.get()
ctxPy = bblfsh.context(pyDict)
encoded = ctxPy.encode(fmt = 1) # YAML
decoded = decode(encoded, format = 1)
encoded = ctxPy.encode(fmt = 0) # Binary encoding
decoded = decode(encoded, format = 0)

self.assertEqual(encoded, decoded.load())
self.assertEqual(pyDict, decoded.load())

if __name__ == "__main__":
unittest.main()

0 comments on commit d1b0798

Please sign in to comment.