/usr/lib/python2.7/dist-packages/autopilot/introspection/types.py is in python-autopilot 1.4.1+17.04.20170305-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Autopilot proxy type support.
=============================
This module defines the classes that are used for all attributes on proxy
objects. All proxy objects contain attributes that transparently mirror the
values present in the application under test. Autopilot takes care of keeping
these values up to date.
Object attributes fall into two categories. Attributes that are a single
string, boolean, or integer property are sent directly across DBus. These are
called "plain" types, and are stored in autopilot as instnaces of the
:class:`PlainType` class. Attributes that are more complex (a rectangle, for
example) are called "complex" types, and are split into several component
values, sent across dbus, and are then reconstituted in autopilot into useful
objects.
"""
from __future__ import absolute_import
from datetime import datetime, time
import dbus
import logging
from testtools.matchers import Equals
import six
from autopilot.introspection.utilities import translate_state_keys
from autopilot.utilities import sleep, compatible_repr
_logger = logging.getLogger(__name__)
class ValueType(object):
"""Store constants for different special types that autopilot understands.
DO NOT add items here unless you have documented them correctly in
docs/appendix/protocol.rst.
"""
PLAIN = 0
RECTANGLE = 1
POINT = 2
SIZE = 3
COLOR = 4
DATETIME = 5
TIME = 6
POINT3D = 7
UNKNOWN = -1
def create_value_instance(value, parent, name):
"""Create an object that exposes the interesing part of the value
specified, given the value_type_id.
:param parent: The object this attribute belongs to.
:param name: The name of this attribute.
:param value: The value array from DBus.
"""
type_dict = {
ValueType.PLAIN: _make_plain_type,
ValueType.RECTANGLE: Rectangle,
ValueType.COLOR: Color,
ValueType.POINT: Point,
ValueType.SIZE: Size,
ValueType.DATETIME: DateTime,
ValueType.TIME: Time,
ValueType.POINT3D: Point3D,
ValueType.UNKNOWN: _make_plain_type,
}
type_id = value[0]
value = value[1:]
if type_id not in type_dict:
_logger.warning("Unknown type id %d", type_id)
type_id = ValueType.UNKNOWN
type_class = type_dict.get(type_id, None)
if type_id == ValueType.UNKNOWN:
value = [dbus.Array(value)]
if len(value) == 0:
raise ValueError("Cannot create attribute, no data supplied")
return type_class(*value, parent=parent, name=name)
class TypeBase(object):
def wait_for(self, expected_value, timeout=10):
"""Wait up to 10 seconds for our value to change to
*expected_value*.
*expected_value* can be a testtools.matcher. Matcher subclass (like
LessThan, for example), or an ordinary value.
This works by refreshing the value using repeated dbus calls.
:raises AssertionError: if the attribute was not equal to the
expected value after 10 seconds.
:raises RuntimeError: if the attribute you called this on was not
constructed as part of an object.
"""
# It's guaranteed that our value is up to date, since __getattr__
# calls refresh_state. This if statement stops us waiting if the
# value is already what we expect:
if self == expected_value:
return
if self.name is None or self.parent is None:
raise RuntimeError(
"This variable was not constructed as part of "
"an object. The wait_for method cannot be used."
)
def make_unicode(value):
if isinstance(value, bytes):
return value.decode('utf8')
return value
if hasattr(expected_value, 'expected'):
expected_value.expected = make_unicode(expected_value.expected)
# unfortunately not all testtools matchers derive from the Matcher
# class, so we can't use issubclass, isinstance for this:
match_fun = getattr(expected_value, 'match', None)
is_matcher = match_fun and callable(match_fun)
if not is_matcher:
expected_value = Equals(expected_value)
time_left = timeout
while True:
# TODO: These next three lines are duplicated from the parent...
# can we just have this code once somewhere?
_, new_state = self.parent.get_new_state()
new_state = translate_state_keys(new_state)
new_value = new_state[self.name][1:]
if len(new_value) == 1:
new_value = make_unicode(new_value[0])
# Support for testtools.matcher classes:
mismatch = expected_value.match(new_value)
if mismatch:
failure_msg = mismatch.describe()
else:
self.parent._set_properties(new_state)
return
if time_left >= 1:
sleep(1)
time_left -= 1
else:
sleep(time_left)
break
raise AssertionError(
"After %.1f seconds test on %s.%s failed: %s" % (
timeout, self.parent.__class__.__name__, self.name,
failure_msg))
class PlainType(TypeBase):
"""Plain type support in autopilot proxy objects.
Instances of this class will be used for all plain attrubites. The word
"plain" in this context means anything that's marshalled as a string,
boolean or integer type across dbus.
Instances of these classes can be used just like the underlying type. For
example, given an object property called 'length' that is marshalled over
dbus as an integer value, the following will be true::
>>> isinstance(object.length, PlainType)
True
>>> isinstance(object.length, int)
True
>>> print(object.length)
123
>>> print(object.length + 32)
155
However, a special case exists for boolean values: because you cannot
subclass from the 'bool' type, the following check will fail (
``object.visible`` is a boolean property)::
>>> isinstance(object.visible, bool)
False
However boolean values will behave exactly as you expect them to.
"""
def __new__(cls, value, parent=None, name=None):
return _make_plain_type(value, parent=parent, name=name)
def _get_repr_callable_for_value_class(cls):
repr_map = {
dbus.Byte: _integer_repr,
dbus.Int16: _integer_repr,
dbus.Int32: _integer_repr,
dbus.UInt16: _integer_repr,
dbus.UInt32: _integer_repr,
dbus.Int64: _integer_repr,
dbus.UInt64: _integer_repr,
dbus.String: _text_repr,
dbus.ObjectPath: _text_repr,
dbus.Signature: _text_repr,
dbus.ByteArray: _bytes_repr,
dbus.Boolean: _boolean_repr,
dbus.Dictionary: _dict_repr,
dbus.Double: _float_repr,
dbus.Struct: _tuple_repr,
dbus.Array: _list_repr,
}
if not six.PY3:
repr_map[dbus.UTF8String] = _bytes_repr
return repr_map.get(cls, None)
def _get_str_callable_for_value_class(cls):
str_map = {
dbus.Boolean: _boolean_str,
dbus.Byte: _integer_str,
}
return str_map.get(cls, None)
@compatible_repr
def _integer_repr(self):
return six.text_type(int(self))
def _create_generic_repr(target_type):
return compatible_repr(lambda self: repr(target_type(self)))
_bytes_repr = _create_generic_repr(six.binary_type)
_text_repr = _create_generic_repr(six.text_type)
_dict_repr = _create_generic_repr(dict)
_list_repr = _create_generic_repr(list)
_tuple_repr = _create_generic_repr(tuple)
_float_repr = _create_generic_repr(float)
_boolean_repr = _create_generic_repr(bool)
def _create_generic_str(target_type):
return compatible_repr(lambda self: str(target_type(self)))
_boolean_str = _create_generic_str(bool)
_integer_str = _integer_repr
def _make_plain_type(value, parent=None, name=None):
new_type = _get_plain_type_class(type(value), parent, name)
return new_type(value)
# Thomi 2014-03-27: dbus types are immutable, which means that we cannot set
# parent and name on the instances we create. This means we have to set them
# as type attributes, which means that this cache doesn't speed things up that
# much. Ideally we'd not rely on the dbus types at all, and simply transform
# them into our own types, but that's work for a separate branch.
#
# Further to the above, we cannot cache these results, since the hash for
# the parent parameter is almost always the same, leading to incorrect cache
# hits. We really need to implement our own types here I think.
def _get_plain_type_class(value_class, parent, name):
new_type_name = value_class.__name__
new_type_bases = (value_class, PlainType)
new_type_dict = dict(parent=parent, name=name)
repr_callable = _get_repr_callable_for_value_class(value_class)
if repr_callable:
new_type_dict['__repr__'] = repr_callable
str_callable = _get_str_callable_for_value_class(value_class)
if str_callable:
new_type_dict['__str__'] = str_callable
return type(new_type_name, new_type_bases, new_type_dict)
def _array_packed_type(num_args):
"""Return a base class that accepts 'num_args' and is packed into a dbus
Array type.
"""
class _ArrayPackedType(dbus.Array, TypeBase):
def __init__(self, *args, **kwargs):
if len(args) != self._required_arg_count:
raise ValueError(
"%s must be constructed with %d arguments, not %d"
% (
self.__class__.__name__,
self._required_arg_count,
len(args)
)
)
super(_ArrayPackedType, self).__init__(args)
# TODO: pop instead of get, and raise on unknown kwarg
self.parent = kwargs.get("parent", None)
self.name = kwargs.get("name", None)
return type(
"_ArrayPackedType_{}".format(num_args),
(_ArrayPackedType,),
dict(_required_arg_count=num_args)
)
class Rectangle(_array_packed_type(4)):
"""The RectangleType class represents a rectangle in cartesian space.
To construct a rectangle, pass the x, y, width and height parameters in to
the class constructor::
my_rect = Rectangle(12,13,100,150)
These attributes can be accessed either using named attributes, or via
sequence indexes::
>>>my_rect = Rectangle(12,13,100,150)
>>> my_rect.x == my_rect[0] == 12
True
>>> my_rect.y == my_rect[1] == 13
True
>>> my_rect.w == my_rect[2] == 100
True
>>> my_rect.h == my_rect[3] == 150
True
You may also access the width and height values using the ``width`` and
``height`` properties::
>>> my_rect.width == my_rect.w
True
>>> my_rect.height == my_rect.h
True
Rectangles can be compared using ``==`` and ``!=``, either to another
Rectangle instance, or to any mutable sequence type::
>>> my_rect == [12, 13, 100, 150]
True
>>> my_rect != Rectangle(1,2,3,4)
True
"""
@property
def x(self):
return self[0]
@property
def y(self):
return self[1]
@property
def w(self):
return self[2]
@property
def width(self):
return self[2]
@property
def h(self):
return self[3]
@property
def height(self):
return self[3]
@compatible_repr
def __repr__(self):
coords = u', '.join((str(c) for c in self))
return u'Rectangle(%s)' % (coords)
class Point(_array_packed_type(2)):
"""The Point class represents a 2D point in cartesian space.
To construct a Point, pass in the x, y parameters to the class
constructor::
>>> my_point = Point(50,100)
These attributes can be accessed either using named attributes, or via
sequence indexes::
>>> my_point.x == my_point[0] == 50
True
>>> my_point.y == my_point[1] == 100
True
Point instances can be compared using ``==`` and ``!=``, either to another
Point instance, or to any mutable sequence type with the correct number of
items::
>>> my_point == [50, 100]
True
>>> my_point != Point(5, 10)
True
"""
@property
def x(self):
return self[0]
@property
def y(self):
return self[1]
@compatible_repr
def __repr__(self):
return u'Point(%d, %d)' % (self.x, self.y)
class Size(_array_packed_type(2)):
"""The Size class represents a 2D size in cartesian space.
To construct a Size, pass in the width, height parameters to the class
constructor::
>>> my_size = Size(50,100)
These attributes can be accessed either using named attributes, or via
sequence indexes::
>>> my_size.width == my_size.w == my_size[0] == 50
True
>>> my_size.height == my_size.h == my_size[1] == 100
True
Size instances can be compared using ``==`` and ``!=``, either to another
Size instance, or to any mutable sequence type with the correct number of
items::
>>> my_size == [50, 100]
True
>>> my_size != Size(5, 10)
True
"""
@property
def w(self):
return self[0]
@property
def width(self):
return self[0]
@property
def h(self):
return self[1]
@property
def height(self):
return self[1]
@compatible_repr
def __repr__(self):
return u'Size(%d, %d)' % (self.w, self.h)
class Color(_array_packed_type(4)):
"""The Color class represents an RGBA Color.
To construct a Color, pass in the red, green, blue and alpha parameters to
the class constructor::
>>> my_color = Color(50, 100, 200, 255)
These attributes can be accessed either using named attributes, or via
sequence indexes::
>>> my_color.red == my_color[0] == 50
True
>>> my_color.green == my_color[1] == 100
True
>>> my_color.blue == my_color[2] == 200
True
>>> my_color.alpha == my_color[3] == 255
True
Color instances can be compared using ``==`` and ``!=``, either to another
Color instance, or to any mutable sequence type with the correct number of
items::
>>> my_color == [50, 100, 200, 255]
True
>>> my_color != Color(5, 10, 0, 0)
True
"""
@property
def red(self):
return self[0]
@property
def green(self):
return self[1]
@property
def blue(self):
return self[2]
@property
def alpha(self):
return self[3]
@compatible_repr
def __repr__(self):
return u'Color(%d, %d, %d, %d)' % (
self.red,
self.green,
self.blue,
self.alpha
)
class DateTime(_array_packed_type(1)):
"""The DateTime class represents a date and time in the UTC timezone.
DateTime is constructed by passing a unix timestamp in to the constructor.
Timestamps are expressed as the number of seconds since 1970-01-01T00:00:00
in the UTC timezone::
>>> my_dt = DateTime(1377209927)
This timestamp can always be accessed either using index access or via a
named property::
>>> my_dt[0] == my_dt.timestamp == 1377209927
True
DateTime objects also expose the usual named properties you would expect on
a date/time object::
>>> my_dt.year
2013
>>> my_dt.month
8
>>> my_dt.day
22
>>> my_dt.hour
22
>>> my_dt.minute
18
>>> my_dt.second
47
Two DateTime objects can be compared for equality::
>>> my_dt == DateTime(1377209927)
True
You can also compare a DateTime with any mutable sequence type containing
the timestamp (although this probably isn't very useful for test authors)::
>>> my_dt == [1377209927]
True
Finally, you can also compare a DateTime instance with a python datetime
instance::
>>> my_datetime = datetime.datetime.fromutctimestamp(1377209927)
True
DateTime instances can be converted to datetime instances:
>>> isinstance(my_dt.datetime, datetime.datetime)
True
"""
def __init__(self, *args, **kwargs):
super(DateTime, self).__init__(*args, **kwargs)
self._cached_dt = datetime.utcfromtimestamp(self[0])
@property
def year(self):
return self._cached_dt.year
@property
def month(self):
return self._cached_dt.month
@property
def day(self):
return self._cached_dt.day
@property
def hour(self):
return self._cached_dt.hour
@property
def minute(self):
return self._cached_dt.minute
@property
def second(self):
return self._cached_dt.second
@property
def timestamp(self):
return self[0]
@property
def datetime(self):
return self._cached_dt
def __eq__(self, other):
if isinstance(other, datetime):
return other == self._cached_dt
return super(DateTime, self).__eq__(other)
@compatible_repr
def __repr__(self):
return u'DateTime(%d-%02d-%02d %02d:%02d:%02d)' % (
self.year,
self.month,
self.day,
self.hour,
self.minute,
self.second
)
class Time(_array_packed_type(4)):
"""The Time class represents a time, without a date component.
You can construct a Time instnace by passing the hours, minutes, seconds,
and milliseconds to the class constructor::
>>> my_time = Time(12, 34, 01, 23)
The values passed in must be valid for their positions (ie..- 0-23 for
hours, 0-59 for minutes and seconds, and 0-999 for milliseconds). Passing
invalid values will cause a ValueError to be raised.
The hours, minutes, seconds, and milliseconds can be accessed using either
index access or named properties::
>>> my_time.hours == my_time[0] == 12
True
>>> my_time.minutes == my_time[1] == 34
True
>>> my_time.seconds == my_time[2] == 01
True
>>> my_time.milliseconds == my_time[3] == 23
True
Time instances can be compared to other time instances, any mutable
sequence containing four integers, or datetime.time instances::
>>> my_time == Time(12, 34, 01, 23)
True
>>> my_time == Time(1,2,3,4)
False
>>> my_time == [12, 34, 01, 23]
True
>>> my_time == datetime.time(12, 34, 01, 23000)
True
Note that the Time class stores milliseconds, while the ``datettime.time``
class stores microseconds.
Finally, you can get a ``datetime.time`` instance from a Time instance::
>>> isinstance(my_time.time, datetime.time)
True
"""
def __init__(self, *args, **kwargs):
super(Time, self).__init__(*args, **kwargs)
# datetime.time uses microseconds, instead of mulliseconds:
self._cached_time = time(self[0], self[1], self[2], self[3] * 1000)
@property
def hour(self):
return self._cached_time.hour
@property
def minute(self):
return self._cached_time.minute
@property
def second(self):
return self._cached_time.second
@property
def millisecond(self):
return self._cached_time.microsecond / 1000
@property
def time(self):
return self._cached_time
def __eq__(self, other):
if isinstance(other, time):
return other == self._cached_time
return super(Time, self).__eq__(other)
@compatible_repr
def __repr__(self):
return u'Time(%02d:%02d:%02d.%03d)' % (
self.hour,
self.minute,
self.second,
self.millisecond
)
class Point3D(_array_packed_type(3)):
"""The Point3D class represents a 3D point in cartesian space.
To construct a Point3D, pass in the x, y and z parameters to the class
constructor::
>>> my_point = Point(50,100,20)
These attributes can be accessed either using named attributes, or via
sequence indexes::
>>> my_point.x == my_point[0] == 50
True
>>> my_point.y == my_point[1] == 100
True
>>> my_point.z == my_point[2] == 20
True
Point3D instances can be compared using ``==`` and ``!=``, either to
another Point3D instance, or to any mutable sequence type with the correct
number of items::
>>> my_point == [50, 100, 20]
True
>>> my_point != Point(5, 10, 2)
True
"""
@property
def x(self):
return self[0]
@property
def y(self):
return self[1]
@property
def z(self):
return self[2]
@compatible_repr
def __repr__(self):
return u'Point3D(%d, %d, %d)' % (
self.x,
self.y,
self.z,
)
|