| 682 | return re.escape(char) |
| 683 | |
| 684 | def _pattern(self, pData): |
| 685 | data = pData |
| 686 | if "" in data and len(data.keys()) == 1: |
| 687 | return None |
| 688 | |
| 689 | alt = [] |
| 690 | cc = [] |
| 691 | q = 0 |
| 692 | for char in sorted(data.keys()): |
| 693 | if isinstance(data[char], dict): |
| 694 | try: |
| 695 | recurse = self._pattern(data[char]) |
| 696 | alt.append(self.quote(char) + recurse) |
| 697 | except Exception: |
| 698 | cc.append(self.quote(char)) |
| 699 | else: |
| 700 | q = 1 |
| 701 | cconly = not len(alt) > 0 |
| 702 | |
| 703 | if len(cc) > 0: |
| 704 | if len(cc) == 1: |
| 705 | alt.append(cc[0]) |
| 706 | else: |
| 707 | alt.append('[' + ''.join(cc) + ']') |
| 708 | |
| 709 | if len(alt) == 1: |
| 710 | result = alt[0] |
| 711 | else: |
| 712 | result = "(?:" + "|".join(alt) + ")" |
| 713 | |
| 714 | if q: |
| 715 | if cconly: |
| 716 | result += "?" |
| 717 | else: |
| 718 | result = f"(?:{result})?" |
| 719 | return result |
| 720 | |
| 721 | def pattern(self): |
| 722 | return self._pattern(self.dump()) |