File: log_tokenizer.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 (34 lines) | stat: -rw-r--r-- 986 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
"""
Log entry samples.

##EmpireID:str:2 Name: Binding_2_pid_2_AI_1_RIdx_4_Aggressive Turn: 2
##EmpireColors:0, 255, 0, 255
##CapitalID:str:927 Name: Imperial Arbol I Species: SP_REPLICON
"""
from typing import Optional

from common.statistic_interface import LOG_PREFIX, StatKey


def _get_token_from_line(line: str) -> Optional[tuple[StatKey, str]]:
    """
    Return token from line, if not token present return None.

    Line sample:
    12:06:13.179169 {0x00002e80} [debug] python : statistic.py:30 : ##EmpireID:empire_id: 2, name: Binding_2_pid_2_AI_1_RIdx_4_Aggressive, turn: 1
    """
    index = line.find(LOG_PREFIX, 45, 80)
    if index == -1:
        return None
    line = line[index + 2 :]
    key_, val = line.split(":", 1)
    return StatKey.get_by_value_name(key_), val


def tokenize_log(path):
    with open(path) as f:
        for line in f:
            token = _get_token_from_line(line)
            if not token:
                continue
            yield token