File: parser.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (43 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from logging import error
from typing import IO


def _get_name_from_line(line):
    if "BuildingType " not in line or "BuildingsParser.cpp" not in line or "[trace]" not in line:
        return None
    body = line.rsplit(" : ", 2)[1]
    return body.removeprefix("BuildingType ")


def get_lines_with_dump(f: IO):
    iterator = iter(f)

    for line in iterator:
        if "Start parsing FOCS for BuildingTypes" in line:
            break
    for line in iterator:
        if "End parsing FOCS for BuildingTypes" in line:
            return
        yield line


def parse_buildings(f: IO):
    _result = []
    _current = []
    for line in get_lines_with_dump(f):
        next_section = _get_name_from_line(line)
        if next_section:
            if _current:
                _result.append((_current[0], "".join(_current[1])))
            _current = [next_section, []]
        else:
            if _current:
                _current[1].append(line)

    if not _current:
        error("No data in log, check that game is run with 'parsing' set to 'trace'")
        exit(1)

    _result.append((_current[0], "".join(_current[1])))

    return _result