Skip to content

Commit 31309d8

Browse files
thermaqettoreleandrotognoli
authored andcommitted
Added Python3.7 compatibility
re._pattern_type has been removed in python3.7 in favor of re.Patern
1 parent 8bed899 commit 31309d8

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

asterisk/ami/event.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
from .utils import basestring
44

5+
try:
6+
PatternType = re._pattern_type
7+
except AttributeError:
8+
PatternType = re.Pattern
9+
510

611
class Event(object):
712
match_regex = re.compile('^Event: .*', re.IGNORECASE)
@@ -76,8 +81,8 @@ def __call__(self, key, value):
7681

7782
class EventListener(object):
7883
def __init__(self, on_event=None, white_list=None, black_list=[], **kwargs):
79-
self.white_list = [white_list] if isinstance(white_list, (basestring, re._pattern_type)) else white_list
80-
self.black_list = [black_list] if isinstance(black_list, (basestring, re._pattern_type)) else black_list
84+
self.white_list = [white_list] if isinstance(white_list, (basestring, PatternType)) else white_list
85+
self.black_list = [black_list] if isinstance(black_list, (basestring, PatternType)) else black_list
8186
for k in list(kwargs.keys()):
8287
if k.startswith('on_'):
8388
setattr(self, k, kwargs.pop(k))
@@ -93,25 +98,25 @@ def check_white_list(self, event_name):
9398
for rule in self.white_list:
9499
if isinstance(rule, basestring) and event_name == rule:
95100
return True
96-
if isinstance(rule, re._pattern_type) and rule.search(event_name) is not None:
101+
if isinstance(rule, PatternType) and rule.search(event_name) is not None:
97102
return True
98103
return False
99104

100105
def check_black_list(self, event_name):
101106
for rule in self.black_list:
102107
if isinstance(rule, basestring) and event_name == rule:
103108
return False
104-
if isinstance(rule, re._pattern_type) and rule.match(event_name) is not None:
109+
if isinstance(rule, PatternType) and rule.match(event_name) is not None:
105110
return False
106111
return True
107112

108113
def check_attribute(self, rules, value):
109-
if isinstance(rules, (re._pattern_type, basestring)):
114+
if isinstance(rules, (PatternType, basestring)):
110115
rules = [rules]
111116
for rule in rules:
112117
if isinstance(rule, basestring) and rule == value:
113118
return True
114-
if isinstance(rule, re._pattern_type) and rule.search(value):
119+
if isinstance(rule, PatternType) and rule.search(value):
115120
return True
116121
return False
117122

0 commit comments

Comments
 (0)