Logo Search packages:      
Sourcecode: viewvc version File versions  Download package

def accept::_LanguageSelector::select_from (   self,
  avail 
)

Select one of the available choices based on the request.

Note: if there isn't a match, then the first available choice is
considered the default. Also, if a number of matches are equally
relevant, then the first-requested will be used.

avail is a list of language-tag strings of available languages

Definition at line 133 of file accept.py.

00133                               :
    """Select one of the available choices based on the request.

    Note: if there isn't a match, then the first available choice is
    considered the default. Also, if a number of matches are equally
    relevant, then the first-requested will be used.

    avail is a list of language-tag strings of available languages
    """

    # tuples of (qvalue, language-tag)
    matches = [ ]

    # try matching all pairs of desired vs available, recording the
    # resulting qvalues. we also need to record the longest language-range
    # that matches since the most specific range "wins"
    for tag in avail:
      longest = 0
      final = 0.0

      # check this tag against the requests from the user
      for want in self.requested:
        qvalue = want.matches(tag)
        #print 'have %s. want %s. qvalue=%s' % (tag, want.name, qvalue)
        if qvalue is not None and len(want.name) > longest:
          # we have a match and it is longer than any we may have had.
          # the final qvalue should be from this tag.
          final = qvalue
          longest = len(want.name)

      # a non-zero qvalue is a potential match
      if final:
        matches.append((final, tag))

    # if there are no matches, then return the default language tag
    if not matches:
      return avail[0]

    # get the highest qvalue and its corresponding tag
    matches.sort()
    qvalue, tag = matches[-1]

    # if the qvalue is zero, then we have no valid matches. return the
    # default language tag.
    if not qvalue:
      return avail[0]

    # if there are two or more matches, and the second-highest has a
    # qvalue equal to the best, then we have multiple "best" options.
    # select the one that occurs first in self.requested
    if len(matches) >= 2 and matches[-2][0] == qvalue:
      # remove non-best matches
      while matches[0][0] != qvalue:
        del matches[0]
      #print "non-deterministic choice", matches

      # sequence through self.requested, in order
      for want in self.requested:
        # try to find this one in our best matches
        for qvalue, tag in matches:
          if want.matches(tag):
            # this requested item is one of the "best" options
            ### note: this request item could match *other* "best" options,
            ### so returning *this* one is rather non-deterministic.
            ### theoretically, we could go further here, and do another
            ### search based on the ordering in 'avail'. however, note
            ### that this generally means that we are picking from multiple
            ### *SUB* languages, so I'm all right with the non-determinism
            ### at this point. stupid client should send a qvalue if they
            ### want to refine.
            return tag

      # NOTREACHED

    # return the best match
    return tag

  def append(self, item):


Generated by  Doxygen 1.6.0   Back to index