Beispiel #1
0
 def setUpClass(cls):
     location = os.path.abspath(
         os.path.join(os.path.dirname(__file__), '..', '..', '..', 'test'))
     exe = mooseutils.find_moose_executable(location)
     raw = mooseutils.runExe(exe, ['--json', '--allow-test-objects'])
     raw = raw.split('**START JSON DATA**\n')[1]
     raw = raw.split('**END JSON DATA**')[0]
     cls.json = mooseutils.json_parse(raw)
Beispiel #2
0
def app_syntax(exe, remove=None, allow_test_objects=False, hide=None, alias=None):
    """
    Creates a tree structure representing the MooseApp syntax for the given executable.
    """
    try:
        raw = mooseutils.runExe(exe, ['--json', '--allow-test-objects'])
        raw = raw.split('**START JSON DATA**\n')[1]
        raw = raw.split('**END JSON DATA**')[0]
        tree = mooseutils.json_parse(raw)

    except Exception as e:
        LOG.error("Failed to execute the MOOSE executable '%s':\n%s", exe, e)
        sys.exit(1)

    root = SyntaxNode('', None)
    for key, value in tree['blocks'].items():
        node = SyntaxNode(key, root)
        __syntax_tree_helper(node, value)

    hidden = set()
    if isinstance(hide, dict):
        for value in hide.values():
            hidden.update(value)
    elif isinstance(hide, (list, set)):
        hidden.update(hide)

    if hidden:
        for node in moosetree.iterate(root):
            if node.fullpath in hidden:
                node.hidden = True

    # Remove
    # TODO: There is multiple iterations over the tree, there should be just none or one
    removed = set()
    if isinstance(remove, dict):
        for value in remove.values():
            removed.update(value)
    elif isinstance(remove, (list, set)):
        removed.update(remove)

    if removed:
        for node in moosetree.iterate(root):
            if any(n.fullpath == prefix for n in node.path for prefix in removed):
                node.removed = True

    if not allow_test_objects:
        for node in moosetree.iterate(root):
            if node.groups and all([group.endswith('TestApp') for group in node.groups]):
                node.removed = True

    # Alias
    if alias:
        for node in moosetree.iterate(root):
            for k, v in alias.items():
                if node.fullpath == k:
                    node.alias = str(v)

    return root
Beispiel #3
0
def get_moose_syntax_tree(exe,
                          remove=None,
                          hide=None,
                          alias=None,
                          unregister=None,
                          allow_test_objects=False):
    """
    Creates a tree structure representing the MooseApp syntax for the given executable using --json.

    Inputs:
      ext[str|dict]: The executable to run or the parsed JSON tree structure
      remove[list|dict]: Syntax to mark as removed. The input data structure can be a single list or
                         a dict of lists.
      hide[list|dict]: Syntax to mark as hidden. The input data structure can be a single list or
                       a dict of lists.
      alias[dict]: A dict of alias information; the key is the actual syntax and the value is the
                   alias to be applied (e.g., {'/Kernels/Diffusion':'/Physics/Diffusion'}).
      unregister[dict]: A dict of classes with duplicate registration information; the key is the
                        "moose_base" name and the value is the syntax from which the object should be
                        removed (e.g., {"Postprocessor":"UserObject/*"}).
    """
    # Create the JSON tree, unless it is provided directly
    if isinstance(exe, dict):
        tree = exe
    else:
        raw = mooseutils.runExe(exe, ['--json', '--allow-test-objects'])
        raw = raw.split('**START JSON DATA**\n')[1]
        raw = raw.split('**END JSON DATA**')[0]
        tree = mooseutils.json_parse(raw)

    # Build the complete syntax tree
    root = SyntaxNode(None, '')
    for key, value in tree['blocks'].items():
        node = SyntaxNode(root, key)
        __syntax_tree_helper(node, value)

    # Build hide/remove sets
    hidden = __build_set_from_yaml(hide)
    removed = __build_set_from_yaml(remove)

    # Initialize dict if not provided
    alias = alias or dict()
    unregister = unregister or dict()
    for key in list(unregister.keys()):
        if isinstance(unregister[key], dict):
            unregister.update(unregister.pop(key))

    # Apply remove/hide/alias/unregister restrictions
    for node in moosetree.iterate(root):

        # Hidden
        if node.fullpath() in hidden:
            node.hidden = True

        # Removed
        if (node.fullpath() in removed) or ((node.parent is not None)
                                            and node.parent.removed):
            node.removed = True

        # Remove 'Test' objects if not allowed
        if (not allow_test_objects) and all(
                grp.endswith('TestApp') for grp in node.groups()):
            node.removed = True

        # Remove unregistered items
        for base, parent_syntax in unregister.items():
            if (node.name == base) and (node.get('action_path')
                                        == parent_syntax):
                node.removed = True

            if (node.get('moose_base') == base) and (node.get('parent_syntax')
                                                     == parent_syntax):
                node.removed = True

        # Apply alias
        for name, alt in alias.items():
            if node.fullpath() == name:
                node.alias = str(alt)

    return root