-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
_factory.py
260 lines (227 loc) · 9.96 KB
/
_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
import os
from typing import List
import typepy
from ._logger import logger
from ._table_format import FormatAttr, TableFormat
from .error import WriterNotFoundError
from .writer._table_writer import AbstractTableWriter
class TableWriterFactory:
"""
A factor class of table writer classes.
"""
@classmethod
def create_from_file_extension(cls, file_extension: str, **kwargs) -> AbstractTableWriter:
"""
Create a table writer class instance from a file extension.
Supported file extensions are as follows:
================== ===================================
Extension Writer Class
================== ===================================
``".css"`` :py:class:`~.CssTableWriter`
``".csv"`` :py:class:`~.CsvTableWriter`
``".htm"`` :py:class:`~.HtmlTableWriter`
``".html"`` :py:class:`~.HtmlTableWriter`
``".js"`` :py:class:`~.JavaScriptTableWriter`
``".json"`` :py:class:`~.JsonTableWriter`
``".jsonl"`` :py:class:`~.JsonLinesTableWriter`
``".ltsv"`` :py:class:`~.LtsvTableWriter`
``".ldjson"`` :py:class:`~.JsonLinesTableWriter`
``".md"`` :py:class:`~.MarkdownTableWriter`
``".ndjson"`` :py:class:`~.JsonLinesTableWriter`
``".py"`` :py:class:`~.PythonCodeTableWriter`
``".rst"`` :py:class:`~.RstGridTableWriter`
``".tsv"`` :py:class:`~.TsvTableWriter`
``".xls"`` :py:class:`~.ExcelXlsTableWriter`
``".xlsx"`` :py:class:`~.ExcelXlsxTableWriter`
``".sqlite"`` :py:class:`~.SqliteTableWriter`
``".sqlite3"`` :py:class:`~.SqliteTableWriter`
``".tsv"`` :py:class:`~.TsvTableWriter`
``".toml"`` :py:class:`~.TomlTableWriter`
``".yml"`` :py:class:`~.YamlTableWriter`
================== ===================================
:param str file_extension:
File extension string (case insensitive).
:param kwargs:
Keyword arguments that passing to writer class constructor.
:return:
Writer instance that coincides with the ``file_extension``.
:rtype:
:py:class:`~pytablewriter.writer._table_writer.TableWriterInterface`
:raises pytablewriter.WriterNotFoundError:
|WriterNotFoundError_desc| the file extension.
"""
ext = os.path.splitext(file_extension)[1]
if typepy.is_null_string(ext):
file_extension = file_extension
else:
file_extension = ext
file_extension = file_extension.lstrip(".").lower()
for table_format in TableFormat:
if file_extension not in table_format.file_extensions:
continue
if table_format.format_attribute & FormatAttr.SECONDARY_EXT:
continue
logger.debug("create a {} instance".format(table_format.writer_class.__name__))
return table_format.writer_class(**kwargs)
raise WriterNotFoundError(
"\n".join(
[
"{:s} (unknown file extension).".format(file_extension),
"",
"acceptable file extensions are: {}.".format(", ".join(cls.get_extensions())),
]
)
)
@classmethod
def create_from_format_name(cls, format_name: str, **kwargs) -> AbstractTableWriter:
"""
Create a table writer class instance from a format name.
Supported file format names are as follows:
============================================= ===================================
Format name Writer Class
============================================= ===================================
``"css"`` :py:class:`~.CssTableWriter`
``"csv"`` :py:class:`~.CsvTableWriter`
``"elasticsearch"`` :py:class:`~.ElasticsearchWriter`
``"excel"`` :py:class:`~.ExcelXlsxTableWriter`
``"html"``/``"htm"`` :py:class:`~.HtmlTableWriter`
``"javascript"``/``"js"`` :py:class:`~.JavaScriptTableWriter`
``"json"`` :py:class:`~.JsonTableWriter`
``"json_lines"`` :py:class:`~.JsonLinesTableWriter`
``"latex_matrix"`` :py:class:`~.LatexMatrixWriter`
``"latex_table"`` :py:class:`~.LatexTableWriter`
``"ldjson"`` :py:class:`~.JsonLinesTableWriter`
``"ltsv"`` :py:class:`~.LtsvTableWriter`
``"markdown"``/``"md"`` :py:class:`~.MarkdownTableWriter`
``"mediawiki"`` :py:class:`~.MediaWikiTableWriter`
``"null"`` :py:class:`~.NullTableWriter`
``"pandas"`` :py:class:`~.PandasDataFrameWriter`
``"py"``/``"python"`` :py:class:`~.PythonCodeTableWriter`
``"rst"``/``"rst_grid"``/``"rst_grid_table"`` :py:class:`~.RstGridTableWriter`
``"rst_simple"``/``"rst_simple_table"`` :py:class:`~.RstSimpleTableWriter`
``"rst_csv"``/``"rst_csv_table"`` :py:class:`~.RstCsvTableWriter`
``"sqlite"`` :py:class:`~.SqliteTableWriter`
``"tsv"`` :py:class:`~.TsvTableWriter`
``"toml"`` :py:class:`~.TomlTableWriter`
``"unicode"`` :py:class:`~.UnicodeTableWriter`
``"yaml"`` :py:class:`~.YamlTableWriter`
============================================= ===================================
:param str format_name: Format name string (case insensitive).
:param kwargs:
Keyword arguments that passing to writer class constructor.
:return: Writer instance that coincides with the ``format_name``:
:rtype:
:py:class:`~pytablewriter.writer._table_writer.TableWriterInterface`
:raises pytablewriter.WriterNotFoundError:
|WriterNotFoundError_desc| for the format.
"""
format_name = format_name.lower()
for table_format in TableFormat:
if format_name in table_format.names and not (
table_format.format_attribute & FormatAttr.SECONDARY_NAME
):
logger.debug("create a {} instance".format(table_format.writer_class.__name__))
return table_format.writer_class(**kwargs)
raise WriterNotFoundError(
"\n".join(
[
"{} (unknown format name).".format(format_name),
"acceptable format names are: {}.".format(", ".join(cls.get_format_names())),
]
)
)
@classmethod
def get_format_names(cls) -> List[str]:
"""
:return: Available format names.
:rtype: list
:Example:
.. code:: python
>>> import pytablewriter as ptw
>>> for name in ptw.TableWriterFactory.get_format_names():
... print(name)
...
css
csv
elasticsearch
excel
htm
html
javascript
js
json
json_lines
jsonl
latex_matrix
latex_table
ldjson
ltsv
markdown
md
mediawiki
ndjson
null
numpy
pandas
py
python
rst
rst_csv
rst_csv_table
rst_grid
rst_grid_table
rst_simple
rst_simple_table
space_aligned
sqlite
toml
tsv
unicode
yaml
"""
format_name_set = set()
for table_format in TableFormat:
for format_name in table_format.names:
format_name_set.add(format_name)
return sorted(list(format_name_set))
@classmethod
def get_extensions(cls) -> List[str]:
"""
:return: Available file extensions.
:rtype: list
:Example:
.. code:: python
>>> import pytablewriter as ptw
>>> for name in ptw.TableWriterFactory.get_extensions():
... print(name)
...
css
csv
htm
html
js
json
jsonl
ldjson
ltsv
md
ndjson
py
rst
sqlite
sqlite3
tex
toml
tsv
xls
xlsx
yml
"""
file_extension_set = set()
for table_format in TableFormat:
for file_extension in table_format.file_extensions:
file_extension_set.add(file_extension)
return sorted(list(file_extension_set))