/
_table_format.py
266 lines (235 loc) · 7.03 KB
/
_table_format.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
261
262
263
264
265
266
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import, unicode_literals
import enum
from .writer import (
CsvTableWriter,
ElasticsearchWriter,
ExcelXlsTableWriter,
ExcelXlsxTableWriter,
HtmlTableWriter,
JavaScriptTableWriter,
JsonLinesTableWriter,
JsonTableWriter,
LatexMatrixWriter,
LatexTableWriter,
LtsvTableWriter,
MarkdownTableWriter,
MediaWikiTableWriter,
NullTableWriter,
NumpyTableWriter,
PandasDataFrameWriter,
PythonCodeTableWriter,
RstCsvTableWriter,
RstGridTableWriter,
RstSimpleTableWriter,
SpaceAlignedTableWriter,
SqliteTableWriter,
TomlTableWriter,
TsvTableWriter,
)
class FormatAttr(object):
"""
Bitmaps to represent table attributes.
"""
NONE = 1 << 1
#: Can create a file with the format.
FILE = 1 << 2
#: Table format that can represent as a text.
TEXT = 1 << 3
#: Table format that can represent as a binary file.
BIN = 1 << 4
#: Can create a source code (variables definition)
#: one of the programming language.
SOURCECODE = 1 << 5
#: Can call API for external service.
API = 1 << 6
SECONDARY_EXT = 1 << 10
SECONDARY_NAME = 1 << 11
@enum.unique
class TableFormat(enum.Enum):
"""
Enum to represent table format attributes.
"""
CSV = ([CsvTableWriter.FORMAT_NAME], CsvTableWriter, FormatAttr.FILE | FormatAttr.TEXT, ["csv"])
ELASTICSEARCH = ([ElasticsearchWriter.FORMAT_NAME], ElasticsearchWriter, FormatAttr.API, [])
EXCEL_XLS = (
[ExcelXlsTableWriter.FORMAT_NAME],
ExcelXlsTableWriter,
FormatAttr.FILE | FormatAttr.BIN | FormatAttr.SECONDARY_NAME,
["xls"],
)
EXCEL_XLSX = (
[ExcelXlsxTableWriter.FORMAT_NAME],
ExcelXlsxTableWriter,
FormatAttr.FILE | FormatAttr.BIN,
["xlsx"],
)
HTML = (
[HtmlTableWriter.FORMAT_NAME, "htm"],
HtmlTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["html", "htm"],
)
JAVASCRIPT = (
[JavaScriptTableWriter.FORMAT_NAME, "js"],
JavaScriptTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SOURCECODE,
["js"],
)
JSON = (
[JsonTableWriter.FORMAT_NAME],
JsonTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["json"],
)
JSON_LINES = (
[JsonLinesTableWriter.FORMAT_NAME, "jsonl", "ldjson", "ndjson"],
JsonLinesTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["jsonl", "ldjson", "ndjson"],
)
LATEX_MATRIX = (
[LatexMatrixWriter.FORMAT_NAME],
LatexMatrixWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["tex"],
)
LATEX_TABLE = (
[LatexTableWriter.FORMAT_NAME],
LatexTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SECONDARY_EXT,
["tex"],
)
LTSV = (
[LtsvTableWriter.FORMAT_NAME],
LtsvTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["ltsv"],
)
MARKDOWN = (
[MarkdownTableWriter.FORMAT_NAME, "md"],
MarkdownTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["md"],
)
MEDIAWIKI = (
[MediaWikiTableWriter.FORMAT_NAME],
MediaWikiTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
[],
)
NULL = ([NullTableWriter.FORMAT_NAME], NullTableWriter, FormatAttr.NONE, [])
NUMPY = (
[NumpyTableWriter.FORMAT_NAME],
NumpyTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SOURCECODE | FormatAttr.SECONDARY_EXT,
["py"],
)
PANDAS = (
[PandasDataFrameWriter.FORMAT_NAME],
PandasDataFrameWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SOURCECODE | FormatAttr.SECONDARY_EXT,
["py"],
)
PYTHON = (
[PythonCodeTableWriter.FORMAT_NAME, "py"],
PythonCodeTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SOURCECODE,
["py"],
)
RST_CSV_TABLE = (
[RstCsvTableWriter.FORMAT_NAME, "rst_csv"],
RstCsvTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SECONDARY_EXT,
["rst"],
)
RST_GRID_TABLE = (
[RstGridTableWriter.FORMAT_NAME, "rst_grid", "rst"],
RstGridTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["rst"],
)
RST_SIMPLE_TABLE = (
[RstSimpleTableWriter.FORMAT_NAME, "rst_simple"],
RstSimpleTableWriter,
FormatAttr.FILE | FormatAttr.TEXT | FormatAttr.SECONDARY_EXT,
["rst"],
)
SPACE_ALIGNED = (
[SpaceAlignedTableWriter.FORMAT_NAME],
SpaceAlignedTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
[],
)
SQLITE = (
[SqliteTableWriter.FORMAT_NAME],
SqliteTableWriter,
FormatAttr.FILE | FormatAttr.BIN,
["sqlite", "sqlite3"],
)
TOML = (
[TomlTableWriter.FORMAT_NAME],
TomlTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
["toml"],
)
TSV = ([TsvTableWriter.FORMAT_NAME], TsvTableWriter, FormatAttr.FILE | FormatAttr.TEXT, ["tsv"])
@property
def name_list(self):
"""
:return: Names associated with the table format.
:rtype: list
"""
return self.__name_list
@property
def writer_class(self):
"""
:return: Table writer class associated with the table format.
:rtype:
:py:class:`~pytablewriter.writer._table_writer.TableWriterInterface`
"""
return self.__writer_class
@property
def format_attribute(self):
"""
:return: Table attributes bitmap.
:rtype: :py:class:`pytablewriter.FormatAttr`
"""
return self.__format_attribute
@property
def file_extension_list(self):
"""
:return: File extensions associated with the table format.
:rtype: list
"""
return self.__file_extension_list
def __init__(self, name_list, writer_class, format_attribute, file_extension_list):
self.__name_list = name_list
self.__writer_class = writer_class
self.__format_attribute = format_attribute
self.__file_extension_list = file_extension_list
@classmethod
def find_all_attr(cls, format_attribute):
"""
Searching table formats which have specific attributes.
:param FormatAttr format_attribute:
Table format attributes to look for.
:return: Table formats that matched the attribute.
:rtype: list
"""
return [
table_format
for table_format in TableFormat
if table_format.format_attribute & format_attribute
]
@classmethod
def search_table_format(cls, format_attribute):
import warnings
warnings.warn(
"search_table_format will be deleted in the future, " "use find_all_attr instead.",
DeprecationWarning,
)
return cls.find_all_attr(format_attribute)