From 2e6269852f4b19a116ee699de328302dbda6196a Mon Sep 17 00:00:00 2001 From: Kyle King Date: Thu, 31 Oct 2024 07:56:50 -0400 Subject: [PATCH] feat: add markdown_tables.format_table --- calcipy/code_tag_collector/_collector.py | 4 ++-- calcipy/{_md_helpers.py => markdown_table.py} | 17 ++++++++++------- calcipy/md_writer/_writer.py | 6 ++++-- docs/docs/CHANGELOG.md | 1 + docs/docs/CODE_TAG_SUMMARY.md | 4 ++-- docs/docs/DEVELOPER_GUIDE.md | 6 +++--- 6 files changed, 22 insertions(+), 16 deletions(-) rename calcipy/{_md_helpers.py => markdown_table.py} (58%) diff --git a/calcipy/code_tag_collector/_collector.py b/calcipy/code_tag_collector/_collector.py index f35cffa9..6a5b52ff 100644 --- a/calcipy/code_tag_collector/_collector.py +++ b/calcipy/code_tag_collector/_collector.py @@ -14,7 +14,7 @@ from corallium.log import LOGGER from corallium.shell import capture_shell -from calcipy._md_helpers import _format_md_table +from calcipy.markdown_table import format_table SKIP_PHRASE = 'calcipy_skip_tags' """String that indicates the file should be excluded from the tag search.""" @@ -279,7 +279,7 @@ def _format_report( ) counter[comment.tag] += 1 if records: - output += '\n' + '\n'.join(_format_md_table(headers=[*records[0]], records=records)) + output += '\n' + format_table(headers=[*records[0]], records=records) LOGGER.text_debug('counter', counter=counter) sorted_counter = {tag: counter[tag] for tag in tag_order if tag in counter} diff --git a/calcipy/_md_helpers.py b/calcipy/markdown_table.py similarity index 58% rename from calcipy/_md_helpers.py rename to calcipy/markdown_table.py index aad9eecc..b74766d4 100644 --- a/calcipy/_md_helpers.py +++ b/calcipy/markdown_table.py @@ -1,21 +1,24 @@ -"""Markdown utilities.""" +"""Markdown table formatting.""" -from beartype.typing import Any, Dict, List +from __future__ import annotations +from typing import Any -def _format_md_table(headers: List[str], records: List[Dict[str, Any]]) -> List[str]: - """Format the input as a Github markdown table.""" + +def format_table(headers: list[str], records: list[dict[str, Any]]) -> str: + """Returns a formatted Github Markdown table.""" table = [[str(_r[col]) for col in headers] for _r in records] widths = [max(len(row[col_idx].strip()) for row in [headers, *table]) for col_idx in range(len(headers))] - def pad(values: List[str]) -> List[str]: + def pad(values: list[str]) -> list[str]: return [val.strip().ljust(widths[col_idx]) for col_idx, val in enumerate(values)] - def join(row: List[str], spacer: str = ' ') -> str: + def join(row: list[str], spacer: str = ' ') -> str: return f'|{spacer}' + f'{spacer}|{spacer}'.join(row) + f'{spacer}|' - return [ + lines = [ join(pad(headers)), join(['-' * widths[col_idx] for col_idx in range(len(headers))], '-'), *[join(pad(row)) for row in table], ] + return '\n'.join(lines) diff --git a/calcipy/md_writer/_writer.py b/calcipy/md_writer/_writer.py index e037e93c..1ed87371 100644 --- a/calcipy/md_writer/_writer.py +++ b/calcipy/md_writer/_writer.py @@ -1,5 +1,7 @@ """Markdown Machine.""" +from __future__ import annotations + import json import re from pathlib import Path @@ -8,9 +10,9 @@ from corallium.file_helpers import read_lines from corallium.log import LOGGER -from calcipy._md_helpers import _format_md_table from calcipy.file_search import find_project_files_by_suffix from calcipy.invoke_helpers import get_project_path +from calcipy.markdown_table import format_table HandlerLookupT = Dict[str, Callable[[str, Path], List[str]]] """Handler Lookup.""" @@ -183,7 +185,7 @@ def _format_cov_table(coverage_data: Dict[str, Any]) -> List[str]: ) records = [{**_r, 'Coverage': f"{round(_r['Coverage'], 1)}%"} for _r in records] - lines_table = _format_md_table(headers=['File', *col_key_map], records=records) + lines_table = format_table(headers=['File', *col_key_map], records=records).split('\n') short_date = coverage_data['meta']['timestamp'].split('T')[0] lines_table.extend(['', f'Generated on: {short_date}']) return lines_table diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index 177c3ee2..16c2f898 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -9,6 +9,7 @@ ### Refactor +- finish removing pydantic dependency - switch serialized BaseModel to dataclass - switch some BaseModels to plain dataclasses - sync Github Labeler diff --git a/docs/docs/CODE_TAG_SUMMARY.md b/docs/docs/CODE_TAG_SUMMARY.md index 909118db..9c61921e 100644 --- a/docs/docs/CODE_TAG_SUMMARY.md +++ b/docs/docs/CODE_TAG_SUMMARY.md @@ -4,11 +4,11 @@ |---------|------------------------------------------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | PLANNED | support completions | 2024-10-07 | [calcipy/cli.py:103](https://github.com/KyleKing/calcipy/blame/aa2adcff703f8fe7da95dc2486030dbacf4a5802/calcipy/cli.py#L103) | | PLANNED | Also see how https://docs.astral.sh/ruff/configuration/#shell-autocompletion | 2024-10-10 | [calcipy/cli.py:112](https://github.com/KyleKing/calcipy/blame/8c9b14b336b2d8f28e2e946018d9add3f260bdfe/calcipy/cli.py#L112) | -| TODO | How to capture output? | 2024-10-05 | [calcipy/collection.py:38](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/collection.py#L38) | +| TODO | How to capture output? | 2024-10-05 | [calcipy/collection.py:50](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/collection.py#L38) | | TODO | Handle ">=3.0.0,<4" | 2024-10-05 | [calcipy/experiments/sync_package_dependencies.py:48](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/experiments/sync_package_dependencies.py#L48) | | TODO | Add unit test | 2024-07-06 | [calcipy/tasks/pack.py:59](https://github.com/KyleKing/calcipy/blame/e45ecadfa5b994d9c0a2a47138fa0e083261e3eb/calcipy/tasks/pack.py#L57) | | TODO | Add unit test | 2024-07-06 | [calcipy/tasks/pack.py:93](https://github.com/KyleKing/calcipy/blame/e45ecadfa5b994d9c0a2a47138fa0e083261e3eb/calcipy/tasks/pack.py#L91) | -| PLANNED | finish updating docstrings for Returns | 2024-10-08 | [pyproject.toml:161](https://github.com/KyleKing/calcipy/blame/9cf3c6d2d9820cec475d35bdb7c53fc83627a4b2/pyproject.toml#L161) | +| PLANNED | finish updating docstrings for Returns | 2024-10-08 | [pyproject.toml:154](https://github.com/KyleKing/calcipy/blame/9cf3c6d2d9820cec475d35bdb7c53fc83627a4b2/pyproject.toml#L161) | | TODO | Capture logging output and check... | 2023-02-19 | [tests/check_for_stale_packages/test_check_for_stale_packages.py:64](https://github.com/KyleKing/calcipy/blame/a8b69e7b04d9b15eabff8897f2de7703898c2afc/tests/check_for_stale_packages/test_check_for_stale_packages.py#L63) | | TODO | Capture logging output and check... | 2023-02-19 | [tests/check_for_stale_packages/test_check_for_stale_packages.py:88](https://github.com/KyleKing/calcipy/blame/3f42ad855eb7024ff48af35d496633a87d4a14ac/tests/check_for_stale_packages/test_check_for_stale_packages.py#L26) | | PLANNED | Add unit test for sync_package_dependencies.py | 2024-10-07 | [tests/experiments/test_sync_package_dependencies.py:1](https://github.com/KyleKing/calcipy/blame/dbe495b3653edb2fd06f9e3865619707d941ed87/tests/experiments/test_sync_package_dependencies.py#L1) | diff --git a/docs/docs/DEVELOPER_GUIDE.md b/docs/docs/DEVELOPER_GUIDE.md index d89e49e9..c5a16a1e 100644 --- a/docs/docs/DEVELOPER_GUIDE.md +++ b/docs/docs/DEVELOPER_GUIDE.md @@ -48,7 +48,6 @@ poetry config pypi-token.pypi ... | File | Statements | Missing | Excluded | Coverage | |-----------------------------------------------------------------|------------|---------|----------|----------| | `calcipy/__init__.py` | 4 | 0 | 0 | 100.0% | -| `calcipy/_md_helpers.py` | 9 | 0 | 0 | 100.0% | | `calcipy/_runtime_type_check_setup.py` | 13 | 0 | 33 | 100.0% | | `calcipy/can_skip.py` | 14 | 1 | 0 | 88.9% | | `calcipy/check_for_stale_packages/__init__.py` | 5 | 2 | 0 | 60.0% | @@ -65,8 +64,9 @@ poetry config pypi-token.pypi ... | `calcipy/experiments/sync_package_dependencies.py` | 47 | 47 | 0 | 0.0% | | `calcipy/file_search.py` | 32 | 0 | 2 | 100.0% | | `calcipy/invoke_helpers.py` | 27 | 4 | 0 | 83.8% | +| `calcipy/markdown_table.py` | 11 | 0 | 0 | 100.0% | | `calcipy/md_writer/__init__.py` | 5 | 2 | 0 | 60.0% | -| `calcipy/md_writer/_writer.py` | 91 | 6 | 0 | 89.6% | +| `calcipy/md_writer/_writer.py` | 92 | 6 | 0 | 89.7% | | `calcipy/noxfile/__init__.py` | 5 | 2 | 0 | 60.0% | | `calcipy/noxfile/_noxfile.py` | 39 | 2 | 51 | 91.5% | | `calcipy/scripts.py` | 6 | 0 | 51 | 100.0% | @@ -84,7 +84,7 @@ poetry config pypi-token.pypi ... | `calcipy/tasks/tags.py` | 18 | 1 | 0 | 90.9% | | `calcipy/tasks/test.py` | 39 | 1 | 2 | 90.9% | | `calcipy/tasks/types.py` | 11 | 0 | 0 | 93.3% | -| **Totals** | 1044 | 133 | 292 | 83.0% | +| **Totals** | 1047 | 133 | 292 | 83.0% | Generated on: 2024-10-31