|
| 1 | +# This file is part of Checkbox. |
| 2 | +# |
| 3 | +# Copyright 2008-2022 Canonical Ltd. |
| 4 | +# |
| 5 | +# Checkbox is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License version 3, |
| 7 | +# as published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# Checkbox is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import re |
| 18 | +from datetime import datetime, timedelta |
| 19 | + |
| 20 | +from checkbox_ng.support.lib.tz import tzutc |
| 21 | + |
| 22 | +DATETIME_RE = re.compile( |
| 23 | + r""" |
| 24 | + ^(?P<year>\d\d\d\d)-?(?P<month>\d\d)-?(?P<day>\d\d) |
| 25 | + T(?P<hour>\d\d):?(?P<minute>\d\d):?(?P<second>\d\d) |
| 26 | + (?:\.(?P<second_fraction>\d{0,6}))? |
| 27 | + (?P<tz> |
| 28 | + (?:(?P<tz_sign>[-+])(?P<tz_hour>\d\d):(?P<tz_minute>\d\d)) |
| 29 | + | Z)?$ |
| 30 | + """, |
| 31 | + re.VERBOSE, |
| 32 | +) |
| 33 | + |
| 34 | +TYPE_FORMATS = ( |
| 35 | + (r"(yes|true)", lambda v: True), |
| 36 | + (r"(no|false)", lambda v: False), |
| 37 | + (r"-?\d+", lambda v: int(v.group(0))), |
| 38 | + (r"-?\d+\.\d+", lambda v: float(v.group(0))), |
| 39 | + (r"(-?\d+) ?([kmgt]?b?)", lambda v: int(v.group(1))), |
| 40 | + (r"(-?\d+\.\d+) ?([kmgt]?b?)", lambda v: float(v.group(1))), |
| 41 | + (r"(-?\d+) ?([kmgt]?hz)", lambda v: int(v.group(1))), |
| 42 | + (r"(-?\d+\.\d+) ?([kmgt]?hz)", lambda v: float(v.group(1))), |
| 43 | +) |
| 44 | +TYPE_FORMATS = tuple( |
| 45 | + (re.compile(r"^%s$" % pattern, re.IGNORECASE), format) |
| 46 | + for pattern, format in TYPE_FORMATS |
| 47 | +) |
| 48 | + |
| 49 | +TYPE_MULTIPLIERS = ( |
| 50 | + (r"b", 1), |
| 51 | + (r"kb?", 1024), |
| 52 | + (r"mb?", 1024 * 1024), |
| 53 | + (r"gb?", 1024 * 1024 * 1024), |
| 54 | + (r"tb?", 1024 * 1024 * 1024 * 1024), |
| 55 | + (r"hz", 1), |
| 56 | + (r"khz?", 1024), |
| 57 | + (r"mhz?", 1024 * 1024), |
| 58 | + (r"ghz?", 1024 * 1024 * 1024), |
| 59 | + (r"thz?", 1024 * 1024 * 1024 * 1024), |
| 60 | +) |
| 61 | +TYPE_MULTIPLIERS = tuple( |
| 62 | + (re.compile(r"^%s$" % pattern, re.IGNORECASE), multiplier) |
| 63 | + for pattern, multiplier in TYPE_MULTIPLIERS |
| 64 | +) |
| 65 | + |
| 66 | + |
| 67 | +def datetime_to_string(dt): |
| 68 | + """Return a consistent string representation for a given datetime. |
| 69 | +
|
| 70 | + :param dt: The datetime object. |
| 71 | + """ |
| 72 | + return dt.isoformat() |
| 73 | + |
| 74 | + |
| 75 | +def string_to_datetime(string): |
| 76 | + """Return a datetime object from a consistent string representation. |
| 77 | +
|
| 78 | + :param string: The string representation. |
| 79 | + """ |
| 80 | + # we cannot use time.strptime: this function accepts neither fractions |
| 81 | + # of a second nor a time zone given e.g. as '+02:30'. |
| 82 | + match = DATETIME_RE.match(string) |
| 83 | + |
| 84 | + # The Relax NG schema allows a leading minus sign and year numbers |
| 85 | + # with more than four digits, which are not "covered" by _time_regex. |
| 86 | + if not match: |
| 87 | + raise ValueError("Datetime with unreasonable value: %s" % string) |
| 88 | + |
| 89 | + time_parts = match.groupdict() |
| 90 | + |
| 91 | + year = int(time_parts["year"]) |
| 92 | + month = int(time_parts["month"]) |
| 93 | + day = int(time_parts["day"]) |
| 94 | + hour = int(time_parts["hour"]) |
| 95 | + minute = int(time_parts["minute"]) |
| 96 | + second = int(time_parts["second"]) |
| 97 | + second_fraction = time_parts["second_fraction"] |
| 98 | + if second_fraction is not None: |
| 99 | + milliseconds = second_fraction + "0" * (6 - len(second_fraction)) |
| 100 | + milliseconds = int(milliseconds) |
| 101 | + else: |
| 102 | + milliseconds = 0 |
| 103 | + |
| 104 | + # The Relax NG validator accepts leap seconds, but the datetime |
| 105 | + # constructor rejects them. The time values submitted by the HWDB |
| 106 | + # client are not necessarily very precise, hence we can round down |
| 107 | + # to 59.999999 seconds without losing any real precision. |
| 108 | + if second > 59: |
| 109 | + second = 59 |
| 110 | + milliseconds = 999999 |
| 111 | + |
| 112 | + dt = datetime( |
| 113 | + year, month, day, hour, minute, second, milliseconds, tzinfo=tzutc |
| 114 | + ) |
| 115 | + |
| 116 | + tz_sign = time_parts["tz_sign"] |
| 117 | + tz_hour = time_parts["tz_hour"] |
| 118 | + tz_minute = time_parts["tz_minute"] |
| 119 | + if tz_sign in ("-", "+"): |
| 120 | + delta = timedelta(hours=int(tz_hour), minutes=int(tz_minute)) |
| 121 | + if tz_sign == "-": |
| 122 | + dt = dt + delta |
| 123 | + else: |
| 124 | + dt = dt - delta |
| 125 | + |
| 126 | + return dt |
| 127 | + |
| 128 | + |
| 129 | +def sizeof_bytes(bytes): |
| 130 | + for x in ["bytes", "KB", "MB", "GB", "TB"]: |
| 131 | + string = "%3.1f%s" % (bytes, x) |
| 132 | + if bytes < 1024.0: |
| 133 | + break |
| 134 | + bytes /= 1024.0 |
| 135 | + |
| 136 | + return string |
| 137 | + |
| 138 | + |
| 139 | +def sizeof_hertz(hertz): |
| 140 | + for x in ["Hz", "KHz", "MHz", "GHz"]: |
| 141 | + string = "%3.1f%s" % (hertz, x) |
| 142 | + if hertz < 1000.0: |
| 143 | + break |
| 144 | + hertz /= 1000.0 |
| 145 | + |
| 146 | + return string |
| 147 | + |
| 148 | + |
| 149 | +def string_to_type(string): |
| 150 | + """Return a typed representation for the given string. |
| 151 | +
|
| 152 | + The result might be a bool, int or float. The string might also be |
| 153 | + supplemented by a multiplier like KB which would return an int or |
| 154 | + float multiplied by 1024 for example. |
| 155 | +
|
| 156 | + :param string: The string representation. |
| 157 | + """ |
| 158 | + for regex, formatter in TYPE_FORMATS: |
| 159 | + match = regex.match(string) |
| 160 | + if match: |
| 161 | + string = formatter(match) |
| 162 | + if len(match.groups()) > 1: |
| 163 | + unit = match.group(2) |
| 164 | + for regex, multiplier in TYPE_MULTIPLIERS: |
| 165 | + match = regex.match(unit) |
| 166 | + if match: |
| 167 | + string *= multiplier |
| 168 | + break |
| 169 | + else: |
| 170 | + raise ValueError("Unknown multiplier: %s" % unit) |
| 171 | + break |
| 172 | + |
| 173 | + return string |
0 commit comments