<commit_before>def parse_partial_toml(raw_text):
    items = {}
    for line in raw_text.splitlines():
        parts = line.split('=')
        assign = parts[0].strip()
        value = parts[1]

        parse_type = check_parse_type(value)
        if parse_type == 'array':
            value = parse_as_array(value)
        elif parse_type == 'string':
            value = parse_as_string(value)

        items[assign] = value
    return items


def check_parse_type(raw):
    if raw.strip()[0] == '[':
        return 'array'
    else:
        return 'string'


def parse_as_string(raw):
    return raw.strip("\t\n\r \"'")


def parse_as_array(raw):
    items = []
    for item in raw.strip("[]\r\n\t ").split(','):
        items.append(parse_as_string(item))
    return items


# Now, do the thing
with open("sessions/0/session.toml") as conffile:
    config = parse_partial_toml(conffile.read())
    print(config)
    for chapterFilename in config["chapters"]:
        with open("sessions/0/" + chapterFilename) as chapterFile:
            pass<commit_msg>Extend partial toml parsing to work with sections a global namespace.<commit_after>GLOBAL_NAMESPACE = 'global'


def parse_partial_toml(raw_text):
    items = {}
    current_section = GLOBAL_NAMESPACE

    for line in raw_text.splitlines():
        # Check (naively) what the line contains
        if len(line) > 0 and line.lstrip()[0] == '[':
            # Open a new section
            name = line.strip().strip("[]")
            current_section = name
            if current_section not in items:
                items[current_section] = {}

        elif line.find('=') >= 0:
            # We have an assignment line
            parts = line.split('=')
            assign = parts[0].strip()
            value = parts[1]

            parse_type = check_parse_type(value)
            if parse_type == 'array':
                value = parse_as_array(value)
            elif parse_type == 'string':
                value = parse_as_string(value)

            # If it's a global assignment, try to put it there, unless the space is already taken
            if current_section == GLOBAL_NAMESPACE and assign not in items:
                items[assign] = value
            elif current_section == GLOBAL_NAMESPACE:
                items[GLOBAL_NAMESPACE][assign] = value
            else:
                items[current_section][assign] = value

        else:
            # Something else, possibly a comment
            pass

    return items


def check_parse_type(raw):
    if raw.strip()[0] == '[':
        return 'array'
    else:
        return 'string'


def parse_as_string(raw):
    return raw.strip("\t\n\r \"'")


def parse_as_array(raw):
    items = []
    for item in raw.strip("[]\r\n\t ").split(','):
        items.append(parse_as_string(item))
    return items


# Now, do the thing
with open("sessions/0/session.toml") as conffile:
    config = parse_partial_toml(conffile.read())
    print(config)
    for chapterFilename in config["book"]["chapters"]:
        with open("sessions/0/" + chapterFilename) as chapterFile:
            pass
{'fraction_non_alphanumeric': 0.11740109359922805, 'fraction_numerical': 0.0041814088131231905, 'mean_word_length': 2.229491173416407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '588409', 'n_tokens_mistral': 940, 'n_tokens_neox': 924, 'n_words': 233}