Димитър обнови решението на 22.04.2014 02:15 (преди над 10 години)
+import re
+
+class PrivacyFilter:
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filterPhone(self, text, preserve_phone_country_code):
+
+ def replacePhone(matchobj):
+ if not preserve_phone_country_code:
+ return DELETE_PHONE
+
+ phone = matchobj.group(0)
+ country_code_matcher = re.match(r'^(0[1-9]*|(00|\+)([1-9][0-9]{,2}))', phone)
+ country_code = country_code_matcher.group(0)
+ return country_code + " " + FILTERED
+
+
+ prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
+ internal = r'([ ()-]{0,2}[0-9]){6,11}'
+ pattern = prefix + internal
+
+ return re.sub(pattern, replacePhone, text)
+
+
+ def filterEmail(self, text, preserve_email_hostname, partially_preserve_email_username):
+
+ def replaceEmail(matchobj):
+ email = matchobj.group(0)
+ split_email = email.rsplit('@', 1)
+ if len(split_email) < 2:
+ return False
+ mailbox = split_email[0]
+ domain = split_email[1]
+
+ if partially_preserve_email_username:
+ if len(mailbox) < 6:
+ mailbox = FILTERED
+ else:
+ mailbox = mailbox[0:3] + FILTERED
+ return mailbox + "@" + domain
+ elif preserve_email_hostname:
+ mailbox = FILTERED
+ return mailbox + "@" + domain
+
+ return DELETE_EMAIL
+
+
+ mailbox = r'[A-Za-z0-9](\w|[_+.-]){,200}'
+
+ tld = r'([A-Za-z]{2,3}(.[A-Za-z]{2,3})?)'
+ domain = r'(\w|(\w)(\w|-){,61}(\w)).'
+ subdomains = r'((\w|(\w)(\w|-){,61}(\w)).)*'
+ domain_name = subdomains + domain + tld
+
+ pattern = mailbox + r'@' + domain_name
+
+ return re.sub(pattern, replaceEmail, text)
+
+ def filtered(self):
+ filtered_result = self.filterEmail(self.text, self.preserve_email_hostname , self.partially_preserve_email_username);
+ return self.filterPhone(filtered_result, self.preserve_phone_country_code);
+
+
+DELETE_PHONE = "[PHONE]"
+FILTERED = "[FILTERED]"
+DELETE_EMAIL = "[EMAIL]"
+
+
+class Validations:
+ @classmethod
+ def is_phone(cls, value):
+ prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
+ internal = r'([ ()-]{0,2}[0-9]){6,11}'
+ m = re.match(r'^' + prefix + internal + r'$', value)
+ return m != None
+
+ @classmethod
+ def is_email(cls, value):
+ split_value = value.rsplit('@', 1)
+ if len(split_value) < 2:
+ return False
+ mailbox = split_value[0]
+ domain = split_value[1]
+ m = re.match(r'^[A-Za-z0-9](\w|[_+.-]){,200}$', mailbox)
+ return m != None and cls.is_hostname(domain)
+
+ @classmethod
+ def is_hostname(cls, value):
+ tld = r'([A-Za-z]{2,3}(\.[A-Za-z]{2,3})?)'
+ domain = r'(([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.'
+ subdomains = r'((([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.)*'
+ m = re.match(r'^' + subdomains + domain + tld + r'$', value)
+ return m != None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ m = re.match(r'^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 255) and (0 <= int(m.group(2)) <= 255) and (0 <= int(m.group(3)) <= 255) and (0 <= int(m.group(4)) <= 255)
+
+ @classmethod
+ def is_number(cls, value):
+ m = re.match(r'^-?(0{1}|[1-9]\d+)(.\d+)?$', value)
+ return m != None
+
+ @classmethod
+ def is_integer(cls, value):
+ return cls.is_number(value) and value.find('.') == -1
+
+ @classmethod
+ def is_date(cls, value):
+ m = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 9999) and (1 <= int(m.group(2)) <= 12) and (1 <= int(m.group(3)) <= 31)
+
+ @classmethod
+ def is_time(cls, value):
+ m = re.match(r'^(\d{2}):(\d{2}):(\d{2})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 23) and (0 <= int(m.group(2)) <= 59) and (0 <= int(m.group(3)) <= 59)
+
+ @classmethod
+ def is_datetime(cls, value):
+ if len(value) != 19:
+ return False
+ date = value[:10]
+ time = value[11:]
+ return value[10] == ' ' and cls.is_date(date) and cls.is_time(time)