Blame src/cli-ng/abrtcli/match.py

Packit Service 8a8a03
import sys
Packit Service 8a8a03
import problem
Packit Service 8a8a03
Packit Service 8a8a03
from abrtcli.l18n import _
Packit Service 8a8a03
from abrtcli.utils import get_human_identifier, sort_problems
Packit Service 8a8a03
Packit Service 8a8a03
Packit Service 8a8a03
def get_match_data(auth=False):
Packit Service 8a8a03
    '''
Packit Service 8a8a03
    Return tuple of two dictionaries: one with components as keys
Packit Service 8a8a03
    and one with short_ids as keys
Packit Service 8a8a03
Packit Service 8a8a03
    Utility function used by match_ functions
Packit Service 8a8a03
    '''
Packit Service 8a8a03
Packit Service 8a8a03
    by_human_id = {}
Packit Service 8a8a03
    by_short_id = {}
Packit Service 8a8a03
Packit Service 8a8a03
    for prob in problem.list(auth=auth):
Packit Service 8a8a03
        comp_or_exe, val = get_human_identifier(prob)
Packit Service 8a8a03
Packit Service 8a8a03
        if val in by_human_id:
Packit Service 8a8a03
            by_human_id[val].append(prob)
Packit Service 8a8a03
        else:
Packit Service 8a8a03
            by_human_id[val] = [prob]
Packit Service 8a8a03
Packit Service 8a8a03
        if prob.short_id in by_short_id:
Packit Service 8a8a03
            by_short_id[prob.short_id].append(prob)
Packit Service 8a8a03
        else:
Packit Service 8a8a03
            by_short_id[prob.short_id] = [prob]
Packit Service 8a8a03
Packit Service 8a8a03
    return by_human_id, by_short_id
Packit Service 8a8a03
Packit Service 8a8a03
Packit Service 8a8a03
def match_completer(prefix, parsed_args, **kwargs):
Packit Service 8a8a03
    '''
Packit Service 8a8a03
    Completer generator used by cli commands using problem lookup
Packit Service 8a8a03
    '''
Packit Service 8a8a03
Packit Service 8a8a03
    by_human_id, by_short_id = get_match_data()
Packit Service 8a8a03
Packit Service 8a8a03
    for short_id in by_short_id.keys():
Packit Service 8a8a03
        yield short_id
Packit Service 8a8a03
Packit Service 8a8a03
    for human_id, probs in by_human_id.items():
Packit Service 8a8a03
        if len(probs) == 1:
Packit Service 8a8a03
            yield '{0}'.format(human_id)
Packit Service 8a8a03
        else:
Packit Service 8a8a03
            for prob in probs:
Packit Service 8a8a03
                yield '{0}@{1}'.format(human_id, prob.short_id)
Packit Service 8a8a03
Packit Service 8a8a03
Packit Service 8a8a03
def match_lookup(in_arg, auth=False):
Packit Service 8a8a03
    '''
Packit Service 8a8a03
    Return problems that match `in_arg` passed on command line
Packit Service 8a8a03
    '''
Packit Service 8a8a03
Packit Service 8a8a03
    by_human_id, by_short_id = get_match_data(auth=auth)
Packit Service 8a8a03
Packit Service 8a8a03
    res = None
Packit Service 8a8a03
Packit Service 8a8a03
    if in_arg in by_human_id:
Packit Service 8a8a03
        res = by_human_id[in_arg]
Packit Service 8a8a03
    elif in_arg in by_short_id:
Packit Service 8a8a03
        res = by_short_id[in_arg]
Packit Service 8a8a03
    elif '@' in in_arg:
Packit Service 8a8a03
        human_id, short_id = in_arg.split('@', 1)
Packit Service 8a8a03
Packit Service 8a8a03
        if human_id in by_human_id:
Packit Service 8a8a03
            probs = by_human_id[human_id]
Packit Service 8a8a03
Packit Service 8a8a03
            res = list(filter(lambda p: p.short_id == short_id, probs))
Packit Service 8a8a03
Packit Service 8a8a03
    return res
Packit Service 8a8a03
Packit Service 8a8a03
Packit Service 8a8a03
def match_get_problem(problem_match, allow_multiple=False, auth=False):
Packit Service 8a8a03
    '''
Packit Service 8a8a03
    Return problem matching `problem_match` pattern
Packit Service 8a8a03
    or exit if there are no such problems or pattern
Packit Service 8a8a03
    results in multiple problems (unless `allow_multiple` is set
Packit Service 8a8a03
    to True).
Packit Service 8a8a03
    '''
Packit Service 8a8a03
Packit Service 8a8a03
    prob = None
Packit Service 8a8a03
    if problem_match == 'last':
Packit Service 8a8a03
        probs = sort_problems(problem.list(auth=auth))
Packit Service 8a8a03
        if not probs:
Packit Service 8a8a03
            print(_('No problems'))
Packit Service 8a8a03
            sys.exit(0)
Packit Service 8a8a03
Packit Service 8a8a03
        prob = probs[0]
Packit Service 8a8a03
    else:
Packit Service 8a8a03
        probs = match_lookup(problem_match, auth=auth)
Packit Service 8a8a03
        if not probs:
Packit Service 8a8a03
            print(_('No problem(s) matched'))
Packit Service 8a8a03
            sys.exit(1)
Packit Service 8a8a03
        elif len(probs) > 1:
Packit Service 8a8a03
            if allow_multiple:
Packit Service 8a8a03
                return probs
Packit Service 8a8a03
Packit Service 8a8a03
            match_collision(probs)
Packit Service 8a8a03
            sys.exit(1)
Packit Service 8a8a03
        else:
Packit Service 8a8a03
            prob = probs[0]
Packit Service 8a8a03
Packit Service 8a8a03
    return prob
Packit Service 8a8a03
Packit Service 8a8a03
Packit Service 8a8a03
def match_collision(probs):
Packit Service 8a8a03
    '''
Packit Service 8a8a03
    Handle matches that result in multiple problems by telling user
Packit Service 8a8a03
    to be more specific
Packit Service 8a8a03
    '''
Packit Service 8a8a03
Packit Service 8a8a03
    print(_('Ambiguous match specified resulting in multiple problems:'))
Packit Service 8a8a03
    for prob in probs:
Packit Service 8a8a03
        field, val = get_human_identifier(prob)
Packit Service 8a8a03
        print('- {}@{} ({})'.format(val, prob.short_id, prob.time))