def get_by_label(self, label, label_idx=None): """ Get an OF list by label. If a label index is supplied, use that to index into the label and return a single OF instead. :param str label: The label to fetch. E.g. ``UP_HOFS``. :param int label_idx: (Optional) an index of an OF in the specified label. :returns: A list of OFs (or if `label_idx` was specified, a single OF). :raises: :any:`SCIONKeyError`: if the label is unknown. :any:`SCIONIndexError`: if the specified label index is out of range """ try: group = self._labels[label] except KeyError: raise SCIONKeyError("Opaque field label (%s) unknown" % label) from None if label_idx is None: return group try: return group[label_idx] except IndexError: raise SCIONIndexError( "Opaque field label index (%d) for label %s out of range" % (label_idx, label)) from None
def __getitem__(self, idx): if idx == 0: return self.isd_as() elif idx == 1: return self.p.ifID raise SCIONIndexError( "Invalid index used on PathInterface object: %d" % idx)
def __getitem__(self, idx): # pragma: no cover if idx == 0: return self._isd elif idx == 1: return self._as else: raise SCIONIndexError("Invalid index used on %s object: %s" % ( (self.NAME, idx)))
def set_current_index(self, index): """ Sets the current index in the chain. """ if index <= 1 or index > self._length - 1: raise SCIONIndexError("Index must be in [2, %d] but was %d.", self._length - 1, index) self._next_ele_ptr = index - 1
def get_label_by_idx(self, idx): """ Returns the label to which idx points to. :param int idx: The index for which we want to know the label. :returns: The label 'idx' points to. :raises: :any:`SCIONIndexError`: if the index is out of range. """ if idx < 0: raise SCIONIndexError( "Index for requested label is negative (%d)" % idx) offset = idx for label in self._order: group = self._labels[label] if offset < len(group): return label offset -= len(group) raise SCIONIndexError("Index (%d) for requested label is out of range " "(max %d)" % (idx, len(self) - 1)) from None
def get_by_idx(self, idx): """ Get an OF by index. The index follows the order supplied when the :class:`OpaqueFieldList` object was created. :param int idx: The index to fetch. :returns: The OF at that index. :rtype: :class:`OpaqueField` :raises: SCIONIndexError: if the index is negative, or too large. """ if idx < 0: raise SCIONIndexError("Requested OF index (%d) is negative" % idx) offset = idx for label in self._order: group = self._labels[label] if offset < len(group): return group[offset] offset -= len(group) raise SCIONIndexError("Requested OF index (%d) is out of range (max %d)" % (idx, len(self) - 1))
def get(self, n=None, bounds=True): """ Return next elements from data. If `n` is not specified, return all remaining elements of data. If `n` is 1, return the next element of data (as an int). If `n` is > 1, return the next `n` elements of data (as bytes). :param n: How many elements to return (see above) :param bool bounds: Perform bounds checking on access if True """ dlen = len(self._data) if n and bounds and (self._offset + n) > dlen: raise SCIONIndexError("%s: Attempted to access beyond end of raw " "data (len=%d, offset=%d, request=%d)" % (self._desc, dlen, self._offset, n)) if n is None: return self._data[self._offset:] elif n == 1: return self._data[self._offset] else: return self._data[self._offset:self._offset + n]