o
    5ti_*                     @   sv   d Z ddlZddlmZ ddlmZmZ ddlZddlm	Z	 ddl
mZmZ ddlmZ dd	lmZ G d
d dZdS )zA
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
    N)chain)AnyFinal   )logger)
FormatAttrTableFormat)WriterNotFoundError)AbstractTableWriterc                   @   sp   e Zd ZdZedededefddZedededefdd	Z	ede
e fd
dZede
e fddZdS )TableWriterFactoryz2
    A factory class of table writer classes.
    file_extensionkwargsreturnc                 K   s   t j|d }t|r|}n|}|d }tD ]"}||jvr#q|j	t
j@ r*qtd|j d |jdi |  S td|dddd	d
|  g)a`	  
        Create a table writer class instance from a file extension.
        Supported file extensions are as follows:

            ==================  ===================================
            Extension           Writer Class
            ==================  ===================================
            ``".adoc"``         :py:class:`~.AsciiDocTableWriter`
            ``".asciidoc"``     :py:class:`~.AsciiDocTableWriter`
            ``".asc"``          :py:class:`~.AsciiDocTableWriter`
            ``".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 pass to a 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.
        r   .	create a 	 instance
sz (unknown file extension). z#acceptable file extensions are: {}., N )ospathsplitexttypepyis_null_stringlstriplowerr   file_extensionsformat_attributer   SECONDARY_EXTr   debugwriter_classr	   joinformatget_extensions)clsr   r   exttable_formatr   r   J/home/ubuntu/.local/lib/python3.10/site-packages/pytablewriter/_factory.pycreate_from_file_extension   s(   /


z-TableWriterFactory.create_from_file_extensionformat_namec              
   K   s|   |  }tD ]#}||jv r)|jtj@ s)|jdi |}td|j	 d |  S qt
d| ddd|  g)	a  
        Create a table writer class instance from a format name.
        Supported file format names are as follows:

            =============================================  ===================================
            Format name                                    Writer Class
            =============================================  ===================================
            ``"adoc"``                                     :py:class:`~.AsciiDocTableWriter`
            ``"asciidoc"``                                 :py:class:`~.AsciiDocTableWriter`
            ``"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`
            ``"ssv"``                                      :py:class:`~.SpaceAlignedTableWriter`
            ``"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 pass to a 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.
        r   r   r   z (unknown format name).z acceptable format names are: {}.r   Nr   )casefoldr   namesr   r   SECONDARY_NAMEr"   r   r!   FORMAT_NAMEr	   r#   r$   get_format_names)r&   r+   r   r(   writerr   r   r)   create_from_format_nameb   s    3

z*TableWriterFactory.create_from_format_namec                 C   s   t tttdd tD  S )a4  
        :return: Available format names.
        :rtype: list

        :Example:
            .. code:: python

                >>> import pytablewriter as ptw
                >>> for name in ptw.TableWriterFactory.get_format_names():
                ...     print(name)
                ...
                adoc
                asciidoc
                bold_unicode
                borderless
                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
                pandas_pickle
                py
                python
                rst
                rst_csv
                rst_csv_table
                rst_grid
                rst_grid_table
                rst_simple
                rst_simple_table
                space_aligned
                sqlite
                ssv
                toml
                tsv
                unicode
                yaml

        c                 s   s    | ]}|j V  qd S )N)r-   ).0r(   r   r   r)   	<genexpr>   s    z6TableWriterFactory.get_format_names.<locals>.<genexpr>)sortedlistsetr   r   )r&   r   r   r)   r0      s   ;z#TableWriterFactory.get_format_namesc                 C   s2   t  }tD ]}|jD ]}|| q
qtt|S )a#  
        :return: Available file extensions.
        :rtype: list

        :Example:
            .. code:: python

                >>> import pytablewriter as ptw
                >>> for name in ptw.TableWriterFactory.get_extensions():
                ...     print(name)
                ...
                adoc
                asc
                asciidoc
                css
                csv
                htm
                html
                js
                json
                jsonl
                ldjson
                ltsv
                md
                ndjson
                py
                rst
                sqlite
                sqlite3
                tex
                toml
                tsv
                xls
                xlsx
                yml
        )r7   r   r   addr5   r6   )r&   file_extension_setr(   r   r   r   r)   r%      s   '
z!TableWriterFactory.get_extensionsN)__name__
__module____qualname____doc__classmethodstrr   r
   r*   r2   r6   r0   r%   r   r   r   r)   r      s    KF<r   )r=   r   	itertoolsr   typingr   r   r   _loggerr   _table_formatr   r   errorr	   r1   r
   r   r   r   r   r)   <module>   s    