pywurfl.algorithms.wurfl.utils

Package pywurfl :: Package algorithms :: Package wurfl :: Module utils |
[hide private]
[frames] | no frames]

# Source Code for Module pywurfl.algorithms.wurfl.utils

      1  # pywurfl - Wireless Universal Resource File Tools in Python
      2  # Copyright (C) 2006-2011 Armand Lynch
      3  #
      4  # This library is free software; you can redistribute it and/or modify it
      5  # under the terms of the GNU Lesser General Public License as published by the
      6  # Free Software Foundation; either version 2.1 of the License, or (at your
      7  # option) any later version.
      8  #
      9  # This library is distributed in the hope that it will be useful, but WITHOUT
     10  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     11  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     12  # details.
     13  #
     14  # You should have received a copy of the GNU Lesser General Public License
     15  # along with this library; if not, write to the Free Software Foundation, Inc.,
     16  # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     17  #
     18  # Armand Lynch <lyncha@users.sourceforge.net>
     19
     20  __doc__ = """
     21  This module contains the supporting classes for the Two Step Analysis user agent
     22  algorithm that is used as the primary way to match user agents with the Java API
     23  for the WURFL.
     24
     25  A description of the way the following source is intended to work can be found
     26  within the source for the original Java API implementation here:
     27  http://sourceforge.net/projects/wurfl/files/WURFL Java API/
     28
     29  The original Java code is GPLd and Copyright (c) WURFL-Pro srl
     30  """
     31
     32  __author__ = "Armand Lynch <lyncha@users.sourceforge.net>"
     33  __copyright__ = "Copyright 2011, Armand Lynch"
     34  __license__ = "LGPL"
     35  __url__ = "http://celljam.net/"
     36  __version__ = "1.2.1"
     37
     38  from functools import partial
     39
     40  mobile_browsers = [u"cldc", u"symbian", u"midp", u"j2me", u"mobile",
     41                     u"wireless", u"palm", u"phone", u"pocket pc",
     42                     u"pocketpc", u"netfront", u"bolt", u"iris", u"brew",
     43                     u"openwave", u"windows ce", u"wap2", u"android",
     44                     u"opera mini", u"opera mobi", u"maemo", u"fennec",
     45                     u"blazer", u"160x160", u"tablet", u"webos", u"sony",
     46                     u"nintendo", u"480x640", u"aspen simulator",
     47                     u"up.browser", u"up.link", u"embider", u"danger hiptop",
     48                     u"obigo", u"foma"]
     49
     50  desktop_browsers = [u"slcc1", u".net clr", u"trident/4", u"media center pc",
     51                      u"funwebproducts", u"macintosh", u"wow64", u"aol 9.",
     52                      u"america online browser", u"googletoolbar"]
     53
     54
     55 -def is_typeof_browser(user_agent, browsers=None):
     56      if browsers is None:
     57          return False
     58
     59      user_agent = user_agent.lower()
     60
     61      for b in browsers:
     62          if b in user_agent:
     63              return True
     64      return False
     65
     66
     67  is_mobile_browser = partial(is_typeof_browser, browsers=mobile_browsers)
     68  is_desktop_browser = partial(is_typeof_browser, browsers=desktop_browsers)
     69
     70
     71 -def ordinal_index(target, needle=u" ", ordinal=1, start_index=0):
     72      index = -1
     73
     74      working_target = target[start_index+1:]
     75
     76      if needle in working_target:
     77          i = 0
     78          for i, x in enumerate(working_target.split(needle)):
     79              if ordinal < 1:
     80                  break
     81              index += len(x)
     82              ordinal -= 1
     83          index += (i * len(needle)) + start_index + 1
     84          index = index - (len(needle) - 1)
     85      if ordinal != 0:
     86          index = -1
     87      return index
     88
     89
     90 -def find_or_length(func, user_agent):
     91      value = func(user_agent)
     92      if value == -1:
     93          value = len(user_agent)
     94      return value
     95
     96
     97 -def indexof_or_length(target, needle=u" ", position=1, start_index=0):
     98      value = ordinal_index(target, needle, position, start_index)
     99      if value == -1:
    100          value = len(target)
    101      return value
    102
    103
    104  first_space = indexof_or_length
    105  first_slash = partial(indexof_or_length, needle=u"/")
    106  second_slash = partial(indexof_or_length, needle=u"/", position=2)
    107  first_semi_colon = partial(indexof_or_length, needle=u";")
    108  third_space = partial(indexof_or_length, position=3)
    109