1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 2010, Armand Lynch"
34 __license__ = "LGPL"
35 __url__ = "http://celljam.net/"
36 __version__ = "1.0.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
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
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
91 value = func(user_agent)
92 if value == -1:
93 value = len(user_agent)
94 return value
95
96
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