fparser Reference Guide  0.0.14
read.py
1 #!/usr/bin/env python
2 # Modified work Copyright (c) 2017-2019 Science and Technology
3 # Facilities Council
4 # Original work Copyright (c) 1999-2008 Pearu Peterson
5 
6 # All rights reserved.
7 
8 # Modifications made as part of the fparser project are distributed
9 # under the following license:
10 
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are
13 # met:
14 
15 # 1. Redistributions of source code must retain the above copyright
16 # notice, this list of conditions and the following disclaimer.
17 
18 # 2. Redistributions in binary form must reproduce the above copyright
19 # notice, this list of conditions and the following disclaimer in the
20 # documentation and/or other materials provided with the distribution.
21 
22 # 3. Neither the name of the copyright holder nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 # --------------------------------------------------------------------
39 
40 # The original software (in the f2py project) was distributed under
41 # the following license:
42 
43 # Redistribution and use in source and binary forms, with or without
44 # modification, are permitted provided that the following conditions are met:
45 
46 # a. Redistributions of source code must retain the above copyright notice,
47 # this list of conditions and the following disclaimer.
48 # b. Redistributions in binary form must reproduce the above copyright
49 # notice, this list of conditions and the following disclaimer in the
50 # documentation and/or other materials provided with the distribution.
51 # c. Neither the name of the F2PY project nor the names of its
52 # contributors may be used to endorse or promote products derived from
53 # this software without specific prior written permission.
54 
55 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
56 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
59 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
61 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
62 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
65 # DAMAGE.
66 
67 """Python script with command line options which calls the Fortran
68 File Reader with the supplied filename(s) and outputs the reader's
69 representation of the code(s).
70 
71 """
72 import sys
73 import logging
74 from fparser.scripts.script_options import set_read_options
75 
76 logging.basicConfig()
77 
78 try:
79  from iocbio.optparse_gui import OptionParser
80 except ImportError:
81  from optparse import OptionParser
82 
83 
84 def runner(_, options, args):
85  """Call the Fortran File reader for each filename in args and print
86  out its content.
87 
88  :param options: command line argument information from the options \
89  parser
90  :type options: :py:class:`optparse.Values`
91  :param args: a list of Fortran filepaths
92  :type args: list of str
93 
94  :raises NotImplementedError: if the task option is not set to \
95  "show".
96 
97  """
98  from fparser.common.readfortran import FortranFileReader
99 
100  for filename in args:
101  reader = FortranFileReader(filename)
102  if options.task == "show":
103  for item in reader:
104  print(item)
105  sys.stdout.flush()
106  else:
107  raise NotImplementedError(
108  "The task option '{0}' is invalid. Currently only "
109  "'show' is supported.".format(repr(options.task))
110  )
111 
112 
113 def main():
114  """Check input options then call the runner function."""
115  parser = OptionParser()
116  set_read_options(parser)
117  options, args = parser.parse_args()
118  runner(parser, options, args)
119 
120 
121 if __name__ == "__main__":
122  main()