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 _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 if isinstance(values, list): ax_args['count.' + alias] = str(len(values)) for i, value in enumerate(values): key = 'value.%s.%d' % (alias, i + 1) ax_args[key] = value else: ax_args['value.%s' % alias] = values return ax_args
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)
def getExtensionArgs(self): """Get the serialized form of this attribute fetch request. @returns: The fetch request message parameters @rtype: {unicode:unicode} """ aliases = NamespaceMap() required = [] if_available = [] ax_args = self._newArgs() for type_uri, attribute in self.requested_attributes.iteritems(): if attribute.alias is None: alias = aliases.add(type_uri) else: # This will raise an exception when the second # attribute with the same alias is added. I think it # would be better to complain at the time that the # attribute is added to this object so that the code # that is adding it is identified in the stack trace, # but it's more work to do so, and it won't be 100% # accurate anyway, since the attributes are # mutable. So for now, just live with the fact that # we'll learn about the error later. # # The other possible approach is to hide the error and # generate a new alias on the fly. I think that would # probably be bad. alias = aliases.addAlias(type_uri, attribute.alias) if attribute.required: required.append(alias) else: if_available.append(alias) if attribute.count != 1: ax_args['count.' + alias] = str(attribute.count) ax_args['type.' + alias] = type_uri if required: ax_args['required'] = ','.join(required) if if_available: ax_args['if_available'] = ','.join(if_available) return ax_args
def getExtensionArgs(self): """Get the serialized form of this attribute fetch request. @returns: The fetch request message parameters @rtype: Dict[six.text_type, six.text_type] """ aliases = NamespaceMap() required = [] if_available = [] ax_args = self._newArgs() for type_uri, attribute in six.iteritems(self.requested_attributes): if attribute.alias is None: alias = aliases.add(type_uri) else: # This will raise an exception when the second # attribute with the same alias is added. I think it # would be better to complain at the time that the # attribute is added to this object so that the code # that is adding it is identified in the stack trace, # but it's more work to do so, and it won't be 100% # accurate anyway, since the attributes are # mutable. So for now, just live with the fact that # we'll learn about the error later. # # The other possible approach is to hide the error and # generate a new alias on the fly. I think that would # probably be bad. alias = aliases.addAlias(type_uri, attribute.alias) if attribute.required: required.append(alias) else: if_available.append(alias) if attribute.count != 1: ax_args['count.' + alias] = six.text_type(attribute.count) ax_args['type.' + alias] = type_uri if required: ax_args['required'] = ','.join(required) if if_available: ax_args['if_available'] = ','.join(if_available) return ax_args
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 six.iteritems(self.data): alias = aliases.add(type_uri) ax_args['type.' + alias] = type_uri ax_args['count.' + alias] = six.text_type(len(values)) for i, value in enumerate(values): key = 'value.%s.%d' % (alias, i + 1) ax_args[key] = value return ax_args
def getExtensionArgs(self): """Serialize this object into arguments in the attribute exchange namespace @returns: The dictionary of unqualified attribute exchange arguments that represent this fetch_response. @rtype: {unicode;unicode} """ aliases = NamespaceMap() zero_value_types = [] if self.request is not None: # Validate the data in the context of the request (the # same attributes should be present in each, and the # counts in the response must be no more than the counts # in the request) for type_uri in self.data: if type_uri not in self.request: raise KeyError( 'Response attribute not present in request: %r' % (type_uri,)) for attr_info in self.request.iterAttrs(): # Copy the aliases from the request so that reading # the response in light of the request is easier if attr_info.alias is None: aliases.add(attr_info.type_uri) else: aliases.addAlias(attr_info.type_uri, attr_info.alias) try: values = self.data[attr_info.type_uri] except KeyError: values = [] zero_value_types.append(attr_info) if (attr_info.count != UNLIMITED_VALUES) and \ (attr_info.count < len(values)): raise AXError( 'More than the number of requested values were ' 'specified for %r' % (attr_info.type_uri,)) kv_args = self._getExtensionKVArgs(aliases) # Add the KV args into the response with the args that are # unique to the fetch_response ax_args = self._newArgs() # For each requested attribute, put its type/alias and count # into the response even if no data were returned. for attr_info in zero_value_types: alias = aliases.getAlias(attr_info.type_uri) kv_args['type.' + alias] = attr_info.type_uri kv_args['count.' + alias] = '0' update_url = ((self.request and self.request.update_url) or self.update_url) if update_url: ax_args['update_url'] = update_url ax_args.update(kv_args) return ax_args
def getExtensionArgs(self): """Serialize this object into arguments in the attribute exchange namespace @returns: The dictionary of unqualified attribute exchange arguments that represent this fetch_response. @rtype: Dict[six.text_type, six.text_type] """ aliases = NamespaceMap() zero_value_types = [] if self.request is not None: # Validate the data in the context of the request (the # same attributes should be present in each, and the # counts in the response must be no more than the counts # in the request) for type_uri in self.data: if type_uri not in self.request: raise KeyError( 'Response attribute not present in request: %r' % (type_uri,)) for attr_info in self.request.iterAttrs(): # Copy the aliases from the request so that reading # the response in light of the request is easier if attr_info.alias is None: aliases.add(attr_info.type_uri) else: aliases.addAlias(attr_info.type_uri, attr_info.alias) try: values = self.data[attr_info.type_uri] except KeyError: values = [] zero_value_types.append(attr_info) if (attr_info.count != UNLIMITED_VALUES) and (attr_info.count < len(values)): raise AXError( 'More than the number of requested values were ' 'specified for %r' % (attr_info.type_uri,)) kv_args = self._getExtensionKVArgs(aliases) # Add the KV args into the response with the args that are # unique to the fetch_response ax_args = self._newArgs() # For each requested attribute, put its type/alias and count # into the response even if no data were returned. for attr_info in zero_value_types: alias = aliases.getAlias(attr_info.type_uri) kv_args['type.' + alias] = attr_info.type_uri kv_args['count.' + alias] = '0' update_url = ((self.request and self.request.update_url) or self.update_url) if update_url: ax_args['update_url'] = update_url ax_args.update(kv_args) return ax_args