<reponame>slarse/labelbot
"""Parse label information from issue and config files.

.. module:: parse
    :synopsis: Functions for extracting info from files.
"""
import re

from typing import List

COMMENT_CHAR = "#"
# maybe need an OS independent match here
ALLOWED_LABEL_SEP = "\n"


def parse_wanted_labels(text: str) -> List[str]:
    """Extract the labels defined by :label:`LABEL` tokens in a string.

    Args:
        text: Typically an issue body with label markup.
    Returns:
        A list of extracted labels.
    """
    label_pattern = ":label:`(.*?)`"
    return re.findall(label_pattern, text, re.MULTILINE)


def parse_allowed_labels(text: str) -> List[str]:
    """Parse the allowd labels from the contents of a .allowed-labels file.
    Each line constitutes a single label. Lines starting with a # sign are
    ignored.

    Args:
        text: The contents of a .allowed-labels file.
    Returns:
        A list of defined labels
    """
    return [
        stripped_line
        for stripped_line in (line.strip() for line in text.split(ALLOWED_LABEL_SEP))
        if not stripped_line.startswith(COMMENT_CHAR) and stripped_line
    ]
