| 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # |
| 3 | # Copyright (c) 2016, Matt Layman |
| 4 | |
| 5 | import re |
| 6 | |
| 7 | |
| 8 | class Directive(object): |
| 9 | """A representation of a result line directive.""" |
| 10 | |
| 11 | skip_pattern = re.compile( |
| 12 | r"""^SKIP\S* |
| 13 | (?P<whitespace>\s*) # Optional whitespace. |
| 14 | (?P<reason>.*) # Slurp up the rest.""", |
| 15 | re.IGNORECASE | re.VERBOSE) |
| 16 | todo_pattern = re.compile( |
| 17 | r"""^TODO\b # The directive name |
| 18 | (?P<whitespace>\s*) # Immediately following must be whitespace. |
| 19 | (?P<reason>.*) # Slurp up the rest.""", |
| 20 | re.IGNORECASE | re.VERBOSE) |
| 21 | |
| 22 | def __init__(self, text): |
| 23 | """Initialize the directive by parsing the text. |
| 24 | |
| 25 | The text is assumed to be everything after a '#\s*' on a result line. |
| 26 | """ |
| 27 | self._text = text |
| 28 | self._skip = False |
| 29 | self._todo = False |
| 30 | self._reason = None |
| 31 | |
| 32 | match = self.skip_pattern.match(text) |
| 33 | if match: |
| 34 | self._skip = True |
| 35 | self._reason = match.group('reason') |
| 36 | |
| 37 | match = self.todo_pattern.match(text) |
| 38 | if match: |
| 39 | if match.group('whitespace'): |
| 40 | self._todo = True |
| 41 | else: |
| 42 | # Catch the case where the directive has no descriptive text. |
| 43 | if match.group('reason') == '': |
| 44 | self._todo = True |
| 45 | self._reason = match.group('reason') |
| 46 | |
| 47 | @property |
| 48 | def text(self): |
| 49 | """Get the entire text.""" |
| 50 | return self._text |
| 51 | |
| 52 | @property |
| 53 | def skip(self): |
| 54 | """Check if the directive is a SKIP type.""" |
| 55 | return self._skip |
| 56 | |
| 57 | @property |
| 58 | def todo(self): |
| 59 | """Check if the directive is a TODO type.""" |
| 60 | return self._todo |
| 61 | |
| 62 | @property |
| 63 | def reason(self): |
| 64 | """Get the reason for the directive.""" |
| 65 | return self._reason |