|
| 1 | +from itertools import chain, islice |
| 2 | +import attr |
| 3 | + |
| 4 | +from allure_commons.types import Severity, LabelType, LinkType |
| 5 | +from allure_commons.types import ALLURE_UNIQUE_LABELS |
| 6 | +from allure_commons.model2 import Label, Link |
| 7 | + |
| 8 | + |
| 9 | +TAG_PREFIX = "allure" |
| 10 | + |
| 11 | + |
| 12 | +def __is(kind, t): |
| 13 | + return kind in [v for k, v in t.__dict__.items() if not k.startswith('__')] |
| 14 | + |
| 15 | + |
| 16 | +def parse_tag(tag): |
| 17 | + """ |
| 18 | + >>> parse_tag("blocker") |
| 19 | + Label(name='severity', value='blocker') |
| 20 | +
|
| 21 | + >>> parse_tag("allure.issue:http://example.com/BUG-42") |
| 22 | + Link(type='issue', url='http://example.com/BUG-42', name='http://example.com/BUG-42') |
| 23 | +
|
| 24 | + >>> parse_tag("allure.link.home:http://qameta.io") |
| 25 | + Link(type='link', url='http://qameta.io', name='home') |
| 26 | +
|
| 27 | + >>> parse_tag("allure.suite:mapping") |
| 28 | + Label(name='suite', value='mapping') |
| 29 | +
|
| 30 | + >>> parse_tag("allure.suite:mapping") |
| 31 | + Label(name='suite', value='mapping') |
| 32 | +
|
| 33 | + >>> parse_tag("allure.label.owner:me") |
| 34 | + Label(name='owner', value='me') |
| 35 | +
|
| 36 | + >>> parse_tag("foo.label:1") |
| 37 | + Label(name='tag', value='foo.label:1') |
| 38 | +
|
| 39 | + >>> parse_tag("allure.foo:1") |
| 40 | + Label(name='tag', value='allure.foo:1') |
| 41 | + """ |
| 42 | + schema, value = islice(chain(tag.split(':', 1), [None]), 2) |
| 43 | + prefix, kind, name = islice(chain(schema.split('.'), [None], [None]), 3) |
| 44 | + |
| 45 | + if tag in [severity for severity in Severity]: |
| 46 | + return Label(name=LabelType.SEVERITY, value=tag) |
| 47 | + |
| 48 | + if prefix == TAG_PREFIX and value is not None: |
| 49 | + |
| 50 | + if __is(kind, LinkType): |
| 51 | + return Link(type=kind, name=name or value, url=value) |
| 52 | + |
| 53 | + if __is(kind, LabelType): |
| 54 | + return Label(name=kind, value=value) |
| 55 | + |
| 56 | + if kind == "label" and name is not None: |
| 57 | + return Label(name=name, value=value) |
| 58 | + |
| 59 | + return Label(name=LabelType.TAG, value=tag) |
| 60 | + |
| 61 | + |
| 62 | +def labels_set(labels): |
| 63 | + """ |
| 64 | + >>> labels_set([Label(name=LabelType.SEVERITY, value=Severity.NORMAL), |
| 65 | + ... Label(name=LabelType.SEVERITY, value=Severity.BLOCKER) |
| 66 | + ... ]) |
| 67 | + [Label(name='severity', value=<Severity.BLOCKER: 'blocker'>)] |
| 68 | +
|
| 69 | + >>> labels_set([Label(name=LabelType.SEVERITY, value=Severity.NORMAL), |
| 70 | + ... Label(name='severity', value='minor') |
| 71 | + ... ]) |
| 72 | + [Label(name='severity', value='minor')] |
| 73 | +
|
| 74 | + >>> labels_set([Label(name=LabelType.EPIC, value="Epic"), |
| 75 | + ... Label(name=LabelType.EPIC, value="Epic") |
| 76 | + ... ]) |
| 77 | + [Label(name='epic', value='Epic')] |
| 78 | +
|
| 79 | + >>> labels_set([Label(name=LabelType.EPIC, value="Epic1"), |
| 80 | + ... Label(name=LabelType.EPIC, value="Epic2") |
| 81 | + ... ]) |
| 82 | + [Label(name='epic', value='Epic1'), Label(name='epic', value='Epic2')] |
| 83 | + """ |
| 84 | + class Wl(object): |
| 85 | + def __init__(self, label): |
| 86 | + self.label = label |
| 87 | + |
| 88 | + def __repr__(self): |
| 89 | + return "{name}{value}".format(**attr.asdict(self.label)) |
| 90 | + |
| 91 | + def __eq__(self, other): |
| 92 | + if self.label.name in ALLURE_UNIQUE_LABELS: |
| 93 | + return self.label.name == other.label.name |
| 94 | + return repr(self) == repr(other) |
| 95 | + |
| 96 | + def __hash__(self): |
| 97 | + if self.label.name in ALLURE_UNIQUE_LABELS: |
| 98 | + return hash(self.label.name) |
| 99 | + return hash(repr(self)) |
| 100 | + |
| 101 | + return sorted([wl.label for wl in set([Wl(l) for l in reversed(labels)])]) |
0 commit comments