@@ -1244,6 +1244,134 @@ def prettyOut(self, value):
12441244 return '.' .join ([str (x ) for x in value ])
12451245
12461246
1247+ class RelativeOID (base .SimpleAsn1Type ):
1248+ """Create |ASN.1| schema or value object.
1249+ |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
1250+ objects are immutable and duck-type Python :class:`tuple` objects
1251+ (tuple of non-negative integers).
1252+ Keyword Args
1253+ ------------
1254+ value: :class:`tuple`, :class:`str` or |ASN.1| object
1255+ Python sequence of :class:`int` or :class:`str` literal or |ASN.1| object.
1256+ If `value` is not given, schema object will be created.
1257+ tagSet: :py:class:`~pyasn1.type.tag.TagSet`
1258+ Object representing non-default ASN.1 tag(s)
1259+ subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
1260+ Object representing non-default ASN.1 subtype constraint(s). Constraints
1261+ verification for |ASN.1| type occurs automatically on object
1262+ instantiation.
1263+ Raises
1264+ ------
1265+ ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
1266+ On constraint violation or bad initializer.
1267+ Examples
1268+ --------
1269+ .. code-block:: python
1270+ class RelOID(RelativeOID):
1271+ '''
1272+ ASN.1 specification:
1273+ id-pad-null RELATIVE-OID ::= { 0 }
1274+ id-pad-once RELATIVE-OID ::= { 5 6 }
1275+ id-pad-twice RELATIVE-OID ::= { 5 6 7 }
1276+ '''
1277+ id_pad_null = RelOID('0')
1278+ id_pad_once = RelOID('5.6')
1279+ id_pad_twice = id_pad_once + (7,)
1280+ """
1281+ #: Set (on class, not on instance) or return a
1282+ #: :py:class:`~pyasn1.type.tag.TagSet` object representing ASN.1 tag(s)
1283+ #: associated with |ASN.1| type.
1284+ tagSet = tag .initTagSet (
1285+ tag .Tag (tag .tagClassUniversal , tag .tagFormatSimple , 0x0d )
1286+ )
1287+
1288+ #: Set (on class, not on instance) or return a
1289+ #: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection` object
1290+ #: imposing constraints on |ASN.1| type initialization values.
1291+ subtypeSpec = constraint .ConstraintsIntersection ()
1292+
1293+ # Optimization for faster codec lookup
1294+ typeId = base .SimpleAsn1Type .getTypeId ()
1295+
1296+ def __add__ (self , other ):
1297+ return self .clone (self ._value + other )
1298+
1299+ def __radd__ (self , other ):
1300+ return self .clone (other + self ._value )
1301+
1302+ def asTuple (self ):
1303+ return self ._value
1304+
1305+ # Sequence object protocol
1306+
1307+ def __len__ (self ):
1308+ return len (self ._value )
1309+
1310+ def __getitem__ (self , i ):
1311+ if i .__class__ is slice :
1312+ return self .clone (self ._value [i ])
1313+ else :
1314+ return self ._value [i ]
1315+
1316+ def __iter__ (self ):
1317+ return iter (self ._value )
1318+
1319+ def __contains__ (self , value ):
1320+ return value in self ._value
1321+
1322+ def index (self , suboid ):
1323+ return self ._value .index (suboid )
1324+
1325+ def isPrefixOf (self , other ):
1326+ """Indicate if this |ASN.1| object is a prefix of other |ASN.1| object.
1327+ Parameters
1328+ ----------
1329+ other: |ASN.1| object
1330+ |ASN.1| object
1331+ Returns
1332+ -------
1333+ : :class:`bool`
1334+ :obj:`True` if this |ASN.1| object is a parent (e.g. prefix) of the other |ASN.1| object
1335+ or :obj:`False` otherwise.
1336+ """
1337+ l = len (self )
1338+ if l <= len (other ):
1339+ if self ._value [:l ] == other [:l ]:
1340+ return True
1341+ return False
1342+
1343+ def prettyIn (self , value ):
1344+ if isinstance (value , RelativeOID ):
1345+ return tuple (value )
1346+ elif octets .isStringType (value ):
1347+ if '-' in value :
1348+ raise error .PyAsn1Error (
1349+ 'Malformed RELATIVE-OID %s at %s: %s' % (value , self .__class__ .__name__ , sys .exc_info ()[1 ])
1350+ )
1351+ try :
1352+ return tuple ([int (subOid ) for subOid in value .split ('.' ) if subOid ])
1353+ except ValueError :
1354+ raise error .PyAsn1Error (
1355+ 'Malformed RELATIVE-OID %s at %s: %s' % (value , self .__class__ .__name__ , sys .exc_info ()[1 ])
1356+ )
1357+
1358+ try :
1359+ tupleOfInts = tuple ([int (subOid ) for subOid in value if subOid >= 0 ])
1360+
1361+ except (ValueError , TypeError ):
1362+ raise error .PyAsn1Error (
1363+ 'Malformed RELATIVE-OID %s at %s: %s' % (value , self .__class__ .__name__ , sys .exc_info ()[1 ])
1364+ )
1365+
1366+ if len (tupleOfInts ) == len (value ):
1367+ return tupleOfInts
1368+
1369+ raise error .PyAsn1Error ('Malformed RELATIVE-OID %s at %s' % (value , self .__class__ .__name__ ))
1370+
1371+ def prettyOut (self , value ):
1372+ return '.' .join ([str (x ) for x in value ])
1373+
1374+
12471375class Real (base .SimpleAsn1Type ):
12481376 """Create |ASN.1| schema or value object.
12491377
0 commit comments