From 98b406b1d6e8669b303fa2da177a9e10dae4fc65 Mon Sep 17 00:00:00 2001 From: Mohamed El Mouctar HAIDARA Date: Sat, 23 Mar 2024 06:38:44 +0100 Subject: [PATCH] re-org files --- ansibleplaybookgrapher/__init__.py | 69 ---------------------- ansibleplaybookgrapher/cli.py | 2 +- ansibleplaybookgrapher/grapher.py | 67 +++++++++++++++++++++ ansibleplaybookgrapher/renderer/mermaid.py | 2 +- ansibleplaybookgrapher/version.py | 2 +- setup.cfg | 2 +- tests/test_parser.py | 2 +- 7 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 ansibleplaybookgrapher/grapher.py diff --git a/ansibleplaybookgrapher/__init__.py b/ansibleplaybookgrapher/__init__.py index 64c03f4c..e69de29b 100644 --- a/ansibleplaybookgrapher/__init__.py +++ b/ansibleplaybookgrapher/__init__.py @@ -1,69 +0,0 @@ -# Copyright (C) 2024 Mohamed El Mouctar HAIDARA -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -from typing import Dict, List, Set, Tuple - -from .graph_model import PlaybookNode, PlayNode, TaskNode, RoleNode, BlockNode -from .graph_model import ( - PlaybookNode, - RoleNode, - PlayNode, -) -from .parser import PlaybookParser -from .parser import PlaybookParser -from .utils import merge_dicts - - -class Grapher: - def __init__(self, playbook_filenames: List[str]): - """ - :param playbook_filenames: List of playbooks to graph - """ - self.playbook_filenames = playbook_filenames - - def parse( - self, - include_role_tasks: bool = False, - tags: List[str] = None, - skip_tags: List[str] = None, - group_roles_by_name: bool = False, - ) -> Tuple[List[PlaybookNode], Dict[RoleNode, Set[PlayNode]]]: - """ - Parses all the provided playbooks - :param include_role_tasks: Should we include the role tasks - :param tags: Only add plays and tasks tagged with these values - :param skip_tags: Only add plays and tasks whose tags do not match these values - :param group_roles_by_name: Group roles by name instead of considering them as separate nodes with different IDs - :return: Tuple of the list of playbook nodes and the dictionary of the role usages: the key is the role and the - value is the set of plays that use the role. - """ - playbook_nodes = [] - roles_usage: Dict[RoleNode, Set[PlayNode]] = {} - - for playbook_file in self.playbook_filenames: - playbook_parser = PlaybookParser( - playbook_filename=playbook_file, - tags=tags, - skip_tags=skip_tags, - include_role_tasks=include_role_tasks, - group_roles_by_name=group_roles_by_name, - ) - playbook_node = playbook_parser.parse() - playbook_nodes.append(playbook_node) - - # Update the usage of the roles - roles_usage = merge_dicts(roles_usage, playbook_node.roles_usage()) - - return playbook_nodes, roles_usage diff --git a/ansibleplaybookgrapher/cli.py b/ansibleplaybookgrapher/cli.py index 4ec2862c..779edf10 100644 --- a/ansibleplaybookgrapher/cli.py +++ b/ansibleplaybookgrapher/cli.py @@ -23,7 +23,7 @@ from ansible.release import __version__ as ansible_version from ansible.utils.display import Display -from ansibleplaybookgrapher import Grapher +from ansibleplaybookgrapher.grapher import Grapher from ansibleplaybookgrapher.version import __prog__, __version__ from ansibleplaybookgrapher.renderer import OPEN_PROTOCOL_HANDLERS from ansibleplaybookgrapher.renderer.graphviz import GraphvizRenderer diff --git a/ansibleplaybookgrapher/grapher.py b/ansibleplaybookgrapher/grapher.py new file mode 100644 index 00000000..ffb389c4 --- /dev/null +++ b/ansibleplaybookgrapher/grapher.py @@ -0,0 +1,67 @@ +# Copyright (C) 2024 Mohamed El Mouctar HAIDARA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from typing import Dict, List, Set, Tuple + +from .graph_model import ( + PlaybookNode, + RoleNode, + PlayNode, +) +from .parser import PlaybookParser +from .utils import merge_dicts + + +class Grapher: + def __init__(self, playbook_filenames: List[str]): + """ + :param playbook_filenames: List of playbooks to graph + """ + self.playbook_filenames = playbook_filenames + + def parse( + self, + include_role_tasks: bool = False, + tags: List[str] = None, + skip_tags: List[str] = None, + group_roles_by_name: bool = False, + ) -> Tuple[List[PlaybookNode], Dict[RoleNode, Set[PlayNode]]]: + """ + Parses all the provided playbooks + :param include_role_tasks: Should we include the role tasks + :param tags: Only add plays and tasks tagged with these values + :param skip_tags: Only add plays and tasks whose tags do not match these values + :param group_roles_by_name: Group roles by name instead of considering them as separate nodes with different IDs + :return: Tuple of the list of playbook nodes and the dictionary of the role usages: the key is the role and the + value is the set of plays that use the role. + """ + playbook_nodes = [] + roles_usage: Dict[RoleNode, Set[PlayNode]] = {} + + for playbook_file in self.playbook_filenames: + playbook_parser = PlaybookParser( + playbook_filename=playbook_file, + tags=tags, + skip_tags=skip_tags, + include_role_tasks=include_role_tasks, + group_roles_by_name=group_roles_by_name, + ) + playbook_node = playbook_parser.parse() + playbook_nodes.append(playbook_node) + + # Update the usage of the roles + roles_usage = merge_dicts(roles_usage, playbook_node.roles_usage()) + + return playbook_nodes, roles_usage diff --git a/ansibleplaybookgrapher/renderer/mermaid.py b/ansibleplaybookgrapher/renderer/mermaid.py index fb208c87..79f9d73d 100644 --- a/ansibleplaybookgrapher/renderer/mermaid.py +++ b/ansibleplaybookgrapher/renderer/mermaid.py @@ -18,7 +18,7 @@ from ansible.utils.display import Display -from ansibleplaybookgrapher import BlockNode, RoleNode, TaskNode, PlayNode, PlaybookNode +from ansibleplaybookgrapher.graph_model import BlockNode, RoleNode, TaskNode, PlayNode, PlaybookNode from ansibleplaybookgrapher.renderer import PlaybookBuilder, Renderer display = Display() diff --git a/ansibleplaybookgrapher/version.py b/ansibleplaybookgrapher/version.py index 7bfb507b..20c62ea2 100644 --- a/ansibleplaybookgrapher/version.py +++ b/ansibleplaybookgrapher/version.py @@ -1,2 +1,2 @@ -__version__ = "2.2.0-dev" +__version__ = "2.2.0.dev0" __prog__ = "ansible-playbook-grapher" diff --git a/setup.cfg b/setup.cfg index 5424cc26..6b947792 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [metadata] -description-file = Readme.md +description_file = Readme.md [bdist_wheel] universal = 1 diff --git a/tests/test_parser.py b/tests/test_parser.py index 3f6b6a0f..80d4d9ab 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -4,7 +4,7 @@ import pytest from ansible.utils.display import Display -from ansibleplaybookgrapher import PlaybookParser +from ansibleplaybookgrapher.parser import PlaybookParser from ansibleplaybookgrapher.cli import PlaybookGrapherCLI from ansibleplaybookgrapher.graph_model import ( TaskNode,