Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Add thrift_meta etc. #138

Merged
merged 3 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,14 @@ def test_e_use_thrift_reserved_keywords():
with pytest.raises(ThriftParserError) as excinfo:
load('parser-cases/e_use_thrift_reserved_keywords.thrift')
assert 'Cannot use reserved language keyword' in str(excinfo.value)


def test_thrift_meta():
thrift = load('parser-cases/tutorial.thrift')
meta = thrift.__thrift_meta__
assert meta['consts'] == [thrift.INT32CONSTANT, thrift.MAPCONSTANT]
assert meta['enums'] == [thrift.Operation]
assert meta['structs'] == [thrift.Work]
assert meta['exceptions'] == [thrift.InvalidOperation]
assert meta['services'] == [thrift.Calculator]
assert meta['includes'] == [thrift.shared]
38 changes: 32 additions & 6 deletions thriftpy/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def p_include(p):
if os.path.exists(path):
child = parse(path)
setattr(thrift, child.__name__, child)
_add_thrift_meta('includes', child)
return
raise ThriftParserError(('Couldn\'t include thrift %s in any '
'directories provided') % p[2])
Expand Down Expand Up @@ -100,6 +101,7 @@ def p_const(p):
raise ThriftParserError('Type error for constant %s at line %d' %
(p[3], p.lineno(3)))
setattr(thrift_stack[-1], p[3], val)
_add_thrift_meta('consts', val)


def p_const_value(p):
Expand Down Expand Up @@ -177,7 +179,9 @@ def p_typedef(p):

def p_enum(p): # noqa
'''enum : ENUM IDENTIFIER '{' enum_seq '}' '''
setattr(thrift_stack[-1], p[2], _make_enum(p[2], p[4]))
val = _make_enum(p[2], p[4])
setattr(thrift_stack[-1], p[2], val)
_add_thrift_meta('enums', val)


def p_enum_seq(p):
Expand All @@ -199,18 +203,23 @@ def p_enum_item(p):

def p_struct(p):
'''struct : STRUCT IDENTIFIER '{' field_seq '}' '''
setattr(thrift_stack[-1], p[2], _make_struct(p[2], p[4]))
val = _make_struct(p[2], p[4])
setattr(thrift_stack[-1], p[2], val)
_add_thrift_meta('structs', val)


def p_union(p):
'''union : UNION IDENTIFIER '{' field_seq '}' '''
setattr(thrift_stack[-1], p[2], _make_struct(p[2], p[4]))
val = _make_struct(p[2], p[4])
setattr(thrift_stack[-1], p[2], val)
_add_thrift_meta('unions', val)


def p_exception(p):
'''exception : EXCEPTION IDENTIFIER '{' field_seq '}' '''
setattr(thrift_stack[-1], p[2], _make_struct(p[2], p[4],
base_cls=TException))
val = _make_struct(p[2], p[4], base_cls=TException)
setattr(thrift_stack[-1], p[2], val)
_add_thrift_meta('exceptions', val)


def p_service(p):
Expand All @@ -235,7 +244,9 @@ def p_service(p):
else:
extends = None

setattr(thrift, p[2], _make_service(p[2], p[len(p) - 2], extends))
val = _make_service(p[2], p[len(p) - 2], extends)
setattr(thrift, p[2], val)
_add_thrift_meta('services', val)


def p_function(p):
Expand Down Expand Up @@ -454,6 +465,21 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
return thrift


def _add_thrift_meta(key, val):
thrift = thrift_stack[-1]

if not hasattr(thrift, '__thrift_meta__'):
meta = {}
setattr(thrift, '__thrift_meta__', meta)
else:
meta = getattr(thrift, '__thrift_meta__')

if key not in meta:
meta[key] = []

meta[key].append(val)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use defaultdict here:

if not hasattr(thrift, '__thrift_meta__'):
    setattr(thrift, '__thrift_meta__', collections.defaultdict(list))
thrift.__thrift_meta__.append(val)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



def _parse_seq(p):
if len(p) == 4:
p[0] = [p[1]] + p[3]
Expand Down