[Catalog-sig] PKG-INFO exports?

A.M. Kuchling amk@amk.ca
Wed, 16 Apr 2003 09:18:23 -0400

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Wed, Apr 16, 2003 at 05:48:23PM -0700, Kevin Turner wrote:
>but could also be set to PKG-INFO.  With that, you could later offer
>extensions for yaml or xml/rdf or what have you, without a proliferation
>of different action types.

HTTP already has a header for this that gets passed as the
ACCEPT_TYPES HTTP header.  So text/plain could get PKG-INFO, text/html
would get the existing HTML, application/rdf+xml could get some future
hypothetical RDF encoding, and so forth.

Here are two functions from Quixote that do the hard part.

# These are needed by 'get_encoding()', to parse the "Accept-Encoding"
# header.  LWS is linear whitespace; the latter two assume that LWS
# has been removed.
_http_lws_re = re.compile("(\r\n)?[ \t]+")
_http_list_re = re.compile(r",+")
_http_encoding_re = re.compile(r"([^;]+)(;q=([\d.]+))?$")

    def get_accepted_types (self):
        """get_accepted_types() : {string:float}
        Return a dictionary mapping MIME types the client will accept
        to the corresponding quality value (1.0 if no value was specified).
        accept_types = self.environ.get('HTTP_ACCEPT', "")
        return self._parse_pref_header(accept_types)

    def _parse_pref_header (self, S):
        """_parse_pref_header(S:string) : {string:float}
        Parse a list of HTTP preferences (content types, encodings) and
        return a dictionary mapping strings to the quality value.

        found = {}
        # remove all linear whitespace
        S = _http_lws_re.sub("", S)
        for coding in _http_list_re.split(S):
            m = _http_encoding_re.match(coding)
            if m:
                encoding = m.group(1).lower()
                q = m.group(3) or 1.0
                    q = float(q)
                except ValueError:
                if encoding == "*":
                    continue # stupid, ignore it
                if q > 0:
                    found[encoding] = q
        return found

--amk                                                             (www.amk.ca)
Homological algebra beckons -- brain relief in this context!
      -- Michael Hudson, 07 Nov 2001, in a discussion of Stackless Python

Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.2.1 (GNU/Linux)