4This module contains the main PYFT class
5PYFT is the file level class (read/write...)
10from multiprocessing
import Lock, RLock
14from pyfortool.util import (debugDecor, tostring, tofortran, fortran2xml,
15 setVerbosity, printInfos, PYFTError)
19def conservativePYFT(filename, parser, parserOptions, wrapH,
20 tree=None, verbosity=None, clsPYFT=None):
22 Return a conservative PYFT object usable for tree manipulation
23 :param filename: name of the file to open
24 :param parser, parserOptions, wrapH: see the PYFT class
25 :param tree: Tree instance or None
26 :param verbosity: if not None, sets the verbosity level
27 :param clsPYFT: PYFT class to use
30 options = PYFT.DEFAULT_FXTRAN_OPTIONS
if parserOptions
is None else parserOptions
31 options = options.copy()
32 if len(set(options).intersection((
'-no-include',
'-noinclude'))) == 0:
34 options.append(
'-no-include')
37 pft = clsPYFT(filename, parser=parser, parserOptions=options, wrapH=wrapH,
38 tree=tree, verbosity=verbosity)
44 Generates an empty PYFT scope
45 :param filename: file name corresponding to this new PYFT scope
46 :param fortran: fortran text to include
47 :param **kwargs: other arguments for the PYFT class
49 with open(filename,
'w', encoding=
'utf-8')
as fo:
50 fo.write(
'SUBROUTINE FOO\nEND' if fortran
is None else fortran)
51 pft =
PYFT(filename, **kwargs)
53 pft.remove(pft.find(
'.//{*}program-unit'))
59 This class extends the PYFTscope one by adding file support (read/write)
61 DEFAULT_FXTRAN_OPTIONS = [
'-construct-tag',
'-no-include',
'-no-cpp',
'-line-length',
'9999']
62 MANDATORY_FXTRAN_OPTIONS = [
'-construct-tag']
64 NO_PARALLEL_LOCK =
None
65 PARALLEL_FILE_LOCKS =
None
68 def __init__(self, filename, output=None, parser=None, parserOptions=None, verbosity=None,
69 wrapH=False, tree=None, enableCache=False):
71 :param filename: Input file name containing FORTRAN code
72 :param output: Output file name, None to replace input file
73 :param parser: path to the fxtran parser
74 :param parserOptions: dictionnary holding the parser options
75 :param verbosity: if not None, sets the verbosity level
76 :param wrapH: if True, content of .h file is put in a .F90 file (to force
77 fxtran to recognize it as free form) inside a module (to
78 enable the reading of files containing only a code part)
79 :param tree: an optional Tree instance
80 :param enableCache: True to cache node parents
83 if not sys.version_info >= (3, 8):
86 raise PYFTError(
"PyForTool needs at least version 3.8 of python")
89 assert os.path.exists(filename),
'Input filename must exist'
91 self.
_parser =
'fxtran' if parser
is None else parser
92 tree =
Tree()
if tree
is None else tree
94 assert tree
is not None,
'tree must be None if setParallel has been called'
96 if parserOptions
is None:
100 for tDir
in tree.getDirs():
106 super().
__init__(xml, enableCache=enableCache, tree=tree)
108 self.
tree.signal(self)
109 if verbosity
is not None:
110 setVerbosity(verbosity)
115 Prepare the class to be used to instanciate parallel objects
116 :param tree: Tree object shared among processes
117 :param clsLock: class to use for Lock (defaults to multiprocessing.Lock)
118 :param clsRLock: class to use for RLock (defaults to multiprocessing.RLock)
127 for file
in tree.getFiles()}
132 Acquire lock for filename
133 :param filename: name of the file whose lock must be acquired
135 filename = os.path.normpath(filename)
144 Release lock for filename
145 :param filename: name of the file whose lock must be acquired
146 :param silence: do not raise exception if file is already unlocked
148 filename = os.path.normpath(filename)
172 Closes the FORTRAN file
180 Returns the xml as a string
182 return tostring(self)
187 Returns the FORTRAN as a string
189 return tofortran(self)
193 The output file will have an upper case extension
199 The output file will have a lower case extension
205 The output file will have a modified extension.
206 :param mod: function to apply to the file extension
208 def _transExt(path, mod):
209 filename, ext = os.path.splitext(path)
210 return filename + mod(ext)
218 Writes the output FORTRAN file
221 encoding=
'utf-8')
as fo:
224 os.fsync(fo.fileno())
233 Writes the output XML file
234 :param filename: XML output file name
236 with open(filename,
'w', encoding=
'utf-8')
as fo: