blob: 60ac68df6818d10ef25601773f864bd0ff73ede0 [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- 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 Afanasyev76b11572013-07-16 21:49:50 -070020import ns.ndnSIM
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070021
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070022class Name (object):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070023 _name = None
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070024
Alexander Afanasyev39f53162013-07-18 16:24:31 -070025 def __init__ (self,
26 value = None,
27 name = None):
28 if name:
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070029 if isinstance (name, ns.ndnSIM.ndn.Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070030 self._name = name
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070031 elif isinstance (name, Name):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070032 self._name = name._name
33 else:
34 raise TypeError ("Incorrect type for 'name' parameter [%s]" % type (name))
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070035 else:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070036 if value:
37 self._name = ns.ndnSIM.ndn.Name (value)
38 else:
39 self._name = ns.ndnSIM.ndn.Name ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070040
Alexander Afanasyev76b11572013-07-16 21:49:50 -070041 @staticmethod
42 def fromWire (wire):
Alexander Afanasyev7992ab62013-07-19 16:50:58 -070043 return Name (name = ns.ndnSIM.ndn.Wire.ToName (wire))
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070044
Alexander Afanasyev7992ab62013-07-19 16:50:58 -070045 def toWire (self):
46 return ns.ndnSIM.ndn.Wire.FromName (self._name)
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070047
48 def append (self, value):
49 self._name.append (value)
50 return self
Alexander Afanasyev76b11572013-07-16 21:49:50 -070051
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070052 def __getattr__ (self, name):
53 return self._name.__getattribute__ (name)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070054
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070055 def __len__ (self):
56 return self._name.size ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070057
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070058 def __add__ (self, other):
59 return self._name.append (other)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070060
61 def __getitem__(self, key):
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070062 if isinstance (key, int):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070063 if abs(key) < self._name.size ():
64 return self._name.get (key)
65 else:
66 raise IndexError ("index out of range")
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070067 elif isinstance (key, slice):
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070068 name = ns.ndnSIM.ndn.Name ()
69 for component in xrange (*key.indices (self.size ())):
70 name.append (self._name.get (component))
71 return name
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070072 else:
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070073 raise ValueError("Unknown __getitem__ type: %s" % type (key))
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070074
Alexander Afanasyev7d0a61d2013-07-17 17:33:05 -070075 def __repr__ (self):
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070076 return "ndn.Name('" + self._name.toUri () + "')"
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070077
Alexander Afanasyevfce5bbd2013-08-07 18:50:00 -070078 def __str__ (self):
79 return self._name.toUri ()
Alexander Afanasyev40bc46d2013-08-08 01:27:12 -070080
81 def isPrefixOf (self, other):
82 return self[:] == other[:len(self)]