robust user agent parser, thanks Angelo

This commit is contained in:
Massimo Di Pierro
2011-12-05 08:21:14 -06:00
parent b71077608a
commit e49229463a
4 changed files with 27 additions and 25 deletions
+1 -1
View File
@@ -1 +1 @@
Version 1.99.3 (2011-12-04 19:35:53) dev Version 1.99.3 (2011-12-05 08:20:58) dev
@@ -67,7 +67,6 @@ body {
.footer { .footer {
background-color: #ECECEC; background-color: #ECECEC;
} }
.header { .header {
// background: <fill here for header image>; // background: <fill here for header image>;
} }
+25 -22
View File
@@ -98,13 +98,13 @@ class DetectorBase(object):
return False return False
return True return True
# It get the version correctly only if the words list # This works only for the first element of look_for
# contains only one element, alternatively, you have to # If you want a different behaviour, you have to
# override this method # override this method
def getVersion(self, agent): def getVersion(self, agent):
# -> version string /None # -> version string /None
vs = self.version_splitters vs = self.version_splitters
return agent.split(self.look_for[0] + vs[0])[1].split(vs[1])[0].strip() return agent.partition(self.look_for[0] + vs[0])[2].partition(vs[1])[0].strip()
class OS(DetectorBase): class OS(DetectorBase):
@@ -147,7 +147,7 @@ class Konqueror(Browser):
class Opera(Browser): class Opera(Browser):
look_for = ["Opera"] look_for = ["Opera"]
def getVersion(self, agent): def getVersion(self, agent):
return agent.split(self.look_for[0])[1][1:].split(' ')[0] return agent.partition(self.look_for[0])[2][1:].partition(' ')[0]
class Netscape(Browser): class Netscape(Browser):
look_for = ["Netscape"] look_for = ["Netscape"]
@@ -169,7 +169,7 @@ class Safari(Browser):
def getVersion(self, agent): def getVersion(self, agent):
if "Version/" in agent: if "Version/" in agent:
return agent.split('Version/')[1].split(' ')[0].strip() return agent.partition('Version/')[2].partition(' ')[0].strip()
class SafariTablet(Browser): class SafariTablet(Browser):
name = "Safari" name = "Safari"
@@ -180,7 +180,7 @@ class SafariTablet(Browser):
def getVersion(self, agent): def getVersion(self, agent):
if "Version/" in agent: if "Version/" in agent:
return agent.split('Version/')[1].split(' ')[0].strip() return agent.partition('Version/')[2].partition(' ')[0].strip()
class SafariMobile(Browser): class SafariMobile(Browser):
name = "Safari" name = "Safari"
@@ -189,7 +189,7 @@ class SafariMobile(Browser):
def getVersion(self, agent): def getVersion(self, agent):
if "Version/" in agent: if "Version/" in agent:
return agent.split('Version/')[1].split(' ')[0].strip() return agent.partition('Version/')[2].partition(' ')[0].strip()
class SafariNokia(Browser): class SafariNokia(Browser):
name = "Safari" name = "Safari"
@@ -208,7 +208,7 @@ class SafariiPad(Browser):
def getVersion(self, agent): def getVersion(self, agent):
if "Version/" in agent: if "Version/" in agent:
return agent.split('Version/')[1].split(' ')[0].strip() return agent.partition('Version/')[2].partition(' ')[0].strip()
class Linux(OS): class Linux(OS):
@@ -254,10 +254,10 @@ class MacOS(Flavor):
def getVersion(self, agent): def getVersion(self, agent):
version_end_chars = [';', ')'] version_end_chars = [';', ')']
part = agent.split('Mac OS')[-1].strip() part = agent.partition('Mac OS')[2].strip()
for c in version_end_chars: for c in version_end_chars:
if c in part: if c in part:
version = part.split(c)[0] version = part.partition(c)[0]
break break
return version.replace('_', '.') return version.replace('_', '.')
@@ -297,11 +297,11 @@ class Chrome(Browser):
class ChromeOS(OS): class ChromeOS(OS):
look_for = ['CrOS'] look_for = ['CrOS']
version_splitters = [" ", " "] version_splitters = [" ", ")"]
prefs = dict(browser=['Chrome']) prefs = dict(browser=['Chrome'])
def getVersion(self, agent): def getVersion(self, agent):
vs = self.version_splitters vs = self.version_splitters
return agent.split(self.look_for[0]+vs[0])[1].split(vs[1])[1].strip()[:-1] return agent.partition(self.look_for[0]+vs[0])[2].partition(vs[1])[0].partition(" ")[2].strip()
class Android(Dist): class Android(Dist):
look_for = ['Android'] look_for = ['Android']
@@ -309,7 +309,7 @@ class Android(Dist):
is_mobile = True is_mobile = True
def getVersion(self, agent): def getVersion(self, agent):
return agent.split('Android')[1].split(';')[0].strip() return agent.partition('Android')[2].partition(';')[0].strip()
class SymbianOS(OS): class SymbianOS(OS):
look_for = ['SymbianOS'] look_for = ['SymbianOS']
@@ -337,10 +337,10 @@ class iPhone(Dist):
version_end_chars = ['like', ';', ')'] version_end_chars = ['like', ';', ')']
if (not 'CPU iPhone OS' in agent) and (not 'CPU OS' in agent): if (not 'CPU iPhone OS' in agent) and (not 'CPU OS' in agent):
return 'X' return 'X'
part = agent.split('OS')[1].strip() part = agent.partition('OS')[2].strip()
for c in version_end_chars: for c in version_end_chars:
if c in part: if c in part:
version = 'iOS ' + part.split(c)[0].strip() version = 'iOS ' + part.partition(c)[0].strip()
break break
return version.replace('_', '.') return version.replace('_', '.')
@@ -353,10 +353,10 @@ class iPad(Dist):
version_end_chars = ['like', ';', ')'] version_end_chars = ['like', ';', ')']
if not 'OS' in agent: if not 'OS' in agent:
return '' return ''
part = agent.split('OS')[1].strip() part = agent.partition('OS')[2].strip()
for c in version_end_chars: for c in version_end_chars:
if c in part: if c in part:
version = 'iOS ' + part.split(c)[0].strip() version = 'iOS ' + part.partition(c)[0].strip()
break break
return version.replace('_', '.') return version.replace('_', '.')
@@ -364,17 +364,20 @@ detectorshub = DetectorsHub()
def detect(agent): def detect(agent):
result = dict() result = dict()
prefs = dict()
result['is_mobile'] = False result['is_mobile'] = False
result['is_tablet'] = False result['is_tablet'] = False
prefs = dict()
for info_type in detectorshub: for info_type in detectorshub:
detectors = detectorshub[info_type] detectors = detectorshub[info_type]
_d_prefs = prefs.get(info_type, []) _d_prefs = prefs.get(info_type, [])
detectors = detectorshub.reorderByPrefs(detectors, _d_prefs) detectors = detectorshub.reorderByPrefs(detectors, _d_prefs)
for detector in detectors: try:
if detector.detect(agent, result): for detector in detectors:
prefs = detector.prefs if detector.detect(agent, result):
break prefs = detector.prefs
break
except Exception as ex:
result['exception'] = ex
return result return result
+1 -1
View File
@@ -1152,7 +1152,7 @@ class BaseAdapter(ConnectionPool):
fields = new_fields fields = new_fields
tablenames = self.tables(query) tablenames = self.tables(query)
if not query.ignore_common_filters: if query and not query.ignore_common_filters:
query = self.common_filter(query,tablenames) query = self.common_filter(query,tablenames)
if not fields: if not fields: