67 Provides functions to determine whether a piece of Fortran source is free or 68 fixed format. It also tries to differentiate between strict and "pyf" although 69 I'm not sure what that is. 81 Describes the nature of a piece of Fortran source. 83 Source can be fixed or free format. It can also be "strict" or 84 "not strict" although it's not entirely clear what that means. It may 85 refer to the strictness of adherance to fixed format although what that 86 means in the context of free format I don't know. 88 :param bool is_free: True for free format, False for fixed. 89 :param bool is_strict: some amount of strictness. 90 :param bool enable_f2py: whether f2py directives are enabled or treated \ 91 as comments (the default). 94 def __init__(self, is_free, is_strict, enable_f2py=False):
96 raise Exception(
"FortranFormat does not accept a None is_free")
98 raise Exception(
"FortranFormat does not accept a None is_strict")
107 Constructs a FortranFormat object from a mode string. 110 mode - (String) One of 'free', 'fix', 'f77' or 'pyf' 113 is_free, is_strict =
True,
False 115 is_free, is_strict =
False,
False 117 is_free, is_strict =
False,
True 119 is_free, is_strict =
True,
True 121 raise NotImplementedError(repr(mode))
122 return cls(is_free, is_strict)
124 def __eq__(self, other):
125 if isinstance(other, FortranFormat):
131 raise NotImplementedError
137 string =
"Non-strict" 144 return string +
" format" 149 Returns true for free format. 156 Returns true for fixed format. 163 Returns true for strict format. 170 Returns true for strict fixed format. 177 Returns true for slack fixed format. 184 Returns true for strict free format. 189 def f2py_enabled(self):
191 :returns: whether or not f2py directives are enabled. 199 Returns a string representing this format. 217 _HAS_F_EXTENSION = re.compile(
r".*[.](for|ftn|f77|f)\Z", re.I).match
219 _HAS_F_HEADER = re.compile(
r"-[*]-\s*(fortran|f77)\s*-[*]-", re.I).search
220 _HAS_F90_HEADER = re.compile(
r"-[*]-\s*f90\s*-[*]-", re.I).search
221 _HAS_F03_HEADER = re.compile(
r"-[*]-\s*f03\s*-[*]-", re.I).search
222 _HAS_F08_HEADER = re.compile(
r"-[*]-\s*f08\s*-[*]-", re.I).search
223 _HAS_FREE_HEADER = re.compile(
r"-[*]-\s*(f90|f95|f03|f08)\s*-[*]-", re.I).search
224 _HAS_FIX_HEADER = re.compile(
r"-[*]-\s*fix\s*-[*]-", re.I).search
225 _HAS_PYF_HEADER = re.compile(
r"-[*]-\s*pyf\s*-[*]-", re.I).search
227 _FREE_FORMAT_START = re.compile(
r"[^c*!]\s*[^\s\d\t]", re.I).match
230 def get_source_info_str(source):
232 Determines the format of Fortran source held in a string. 234 Returns a FortranFormat object. 236 lines = source.splitlines()
240 firstline = lines[0].lstrip()
241 if _HAS_F_HEADER(firstline):
243 if _HAS_FIX_HEADER(firstline):
245 if _HAS_FREE_HEADER(firstline):
247 if _HAS_PYF_HEADER(firstline):
252 while line_tally > 0
and lines:
253 line = lines.pop(0).rstrip()
254 if line
and line[0] !=
"!":
256 if line[0] !=
"\t" and _FREE_FORMAT_START(line[:5])
or line[-1:] ==
"&":
266 def get_source_info(file_candidate):
268 Determines the format of Fortran source held in a file. 270 :param file_candidate: a filename or a file object 271 :type file_candidate: str or _io.TextIOWrapper (py3) 273 :returns: the Fortran format encoded as a string. 277 if hasattr(file_candidate,
"name")
and hasattr(file_candidate,
"read"):
278 filename = file_candidate.name
282 if isinstance(filename, int):
285 elif isinstance(file_candidate, str):
286 filename = file_candidate
288 message =
"Argument must be a filename or file-like object." 289 raise ValueError(message)
292 _, ext = os.path.splitext(filename)
296 if hasattr(file_candidate,
"read"):
304 pointer = file_candidate.tell()
305 file_candidate.seek(0)
306 source_info = get_source_info_str(file_candidate.read())
307 file_candidate.seek(pointer)
323 file_candidate,
"r", encoding="utf-8", errors="fparser-logging" )
as file_object:
324 string = get_source_info_str(file_object.read())