def test_iteration(self): nsm = NamespaceMap() uripat = 'http://example.com/foo%r' nsm.add(uripat % 0) for n in range(1, 23): self.assertIn(uripat % (n - 1), nsm) self.assertTrue(nsm.isDefined(uripat % (n - 1))) nsm.add(uripat % n) for (uri, alias) in nsm.iteritems(): self.assertEqual(uri[22:], alias[3:]) i = 0 it = nsm.iterAliases() try: while True: it.next() i += 1 except StopIteration: self.assertEqual(i, 23) i = 0 it = nsm.iterNamespaceURIs() try: while True: it.next() i += 1 except StopIteration: self.assertEqual(i, 23)
def parseExtensionArgs(self, ax_args): """Parse attribute exchange key/value arguments into this object. @param ax_args: The attribute exchange fetch_response arguments, with namespacing removed. @type ax_args: {unicode:unicode} @returns: None @raises ValueError: If the message has bad values for particular fields @raises KeyError: If the namespace mapping is bad or required arguments are missing """ self._checkMode(ax_args) aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): type_uri = value alias = key[5:] checkAlias(alias) aliases.addAlias(type_uri, alias) for type_uri, alias in aliases.iteritems(): try: count_s = ax_args['count.' + alias] except KeyError: value = ax_args['value.' + alias] if value == u'': values = [] else: values = [value] else: count = int(count_s) values = [] for i in range(1, count + 1): value_key = 'value.%s.%d' % (alias, i) value = ax_args[value_key] values.append(value) self.data[type_uri] = values
def parseExtensionArgs(self, ax_args): """Parse attribute exchange key/value arguments into this object. @param ax_args: The attribute exchange fetch_response arguments, with namespacing removed. @type ax_args: {unicode:unicode} @returns: None @raises ValueError: If the message has bad values for particular fields @raises KeyError: If the namespace mapping is bad or required arguments are missing """ self._checkMode(ax_args) aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): type_uri = value alias = key[5:] checkAlias(alias) aliases.addAlias(type_uri, alias) for type_uri, alias in aliases.iteritems(): try: count_s = ax_args['count.' + alias] except KeyError: value = ax_args['value.' + alias] if value == u'': values = [] else: values = [value] else: count = int(count_s) values = [] for i in range(1, count + 1): value_key = 'value.%s.%d' % (alias, i) value = ax_args[value_key] values.append(value) self.data[type_uri] = values
def test_iteration(self): nsm = NamespaceMap() uripat = 'http://example.com/foo%r' nsm.add(uripat % 0) for n in range(1, 23): self.assertIn(uripat % (n - 1), nsm) self.assertTrue(nsm.isDefined(uripat % (n - 1))) nsm.add(uripat % n) for (uri, alias) in nsm.items(): self.assertEqual(uri[22:], alias[3:]) for (uri, alias) in nsm.iteritems(): self.assertEqual(uri[22:], alias[3:]) self.assertEqual(len(tuple(nsm.iterAliases())), 23) self.assertEqual(len(tuple(nsm.iterNamespaceURIs())), 23)
class AXKeyValueMessage(AXMessage): """An abstract class that implements a message that has attribute keys and values. It contains the common code between fetch_response and store_request. """ # This class is abstract, so it's OK that it doesn't override the # abstract method in Extension: # #pylint:disable-msg=W0223 def __init__(self): AXMessage.__init__(self) self.data = {} self.aliases = None def addValue(self, type_uri, value): """Add a single value for the given attribute type to the message. If there are already values specified for this type, this value will be sent in addition to the values already specified. @param type_uri: The URI for the attribute @param value: The value to add to the response to the relying party for this attribute @type value: unicode @returns: None """ try: values = self.data[type_uri] except KeyError: values = self.data[type_uri] = [] values.append(value) def setValues(self, type_uri, values): """Set the values for the given attribute type. This replaces any values that have already been set for this attribute. @param type_uri: The URI for the attribute @param values: A list of values to send for this attribute. @type values: [unicode] """ self.data[type_uri] = values def _getExtensionKVArgs(self, aliases=None): """Get the extension arguments for the key/value pairs contained in this message. @param aliases: An alias mapping. Set to None if you don't care about the aliases for this request. """ if aliases is None: aliases = NamespaceMap() ax_args = {} for type_uri, values in self.data.iteritems(): alias = aliases.add(type_uri) ax_args['type.' + alias] = type_uri ax_args['count.' + alias] = str(len(values)) for i, value in enumerate(values): key = 'value.%s.%d' % (alias, i + 1) ax_args[key] = value return ax_args def parseExtensionArgs(self, ax_args): """Parse attribute exchange key/value arguments into this object. @param ax_args: The attribute exchange fetch_response arguments, with namespacing removed. @type ax_args: {unicode:unicode} @returns: None @raises ValueError: If the message has bad values for particular fields @raises KeyError: If the namespace mapping is bad or required arguments are missing """ self._checkMode(ax_args) if not self.aliases: self.aliases = NamespaceMap() for key, value in ax_args.iteritems(): if key.startswith('type.'): type_uri = value alias = key[5:] checkAlias(alias) self.aliases.addAlias(type_uri, alias) for type_uri, alias in self.aliases.iteritems(): try: count_s = ax_args['count.' + alias] except KeyError: value = ax_args['value.' + alias] if value == u'': values = [] else: values = [value] else: count = int(count_s) values = [] for i in range(1, count + 1): value_key = 'value.%s.%d' % (alias, i) value = ax_args[value_key] values.append(value) self.data[type_uri] = values def getSingle(self, type_uri, default=None): """Get a single value for an attribute. If no value was sent for this attribute, use the supplied default. If there is more than one value for this attribute, this method will fail. @type type_uri: str @param type_uri: The URI for the attribute @param default: The value to return if the attribute was not sent in the fetch_response. @returns: The value of the attribute in the fetch_response message, or the default supplied @rtype: unicode or NoneType @raises ValueError: If there is more than one value for this parameter in the fetch_response message. @raises KeyError: If the attribute was not sent in this response """ values = self.data.get(type_uri) if not values: return default elif len(values) == 1: return values[0] else: raise AXError( 'More than one value present for %r' % (type_uri,)) def get(self, type_uri): """Get the list of values for this attribute in the fetch_response. XXX: what to do if the values are not present? default parameter? this is funny because it's always supposed to return a list, so the default may break that, though it's provided by the user's code, so it might be okay. If no default is supplied, should the return be None or []? @param type_uri: The URI of the attribute @returns: The list of values for this attribute in the response. May be an empty list. @rtype: [unicode] @raises KeyError: If the attribute was not sent in the response """ return self.data[type_uri] def count(self, type_uri): """Get the number of responses for a particular attribute in this fetch_response message. @param type_uri: The URI of the attribute @returns: The number of values sent for this attribute @raises KeyError: If the attribute was not sent in the response. KeyError will not be raised if the number of values was zero. """ return len(self.get(type_uri))