Пример #1
0
def get_reader(input, isfree=None, isstrict=None, include_dirs=None):
    import os
    import re
    from readfortran import FortranFileReader, FortranStringReader
    from parsefortran import FortranParser
    if os.path.isfile(input):
        name, ext = os.path.splitext(input)
        if ext.lower() in ['.c']:
            # get signatures from C file comments starting with `/*f2py` and ending with `*/`.
            # TODO: improve parser to take line number offset making line numbers in
            #       parser messages correct.
            f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/', re.I | re.M)
            f = open(filename, 'r')
            c_input = ''
            for s1 in f2py_c_comments.findall(f.read()):
                c_input += s1[2:-2].lstrip()[4:] + '\n'
            f.close()
            if isfree is None: isfree = True
            if isstrict is None: isstrict = True
            return parse(c_input, isfree, isstrict, include_dirs)
        reader = FortranFileReader(input, include_dirs=include_dirs)
        if isfree is None: isfree = reader.isfree
        if isstrict is None: isstrict = reader.isstrict
        reader.set_mode(isfree, isstrict)
    elif isinstance(input, str):
        if isfree is None: isfree = True
        if isstrict is None: isstrict = False
        reader = FortranStringReader(input,
                                     isfree,
                                     isstrict,
                                     include_dirs=include_dirs)
    else:
        raise TypeError, 'Expected string or filename input but got %s' % (
            type(input))
    return reader
Пример #2
0
def get_reader(input, isfree=None, isstrict=None, include_dirs = None, source_only = None,
               ignore_comments = True):
    """ Returns Fortran reader instance.

    Parameters
    ----------
    input : str
      Specify a string or filename containing Fortran code.    
    isfree, isstrict : {None, bool}
      Specify input Fortran format. The values are determined from the
      input. If that fails then isfree=True and isstrict=False is assumed.
    include_dirs : {None, list}
      Specify a list of include directories. The default list (when
      include_dirs=None) contains the current working directory and
      the directory of ``filename``.
    source_only : {None, list}
      Specify a list of Fortran file names that are searched when the
      ``USE`` statement is encountered.

    Returns
    -------
    reader : `FortranReader`

    Notes
    -----
    If ``input`` is a C filename then the functions searches for comment
    lines starting with ``/*f2py`` and reads following lines as PYF file
    content until a line ``*/`` is found.

    See also
    --------
    parse
    """
    import os
    import re
    from readfortran import FortranFileReader, FortranStringReader
    if os.path.isfile(input):
        name,ext = os.path.splitext(input)
        if ext.lower() in ['.c']:
            # get signatures from C file comments starting with `/*f2py` and ending with `*/`.
            # TODO: improve parser to take line number offset making line numbers in
            #       parser messages correct.
            f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/',re.I | re.M)
            f = open(filename,'r')
            c_input = ''
            for s1 in f2py_c_comments.findall(f.read()):
                c_input += s1[2:-2].lstrip()[4:] + '\n'
            f.close()
            if isfree is None: isfree = True
            if isstrict is None: isstrict = True
            return parse(c_input, isfree, isstrict, include_dirs)
        reader = FortranFileReader(input, include_dirs = include_dirs, source_only = source_only)
    elif isinstance(input, str):
        reader = FortranStringReader(input, include_dirs = include_dirs, source_only = source_only)
    else:
        raise TypeError,'Expected string or filename input but got %s' % (type(input))
    if isfree is None: isfree = reader.isfree
    if isstrict is None: isstrict = reader.isstrict
    reader.set_mode(isfree, isstrict)
    return reader
Пример #3
0
def get_reader(input, isfree=None, isstrict=None, include_dirs = None):
    import os
    import re
    from readfortran import FortranFileReader, FortranStringReader
    from parsefortran import FortranParser
    if os.path.isfile(input):
        name,ext = os.path.splitext(input)
        if ext.lower() in ['.c']:
            # get signatures from C file comments starting with `/*f2py` and ending with `*/`.
            # TODO: improve parser to take line number offset making line numbers in
            #       parser messages correct.
            f2py_c_comments = re.compile('/[*]\s*f2py\s.*[*]/',re.I | re.M)
            f = open(filename,'r')
            c_input = ''
            for s1 in f2py_c_comments.findall(f.read()):
                c_input += s1[2:-2].lstrip()[4:] + '\n'
            f.close()
            if isfree is None: isfree = True
            if isstrict is None: isstrict = True
            return parse(c_input, isfree, isstrict, include_dirs)
        reader = FortranFileReader(input,
                                   include_dirs = include_dirs)
        if isfree is None: isfree = reader.isfree
        if isstrict is None: isstrict = reader.isstrict
        reader.set_mode(isfree, isstrict)
    elif isinstance(input, str):
        if isfree is None: isfree = True
        if isstrict is None: isstrict = False
        reader = FortranStringReader(input,
                                     isfree, isstrict,
                                     include_dirs = include_dirs)
    else:
        raise TypeError,'Expected string or filename input but got %s' % (type(input))
    return reader