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

Commit d749d09

Browse files
committed
Merge pull request #131 from hit9/enable-multiple-include-dirs
Enable multiple include dirs
2 parents 6168106 + c6caf35 commit d749d09

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

tests/test_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ def test_constants():
2828

2929

3030
def test_include():
31-
thrift = load('parser-cases/include.thrift', include_dir='./parser-cases')
31+
thrift = load('parser-cases/include.thrift', include_dirs=[
32+
'./parser-cases'])
3233
assert thrift.datetime == 1422009523
3334

3435

3536
def test_tutorial():
36-
thrift = load('parser-cases/tutorial.thrift', include_dir='./parser-cases')
37+
thrift = load('parser-cases/tutorial.thrift', include_dirs=[
38+
'./parser-cases'])
3739
assert thrift.INT32CONSTANT == 9853
3840
assert thrift.MAPCONSTANT == {'hello': 'world', 'goodnight': 'moon'}
3941
assert thrift.Operation.ADD == 1 and thrift.Operation.SUBTRACT == 2 \

thriftpy/parser/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
from .parser import parse
1616

1717

18-
def load(path, module_name=None, include_dir=None):
18+
def load(path, module_name=None, include_dirs=None, include_dir=None):
1919
"""Load thrift_file as a module
2020
The module loaded and objects inside may only be pickled if module_name
2121
was provided.
22+
23+
Note: `include_dir` will be depreacated in the future, use `include_dirs`
24+
instead. If `include_dir` was provided (not None), it will be appended to
25+
`include_dirs`.
2226
"""
2327
real_module = bool(module_name)
24-
thrift = parse(path, module_name, include_dir=include_dir)
28+
thrift = parse(path, module_name, include_dirs=include_dirs,
29+
include_dir=include_dir)
2530

2631
if real_module:
2732
sys.modules[module_name] = thrift

thriftpy/parser/parser.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ def p_header_unit(p):
4444
def p_include(p):
4545
'''include : INCLUDE LITERAL'''
4646
thrift = thrift_stack[-1]
47-
path = os.path.join(include_dir_, p[2])
48-
child = parse(path)
49-
setattr(thrift, child.__name__, child)
47+
48+
for include_dir in include_dirs_:
49+
path = os.path.join(include_dir, p[2])
50+
if os.path.exists(path):
51+
child = parse(path)
52+
setattr(thrift, child.__name__, child)
53+
return
54+
raise ThriftParserError(('Couldn\'t include thrift %s in any '
55+
'directories provided') % p[2])
5056

5157

5258
def p_namespace(p):
@@ -390,11 +396,11 @@ def p_definition_type(p):
390396

391397

392398
thrift_stack = []
393-
include_dir_ = '.'
399+
include_dirs_ = ['.']
394400
thrift_cache = {}
395401

396402

397-
def parse(path, module_name=None, include_dir=None,
403+
def parse(path, module_name=None, include_dirs=None, include_dir=None,
398404
lexer=None, parser=None, enable_cache=True):
399405

400406
# dead include checking on current stack
@@ -414,10 +420,12 @@ def parse(path, module_name=None, include_dir=None,
414420
if parser is None:
415421
parser = yacc.yacc(debug=False, write_tables=0)
416422

417-
global include_dir_
423+
global include_dirs_
418424

425+
if include_dirs is not None:
426+
include_dirs_ = include_dirs
419427
if include_dir is not None:
420-
include_dir_ = include_dir
428+
include_dirs_.append(include_dir)
421429

422430
if not path.endswith('.thrift'):
423431
raise ThriftParserError('Path should end with .thrift')

0 commit comments

Comments
 (0)