From 31309d8e329b161b34f9b52f2f3fbd933d859a03 Mon Sep 17 00:00:00 2001 From: Marek Matys <57749215+thermaq@users.noreply.github.com> Date: Tue, 26 Nov 2019 06:52:34 +0100 Subject: [PATCH] Added Python3.7 compatibility re._pattern_type has been removed in python3.7 in favor of re.Patern --- asterisk/ami/event.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/asterisk/ami/event.py b/asterisk/ami/event.py index 33a61eb..b149a8c 100644 --- a/asterisk/ami/event.py +++ b/asterisk/ami/event.py @@ -2,6 +2,11 @@ from .utils import basestring +try: + PatternType = re._pattern_type +except AttributeError: + PatternType = re.Pattern + class Event(object): match_regex = re.compile('^Event: .*', re.IGNORECASE) @@ -76,8 +81,8 @@ def __call__(self, key, value): class EventListener(object): def __init__(self, on_event=None, white_list=None, black_list=[], **kwargs): - self.white_list = [white_list] if isinstance(white_list, (basestring, re._pattern_type)) else white_list - self.black_list = [black_list] if isinstance(black_list, (basestring, re._pattern_type)) else black_list + self.white_list = [white_list] if isinstance(white_list, (basestring, PatternType)) else white_list + self.black_list = [black_list] if isinstance(black_list, (basestring, PatternType)) else black_list for k in list(kwargs.keys()): if k.startswith('on_'): setattr(self, k, kwargs.pop(k)) @@ -93,7 +98,7 @@ def check_white_list(self, event_name): for rule in self.white_list: if isinstance(rule, basestring) and event_name == rule: return True - if isinstance(rule, re._pattern_type) and rule.search(event_name) is not None: + if isinstance(rule, PatternType) and rule.search(event_name) is not None: return True return False @@ -101,17 +106,17 @@ def check_black_list(self, event_name): for rule in self.black_list: if isinstance(rule, basestring) and event_name == rule: return False - if isinstance(rule, re._pattern_type) and rule.match(event_name) is not None: + if isinstance(rule, PatternType) and rule.match(event_name) is not None: return False return True def check_attribute(self, rules, value): - if isinstance(rules, (re._pattern_type, basestring)): + if isinstance(rules, (PatternType, basestring)): rules = [rules] for rule in rules: if isinstance(rule, basestring) and rule == value: return True - if isinstance(rule, re._pattern_type) and rule.search(value): + if isinstance(rule, PatternType) and rule.search(value): return True return False