Skip to content

Commit

Permalink
Make it possible to initialize writer instance with constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Aug 22, 2020
1 parent 54c7071 commit 95f8f8c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
16 changes: 8 additions & 8 deletions pytablewriter/writer/_table_writer.py
Expand Up @@ -347,8 +347,8 @@ def _write_table(self, **kwargs) -> None:
def __init__(self, **kwargs) -> None:
self._logger = WriterLogger(self)

self.table_name = ""
self.value_matrix = []
self.table_name = kwargs.get("table_name", "")
self.value_matrix = kwargs.get("value_matrix", [])

self.is_write_header = True
self.is_write_header_separator_row = True
Expand All @@ -366,11 +366,11 @@ def __init__(self, **kwargs) -> None:
self._dp_extractor.matrix_formatting = MatrixFormatting.HEADER_ALIGNED
self._dp_extractor.update_strict_level_map({Typecode.BOOL: 1})

self.is_formatting_float = True
self.is_formatting_float = kwargs.get("is_formatting_float", True)
self.is_padding = True

self.headers = []
self.type_hints = []
self.headers = kwargs.get("headers", [])
self.type_hints = kwargs.get("type_hints", [])
self._quoting_flags = {
Typecode.BOOL: False,
Typecode.DATETIME: True,
Expand All @@ -394,14 +394,14 @@ def __init__(self, **kwargs) -> None:
self._iter_count = None # type: Optional[int]

self.__default_style = Style()
self.__col_style_list = [] # type: List[Optional[Style]]
self.__col_style_list = kwargs.get("column_styles", []) # type: List[Optional[Style]]
self._style_filters = [] # type: List[StyleFilterFunc]
self._styler = self._create_styler(self)
self.style_filter_kwargs = {} # type: Dict[str, Any]
self.__colorize_terminal = True
self.__colorize_terminal = kwargs.get("colorize_terminal", True)
self.__enable_ansi_escape = True

self.max_workers = 1
self.max_workers = kwargs.get("max_workers", 1)

self.__clear_preprocess()

Expand Down
4 changes: 2 additions & 2 deletions pytablewriter/writer/text/_text_writer.py
Expand Up @@ -145,7 +145,7 @@ def __init__(self, **kwargs) -> None:
self.char_closing_row = "-"
self.char_closing_row_cross_point = "-"

self.margin = 0
self.margin = kwargs.get("margin", 0)

self._dp_extractor.preprocessor.line_break_handling = LineBreakHandling.REPLACE
self.is_write_null_line_after_table = False
Expand Down Expand Up @@ -541,7 +541,7 @@ class IndentationTextTableWriter(TextTableWriter, IndentationInterface):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

self.set_indent_level(0)
self.set_indent_level(kwargs.get("indent_level", 0))
self.indent_string = ""

def set_indent_level(self, indent_level: int) -> None:
Expand Down
77 changes: 75 additions & 2 deletions test/writer/text/test_markdown_writer.py
Expand Up @@ -417,6 +417,29 @@ def test_normal(self, capsys):
assert out == "\n"


class Test_MarkdownTableWriter_constructor:
def test_normal_kwargs(self):
writer = table_writer_class(
headers=["w/ strike", "w/ line through"],
value_matrix=[["strike", "line-through"]],
column_styles=[Style(decoration_line="strike"), Style(decoration_line="line-through"),],
)

expected = dedent(
"""\
|w/ strike|w/ line through|
|---------|---------------|
|strike |line-through |
"""
)

out = str(writer)
print_test_result(expected=expected, actual=out)

assert regexp_ansi_escape.search(out)
assert regexp_ansi_escape.sub("", out) == expected


class Test_MarkdownTableWriter_repr:
def test_normal_empty(self):
writer = table_writer_class()
Expand Down Expand Up @@ -483,6 +506,16 @@ def test_normal(self, capsys, table, indent, header, value, is_formatting_float,
assert writer.dumps() == expected
assert str(writer) == expected

writer = table_writer_class(
table_name=table,
headers=header,
value_matrix=value,
indent_level=indent,
is_formatting_float=is_formatting_float,
)
writer.register_trans_func(trans_func)
assert writer.dumps() == expected

def test_normal_single_tabledata(self, capsys):
writer = table_writer_class()
writer.from_tabledata(
Expand Down Expand Up @@ -757,6 +790,27 @@ def style_filter(cell: Cell, **kwargs) -> Optional[Style]:
assert output_w_theme != output_wo_theme
assert output_w_theme == expected

assert (
table_writer_class(
table_name="style filter",
headers=["left", "center", "right", "overwrite l", "overwrite c", "overwrite r"],
value_matrix=[
[1, "c", "r", 1, "c", "r"],
[2.2, "left", "left", 2.2, "right", "center"],
],
margin=1,
column_styles=[
None,
None,
None,
Style(align="center"),
Style(align="right"),
Style(align="center"),
],
).dumps()
== output_wo_theme
)

def test_normal_clear_theme(self):
writer = table_writer_class()
writer.table_name = "style test: bold"
Expand Down Expand Up @@ -878,21 +932,40 @@ def test_normal_ansi_style(self):
assert regexp_ansi_escape.sub("", out) == expected

def test_normal_colorize_terminal(self):
writer = table_writer_class()
writer.column_styles = [
column_styles = [
Style(color="red"),
Style(bg_color="white"),
]
writer = table_writer_class()
writer.column_styles = column_styles
writer.headers = ["fg color", "bg color"]
writer.value_matrix = [["hoge", "foo"]]

writer.colorize_terminal = True
out = writer.dumps()
assert regexp_ansi_escape.search(out)
assert (
table_writer_class(
headers=["fg color", "bg color"],
value_matrix=[["hoge", "foo"]],
column_styles=column_styles,
colorize_terminal=True,
).dumps()
== out
)

writer.colorize_terminal = False
out = writer.dumps()
assert regexp_ansi_escape.search(out) is None
assert (
table_writer_class(
headers=["fg color", "bg color"],
value_matrix=[["hoge", "foo"]],
column_styles=column_styles,
colorize_terminal=False,
).dumps()
== out
)

def test_normal_enable_ansi_escape(self):
writer = table_writer_class()
Expand Down

0 comments on commit 95f8f8c

Please sign in to comment.