~ubuntu-branches/ubuntu/quantal/checkbox/quantal

« back to all changes in this revision

Viewing changes to checkbox/lib/conversion.py

  • Committer: Package Import Robot
  • Author(s): Daniel Manrique, Marc Tardif, Daniel Manrique, Jeff Lane, Ara Pulido, Brendan Donegan, Javier Collado
  • Date: 2011-11-18 12:46:21 UTC
  • Revision ID: package-import@ubuntu.com-20111118124621-87rjnjm27ool11e6
Tags: 0.13
New upstream release (LP: #892268):

[Marc Tardif]
* Generate a submission.xml file that contains all device and attachment
* Write the report before reporting the validation error.
* Changed device.product to dmi.product for the formfactor (LP: #875312)

[Daniel Manrique]
* Use gettext for string (LP: #869267)
* Move progress indicator to main checkbox dialog instead of a 
  transient window (LP: #868995)
* Ignore malformed dpkg entries in package_resource (LP: #794747)
* Reset window title after finishing a manual test (LP: #874690)
* Handle "@" in locale names (as in ca@valencia).

[Jeff Lane]
* Went through all the job files and:
  * Updated descriptions to match Unity UI structure
  * Added descriptions where necessary
  * Added further details to some descriptions
  * Moved some jobs to more appropriate files
  * Fixed job names in older job files to match new naming scheme 
    (suite/testname)
  * Added jobs to local.txt to ensure all job files are now parsed
    (this allows easier addition of existing tests to whitelists)
  * Changed remaining manual job descriptions to match the new format
* Updated CD and DVD write tests to be more clear about when to skip
  them (LP: #772794)

[Ara Pulido]
* Rewrote all job descriptions to match OEM QA syntax

[Brendan Donegan]  
* Fix the code that assigns keys in checkbox-cli so that it never assigns
  keys which have other uses. (LP: #877467)
* Show details of unmet job requirements (LP: #855852)
* Ensure that connect_wireless chooses a wireless connection from the list
  of available connections (LP: #877752)
* Have the bluetooth/detect tests require a device with the category
  BLUETOOTH to run, thus preventing the test from failing on systems with
  no Bluetooth device (LP: #862322)
* Rename attachment jobs to not have a forward slash in their name
  (LP: #887964)
* Guard against trying to write files to logical partitions on USB sticks
  (which will obviously fail) in usb_test (LP: #887049)
* Make the OpenGL test ignore the return value of glxgears and improve
  the test description (LP: #890725)
* Allow input/mouse test to run if a TOUCH device is present
  (LP: #886129)

[ Javier Collado ]
* Broken job dependencies fixed (LP: #888447)
* Regex support when specifying blacklists and whitelists on the
  commandline (LP: #588647)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#
19
19
import re
20
20
 
21
 
 
22
 
def string_to_type(value):
23
 
    conversion_table = (
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))))
32
 
 
33
 
    multiplier_table = (
34
 
        ("b", 1),
35
 
        ("kb?", 1024),
36
 
        ("mb?", 1024 * 1024),
37
 
        ("gb?", 1024 * 1024 * 1024),
38
 
        ("tb?", 1024 * 1024 * 1024 * 1024),
39
 
        ("hz", 1),
40
 
        ("khz?", 1024),
41
 
        ("mhz?", 1024 * 1024),
42
 
        ("ghz?", 1024 * 1024 * 1024),
43
 
        ("thz?", 1024 * 1024 * 1024 * 1024))
44
 
 
45
 
    if isinstance(value, basestring):
46
 
        for regex, conversion in conversion_table:
47
 
            match = re.match("^%s$" % regex, value, re.IGNORECASE)
48
 
            if match:
49
 
                value = conversion(match)
50
 
                if len(match.groups()) < 2:
51
 
                    return value
52
 
 
53
 
                unit = match.group(2)
54
 
                for regex, multiplier in multiplier_table:
55
 
                    match = re.match("^%s$" % regex, unit, re.IGNORECASE)
56
 
                    if match:
57
 
                        value *= multiplier
58
 
                        return value
59
 
                else:
60
 
                    raise Exception, "Unknown multiplier: %s" % unit
61
 
 
62
 
    return value
 
21
from dateutil import tz
 
22
from datetime import (
 
23
    datetime,
 
24
    timedelta,
 
25
    )
 
26
 
 
27
 
 
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}))?
 
32
    (?P<tz>
 
33
        (?:(?P<tz_sign>[-+])(?P<tz_hour>\d\d):(?P<tz_minute>\d\d))
 
34
        | Z)?$
 
35
    """, re.VERBOSE)
 
36
 
 
37
TYPE_FORMATS = (
 
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))))
 
46
TYPE_FORMATS = tuple(
 
47
    (re.compile(r"^%s$" % pattern, re.IGNORECASE), format)
 
48
    for pattern, format in TYPE_FORMATS)
 
49
 
 
50
TYPE_MULTIPLIERS = (
 
51
    (r"b", 1),
 
52
    (r"kb?", 1024),
 
53
    (r"mb?", 1024 * 1024),
 
54
    (r"gb?", 1024 * 1024 * 1024),
 
55
    (r"tb?", 1024 * 1024 * 1024 * 1024),
 
56
    (r"hz", 1),
 
57
    (r"khz?", 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)
 
64
 
 
65
 
 
66
def datetime_to_string(dt):
 
67
    """Return a consistent string representation for a given datetime.
 
68
 
 
69
    :param dt: The datetime object.
 
70
    """
 
71
    return dt.isoformat()
 
72
 
 
73
def string_to_datetime(string):
 
74
    """Return a datetime object from a consistent string representation.
 
75
 
 
76
    :param string: The string representation.
 
77
    """
 
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)
 
81
 
 
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.
 
84
    if not match:
 
85
        raise ValueError("Datetime with unreasonable value: %s" % string)
 
86
 
 
87
    time_parts = match.groupdict()
 
88
 
 
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)
 
99
    else:
 
100
        milliseconds = 0
 
101
 
 
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.
 
106
    if second > 59:
 
107
        second = 59
 
108
        milliseconds = 999999
 
109
 
 
110
    dt = datetime(
 
111
        year, month, day, hour, minute, second, milliseconds,
 
112
        tzinfo=tz.tzutc())
 
113
 
 
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))
 
119
        if tz_sign == '-':
 
120
            dt = dt + delta
 
121
        else:
 
122
            dt = dt - delta
 
123
 
 
124
    return dt
63
125
 
64
126
def sizeof_bytes(bytes):
65
127
    for x in ["bytes", "KB", "MB", "GB", "TB"]:
78
140
        hertz /= 1000.0
79
141
 
80
142
    return string
 
143
 
 
144
def string_to_type(string):
 
145
    """Return a typed representation for the given string.
 
146
 
 
147
    The result might be a bool, int or float. The string might also be
 
148
    supplemented by a multiplier like KB which would return an int or
 
149
    float multiplied by 1024 for example.
 
150
 
 
151
    :param string: The string representation.
 
152
    """
 
153
    if isinstance(string, basestring):
 
154
        for regex, formatter in TYPE_FORMATS:
 
155
            match = regex.match(string)
 
156
            if match:
 
157
                string = formatter(match)
 
158
                if len(match.groups()) > 1:
 
159
                    unit = match.group(2)
 
160
                    for regex, multiplier in TYPE_MULTIPLIERS:
 
161
                        match = regex.match(unit)
 
162
                        if match:
 
163
                            string *= multiplier
 
164
                            break
 
165
                    else:
 
166
                        raise ValueError("Unknown multiplier: %s" % unit)
 
167
                break
 
168
 
 
169
    return string