# Path: openpyxl/shared/xmltools.py
# def get_document_content(xml_node):
# def pretty_indent(elem, level=0):
# def start_tag(doc, name, attr=None, body=None, namespace=None):
# def end_tag(doc, name, namespace=None):
# def tag(doc, name, attr=None, body=None, namespace=None):


from StringIO import StringIO
from openpyxl.shared.xmltools import start_tag, end_tag, tag, XMLGenerator
# @license: http://www.opensource.org/licenses/mit-license.php
# @author: Eric Gazoni

"""Write the shared string table."""

# Python stdlib imports

# package imports


def create_string_table(workbook):
    """Compile the string table for a workbook."""
    strings = set()
    for sheet in workbook.worksheets:
        for cell in sheet.get_cell_collection():
            if cell.data_type == cell.TYPE_STRING and cell._value is not None:
                strings.add(cell.value)
    return dict((key, i) for i, key in enumerate(strings))


def write_string_table(string_table):
    """Write the string table xml."""
    temp_buffer = StringIO()
    doc = XMLGenerator(temp_buffer, 'utf-8')
    start_tag(doc, 'sst', {'xmlns':
            'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
            'uniqueCount': '%d' % len(string_table)})
    strings_to_write = sorted(string_table.iteritems(),
            key=lambda pair: pair[1])
    for key in [pair[0] for pair in strings_to_write]:

# Path: openpyxl/shared/xmltools.py
# def get_document_content(xml_node):
# def pretty_indent(elem, level=0):
# def start_tag(doc, name, attr=None, body=None, namespace=None):
# def end_tag(doc, name, namespace=None):
# def tag(doc, name, attr=None, body=None, namespace=None):


# Path: openpyxl/writer/strings.py
from StringIO import StringIO
from openpyxl.shared.xmltools import start_tag, end_tag, tag, XMLGenerator

# @license: http://www.opensource.org/licenses/mit-license.php
# @author: Eric Gazoni

"""Write the shared string table."""

# Python stdlib imports

# package imports


def create_string_table(workbook):
    """Compile the string table for a workbook."""
    strings = set()
    for sheet in workbook.worksheets:
        for cell in sheet.get_cell_collection():
            if cell.data_type == cell.TYPE_STRING and cell._value is not None:
                strings.add(cell.value)
    return dict((key, i) for i, key in enumerate(strings))


def write_string_table(string_table):
    """Write the string table xml."""
    temp_buffer = StringIO()
    doc = XMLGenerator(temp_buffer, 'utf-8')
    start_tag(doc, 'sst', {'xmlns':
            'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
            'uniqueCount': '%d' % len(string_table)})
    strings_to_write = sorted(string_table.iteritems(),
            key=lambda pair: pair[1])
    for key in [pair[0] for pair in strings_to_write]:

        start_tag(doc, 'si')