· 7 years ago · Oct 10, 2018, 12:22 PM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2011 The Octopus Apps Inc.
5# Licensed under the Apache License, Version 2.0 (the "License")
6#
7# Author: Alejandro M. Bernardis
8# Email: alejandro.m.bernardis at gmail.com
9# Created: May 8, 2012, 2:15:08 PM
10
11import datetime
12import re
13
14from itertools import izip
15
16#: -- helpers ------------------------------------------------------------------
17
18__all__ = [
19 "safe_str_cmp",
20 "is_email",
21 "bool_to_str",
22 "unicode_to_str",
23 "datetime_to_str",
24 "datetimefull_to_str",
25 "date_to_str",
26 "time_to_str",
27 "timefull_to_str",
28 "str_to_unicode",
29 "str_to_date",
30 "str_to_time",
31 "str_to_datetime",
32 "get_plural",
33 "get_str_plural",
34 "regex_email",
35 "regex_email_str",
36 "regex_username_str",
37 "regex_password_str",
38 "is_primitive",
39]
40
41#: -- safe_str_cmp -------------------------------------------------------------
42
43def safe_str_cmp(a, b):
44 if len(a) != len(b):
45 return False
46 result = 0
47 for x, y in izip(a, b):
48 result |= ord(x) ^ ord(y)
49 return result == 0
50
51#: -- regex_* ------------------------------------------------------------------
52
53regex_username_str = r"^([a-zA-Z0-9_\-\.@]+)$"
54
55regex_password_str = r"^[a-zA-Z0-9+_\-!\$\.#@%]+$"
56
57regex_email_str = r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"\
58 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"'\
59 r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$'
60
61#: -- is_email -----------------------------------------------------------------
62
63regex_email = re.compile(regex_email_str, re.IGNORECASE)
64
65def is_email(value):
66 return True if bool(regex_email.match(value)) else False
67
68#: -- wrapper ------------------------------------------------------------------
69
70def swallow_args(func):
71 def Decorator(arg, *unused_args):
72 if not arg:
73 return None
74 return func(arg)
75 return Decorator
76
77#: -- convert ------------------------------------------------------------------
78
79@swallow_args
80def bool_to_str(arg):
81 return str(arg).lower()
82
83@swallow_args
84def unicode_to_str(arg):
85 return arg.encode("utf-8")
86
87@swallow_args
88def datetime_to_str(arg):
89 return "%d-%02d-%02d %02d:%02d:%02d" %\
90 (arg.year, arg.month, arg.day,
91 arg.hour, arg.minute, arg.second)
92
93@swallow_args
94def datetimefull_to_str(arg):
95 return "%d-%02d-%02d %02d:%02d:%02d.%06d" %\
96 (arg.year, arg.month, arg.day,
97 arg.hour, arg.minute, arg.second, arg.microsecond)
98
99@swallow_args
100def date_to_str(arg):
101 return arg.strftime("%Y-%m-%d")
102
103@swallow_args
104def time_to_str(arg):
105 return "%02d:%02d:%02d" %\
106 (arg.hour, arg.minute, arg.second)
107
108@swallow_args
109def timefull_to_str(arg):
110 return "%02d:%02d:%02d.%06d" %\
111 (arg.hour, arg.minute, arg.second, arg.microsecond)
112
113#: -----------------------------------------------------------------------------
114
115def _strptime(arg, strptime_format):
116 split_arg = arg.split(".")
117 datetime_obj = datetime.datetime.strptime(split_arg[0], strptime_format)
118 if len(split_arg) == 2:
119 datetime_obj = datetime_obj.replace(microsecond=int(split_arg[1]))
120 return datetime_obj
121
122#: -----------------------------------------------------------------------------
123
124def str_to_unicode(arg):
125 return unicode(arg, "utf-8")
126
127def str_to_date(arg):
128 return _strptime(arg, "%Y-%m-%d").date()
129
130def str_to_time(arg):
131 return _strptime(arg, "%H:%M:%S").time()
132
133def str_to_datetime(arg):
134 return _strptime(arg, "%Y-%m-%d %H:%M:%S")
135
136#: -- plural -------------------------------------------------------------------
137
138def get_plural(value, singular, plural, message=""):
139 try: return plural if value > 1 else singular
140 except: return message
141
142def get_str_plural(value, singular, plural, message="", template=u"%s %s"):
143 return template % (value, get_plural(value, singular, plural))
144
145#: -- minsc --------------------------------------------------------------------
146
147def is_primitive(value):
148 primitive = [complex, int, float, long,
149 bool, str, basestring, unicode, tuple, list]
150 return type(value) in primitive