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 | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 22 | class Name (): |
| 23 | _name = None |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 25 | def __init__ (self, value = None): |
| 26 | if value: |
| 27 | self._name = ns.ndnSIM.ndn.Name (value) |
| 28 | else: |
| 29 | self._name = ns.ndnSIM.ndn.Name () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 31 | @staticmethod |
| 32 | def fromWire (wire): |
| 33 | return ns.ndnSIM.ndn.Wire.ToName (wire) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 35 | @staticmethod |
| 36 | def toWire (name): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 37 | return ns.ndnSIM.ndn.Wire.FromName (name) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 39 | def __getattr__ (self, name): |
| 40 | return self._name.__getattribute__ (name) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 42 | def __len__ (self): |
| 43 | return self._name.size () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 45 | def __add__ (self, other): |
| 46 | return self._name.append (other) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 47 | |
| 48 | def __getitem__(self, key): |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 49 | if type (key) is int: |
| 50 | if abs(key) < self._name.size (): |
| 51 | return self._name.get (key) |
| 52 | else: |
| 53 | raise IndexError ("index out of range") |
| 54 | elif type (key) is slice: |
| 55 | name = ns.ndnSIM.ndn.Name () |
| 56 | for component in xrange (*key.indices (self.size ())): |
| 57 | name.append (self._name.get (component)) |
| 58 | return name |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 59 | else: |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 60 | raise ValueError("Unknown __getitem__ type: %s" % type (key)) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 7d0a61d | 2013-07-17 17:33:05 -0700 | [diff] [blame^] | 62 | def __repr__ (self): |
| 63 | return "ndnSIM.Name('" + self._name.toUri () + "')" |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 64 | |