Skip to content

Commit

Permalink
Added Python3.7 compatibility
Browse files Browse the repository at this point in the history
re._pattern_type has been removed in python3.7 in favor of re.Patern
  • Loading branch information
thermaq authored and ettoreleandrotognoli committed Jan 9, 2020
1 parent 8bed899 commit 31309d8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions asterisk/ami/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -93,25 +98,25 @@ 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

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

Expand Down

0 comments on commit 31309d8

Please sign in to comment.