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

Add support for thrift_file path in 'http' protocol #225

Merged
merged 1 commit into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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