#!/usr/bin/python
#
# $FreeBSD$
#
# Script for parsing the xearth committers markers file and generating a
# heat map with countries of the world colored according to how many committers
# reside there.
# 
# See http://code.google.com/apis/chart/#iso_codes
#
# Murray Stokely, March 30, 2008
#

import re
import string

country_iso3166 = {
    'western australia': 'AU',
    'usa': 'US',
    'poland': 'PL',
    'canada': 'CA',
    'ukraine': 'UA',
    'sweden': 'SE',
    'uk': 'GB',
    'russia': 'RU',
    'bulgaria': 'BG',
    'switzerland': 'CH',
    'czech republic': 'CZ',
    'denmark': 'DK',
    'mexico': 'MX',
    'portugal': 'PT',
    'ca usa': 'US',
    'romania': 'RO',
    'india': 'IN',
    'malaysia': 'MY',
    'the netherlands': 'NL',
    'greece': 'GR',
    'spain': 'ES',
    'japan': 'JP',
    'taiwan': 'TW',
    'austria': 'AT',
    'brazil': 'BR',
    'hungary': 'HU',
    'south africa': 'ZA',
    'italy': 'IT',
    'belgium': 'BE',
    'france': 'FR',
    'mongolia': 'MN',
    'united kingdom': 'GB',
    'slovakia': 'SK',
    'peru': 'PE',
    'ireland': 'IE',
    'south korea': 'KR',
    'norway': 'NO',
    'australia': 'AU',
    'new zealand': 'NZ',
    'slovenia': 'SI',
    'macau sar': 'MO',
    'argentina': 'AR',
    'china': 'CN',
    'germany': 'DE',
    'kazakhstan': 'KZ',
}


#format
#-41.2124,	174.8870,	"thompsa"			# Wellington, New Zealand
#-32.90,		151.70,		"lawrance"			# Newcastle, Australia
#-34.05,		151.15,		"edwin"				# Cronulla, Australia

def simple_encode(num_str):
    num = int(num_str)
    if num >= 0:
        if num <=25:
            return chr(ord('A')+num)
        elif num<=51:
            return chr(ord('a')+num)
        elif num<=61:
            return chr(ord('0') + num - 52)

def main():
    filename="freebsd.committers.markers"
    xearth_markers = re.compile('[\d]+\.[\d]+,?\s+-?[\d]+\.[\d]+,?\s+"([^"]+)"[^#]*#(.*)$')
    # First match is user list, second match is location text.
    f = open(filename, 'r')
    country_dict = {}
    for line in f:
        m = xearth_markers.search(line)
        if m:
#            print "match : (%s) (%s)" % (m.group(1), m.group(2))
            ppl = m.group(1).split(',')
            people = [x for x in ppl if not re.search('^\s*$', x)]
#            print "len ppl : %s" % len(people)
            country = string.lower(m.group(2).split(',')[-1].strip())
#            print "country: %s" % country
            if country in country_dict:
                country_dict[country] += len(people)
            else:
                country_dict[country] = len(people)

    worldmap = {}
    worldmap['chs'] = '440x220'
    worldmap['chco'] = 'ffffff,ffbe38,600000'
    worldmap['cht'] = 't'
    worldmap['chtm'] = 'world'
    worldmap['chld'] = ''
    worldmap['chd'] = 's:'
    worldmap['chf'] = 'bg,s,EAF7FE'
    max_ppl = 0                
    for country in country_dict:
        if country_dict[country] > max_ppl:
            max_ppl = country_dict[country]
        print "%s: %s" % (country_iso3166[country], country_dict[country])

    country_pct = {}
    for country in country_dict:
        country_pct[country_iso3166[country]] = float(100 * country_dict[country]) / max_ppl
        worldmap['chld'] += country_iso3166[country]
        worldmap['chd'] += simple_encode("%d" % (((60 * country_dict[country]) / int(max_ppl)) + 1))
        print "code = %d" % (((60 * country_dict[country]) / int(max_ppl)) + 1)


    CHART_URL = 'http://chart.apis.google.com/chart?'
    print CHART_URL + '&'.join(["%s=%s" % (k,v) for (k,v) in worldmap.items()])

#    print '&'.join(key
#    for country in country_pct:
#        print "%s: %s" % (country, country_pct[country])

if __name__ == "__main__":
    main()
