<filename>cyther/extractor.py

"""
This module provides tools to extract different patterns from raw text or files
"""

import os
import re

# Although NONE and MULTIPLE arent used here, they may be used BY THE USER
from .tools import process_output, assert_output, NONE, MULTIPLE


def get_content(pattern, string, tag='content'):
    """
    Finds the 'content' tag from a 'pattern' in the provided 'string'
    """
    output = []
    for match in re.finditer(pattern, string):
        output.append(match.group(tag))
    return output


def extract(pattern, string, *, assert_equal=False, one=False,
            condense=False, default=None, default_if_multiple=True,
            default_if_none=True):
    """
    Used to extract a given regex pattern from a string, given several options
    """

    if isinstance(pattern, str):
        output = get_content(pattern, string)
    else:
        # Must be a linear container
        output = []
        for p in pattern:
            output += get_content(p, string)

    output = process_output(output, one=one, condense=condense,
                            default=default,
                            default_if_multiple=default_if_multiple,
                            default_if_none=default_if_none)

    if assert_equal:
        assert_output(output, assert_equal)
    else:
        return output


RUNTIME_PATTERN = r"(?<=lib)(?P<content>.+?)(?=\.so|\.a)"


def extractRuntime(runtime_dirs):
    """
    Used to find the correct static lib name to pass to gcc
    """
    names = [str(item) for name in runtime_dirs for item in os.listdir(name)]
    string = '\n'.join(names)
    result = extract(RUNTIME_PATTERN, string, condense=True)
    return result


POUND_PATTERN = r"#\s*@\s?[Cc]yther +(?P<content>.+?)\s*?(\n|$)"
TRIPPLE_PATTERN = r"(?P<quote>'{3}|\"{3})(.|\n)+?@[Cc]yther\s+" \
                  r"(?P<content>(.|\n)+?)\s*(?P=quote)"


def extractAtCyther(string):
    """
    Extracts the '@cyther' code to be run as a script after compilation
    """
    if isinstance(string, str) and os.path.isfile(string):
        with open(string) as file:
            string = file.read()

    found_pound = extract(POUND_PATTERN, string)
    found_tripple = extract(TRIPPLE_PATTERN, string)
    all_found = found_pound + found_tripple
    code = '\n'.join([item for item in all_found])

    return code


VERSION_PATTERN = r"[Vv]((\s*)|(ersion:?\s+))" \
                  r"(?P<content>(\d+\.){1,}((dev)?\d+))"


def extractVersion(string, default='?'):
    """
    Extracts a three digit standard format version number
    """
    return extract(VERSION_PATTERN, string, condense=True, default=default,
                   one=True)


MAJORMINOR_PATTERN = r"(?<=[^\d\.])(?P<content>\d\.\d)"


def extractMajorMinor(dirpath):
    """
    Extracts the version number (excluding the patch number) from a path
    Ex) python/thing/3.5.1/include -> '3.5'
    """
    return extract(MAJORMINOR_PATTERN, dirpath, condense=True, one=True)

<filename>cyther/extractor.py

"""
This module provides tools to extract different patterns from raw text or files
"""

import os
import re

# Although NONE and MULTIPLE arent used here, they may be used BY THE USER
from .tools import process_output, assert_output, NONE, MULTIPLE


def get_content(pattern, string, tag='content'):
    """
    Finds the 'content' tag from a 'pattern' in the provided 'string'
    """
    output = []
    for match in re.finditer(pattern, string):
        output.append(match.group(tag))
    return output


def extract(pattern, string, *, assert_equal=False, one=False,
            condense=False, default=None, default_if_multiple=True,
            default_if_none=True):
    """
    Used to extract a given regex pattern from a string, given several options
    """

    if isinstance(pattern, str):
        output = get_content(pattern, string)
    else:
        # Must be a linear container
        output = []
        for p in pattern:
            output += get_content(p, string)

    output = process_output(output, one=one, condense=condense,
                            default=default,
                            default_if_multiple=default_if_multiple,
                            default_if_none=default_if_none)

    if assert_equal:
        assert_output(output, assert_equal)
    else:
        return output


RUNTIME_PATTERN = r"(?<=lib)(?P<content>.+?)(?=\.so|\.a)"


def extractRuntime(runtime_dirs):
    """
    Used to find the correct static lib name to pass to gcc
    """
    names = [str(item) for name in runtime_dirs for item in os.listdir(name)]
    string = '\n'.join(names)
    result = extract(RUNTIME_PATTERN, string, condense=True)
    return result


POUND_PATTERN = r"#\s*@\s?[Cc]yther +(?P<content>.+?)\s*?(\n|$)"
TRIPPLE_PATTERN = r"(?P<quote>'{3}|\"{3})(.|\n)+?@[Cc]yther\s+" \
                  r"(?P<content>(.|\n)+?)\s*(?P=quote)"


def extractAtCyther(string):
    """
    Extracts the '@cyther' code to be run as a script after compilation
    """
    if isinstance(string, str) and os.path.isfile(string):
        with open(string) as file:
            string = file.read()

    found_pound = extract(POUND_PATTERN, string)
    found_tripple = extract(TRIPPLE_PATTERN, string)
    all_found = found_pound + found_tripple
    code = '\n'.join([item for item in all_found])

    return code


VERSION_PATTERN = r"[Vv]((\s*)|(ersion:?\s+))" \
                  r"(?P<content>(\d+\.){1,}((dev)?\d+))"


def extractVersion(string, default='?'):
    """
    Extracts a three digit standard format version number
    """
    return extract(VERSION_PATTERN, string, condense=True, default=default,
                   one=True)


MAJORMINOR_PATTERN = r"(?<=[^\d\.])(?P<content>\d\.\d)"


def extractMajorMinor(dirpath):
    """
    Extracts the version number (excluding the patch number) from a path
    Ex) python/thing/3.5.1/include -> '3.5'
    """
    return extract(MAJORMINOR_PATTERN, dirpath, condense=True, one=True)

This module provides tools to extract different patterns from raw text or files # Although NONE and MULTIPLE arent used here, they may be used BY THE USER Finds the 'content' tag from a 'pattern' in the provided 'string' Used to extract a given regex pattern from a string, given several options # Must be a linear container Used to find the correct static lib name to pass to gcc Extracts the '@cyther' code to be run as a script after compilation Extracts a three digit standard format version number Extracts the version number (excluding the patch number) from a path     Ex) python/thing/3.5.1/include -> '3.5'