from pathlib import Path
from typing import Generator, Tuple, TextIO

def _get_version(path: Path) -> str:
    # Python 3.8 doesn't support TOML natively, so we'll parse the version info manually,
    # assuming a very minimal TOML subset
    def _reader(file: TextIO) -> Generator[Tuple[str, str], None, None]:
        for line in file.readlines():
            line = line.strip()
            if not line or line.startswith('#'): continue
            k, v = line.split('=', 1)
            k, v = k.strip(), v.strip()
            if v.startswith('"') and v.endswith('"') and len(v) >= 2: v = v[1:-1]
            yield k, v

    try:
        with open(path) as file:
            info = dict(_reader(file))
        if 'flag' not in info: info['flag'] = ''
        if 'fourth' in info: info['fourth'] = '.' + info['fourth']
        else: info['fourth'] = ''
        return '{major}.{minor}.{micro}{fourth} ({branch}, Build {prefix}{date}-{micro}{flag})'.format(**info)
    except FileNotFoundError:
        return 'Development'
    except Exception:
        return '<invalid version information>'

__version__ = _get_version(Path(__file__).with_name('version_info.toml'))
