Пример #1
0
 def __add__(self, other, reversed=False):
     if isinstance(other, Accept):
         other = other.header_value
     if hasattr(other, 'items'):
         other = sorted(other.items(), key=lambda item: -item[1])
     if isinstance(other, (list, tuple)):
         result = []
         for item in other:
             if isinstance(item, (list, tuple)):
                 name, quality = item
                 result.append('%s; q=%s' % (name, quality))
             else:
                 result.append(item)
         other = ', '.join(result)
     other = str(other)
     my_value = self.header_value
     if reversed:
         other, my_value = my_value, other
     if not other:
         new_value = my_value
     elif not my_value:
         new_value = other
     else:
         new_value = my_value + ', ' + other
     return self.__class__(self.header_name, new_value)
Пример #2
0
 def __add__(self, other, reversed=False):
     if isinstance(other, Accept):
         other = other.header_value
     if hasattr(other, 'items'):
         other = sorted(other.items(), key=lambda item: -item[1])
     if isinstance(other, (list, tuple)):
         result = []
         for item in other:
             if isinstance(item, (list, tuple)):
                 name, quality = item
                 result.append('%s; q=%s' % (name, quality))
             else:
                 result.append(item)
         other = ', '.join(result)
     other = str(other)
     my_value = self.header_value
     if reversed:
         other, my_value = my_value, other
     if not other:
         new_value = my_value
     elif not my_value:
         new_value = other
     else:
         new_value = my_value + ', ' + other
     return self.__class__(self.header_name, new_value)
Пример #3
0
def serialize_cache_control(properties):
    if isinstance(properties, CacheControl):
        properties = properties.properties
    parts = []
    for name, value in sorted(properties.items()):
        if value is None:
            parts.append(name)
            continue
        value = str(value)
        if need_quote_re.search(value):
            value = '"%s"' % value
        parts.append('%s=%s' % (name, value))
    return ', '.join(parts)
Пример #4
0
def serialize_cache_control(properties):
    if isinstance(properties, CacheControl):
        properties = properties.properties
    parts = []
    for name, value in sorted(properties.items()):
        if value is None:
            parts.append(name)
            continue
        value = str(value)
        if need_quote_re.search(value):
            value = '"%s"' % value
        parts.append('%s=%s' % (name, value))
    return ', '.join(parts)
Пример #5
0
 def best_matches(self, fallback=None):
     """
     Return all the matches in order of quality, with fallback (if
     given) at the end.
     """
     items = [i for i, q in sorted(self._parsed, key=lambda iq: -iq[1])]
     if fallback:
         for index, item in enumerate(items):
             if self._match(item, fallback):
                 items[index + 1:] = []
                 break
         else:
             items.append(fallback)
     return items
Пример #6
0
 def _content_type_params__set(self, value_dict):
     if not value_dict:
         del self.content_type_params
         return
     params = []
     for k, v in sorted(value_dict.items()):
         if not _OK_PARAM_RE.search(v):
             ## FIXME: I'm not sure what to do with "'s in the parameter value
             ## I think it might be simply illegal
             v = '"%s"' % v.replace('"', '\\"')
         params.append('; %s=%s' % (k, v))
     ct = self.headers.pop('Content-Type', '').split(';', 1)[0]
     ct += ''.join(params)
     self.headers['Content-Type'] = ct
Пример #7
0
 def _content_type_params__set(self, value_dict):
     if not value_dict:
         del self.content_type_params
         return
     params = []
     for k, v in sorted(value_dict.items()):
         if not _OK_PARAM_RE.search(v):
             ## FIXME: I'm not sure what to do with "'s in the parameter value
             ## I think it might be simply illegal
             v = '"%s"' % v.replace('"', '\\"')
         params.append('; %s=%s' % (k, v))
     ct = self.headers.pop('Content-Type', '').split(';', 1)[0]
     ct += ''.join(params)
     self.headers['Content-Type'] = ct
Пример #8
0
 def best_matches(self, fallback=None):
     """
     Return all the matches in order of quality, with fallback (if
     given) at the end.
     """
     items = [
         i for i, q in sorted(self._parsed, key=lambda iq: -iq[1])]
     if fallback:
         for index, item in enumerate(items):
             if self._match(item, fallback):
                 items[index+1:] = []
                 break
         else:
             items.append(fallback)
     return items