Package madrona :: Package common :: Package uaparser :: Module clientos
[hide private]

Source Code for Module madrona.common.uaparser.clientos

 1  """ 
 2  Based on http://vaig.be/2009/03/getting-client-os-in-django.html 
 3  """ 
 4   
 5  import re 
 6   
7 -def client_os(user_agent):
8 ''' 9 Context processor for Django that provides operating system 10 information base on HTTP user agent. 11 A user agent looks like (line break added): 12 "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) \ 13 Gecko/2009020409 Iceweasel/3.0.6 (Debian-3.0.6-1)" 14 ''' 15 # Mozilla/5.0 16 regex = '(?P<application_name>\w+)/(?P<application_version>[\d\.]+)' 17 regex += ' \(' 18 # X11 19 regex += '(?P<compatibility_flag>\w+)' 20 regex += '; ' 21 # U 22 if "U;" in user_agent or "MSIE" in user_agent: # some UA strings leave out the U; 23 regex += '(?P<version_token>[\w .]+)' 24 regex += '; ' 25 # Linux i686 26 regex += '(?P<platform_token>[\w ._]+)' 27 # anything else 28 regex += '; .*' 29 30 result = re.match(regex, user_agent) 31 if result: 32 result_dict = result.groupdict() 33 full_platform = result_dict['platform_token'] 34 platform_values = full_platform.split(' ') 35 if platform_values[0] in ('Windows', 'Linux', 'Mac', 'Ubuntu'): 36 platform = platform_values[0] 37 elif platform_values[1] in ('Mac',): 38 # Mac is given as "PPC Mac" or "Intel Mac" 39 platform = platform_values[1] 40 else: 41 platform = None 42 else: 43 # Total hack to avoid dealing with regex nightmares 44 if 'mac' in user_agent.lower(): 45 full_platform = "Intel Mac 10.6" 46 platform = 'Mac' 47 elif 'windows' in user_agent.lower(): 48 full_platform = "Windows" 49 platform = 'Windows' 50 else: 51 full_platform = None 52 platform = None 53 54 return { 55 'full_platform': full_platform, 56 'platform': platform, 57 }
58