def fetch_ods(uri):
    import io, string, urllib.request
    from odf.opendocument import load
    from odf.table import Table, TableRow, TableCell
    from odf.text import P

    with urllib.request.urlopen(uri) as response:
        file_content = io.BytesIO(response.read())

    doc    = load(file_content)
    sheets = doc.spreadsheet.getElementsByType(Table)
    sheet  = sheets[0]

    full_data    = []
    column_count = 0

    for row in sheet.getElementsByType(TableRow):
        row_data = []
        with_data = False

        for cell in row.getElementsByType(TableCell):
            ps = cell.getElementsByType(P)
            textContent = ""

            for p in ps:
                for n in p.childNodes:
                    if (n.nodeType == 1 and
                            ((n.tagName == "text:span") or (n.tagName == "text:a"))):
                        for c in n.childNodes:
                            if (c.nodeType == 3):
                                textContent = u'{}{}'.format(textContent, c.data)

                    if (n.nodeType == 3):
                        textContent = u'{}{}'.format(textContent, n.data)

            if textContent:
                row_data.append(textContent)
                with_data = True
            else:
                row_data.append(None)

        if with_data:
            while row_data and row_data[-1] is None:
                row_data.pop()

            full_data.append(row_data)
            column_count = max(column_count, len(row_data))

    full_data.insert(0,list(string.ascii_uppercase[:column_count]))
    return full_data

def valentina_extension_init(db):
    valentina.create_procedure('fetch_ods', fetch_ods)
