<filename>stack_ck2/parser.py
import collections
import mmap

from stack_ck2.handler import Handler
from stack_ck2.CK2ParseException import CK2ParseException

CK2_HEADER_BYTES = b"CK2txt"

def parse(file_name: str, base: Handler, save_file: bool = True) -> None:
    """
    :param file_name: The file to read.
    :param base: The base handler.
    :param save_file: If reading a save file.
    """
    stack = collections.deque()
    stack.append(base)
    # Save files are cp-1252, but we only need to check ascii non-control
    # characters. Note that we need to decode the bytes before
    # emitting an event.
    with open(file_name, "rb") as f, \
            mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
        mm.madvise(mmap.MADV_SEQUENTIAL)
        if save_file and mm.read(6) != CK2_HEADER_BYTES:
            # Expect header token for .ck2 files
            raise CK2ParseException("Save file is missing header")

        comment_flag = False
        string_flag = False
        buffer = bytearray()
        lookahead_buffer = bytearray()
        lookahead_buffer_2 = bytearray()

        # Start parsing
        base.startDocument()

        base.endDocument()

<filename>stack_ck2/parser.py
import collections
import mmap

from stack_ck2.handler import Handler
from stack_ck2.CK2ParseException import CK2ParseException

CK2_HEADER_BYTES = b"CK2txt"

def parse(file_name: str, base: Handler, save_file: bool = True) -> None:
    """
    :param file_name: The file to read.
    :param base: The base handler.
    :param save_file: If reading a save file.
    """
    stack = collections.deque()
    stack.append(base)
    # Save files are cp-1252, but we only need to check ascii non-control
    # characters. Note that we need to decode the bytes before
    # emitting an event.
    with open(file_name, "rb") as f, \
            mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
        mm.madvise(mmap.MADV_SEQUENTIAL)
        if save_file and mm.read(6) != CK2_HEADER_BYTES:
            # Expect header token for .ck2 files
            raise CK2ParseException("Save file is missing header")

        comment_flag = False
        string_flag = False
        buffer = bytearray()
        lookahead_buffer = bytearray()
        lookahead_buffer_2 = bytearray()

        # Start parsing
        base.startDocument()

        base.endDocument()

:param file_name: The file to read.     :param base: The base handler.     :param save_file: If reading a save file. # Save files are cp-1252, but we only need to check ascii non-control # characters. Note that we need to decode the bytes before # emitting an event. # Expect header token for .ck2 files # Start parsing