(path, module_name=None, include_dirs=None, include_dir=None,
          lexer=None, parser=None, enable_cache=True)
return thrift
Parse a single thrift file to module object, e.g.::

        >>> from thriftpy.parser.parser import parse
        >>> note_thrift = parse("path/to/note.thrift")
        <module 'note_thrift' (built-in)>

    :param path: file path to parse, should be a string ending with '.thrift'.
    :param module_name: the name for parsed module, the default is the basename
                        without extension of `path`.
    :param include_dirs: directories to find thrift files while processing
                         the `include` directive, by default: ['.'].
    :param include_dir: directory to find child thrift files. Note this keyword
                        parameter will be deprecated in the future, it exists
                        for compatiable reason. If it's provided (not `None`),
                        it will be appended to `include_dirs`.
    :param lexer: ply lexer to use, if not provided, `parse` will new one.
    :param parser: ply parser to use, if not provided, `parse` will new one.
    :param enable_cache: if this is set to be `True`, parsed module will be
                         cached, this is enabled by default. If `module_name`
                         is provided, use it as cache key, else use the `path`.
Parse a single thrift file to module object, e.g.::
Parse a single thrift file to module object e . g . ::
def parse(path, module_name=None, include_dirs=None, include_dir=None,
          lexer=None, parser=None, enable_cache=True):
    """Parse a single thrift file to module object, e.g.::

        >>> from thriftpy.parser.parser import parse
        >>> note_thrift = parse("path/to/note.thrift")
        <module 'note_thrift' (built-in)>

    :param path: file path to parse, should be a string ending with '.thrift'.
    :param module_name: the name for parsed module, the default is the basename
                        without extension of `path`.
    :param include_dirs: directories to find thrift files while processing
                         the `include` directive, by default: ['.'].
    :param include_dir: directory to find child thrift files. Note this keyword
                        parameter will be deprecated in the future, it exists
                        for compatiable reason. If it's provided (not `None`),
                        it will be appended to `include_dirs`.
    :param lexer: ply lexer to use, if not provided, `parse` will new one.
    :param parser: ply parser to use, if not provided, `parse` will new one.
    :param enable_cache: if this is set to be `True`, parsed module will be
                         cached, this is enabled by default. If `module_name`
                         is provided, use it as cache key, else use the `path`.
    """
    if os.name == 'nt' and sys.version_info[0] < 3:
        os.path.samefile = lambda f1, f2: os.stat(f1) == os.stat(f2)

    # dead include checking on current stack
    for thrift in thrift_stack:
        if thrift.__thrift_file__ is not None and \
                os.path.samefile(path, thrift.__thrift_file__):
            raise ThriftParserError('Dead including on %s' % path)

    global thrift_cache

    cache_key = module_name or os.path.normpath(path)

    if enable_cache and cache_key in thrift_cache:
        return thrift_cache[cache_key]

    if lexer is None:
        lexer = lex.lex()
    if parser is None:
        parser = yacc.yacc(debug=False, write_tables=0)

    global include_dirs_

    if include_dirs is not None:
        include_dirs_ = include_dirs
    if include_dir is not None:
        include_dirs_.append(include_dir)

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

    url_scheme = urlparse(path).scheme
    if url_scheme == 'file':
        with open(urlparse(path).netloc + urlparse(path).path) as fh:
            data = fh.read()
    elif 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 '
                                '\'_thrift\' suffix')

    if module_name is None:
        basename = os.path.basename(path)
        module_name = os.path.splitext(basename)[0]

    thrift = types.ModuleType(module_name)
    setattr(thrift, '__thrift_file__', path)
    thrift_stack.append(thrift)
    lexer.lineno = 1
    parser.parse(data)
    thrift_stack.pop()

    if enable_cache:
        thrift_cache[cache_key] = thrift
    return thrift
def parse ( path , module_name = None , include_dirs = None , include_dir = None , lexer = None , parser = None , enable_cache = True ) : if os . name == 'nt' and sys . version_info [ 0 ] < 3 : os . path . samefile = lambda f1 , f2 : os . stat ( f1 ) == os . stat ( f2 ) # dead include checking on current stack for thrift in thrift_stack : if thrift . __thrift_file__ is not None and os . path . samefile ( path , thrift . __thrift_file__ ) : raise ThriftParserError ( 'Dead including on %s' % path ) global thrift_cache cache_key = module_name or os . path . normpath ( path ) if enable_cache and cache_key in thrift_cache : return thrift_cache [ cache_key ] if lexer is None : lexer = lex . lex ( ) if parser is None : parser = yacc . yacc ( debug = False , write_tables = 0 ) global include_dirs_ if include_dirs is not None : include_dirs_ = include_dirs if include_dir is not None : include_dirs_ . append ( include_dir ) if not path . endswith ( '.thrift' ) : raise ThriftParserError ( 'Path should end with .thrift' ) url_scheme = urlparse ( path ) . scheme if url_scheme == 'file' : with open ( urlparse ( path ) . netloc + urlparse ( path ) . path ) as fh : data = fh . read ( ) elif 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 ' '\'_thrift\' suffix' ) if module_name is None : basename = os . path . basename ( path ) module_name = os . path . splitext ( basename ) [ 0 ] thrift = types . ModuleType ( module_name ) setattr ( thrift , '__thrift_file__' , path ) thrift_stack . append ( thrift ) lexer . lineno = 1 parser . parse ( data ) thrift_stack . pop ( ) if enable_cache : thrift_cache [ cache_key ] = thrift return thrift