# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_util.ipynb.

# %% auto 0
__all__ = ['ispipedopts', 'attropts', 'regexopts', 'attrpttn', 'getgroup', 'getpttn']

# %% ../nbs/00_util.ipynb 6
import re

# %% ../nbs/00_util.ipynb 8
#| export


# %% ../nbs/00_util.ipynb 10
#| export


# %% ../nbs/00_util.ipynb 12
from nchr import PIPE, CARET, DOLLAR
from chck import allstr, ispipestr, notnone
from putl import uniattrs
from asto import asnum, cast

# %% ../nbs/00_util.ipynb 14
#| export


# %% ../nbs/00_util.ipynb 16
def ispipedopts(*opts) -> bool:
    if allstr(opts) and len(opts) == 1 and ispipestr(opts[0]): return True

def attropts(*opts: str | list | list[str], attr: str = None) -> set:
    if ispipedopts(*opts): return set(list(map(str, opts[0].split(PIPE))))
    elif allstr(opts): return set(list(map(str, opts)))
    else: return set(list(map(str, uniattrs(attr, *opts))))

def regexopts(*opts: str | list | list[str], attr: str = None) -> str:
    return PIPE.join(attropts(*opts, attr=attr))

# %% ../nbs/00_util.ipynb 17
def attrpttn(
    attr: str, 
    *objs,
    ignorecase: bool = True, 
    use_digits: bool = True,
    
    match_beg: bool = False,
    match_end: bool = False,
    prefix: str = None, suffix: str = None
) -> re.compile:
    # Make the regex to check if the attribute matches any of the condition replicates 
    # E.g. wt1 so the regex might be: r'^(ko|wt)\d+'
    options = regexopts(*objs, attr=attr)
    usenums = '(\d+)' if use_digits else ''
    
    begstr = CARET  if match_beg else ''
    endstr = DOLLAR if match_end else ''
    prefix = prefix if notnone(prefix) else ''
    suffix = suffix if notnone(suffix) else ''
    
    pttnstr = rf'{begstr}({options}){usenums}{endstr}'
    pttnstr = rf'{prefix}{pttnstr}{suffix}'
    flagnum = re.IGNORECASE if ignorecase else 0
    return re.compile(pttnstr, flagnum)

# %% ../nbs/00_util.ipynb 18
def getgroup(s: str, pttn: re.compile = None, group: int = 1, flags: re.RegexFlag = re.NOFLAG):
    return res.group(group) if (res := re.search(pttn, s, flags)) else None

def getpttn(s: str,  pttn: re.compile = None, group: int = 1, dtype: type = None):
    return cast(asnum(val), dtype, retnone=False) if notnone(val := getgroup(s, pttn, group)) else None
