def fetch_toml(uri, section='', key='', fallback=None):
    import tomllib, urllib.request

    with urllib.request.urlopen(uri) as response:
        data = response.read().decode('utf-8')

    config = tomllib.loads(data)

    yield valentina.TableHeader([
        ('section' , str),
        ('key'     , str),
        ('value'   , str)])

    if section:
        if key:
            yield section, key, config.get(section, {}).get(key, fallback)
        else:
            for key, value in config.get(section, {}).items():
                yield section, key, value
    else:
        for section, values in config.items():
            if isinstance(values, dict):
                for key, value in values.items():
                    yield section, key, value


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