Skip to content

Commit

Permalink
csv-to-md: make fn public
Browse files Browse the repository at this point in the history
  • Loading branch information
leroyguillaume committed Feb 5, 2022
1 parent 6607732 commit 88853b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
64 changes: 33 additions & 31 deletions markdown_table_generator/markdown_table_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from argparse import ArgumentParser
from enum import Enum
from math import ceil, floor
from typing import List, Optional
from typing import List, Optional


class Alignment(Enum):
Expand Down Expand Up @@ -46,6 +46,38 @@ def __init__(self, alignment: str) -> None:
Table = List[List[Optional[Cell]]]


def csv_to_md() -> None:
"""Generate markdown table from CSV files given as command line arguments"""

arg_parser = ArgumentParser(
description="Generate markdown table from CSV",
)
arg_parser.add_argument("file", nargs="+")
arg_parser.add_argument(
"-a", "--alignment",
help="table alignment (l|c|r for left, center or right;default: 'l')",
metavar="alignment",
choices=["l", "c", "r"],
default="l",
)
arg_parser.add_argument(
"-s", "--separator",
help="column separator (default: ',')",
metavar="separator",
default=",",
)
args = arg_parser.parse_args()
alignment = __alignment_from_string(args.alignment)
for i, filepath in enumerate(args.file):
with open(filepath, "r", encoding="utf-8") as file:
csv = file.readlines()
table = table_from_csv(csv, args.separator, alignment)
markdown = generate_markdown(table)
print(markdown)
if i < len(args.file) - 1:
print()


def generate_markdown(table: Table) -> str:
"""Generate markdown table
Expand Down Expand Up @@ -137,36 +169,6 @@ def __alignment_from_string(string: str) -> Alignment:
raise InvalidAlignment(string)


def __csv_to_md() -> None:
arg_parser = ArgumentParser(
description="Generate markdown table from CSV",
)
arg_parser.add_argument("file", nargs="+")
arg_parser.add_argument(
"-a", "--alignment",
help="table alignment (l|c|r for left, center or right;default: 'l')",
metavar="alignment",
choices=["l", "c", "r"],
default="l",
)
arg_parser.add_argument(
"-s", "--separator",
help="column separator (default: ',')",
metavar="separator",
default=",",
)
args = arg_parser.parse_args()
alignment = __alignment_from_string(args.alignment)
for i, filepath in enumerate(args.file):
with open(filepath, "r", encoding="utf-8") as file:
csv = file.readlines()
table = table_from_csv(csv, args.separator, alignment)
markdown = generate_markdown(table)
print(markdown)
if i < len(args.file) - 1:
print()


def __compute_columns_size(table: Table) -> List[int]:
columns_size = []
for row in table:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="markdown_table_generator",
version="1.0.2",
version="1.0.3",
author="Guillaume Leroy",
author_email="pro.guillaume.leroy@gmail.com",
description="Library to generate Markdown table",
Expand All @@ -22,7 +22,7 @@
install_requires=["typing"],
entry_points={
"console_scripts": [
"csv-to-md = markdown_table_generator:__csv_to_md"
"csv-to-md = markdown_table_generator:csv_to_md"
]
}
)

0 comments on commit 88853b0

Please sign in to comment.