| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to deal |
| 7 | # in the Software without restriction, including without limitation the rights |
| 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | # copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | # THE SOFTWARE. |
| 22 | |
| 23 | from bt2 import native_bt, object, utils |
| 24 | import collections.abc |
| 25 | import bt2.clock_class |
| 26 | import copy |
| 27 | import bt2 |
| 28 | |
| 29 | |
| 30 | class _ClockClassIterator(collections.abc.Iterator): |
| 31 | def __init__(self, cc_prio_map): |
| 32 | self._cc_prio_map = cc_prio_map |
| 33 | self._at = 0 |
| 34 | |
| 35 | def __next__(self): |
| 36 | if self._at == len(self._cc_prio_map): |
| 37 | raise StopIteration |
| 38 | |
| 39 | cc_ptr = native_bt.clock_class_priority_map_get_clock_class_by_index(self._cc_prio_map._ptr, |
| 40 | self._at) |
| 41 | assert(cc_ptr) |
| 42 | clock_class = bt2.ClockClass._create_from_ptr(cc_ptr) |
| 43 | self._at += 1 |
| 44 | return clock_class |
| 45 | |
| 46 | |
| 47 | class ClockClassPriorityMap(object._Object, collections.abc.MutableMapping): |
| 48 | def __init__(self, clock_class_priorities=None): |
| 49 | ptr = native_bt.clock_class_priority_map_create() |
| 50 | |
| 51 | if ptr is None: |
| 52 | raise bt2.CreationError('cannot create clock class priority map object') |
| 53 | |
| 54 | super().__init__(ptr) |
| 55 | |
| 56 | if clock_class_priorities is not None: |
| 57 | for clock_class, priority in clock_class_priorities.items(): |
| 58 | self[clock_class] = priority |
| 59 | |
| 60 | def __getitem__(self, key): |
| 61 | utils._check_type(key, bt2.ClockClass) |
| 62 | ret, prio = native_bt.clock_class_priority_map_get_clock_class_priority(self._ptr, |
| 63 | key._ptr) |
| 64 | |
| 65 | if ret != 0: |
| 66 | raise KeyError(key) |
| 67 | |
| 68 | return prio |
| 69 | |
| 70 | def __len__(self): |
| 71 | count = native_bt.clock_class_priority_map_get_clock_class_count(self._ptr) |
| 72 | assert(count >= 0) |
| 73 | return count |
| 74 | |
| 75 | def __delitem__(self): |
| 76 | raise NotImplementedError |
| 77 | |
| 78 | def __setitem__(self, key, value): |
| 79 | utils._check_type(key, bt2.ClockClass) |
| 80 | utils._check_uint64(value) |
| 81 | ret = native_bt.clock_class_priority_map_add_clock_class(self._ptr, |
| 82 | key._ptr, |
| 83 | value) |
| 84 | utils._handle_ret(ret, "cannot set clock class's priority in clock class priority map object") |
| 85 | |
| 86 | def __iter__(self): |
| 87 | return _ClockClassIterator(self) |
| 88 | |
| 89 | @property |
| 90 | def highest_priority_clock_class(self): |
| 91 | cc_ptr = native_bt.clock_class_priority_map_get_highest_priority_clock_class(self._ptr) |
| 92 | |
| 93 | if cc_ptr is None: |
| 94 | return |
| 95 | |
| 96 | return bt2.ClockClass._create_from_ptr(cc_ptr) |
| 97 | |
| 98 | def _get_prios(self): |
| 99 | prios = {} |
| 100 | |
| 101 | for clock_class, prio in self.items(): |
| 102 | prios[clock_class] = prio |
| 103 | |
| 104 | return prios |
| 105 | |
| 106 | def __eq__(self, other): |
| 107 | if type(other) is not type(self): |
| 108 | return False |
| 109 | |
| 110 | if self.addr == other.addr: |
| 111 | return True |
| 112 | |
| 113 | return self._get_prios() == other._get_prios() |
| 114 | |
| 115 | def _copy(self, cc_copy_func): |
| 116 | cpy = ClockClassPriorityMap() |
| 117 | |
| 118 | for clock_class, prio in self.items(): |
| 119 | cpy[cc_copy_func(clock_class)] = prio |
| 120 | |
| 121 | return cpy |
| 122 | |
| 123 | def __copy__(self): |
| 124 | return self._copy(lambda obj: obj) |
| 125 | |
| 126 | def __deepcopy__(self, memo): |
| 127 | cpy = self._copy(copy.deepcopy) |
| 128 | memo[id(self)] = cpy |
| 129 | return cpy |