Skip to content

Commit

Permalink
refac(markdown_table): Switch to new library for markdown tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Noahnc committed Nov 28, 2023
1 parent d1366b7 commit 93a976a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
17 changes: 6 additions & 11 deletions infrapatch/action/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,12 @@ def main(debug: bool):
def get_pr_body(provider_handler: ProviderHandler) -> str:
body = ""
markdown_tables = provider_handler.get_markdown_tables()
for provider_name, table in markdown_tables.items():
table.setParams(padding_width=3, quote=False)
body += f"## {provider_name}\n"
body += "```\n"
body += table.get_markdown()
body += "\n```\n"

body += "## Statistics\n"
body += "```\n"
body += provider_handler._get_statistics().get_markdown_table().setParams(padding_width=3, quote=False).get_markdown()
body += "\n```\n"
for table in markdown_tables:
body += table.dumps()
body += "\n"

body += provider_handler._get_statistics().get_markdown_table().dumps()
body += "\n"
return body


Expand Down
14 changes: 9 additions & 5 deletions infrapatch/core/models/statistics.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from dataclasses import dataclass
import dataclasses
from typing import Any, Sequence
from pytablewriter import MarkdownTableWriter
from rich.table import Table
from infrapatch.core.models.versioned_resource import VersionedResource
from py_markdown_table.markdown_table import markdown_table


@dataclass
Expand Down Expand Up @@ -42,12 +42,16 @@ def get_rich_table(self) -> Table:
)
return table

def get_markdown_table(self) -> markdown_table:
def get_markdown_table(self) -> MarkdownTableWriter:
dict_element = {
"Errors": self.errors,
"Patched": self.resources_patched,
"Pending_Update": self.resources_pending_update,
"Pending Update": self.resources_pending_update,
"Total": self.total_resources,
"Enabled_Providers": len(self.providers),
"Enabled Providers": len(self.providers),
}
return markdown_table([dict_element])
return MarkdownTableWriter(
table_name="Statistics",
headers=list(dict_element.keys()),
value_matrix=[list(dict_element.values())],
)
8 changes: 4 additions & 4 deletions infrapatch/core/provider_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging as log
from pathlib import Path
from typing import Sequence, Union
from py_markdown_table.markdown_table import markdown_table
from git import Repo
from pytablewriter import MarkdownTableWriter
from rich.console import Console

from infrapatch.core.models.statistics import ProviderStatistics, Statistics
Expand Down Expand Up @@ -118,14 +118,14 @@ def print_statistics_table(self, disable_cache: bool = False):
table = self._get_statistics(disable_cache).get_rich_table()
self.console.print(table)

def get_markdown_tables(self) -> dict[str, markdown_table]:
def get_markdown_tables(self) -> list[MarkdownTableWriter]:
if self._resource_cache is None:
raise Exception("No resources found. Run get_resources() first.")

markdown_tables: dict[str, markdown_table] = {}
markdown_tables = []
for provider_name, provider in self.providers.items():
changed_resources = [
resource for resource in self._resource_cache[provider_name] if resource.status == ResourceStatus.PATCHED or resource.status == ResourceStatus.PATCH_ERROR
]
markdown_tables[provider.get_provider_display_name()] = provider.get_markdown_table(changed_resources)
markdown_tables.append(provider.get_markdown_table(changed_resources))
return markdown_tables
4 changes: 2 additions & 2 deletions infrapatch/core/providers/base_provider_interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Protocol, Sequence
from pytablewriter import MarkdownTableWriter
from rich.table import Table
from infrapatch.core.models.versioned_resource import VersionedResource
from py_markdown_table.markdown_table import markdown_table


class BaseProviderInterface(Protocol):
Expand All @@ -20,7 +20,7 @@ def patch_resource(self, resource: VersionedResource) -> VersionedResource:
def get_rich_table(self, resources: Sequence[VersionedResource]) -> Table:
...

def get_markdown_table(self, resources: Sequence[VersionedResource]) -> markdown_table:
def get_markdown_table(self, resources: Sequence[VersionedResource]) -> MarkdownTableWriter:
...

def get_resources_as_dict_list(self, resources: Sequence[VersionedResource]):
Expand Down
10 changes: 7 additions & 3 deletions infrapatch/core/providers/terraform/base_terraform_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Any, Sequence

from abc import abstractmethod
from pytablewriter import MarkdownTableWriter

from rich import progress
from py_markdown_table.markdown_table import markdown_table
from rich.table import Table
from infrapatch.core.models.versioned_resource import VersionedResource
from infrapatch.core.models.versioned_terraform_resources import VersionedTerraformResource
Expand Down Expand Up @@ -69,7 +69,7 @@ def get_rich_table(self, resources: Sequence[VersionedTerraformResource]) -> Tab
table.add_row(resource.name, resource.source, resource.current_version, resource.newest_version, str(not resource.installed_version_equal_or_newer_than_new_version()))
return table

def get_markdown_table(self, resources: Sequence[VersionedTerraformResource]) -> markdown_table:
def get_markdown_table(self, resources: Sequence[VersionedTerraformResource]) -> MarkdownTableWriter:
dict_list = []
for resource in resources:
dict_element = {
Expand All @@ -80,7 +80,11 @@ def get_markdown_table(self, resources: Sequence[VersionedTerraformResource]) ->
"Upgradeable": str(not resource.installed_version_equal_or_newer_than_new_version()),
}
dict_list.append(dict_element)
return markdown_table(dict_list)
return MarkdownTableWriter(
title=self.get_provider_display_name(),
headers=list(dict_list[0].keys()),
value_matrix=[list(dict_element.values()) for dict_element in dict_list],
)

def get_resources_as_dict_list(self, resources: Sequence[VersionedTerraformResource]) -> list[dict[str, Any]]:
return [resource.to_dict() for resource in resources]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ setuptools~=65.5.1
pygit2~=1.13.1
semantic-version~=2.10.0
PyGithub~=2.1.1
py-markdown-table~=0.4.0
pytablewriter~=4.2.1.6
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
version=__version__,
packages=find_packages(where=".", include=["infrapatch*"], exclude=["action*"]),
package_data={"infrapatch": ["core/utils/terraform/bin/*"]},
install_requires=["click~=8.1.7", "rich~=13.6.0", "pygohcl~=1.0.7", "GitPython~=3.1.40", "setuptools~=65.5.1", "semantic_version~=2.10.0", "py-markdown-table~=0.4.0"],
install_requires=["click~=8.1.7", "rich~=13.6.0", "pygohcl~=1.0.7", "GitPython~=3.1.40", "setuptools~=65.5.1", "semantic_version~=2.10.0", "pytablewriter~=4.2.1.6"],
python_requires=">=3.11",
entry_points="""
[console_scripts]
Expand Down

0 comments on commit 93a976a

Please sign in to comment.