22
def string_to_type(value):
24
("(yes|true)", lambda v: True),
25
("(no|false)", lambda v: False),
26
("\d+", lambda v: int(v.group(0))),
27
("\d+\.\d+", lambda v: float(v.group(0))),
28
("(\d+) ?([kmgt]?b?)", lambda v: int(v.group(1))),
29
("(\d+\.\d+) ?([kmgt]?b?)", lambda v: float(v.group(1))),
30
("(\d+) ?([kmgt]?hz?)", lambda v: int(v.group(1))),
31
("(\d+\.\d+) ?([kmgt]?hz?)", lambda v: float(v.group(1))))
37
("gb?", 1024 * 1024 * 1024),
38
("tb?", 1024 * 1024 * 1024 * 1024),
41
("mhz?", 1024 * 1024),
42
("ghz?", 1024 * 1024 * 1024),
43
("thz?", 1024 * 1024 * 1024 * 1024))
45
if isinstance(value, basestring):
46
for regex, conversion in conversion_table:
47
match = re.match("^%s$" % regex, value, re.IGNORECASE)
49
value = conversion(match)
50
if len(match.groups()) < 2:
54
for regex, multiplier in multiplier_table:
55
match = re.match("^%s$" % regex, unit, re.IGNORECASE)
60
raise Exception, "Unknown multiplier: %s" % unit
21
from dateutil import tz
22
from datetime import (
28
DATETIME_RE = re.compile(r"""
29
^(?P<year>\d\d\d\d)-?(?P<month>\d\d)-?(?P<day>\d\d)
30
T(?P<hour>\d\d):?(?P<minute>\d\d):?(?P<second>\d\d)
31
(?:\.(?P<second_fraction>\d{0,6}))?
33
(?:(?P<tz_sign>[-+])(?P<tz_hour>\d\d):(?P<tz_minute>\d\d))
38
(r"(yes|true)", lambda v: True),
39
(r"(no|false)", lambda v: False),
40
(r"-?\d+", lambda v: int(v.group(0))),
41
(r"-?\d+\.\d+", lambda v: float(v.group(0))),
42
(r"(-?\d+) ?([kmgt]?b?)", lambda v: int(v.group(1))),
43
(r"(-?\d+\.\d+) ?([kmgt]?b?)", lambda v: float(v.group(1))),
44
(r"(-?\d+) ?([kmgt]?hz)", lambda v: int(v.group(1))),
45
(r"(-?\d+\.\d+) ?([kmgt]?hz)", lambda v: float(v.group(1))))
47
(re.compile(r"^%s$" % pattern, re.IGNORECASE), format)
48
for pattern, format in TYPE_FORMATS)
53
(r"mb?", 1024 * 1024),
54
(r"gb?", 1024 * 1024 * 1024),
55
(r"tb?", 1024 * 1024 * 1024 * 1024),
58
(r"mhz?", 1024 * 1024),
59
(r"ghz?", 1024 * 1024 * 1024),
60
(r"thz?", 1024 * 1024 * 1024 * 1024))
61
TYPE_MULTIPLIERS = tuple(
62
(re.compile(r"^%s$" % pattern, re.IGNORECASE), multiplier)
63
for pattern, multiplier in TYPE_MULTIPLIERS)
66
def datetime_to_string(dt):
67
"""Return a consistent string representation for a given datetime.
69
:param dt: The datetime object.
73
def string_to_datetime(string):
74
"""Return a datetime object from a consistent string representation.
76
:param string: The string representation.
78
# we cannot use time.strptime: this function accepts neither fractions
79
# of a second nor a time zone given e.g. as '+02:30'.
80
match = DATETIME_RE.match(string)
82
# The Relax NG schema allows a leading minus sign and year numbers
83
# with more than four digits, which are not "covered" by _time_regex.
85
raise ValueError("Datetime with unreasonable value: %s" % string)
87
time_parts = match.groupdict()
89
year = int(time_parts['year'])
90
month = int(time_parts['month'])
91
day = int(time_parts['day'])
92
hour = int(time_parts['hour'])
93
minute = int(time_parts['minute'])
94
second = int(time_parts['second'])
95
second_fraction = time_parts['second_fraction']
96
if second_fraction is not None:
97
milliseconds = second_fraction + '0' * (6 - len(second_fraction))
98
milliseconds = int(milliseconds)
102
# The Relax NG validator accepts leap seconds, but the datetime
103
# constructor rejects them. The time values submitted by the HWDB
104
# client are not necessarily very precise, hence we can round down
105
# to 59.999999 seconds without losing any real precision.
108
milliseconds = 999999
111
year, month, day, hour, minute, second, milliseconds,
114
tz_sign = time_parts['tz_sign']
115
tz_hour = time_parts['tz_hour']
116
tz_minute = time_parts['tz_minute']
117
if tz_sign in ('-', '+'):
118
delta = timedelta(hours=int(tz_hour), minutes=int(tz_minute))
64
126
def sizeof_bytes(bytes):
65
127
for x in ["bytes", "KB", "MB", "GB", "TB"]: