Евгени обнови решението на 21.04.2014 22:22 (преди над 10 години)
+import re
+
+
+LOCAL_FORMAT = r'\b0([\s()-]{,2}\d){6,11}\b'
+INTERNATIONAL_FORMAT = r'((00|\+)[1-9]\d{0,2})([\s()-]{,2}\d){6,11}\b'
+DOMAIN = r'(([a-zA-Z\d]\.)|([a-zA-Z\d][-a-zA-Z\d]{,61}[a-zA-Z\d])\.)+'
+TLD = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+USERNAME = r'[a-zA-Z\d][\w\+\.-]{,200}'
+DATE = r'[0-9]{4}-(0[1-9]|11|12)-(0[1-9]|((1|2)[0-9])|(3[0-1]))'
+TIME = r'(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
+BYTE = r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
+THREE_BYTES = r'((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
+NUMBER = r'-?(0|[1-9][0-9]*)(\.[0-9]+)?'
+INTEGER = r'-?(0|[1-9][0-9]*)'
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def local_format_phone_filtered(self, text):
+ return re.sub(LOCAL_FORMAT, '[PHONE]', text)
+
+ def international_format_phone_filtered(self, text):
+ return re.sub(INTERNATIONAL_FORMAT, '[PHONE]', text)
+
+ def partially_international_format_phone(self, text):
+ match = re.search(INTERNATIONAL_FORMAT, text)
+ code = match.group(1)
+ return re.sub(INTERNATIONAL_FORMAT, code + " [FILTERED]", text)
+
+ def email_filter(self, text):
+ pattern = r'\b' + USERNAME + r'@' + DOMAIN + TLD + r'\b'
+ return re.sub(pattern, '[EMAIL]', text)
+
+ def email_components_filter(self, text):
+ pattern = r'\b' + USERNAME + r'@' + DOMAIN + TLD + r'\b'
+ match = re.search(pattern, text)
+ start, end = match.span()
+ username, hostname = text[start:end].split('@')
+ if len(username) < 6:
+ return "[FILTERED]@" + hostname
+ elif self.preserve_email_hostname:
+ return "[FILTERED]@" + hostname
+ return username[0:3] + '[FILTERED]@' + hostname
+
+ def filtered(self):
+ if self.preserve_email_hostname or\
+ self.partially_preserve_email_username:
+ result = self.email_components_filter(self.text)
+ else:
+ result = self.email_filter(self.text)
+ if re.search(LOCAL_FORMAT, result):
+ return self.local_format_phone_filtered(result)
+ if re.search(INTERNATIONAL_FORMAT, result) and\
+ self.preserve_phone_country_code:
+ return self.partially_international_format_phone(result)
+ if re.search(INTERNATIONAL_FORMAT, result):
+ return self.international_format_phone_filtered(result)
+ return result
+
+
+class Validations:
+ @classmethod
+ def equal(cls, pattern, value):
+ match = re.search(pattern, value)
+ if not match:
+ return False
+ start, end = match.span()
+ if value[start:end] == value:
+ return True
+ return False
+
+ @classmethod
+ def is_email(cls, value):
+ pattern = r'\b' + USERNAME + r'@' + DOMAIN + TLD + r'\b'
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_phone(csl, value):
+ if Validations.equal(INTERNATIONAL_FORMAT, value) or\
+ Validations.equal(LOCAL_FORMAT, value):
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ pattern = r'\b' + DOMAIN + TLD + r'\b'
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_ip_address(cls, value):
+ pattern = THREE_BYTES + BYTE
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = NUMBER
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = INTEGER
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = DATE
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = TIME
+ return Validations.equal(pattern, value)
+
+ @classmethod
+ def is_datetime(cls, value):
+ pattern = DATE + r'( |T)' + TIME
+ return Validations.equal(pattern, value)