Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 1 | ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | # |
| 3 | # Copyright (c) 2011-2013, Regents of the University of California |
| 4 | # Alexander Afanasyev |
| 5 | # |
| 6 | # GNU 3.0 license, See the LICENSE file for more information |
| 7 | # |
| 8 | # Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | # |
| 10 | |
| 11 | # |
| 12 | # Based on PyCCN code, copyrighted and licensed as follows |
| 13 | # |
| 14 | # Copyright (c) 2011-2013, Regents of the University of California |
| 15 | # BSD license, See the COPYING file for more information |
| 16 | # Written by: Derek Kulinski <takeda@takeda.tk> |
| 17 | # Jeff Burke <jburke@ucla.edu> |
| 18 | # |
| 19 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 20 | import ns.ndnSIM |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 21 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 22 | class Name (object): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 23 | _name = None |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 25 | def __init__ (self, |
| 26 | value = None, |
| 27 | name = None): |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 28 | if name is not None: |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 29 | if isinstance (name, ns.ndnSIM.ndn.Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 30 | self._name = name |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 31 | elif isinstance (name, Name): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 32 | self._name = name._name |
| 33 | else: |
| 34 | raise TypeError ("Incorrect type for 'name' parameter [%s]" % type (name)) |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 35 | else: |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 36 | if value is not None: |
| 37 | if isinstance (value, Name): |
| 38 | self._name = ns.ndnSIM.ndn.Name (value._name) |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 39 | else: |
Alexander Afanasyev | 7ff81b7 | 2013-08-08 21:43:16 -0700 | [diff] [blame] | 40 | self._name = ns.ndnSIM.ndn.Name (value) |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 41 | else: |
| 42 | self._name = ns.ndnSIM.ndn.Name () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 44 | @staticmethod |
| 45 | def fromWire (wire): |
Alexander Afanasyev | 7992ab6 | 2013-07-19 16:50:58 -0700 | [diff] [blame] | 46 | return Name (name = ns.ndnSIM.ndn.Wire.ToName (wire)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 7992ab6 | 2013-07-19 16:50:58 -0700 | [diff] [blame] | 48 | def toWire (self): |
| 49 | return ns.ndnSIM.ndn.Wire.FromName (self._name) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 50 | |
| 51 | def append (self, value): |
Alexander Afanasyev | 7ff81b7 | 2013-08-08 21:43:16 -0700 | [diff] [blame] | 52 | if isinstance (value, Name): |
| 53 | self._name.append (value._name) |
| 54 | else: |
| 55 | self._name.append (value) |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 56 | return self |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 58 | def __getattr__ (self, name): |
| 59 | return self._name.__getattribute__ (name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 61 | def __len__ (self): |
| 62 | return self._name.size () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 64 | def __add__ (self, other): |
| 65 | return self._name.append (other) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 66 | |
| 67 | def __getitem__(self, key): |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 68 | if isinstance (key, int): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 69 | if abs(key) < self._name.size (): |
| 70 | return self._name.get (key) |
| 71 | else: |
| 72 | raise IndexError ("index out of range") |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 73 | elif isinstance (key, slice): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 74 | name = ns.ndnSIM.ndn.Name () |
| 75 | for component in xrange (*key.indices (self.size ())): |
| 76 | name.append (self._name.get (component)) |
| 77 | return name |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 78 | else: |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 79 | raise ValueError("Unknown __getitem__ type: %s" % type (key)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame] | 81 | def __repr__ (self): |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 82 | return "ndn.Name('" + self._name.toUri () + "')" |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 84 | def __str__ (self): |
| 85 | return self._name.toUri () |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 7ff81b7 | 2013-08-08 21:43:16 -0700 | [diff] [blame] | 87 | def __eq__ (self, other): |
| 88 | return self._name == other._name |
| 89 | |
Alexander Afanasyev | 40bc46d | 2013-08-08 01:27:12 -0700 | [diff] [blame] | 90 | def isPrefixOf (self, other): |
| 91 | return self[:] == other[:len(self)] |
Alexander Afanasyev | 36f1053 | 2013-08-08 11:25:14 -0700 | [diff] [blame] | 92 | |
| 93 | @property |
| 94 | def keyName (self): |
| 95 | return self |