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

Commit

Permalink
Merge pull request #138 from hit9/add-interface-for-services-etc
Browse files Browse the repository at this point in the history
Add thrift_meta etc.
  • Loading branch information
maralla committed Jun 15, 2015
2 parents 08219bc + 5a7fd7d commit f9f0ca4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
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]
36 changes: 30 additions & 6 deletions thriftpy/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import absolute_import

import collections
import os
import types
from ply import lex, yacc
Expand Down Expand Up @@ -50,6 +51,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 +102,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 +180,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 +204,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 +245,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 +466,18 @@ 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 = collections.defaultdict(list)
setattr(thrift, '__thrift_meta__', meta)
else:
meta = getattr(thrift, '__thrift_meta__')

meta[key].append(val)


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

0 comments on commit f9f0ca4

Please sign in to comment.