diff --git a/thriftpy/_compat.py b/thriftpy/_compat.py index eabf603..fab003a 100644 --- a/thriftpy/_compat.py +++ b/thriftpy/_compat.py @@ -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): diff --git a/thriftpy/parser/parser.py b/thriftpy/parser/parser.py index f93c305..0064a12 100644 --- a/thriftpy/parser/parser.py +++ b/thriftpy/parser/parser.py @@ -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 @@ -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 '