DISPATCH
__init__.py
1 """A Fortran 90 namelist parser and generator.
2 
3 :copyright: Copyright 2014 Marshall Ward, see AUTHORS for details.
4 :license: Apache License, Version 2.0, see LICENSE for details.
5 """
6 from f90nml.parser import Parser
7 from f90nml.namelist import Namelist
8 
9 __version__ = '1.0.2'
10 
11 
12 def read(nml_path):
13  """Parse a Fortran namelist file and return its contents.
14 
15  File object usage:
16 
17  >>> with open(nml_path) as nml_file:
18  >>> nml = f90nml.read(nml_file)
19 
20  File path usage:
21 
22  >>> nml = f90nml.read(nml_path)
23 
24  This function is equivalent to the ``read`` function of the ``Parser``
25  object.
26 
27  >>> parser = f90nml.Parser()
28  >>> nml = parser.read(nml_file)
29  """
30  parser = Parser()
31 
32  return parser.read(nml_path)
33 
34 
35 def write(nml, nml_path, force=False, sort=False):
36  """Save a namelist to disk using either a file object or its file path.
37 
38  File object usage:
39 
40  >>> with open(nml_path, 'w') as nml_file:
41  >>> f90nml.write(nml, nml_file)
42 
43  File path usage:
44 
45  >>> f90nml.write(nml, 'data.nml')
46 
47  This function is equivalent to the ``write`` function of the ``Namelist``
48  object ``nml``.
49 
50  >>> nml.write('data.nml')
51 
52  By default, ``write`` will not overwrite an existing file. To override
53  this, use the ``force`` flag.
54 
55  >>> nml.write('data.nml', force=True)
56 
57  To alphabetically sort the ``Namelist`` keys, use the ``sort`` flag.
58 
59  >>> nml.write('data.nml', sort=True)
60  """
61  # Promote dicts to Namelists
62  if not isinstance(nml, Namelist) and isinstance(nml, dict):
63  nml_in = Namelist(nml)
64  else:
65  nml_in = nml
66 
67  nml_in.write(nml_path, force=force, sort=sort)
68 
69 
70 def patch(nml_path, nml_patch, out_path=None):
71  """Create a new namelist based on an input namelist and reference dict.
72 
73  >>> f90nml.patch('data.nml', nml_patch, 'patched_data.nml')
74 
75  This function is equivalent to the ``read`` function of the ``Parser``
76  object with the patch output arguments.
77 
78  >>> parser = f90nml.Parser()
79  >>> nml = parser.read('data.nml', nml_patch, 'patched_data.nml')
80 
81  A patched namelist file will retain any formatting or comments from the
82  original namelist file. Any modified values will be formatted based on the
83  settings of the ``Namelist`` object.
84  """
85  parser = Parser()
86 
87  return parser.read(nml_path, nml_patch, out_path)
def patch(nml_path, nml_patch, out_path=None)
Definition: __init__.py:70
def read(nml_path)
Definition: __init__.py:12
def write(nml, nml_path, force=False, sort=False)
Definition: __init__.py:35