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-2010 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) 2008-2009 WURFL-Pro srl
    30  """
    31
    32  __author__ = "Armand Lynch <lyncha@users.sourceforge.net>"
    33  __copyright__ = "Copyright 2010, Armand Lynch"
    34  __license__ = "LGPL"
    35  __url__ = "http://celljam.net/"
    36  __version__ = "1.0.1"
    37
    38  from functools import partial
    39
    40
    41 -def is_mobile_browser(user_agent):
    42      user_agent = user_agent.lower()
    43      mobile_browsers = [u"tablet", u"mobile", u"wireless", u"palm", u"blazer",
    44                         u"netfront", u"symbian", u"bolt", u"iris", u"pocketpc"]
    45      for mb in mobile_browsers:
    46          if mb in user_agent:
    47              return True
    48      return False
    49
    50
    51 -def ordinal_index(target, needle=u" ", ordinal=1, start_index=0):
    52      index = -1
    53
    54      working_target = target[start_index+1:]
    55
    56      if needle in working_target:
    57          i = 0
    58          for i, x in enumerate(working_target.split(needle)):
    59              if ordinal < 1:
    60                  break
    61              index += len(x)
    62              ordinal -= 1
    63          index += (i * len(needle)) + start_index + 1
    64          index = index - (len(needle) - 1)
    65      if ordinal != 0:
    66          index = -1
    67      return index
    68
    69
    70 -def find_or_length(func, user_agent):
    71      value = func(user_agent)
    72      if value == -1:
    73          value = len(user_agent)
    74      return value
    75
    76
    77 -def indexof_or_length(target, needle=u" ", position=1, start_index=0):
    78      value = ordinal_index(target, needle, position, start_index)
    79      if value == -1:
    80          value = len(target)
    81      return value
    82
    83
    84  first_space = indexof_or_length
    85  first_slash = partial(indexof_or_length, needle=u"/")
    86  second_slash = partial(indexof_or_length, needle=u"/", position=2)
    87  first_semi_colon = partial(indexof_or_length, needle=u";")
    88