import os
from math import log10
from tiny_bits import nice_wsize

def list_of_top_level_domains():
	'''
	#+
	# NAME:
	#	list_of_top_level_domains
	# PURPOSE:
	#	Provides a hash with translations of top-level domains to
	#	full names. These are mostly the standard 2-letter country
	#	abbreviations to country names, with a few additions.
	# CALLING SEQUENCE:
	# 	tld_name = list_of_top_level_domains()
	# OUTPUTS:
	#	tld_name   dictionary with as key the tld (country abbreviation)
	#	and as value the full name (country name).
	# NOTE:
	#	There also is a Perl version of this routine
	# PROCEDURE:
	#	Reads the file $ECLIPSE/general/etc/top_level_domains.txt.
	#	(if not found the function dies).
	#
	# The content of the file comes from http://www.thrall.org/domains.htm
	# (Dec 2012) with a few manual additions:
	#	me (Montenegro), sr (Serbia), cat (Catalonia), ps (Palestine)
	#	eu (European Union)
	# MODIFICATION HISTORY:
	#	DEC-2012, Paul Hick (UCSD/CAIDA; pphick@caida.org)
	#-
	'''
	file = os.path.join( os.environ['ECLIPSE'], 'general','etc', 'top_level_domains.txt' )
	country_name = dict()
	for line in open(file,'r').read().split('\n')[0:-1]:
		parts  = line.split()	# "SV  El Salvador" --> country_name['SV'] = "El Salvador"
		country_name[parts[0]] = ' '.join(parts[1:])

	return country_name

def table_by_month( count,
		key_replacement	= None	,
		sort_by_count	= False	,
		in_html			= False	,
		sizes			= False	,
		in_bytes		= False	,
		fraction		= 1		,
		open_table		= False	,
		column_width	= 50	,
		header			= []	,
	):
	'''
	#+
	# NAME:
	#	table_by_month
	# PURPOSE:
	#	Prints counts stored in a dictionary in tabular form
	# CALLING SEQUENCE:
	# 	table_by_month( count, key_replacement, sort_by_count, in_html )
	# INPUTS:
	# PROCEDURE:
	#	in_html = False
	#		print as simple table: year, 12 months, followed by annual total
	#		final line of dots, with total counts over all years and months
	#	; key-name (or replacement value)
	#	; code-red
	#	 2011     0    0    0    0    0    1    0    0    0    0    0    0 |    1
	#	 2012     0    0    0    0    0    0    0    0    1    0    0    0 |    1
	#	;................................................................. |    2
	#
	#	in_html = True
	#		print as html table
	# MODIFICATION HISTORY:
	#	MAY-2013, Paul Hick (UCSD/CAIDA; pphick@caida.org)
	#-
	'''

	# Check first key for presence of month zero. This should contain the total for the whole
	# year. If it is not present, then calculate the totals and add to hash

	key  = count.keys()[0]
	year = count[key].keys()[0]

	if not count[key][year].has_key(0):		# Annual totals not present
		for key in count:
			for year in count[key]:			# Sum over 12 months, and add to dict at month=0
				count[key][year][0] = sum( [ count[key][year][month] for month in count[key][year].keys() if month > 0] )

	# If sort_by_count is set then counts for the keys with the highest number of counts
	# (sum over all years and month) are printed first. Otherwise, print in alpha-numeric order.

	sorted_keys = sorted( count, key=lambda x: sum( [ count[x][year][0] for year in count[x] ] ), reverse=True ) if sort_by_count else	\
				  sorted( count, key=lambda x: key_replacement[x] )												 if key_replacement != None else \
				  sorted( count )

	total_in_key = dict( zip( sorted_keys, [ sum( [ count[key][year][0] for year in count[key] ] ) for key in sorted_keys ] ) )

	ndig = 10 if sizes and not in_bytes else long(2+log10( max( total_in_key.values() ) ))
	fmt1 = '%5s '+'%%%ss'%ndig*12+' |%%%ss'%ndig
	fmt2 = ';'+'.'*(5+12*ndig)+' |%%%ss'%ndig

	content = []
	if not in_html:
		for line in header:
			content.append('; '+line)

	for key in sorted_keys:

		if total_in_key[key] ==  0:
			continue

		years = count[key].keys()
		years.sort()
		years = years[ years.index( [ year for year in years if count[key][year][0] != 0 ][0] ): ]

		print_key = key if key_replacement == None else key_replacement[key]

		calendar = dict( zip( years, [ [ count[key][year][month] if count[key][year].has_key(month) else 0 for month in range(0,13) ] for year in years ] ) )

		if in_html:

			if not open_table:
				content.append('<table>')

			column_layout = '<tr><th width="50">%s</th>'+('<th width="%s">%%s</th>'%column_width*13)+'</tr>'
			content.append(
				column_layout%(print_key,'Total','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
			)

			if sizes:
				content.extend(	\
					[ '<tr><td>%s</td>'%year + '<td align="right">%s</td>'*13%tuple( [ nice_wsize( calendar[year][month], in_bytes=in_bytes, fraction=fraction ) for month in range(0,13) ] )+'</tr>' for year in years ]
				)
			else:
				content.extend(
					[ '<tr><td>%s</td>'%year + '<td align="right">%s</td>'*13%tuple( calendar[year] )+'</tr>' for year in years ]
				)

			if not open_table:
				content.append('</table>')

		else:
			if sizes:
				content.extend(				\
					[ '; %s'%print_key ] +	\
					[ fmt1%tuple( [year]+[ nice_wsize( calendar[year][month], in_bytes=in_bytes, fraction=fraction ) for month in range(1,13) ]+[ nice_wsize( calendar[year][0], in_bytes=in_bytes, fraction=fraction ) ] ) for year in years ] + \
					[ fmt2% nice_wsize( total_in_key[key], in_bytes=in_bytes, fraction=fraction ) ]
				)
			else:
				content.extend(				\
					[ '; %s'%print_key ] +	\
					[ fmt1%tuple( [year]+calendar[year][1:13]+[ calendar[year][0] ] ) for year in years ] + \
					[ fmt2%total_in_key[key] ]
				)

	return content
