Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"],
#: 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,
[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"],
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],
``"unicode"`` :py:class:`~.UnicodeTableWriter`
============================================= ===================================
:param str format_name: Format name string (case insensitive).
: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
):
return table_format.writer_class()
raise WriterNotFoundError(
"\n".join(
[
"{} (unknown format name).".format(format_name),
"acceptable format names are: {}.".format(", ".join(cls.get_format_names())),
]
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,
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"])
UNICODE = (
|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
return table_format.writer_class()
raise WriterNotFoundError(
"\n".join(
[
"{:s} (unknown file extension).".format(file_extension),
"",
"acceptable file extensions are: {}.".format(", ".join(cls.get_extensions())),
]
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"])
UNICODE = (
[UnicodeTableWriter.FORMAT_NAME],
UnicodeTableWriter,
FormatAttr.FILE | FormatAttr.TEXT,
[],
)
@property
def names(self):
"""
:return: Names associated with the table format.
:rtype: list
"""
return self.__names