Ejemplo n.º 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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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:] = [fallback]
                 break
         else:
             items.append(fallback)
     return items
Ejemplo n.º 4
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