def testFind(self): root = build_tree() node = moosetree.find(root, lambda n: n.name.endswith('AB')) self.assertEqual(node.name, 'AB') node = moosetree.find(root, lambda n: n.name.endswith('not this')) self.assertIs(node, None) node = moosetree.find(root, lambda n: n.get('year') == 2013) self.assertEqual(node.name, 'ABCAB')
def test(self): # MOOSEDOCS:example-begin # Load the packages from moosetools import pyhit from moosetools import moosetree # Read the file root = pyhit.load(os.path.join(os.path.dirname(__file__), 'input.i')) # Locate and modify "x_max" parameter for the mesh mesh = moosetree.find(root, func=lambda n: n.fullpath == '/Mesh/gen') mesh["x_max"] = 4 # Set the comment on altered parameter mesh.setComment("x_max", "Changed from 3 to 4") # Write the modified file pyhit.write("input_modified.i", root) # MOOSEDOCS:example-end self.assertEqual(mesh["x_max"], 4) self.assertEqual(mesh.comment("x_max"), "Changed from 3 to 4") out = mesh.render() self.assertIn("x_max = 4", out) self.assertIn("Changed from 3 to 4", out)
def make_formatter(filename, root, plugin_dirs): """ Create the `Formatter` object from the [Formatter] block of the `pyhit.Node` of *root*. By default, a `BasicFormatter` is created. Refer to `make_controllers` function for information on the supplied input arguments. """ # Locate/create the [Formatter] node f_node = moosetree.find(root, func=lambda n: n.fullpath == '/Formatter') if f_node is None: f_node = root.append('Formatter', type='BasicFormatter') # Factory for building Formatter objects f_factory = factory.Factory(plugin_dirs=plugin_dirs, plugin_types=(Formatter, )) f_factory.load() if f_factory.status() > 0: msg = "An error occurred registering the Formatter type, see console message(s) for details." raise RuntimeError(msg) # Create the Formatter object by parsing the input file formatters = list() f_parser = factory.Parser(f_factory, formatters) with mooseutils.CurrentWorkingDirectory(os.path.dirname(filename)): f_parser._parseNode(filename, f_node) if f_parser.status() > 0: msg = "An error occurred during parsing of the root level parameters for creation of the Formatter object, see console message(s) for details." raise RuntimeError(msg) return formatters[0]
def setup_environment(filename, root): """ Update environment from the [Environment] block. """ e_node = moosetree.find(root, func=lambda n: n.fullpath == '/Environment') if e_node is not None: for name, value in e_node.params(): if name not in os.environ: with mooseutils.CurrentWorkingDirectory( os.path.dirname(filename)): path = mooseutils.eval_path(value) if os.path.exists(path): value = os.path.abspath(path) os.environ[name] = value
def make_controllers(filename, root, plugin_dirs): """ Create the `Controller` object from the [Controllers] block of the `pyhit.Node` of *root*. The *filename* is provided for error reporting and setting the current working directory for creating object defined in the configuration file. It should be the file used for generating the tree structure in *root*. The *plugin_dirs* should contain a list of absolute paths to include when registering Controller objects with the factory. By default, regardless of the contents of *root*, all registered Controller objects are created. """ # Locate/create the [Controllers] node c_node = moosetree.find(root, func=lambda n: n.fullpath == '/Controllers') if c_node is None: c_node = root.append('Controllers') # Factory for building Controller objects c_factory = factory.Factory(plugin_dirs=plugin_dirs, plugin_types=(Controller, )) c_factory.load() if c_factory.status() > 0: msg = "An error occurred registering the Controller type, see console message(s) for details." raise RuntimeError(msg) # All Controller object type found by the Factory are automatically included with the default # configuration. This adds them to the configuration tree so they will be built by the factory c_types = set(child['type'] for child in c_node) for name in [ key for key, value in c_factory._registered_types.items() if value.AUTO_BUILD ]: if name not in c_types: c_node.append(f"_moosetools_{name}", type=name) # Use the Parser to create the Controller objects controllers = list() c_parser = factory.Parser(c_factory, controllers) with mooseutils.CurrentWorkingDirectory(os.path.dirname(filename)): c_parser.parse(filename, c_node) if c_parser.status() > 0: msg = "An error occurred during parsing of the Controller block, see console message(s) for details." raise RuntimeError(msg) return tuple(controllers)