| 30 | |
| 31 | |
| 32 | class Pattern(object): |
| 33 | |
| 34 | def __eq__(self, other): |
| 35 | return repr(self) == repr(other) |
| 36 | |
| 37 | def __hash__(self): |
| 38 | return hash(repr(self)) |
| 39 | |
| 40 | def fix(self): |
| 41 | self.fix_identities() |
| 42 | self.fix_repeating_arguments() |
| 43 | return self |
| 44 | |
| 45 | def fix_identities(self, uniq=None): |
| 46 | """Make pattern-tree tips point to same object if they are equal.""" |
| 47 | if not hasattr(self, 'children'): |
| 48 | return self |
| 49 | uniq = list(set(self.flat())) if uniq is None else uniq |
| 50 | for i, child in enumerate(self.children): |
| 51 | if not hasattr(child, 'children'): |
| 52 | assert child in uniq |
| 53 | self.children[i] = uniq[uniq.index(child)] |
| 54 | else: |
| 55 | child.fix_identities(uniq) |
| 56 | |
| 57 | def fix_repeating_arguments(self): |
| 58 | """Fix elements that should accumulate/increment values.""" |
| 59 | either = [list(child.children) for child in transform(self).children] |
| 60 | for case in either: |
| 61 | for e in [child for child in case if case.count(child) > 1]: |
| 62 | if type(e) is Argument or type(e) is Option and e.argcount: |
| 63 | if e.value is None: |
| 64 | e.value = [] |
| 65 | elif type(e.value) is not list: |
| 66 | e.value = e.value.split() |
| 67 | if type(e) is Command or type(e) is Option and e.argcount == 0: |
| 68 | e.value = 0 |
| 69 | return self |
| 70 | |
| 71 | |
| 72 | def transform(pattern): |
nothing calls this directly
no outgoing calls
no test coverage detected