useragents.py (3003B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\werkzeug\useragents.py 2 import re 3 4 class UserAgentParser(object): 5 platforms = (('iphone', 'iphone'), 6 ('darwin|mac|os\\s*x', 'macos'), 7 ('win', 'windows'), 8 ('android', 'android'), 9 ('x11|lin(\\b|ux)?', 'linux'), 10 ('(sun|i86)os', 'solaris'), 11 ('nintendo\\s+wii', 'wii'), 12 ('irix', 'irix'), 13 ('hp-?ux', 'hpux'), 14 ('aix', 'aix'), 15 ('sco|unix_sv', 'sco'), 16 ('bsd', 'bsd'), 17 ('amiga', 'amiga')) 18 browsers = (('googlebot', 'google'), 19 ('msnbot', 'msn'), 20 ('yahoo', 'yahoo'), 21 ('ask jeeves', 'ask'), 22 ('aol|america\\s+online\\s+browser', 'aol'), 23 ('opera', 'opera'), 24 ('chrome', 'chrome'), 25 ('firefox|firebird|phoenix|iceweasel', 'firefox'), 26 ('galeon', 'galeon'), 27 ('safari', 'safari'), 28 ('webkit', 'webkit'), 29 ('camino', 'camino'), 30 ('konqueror', 'konqueror'), 31 ('k-meleon', 'kmeleon'), 32 ('netscape', 'netscape'), 33 ('msie|microsoft\\s+internet\\s+explorer', 'msie'), 34 ('lynx', 'lynx'), 35 ('links', 'links'), 36 ('seamonkey|mozilla', 'seamonkey')) 37 _browser_version_re = '(?:%s)[/\\sa-z(]*(\\d+[.\\da-z]+)?(?i)' 38 _language_re = re.compile('(?:;\\s*|\\s+)(\\b\\w{2}\\b(?:-\\b\\w{2}\\b)?)\\s*;|(?:\\(|\\[|;)\\s*(\\b\\w{2}\\b(?:-\\b\\w{2}\\b)?)\\s*(?:\\]|\\)|;)') 39 40 def __init__(self): 41 self.platforms = [ (b, re.compile(a, re.I)) for a, b in self.platforms ] 42 self.browsers = [ (b, re.compile(self._browser_version_re % a)) for a, b in self.browsers ] 43 44 def __call__(self, user_agent): 45 for platform, regex in self.platforms: 46 match = regex.search(user_agent) 47 if match is not None: 48 break 49 else: 50 platform = None 51 52 for browser, regex in self.browsers: 53 match = regex.search(user_agent) 54 if match is not None: 55 version = match.group(1) 56 break 57 else: 58 browser = version = None 59 60 match = self._language_re.search(user_agent) 61 if match is not None: 62 language = match.group(1) or match.group(2) 63 else: 64 language = None 65 return (platform, 66 browser, 67 version, 68 language) 69 70 71 class UserAgent(object): 72 _parser = UserAgentParser() 73 74 def __init__(self, environ_or_string): 75 if isinstance(environ_or_string, dict): 76 environ_or_string = environ_or_string.get('HTTP_USER_AGENT', '') 77 self.string = environ_or_string 78 self.platform, self.browser, self.version, self.language = self._parser(environ_or_string) 79 80 def to_header(self): 81 return self.string 82 83 def __str__(self): 84 return self.string 85 86 def __nonzero__(self): 87 return bool(self.browser) 88 89 def __repr__(self): 90 return '<%s %r/%s>' % (self.__class__.__name__, self.browser, self.version) 91 92 93 from werkzeug.wrappers import UserAgentMixin