#!/usr/bin/env python2.4
#
# $Id$
#
# compare translated language files with the master (english) language files
# it is useful for finding incomplete translations
#
# Copyright (c) 02006 sense.lab <senselab@systemausfall.org>
#
# License: This script is distributed under the terms of version 2
#          of the GNU GPL. See the LICENSE file included with the package.
#
# Parameter: LANGUAGE (e.g. "de")
#

import os, sys
try:
	import neo_cgi, neo_util
except ImportError, errMsg:
	sys.stderr.write("Could not import the python clearsilver module: %s\n" % (errMsg,))
	sys.stderr.write("Maybe you should try 'apt-get install python-clearsilver'?\n")
	sys.exit(1)

SUFFIX = ".hdf"
DEFAULT_LANGUAGE = "en"


def getHDFkeys(hdf):
	import types
	if type(hdf.dump()) == types.NoneType: return []
	return [e.split(" = ",1)[0].strip() for e in hdf.dump().splitlines()]


def compareFiles(langFile1, langFile2):
	## check for existence of files
	for filename in (langFile2, langFile1):
		if not os.path.exists(filename):
			sys.stderr.write("could not find language file: %s\n" % filename)
			return

	## check if the files are the same
	if langFile1 == langFile2:
		sys.stderr.write("cannot compare the language file with itself: %s\n" % langFile1)
		return

	print "Comparing '%s' and '%s':" % (langFile1, langFile2)

	hdf1 = neo_util.HDF()
	hdf1.readFile(langFile1)
	hdf2 = neo_util.HDF()
	hdf2.readFile(langFile2)

	hdfKeys1 = getHDFkeys(hdf1)
	hdfKeys2 = getHDFkeys(hdf2)

	## check for empty language data sets
	for (keys, filename) in ((hdfKeys1, langFile1), (hdfKeys2, langFile2)):
		if len(keys) == 0:
			sys.stderr.write("\tthe language file is empty: %s\n" % filename)
			return

	## check for missing keys (only part of the first (master) file)
	for key in hdfKeys1:
		if key.endswith(".Link.Abs") or key.endswith(".Link.Prot") or key.endswith(".Link.Rel"):
			## ignore link attributes
			pass
		elif not key in hdfKeys2:
			print "\tmissing key: %s" % key

	## check for superfluous keys (only part of the second file)
	for key in hdfKeys2:
		if key.endswith(".Link.Abs") or key.endswith(".Link.Prot") or key.endswith(".Link.Rel"):
			print "\tyou should not override link attributes: %s" % key
		elif not key in hdfKeys1:
			print "\tsuperfluous key: %s" % key


def find_lang_file(arg, dirname, fnames):
	lfile = os.path.join(dirname, arg["fname"])
	if os.path.isfile(lfile):
		arg["list"].append(lfile)



############ main #############
if "__main__" != __name__:
	sys.exit(0)

args = sys.argv[1:]

## check parameters
if len(args) != 1:
	sys.stderr.write("invalid number of parameters: a language name is required\n")
	sys.exit(1)


language = args[0]

if language == DEFAULT_LANGUAGE:
	sys.stderr.write("you must specify a language different from the default (%s)!\n" % DEFAULT_LANGUAGE)
	sys.exit(1)

obj = { "fname":"%s%s" % (DEFAULT_LANGUAGE, SUFFIX),
	"list": [] }

os.path.walk(os.getcwd(), find_lang_file, obj)

for langFile in obj["list"]:
	refLang = langFile.replace("/%s%s" % (DEFAULT_LANGUAGE, SUFFIX), "/%s%s" % (language, SUFFIX))
	compareFiles(langFile, refLang)

