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

Commit

Permalink
Add support for thrift_file path in http protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
azurefang committed Aug 8, 2016
1 parent e9c087f commit f0a3514
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions thriftpy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
if PY3:
text_type = str
string_types = (str,)
from urllib.request import urlopen
from urllib.parse import urlparse

def u(s):
return s
else:
text_type = unicode # noqa
string_types = (str, unicode) # noqa
from urllib2 import urlopen
from urlparse import urlparse

def u(s):
if not isinstance(s, text_type):
Expand Down
13 changes: 11 additions & 2 deletions thriftpy/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ply import lex, yacc
from .lexer import * # noqa
from .exc import ThriftParserError, ThriftGrammerError
from thriftpy._compat import urlopen, urlparse
from ..thrift import gen_init, TType, TPayload, TException


Expand Down Expand Up @@ -484,8 +485,16 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
if not path.endswith('.thrift'):
raise ThriftParserError('Path should end with .thrift')

with open(path) as fh:
data = fh.read()
url_scheme = urlparse(path).scheme
if url_scheme == '':
with open(path) as fh:
data = fh.read()
elif url_scheme in ('http', 'https'):
data = urlopen(path).read()
else:
raise ThriftParserError('ThriftPy does not support generating module '
'with path in protocol \'{}\''.format(
url_scheme))

if module_name is not None and not module_name.endswith('_thrift'):
raise ThriftParserError('ThriftPy can only generate module with '
Expand Down

0 comments on commit f0a3514

Please sign in to comment.