"""ua to os - from a user agent return operating system, architecture, and browser"""

import sys,splunk.Intersplunk
import re

os_mapping = (
    ('Windows .. 5.1',      'Windows XP'),
    ('Windows .. 5.2',      'Windows XP'),
    ('Windows NT 6.0',      'Windows Vista'),
    ('Windows 6.0',         'Windows Server 2008'),
    ('Windows NT 6.1',      'Windows 7'),
    ('OS X 10.5',           'MAC OS X 10.5.x'),
    ('OS X 10.6',           'MAC OS X 10.6.x'),
    ('OS X 10.4',           'MAC OS X 10.4.x'),
    ('OS X 10.3',           'MAC OS X 10.3.x'),
    ('SunOS',               'Solaris'),
    ('droid',               'Android'),
    ('Linux ',              'Linux'),
    ('Windows',             'Windows - Other'),
    ('Skype for Macintosh', 'MAC OS X other'),
    ('Xbox Live Client',    'Xbox'),
    ('urlgrabber/.* yum',   'Linux - redhat/fedora'),
    ('iPad',                'ipad'),
    ('iPod',                'ipod'),
    ('iPhone',              'iphone'),
    ('OS X',                'MAC OS X other'),
    ('Darwin',              'MAC OS X other'),
    ('winhttp',             'Windows - Other'),
    ('MSIE 4.0;',           'Windows - Other'),
    ('Microsoft',           'Windows - Other'),
    ('Win32',               'Windows - Other'),
    ('BlackBerry',          'BlackBerry'),
)

browser_mapping = (
    ('MSIE 7.*Trident/4.0', 'Internet Explorer 8.0'),
    ('MSIE 7.0',            'Internet Explorer 7.0'),
    ('MSIE 8.0',            'Internet Explorer 8.0'),
    ('MSIE 6.0',            'Internet Explorer 6.0'),
    ('Firefox/3',           'Firefox 3'),
    ('Firefox/2',           'Firefox 2'),
    ('iTunes',              'iTunes'),
    ('Google Update',       'Google Update'),
    ('MSIE 5.00',           'Internet Explorer 5.0'),
    ('urlgrabber/.* yum',   'yum'),
    ('IE',                  'Internet Explorer - Other'),
    ('Firefox/1',           'Firefox 1'),
    ('iPhone',              'Safari - mobile'),
    ('Chrome',              'Chrome'),
    ('Safari/',             'Safari'),
    ('AppleWebKit',         'Safari'),
    ('Opera',               'Opera'),
)

arch_mapping = (
    ('Windows .. 5.2',      'x64'),
    ('x64',                 'x64'),
    ('i386',                'i386'),
    ('x86_64',              'x64'),
    ('PPC',                 'PowerPC'),
    ('Power.{1,3}Macint',   'PowerPC'),
    ('droid',               'android'),
    ('iPad',                'ipad'),
    ('iPod',                'ipod'),
    ('iPhone',              'iphone'),
    ('Intel',               'Intel'),
    ('BlackBerry',          'BlackBerry'),
)

os_mapping      = [(re.compile(a, re.IGNORECASE),b) for (a,b) in os_mapping]
browser_mapping = [(re.compile(a, re.IGNORECASE),b) for (a,b) in browser_mapping]
arch_mapping    = [(re.compile(a, re.IGNORECASE),b) for (a,b) in arch_mapping]

def get_thing(line, mapping):
    for r, name in mapping:
        if r.search(line):
            return name
    return 'unknown'

def get_ua_info(line):
    i = {}
    i['operating_system'] = get_thing(line, os_mapping)
    i['architecture']     = get_thing(line, arch_mapping)
    i['browser']          = get_thing(line, browser_mapping)
    return i

try:
    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()
    for r in results:
        if "_raw" not in r:
            continue
        info = get_ua_info(r['_raw'])
        r.update(info)
except:
    import traceback
    stack =  traceback.format_exc()
    results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))

splunk.Intersplunk.outputResults( results )

@@ -644,222 +644,46 @@
   ('
-Linux ',              'Linux'),%0A    ('Windows',             'Windows - Other'),%0A    ('Skype for Macintosh', 'MAC OS X other'),%0A    ('Xbox Live Client',    'Xbox'),%0A    ('urlgrabber/.* yum',   'Linux - redhat/fedora
+Windows',             'Windows - Other
 '),%0A
@@ -885,24 +885,62 @@
  X other'),%0A
+    ('Linux ',              'Linux'),%0A
     ('winhtt
@@ -1162,16 +1162,154 @@
 erry'),%0A
+    ('urlgrabber/.* yum',   'Linux - redhat/fedora'),%0A    ('Skype for Macintosh', 'MAC OS X other'),%0A    ('Xbox Live Client',    'Xbox'),%0A
 )%0A%0Abrows
@@ -1549,17 +1549,14 @@
   ('
-Firefox/3
+iPhone
 ',  
@@ -1568,39 +1568,48 @@
     
-'Firefox 3'),%0A    ('Firefox/2',
+   'Safari - mobile'),%0A    ('Safari/',  
     
@@ -1620,17 +1620,14 @@
    '
-Firefox 2
+Safari
 '),%0A
@@ -1675,44 +1675,82 @@
   ('
-Google Update',       'Google Update
+Firefox/3',           'Firefox 3'),%0A    ('Firefox/2',           'Firefox 2
 '),%0A
@@ -1807,44 +1807,8 @@
 '),%0A
-    ('urlgrabber/.* yum',   'yum'),%0A
     
@@ -1871,17 +1871,14 @@
   ('
-Firefox/1
+Chrome
 ',  
@@ -1890,41 +1890,41 @@
     
+ 
-'Firefox 1'),%0A    ('iPhone',     
+  'Chrome'),%0A    ('AppleWebKit',
     
@@ -1931,33 +1931,24 @@
      'Safari
- - mobile
 '),%0A    ('Ch
@@ -1949,36 +1949,43 @@
   ('
-Chrome',              'Chrom
+Google Update',       'Google Updat
 e'),
@@ -1983,39 +1983,41 @@
 Update'),%0A    ('
-Safari/
+Firefox/1
 ',             '
@@ -2017,94 +2017,92 @@
     
-  'Safari'),%0A    ('AppleWebKit',         'Safari'),%0A
+'Firefox 1'),%0A    ('Opera',         
     
-(
+  
 'Opera'
-,
+),%0A
     
-           'Opera
+('urlgrabber/.* yum',   'yum
 '),%0A

tweak order of regexes