# Path: logsparser/normalizer.py
# def get_generic_tagTypes(path = 'normalizers/common_tagTypes.xml'):
#     """Imports the common tag types.
#     
#     @return: a dictionary of tag types."""
#     generic = {}
#     try:
#         tagTypes = parse(open(path, 'r')).getroot()
#         for tagType in tagTypes:
#             tt_name = tagType.get('name')
#             tt_type = tagType.get('ttype') or 'basestring'
#             tt_desc = {}
#             for child in tagType:
#                 if child.tag == 'description':
#                     for desc in child:
#                         lang = desc.get('language') or 'en'
#                         tt_desc[lang] = child.text
#                 elif child.tag == 'regexp':
#                     tt_regexp = child.text
#             generic[tt_name] = TagType(tt_name, tt_type, tt_regexp, tt_desc)
#         return generic
#     except StandardError, err:
#         warnings.warn("Could not load generic tags definition file : %s \
#                        - generic tags will not be available." % err)
#         return {}
# 
# Path: logsparser/normalizer.py
# def get_generic_callBacks(path = 'normalizers/common_callBacks.xml'):
#     """Imports the common callbacks.
# 
#     @return a dictionnary of callbacks."""
#     generic = {}
#     try:
#         callBacks = parse(open(path, 'r')).getroot()
#         for callBack in callBacks:
#             cb_name = callBack.get('name')
#             # cb_desc = {}
#             for child in callBack:
#                 if child.tag == 'code':
#                     cb_code = child.text
# 		# descriptions are not used yet but implemented in xml and dtd files for later use
#                 # elif child.tag == 'description':
#                 #     for desc in child:
#                 #         lang = desc.get('language')
#                 #         cb_desc[lang] = desc.text
#             generic[cb_name] = CallbackFunction(cb_code, cb_name)
#         return generic
#     except StandardError, err:
#         warnings.warn("Could not load generic callbacks definition file : %s \
#                        - generic callbacks will not be available." % err)
#         return {}


import os
import unittest
from datetime import datetime, timedelta
from logsparser.normalizer import get_generic_tagTypes
from logsparser.normalizer import get_generic_callBacks
    # special cases
    if pattern == "ISO8601":
        pattern = "%Y-%m-%dT%H:%M:%SZ"
    for d in DATES_TO_TEST:
        if pattern == "EPOCH":
            #value = d.strftime('%s') + ".%i" % (d.microsecond/1000)
            # Fix for windows strftime('%s'), and python timedelta total_seconds not exists in 2.6
            td = d - datetime(1970, 1, 1)
            total_seconds_since_epoch = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
            value = str(total_seconds_since_epoch) + ".%i" % (d.microsecond/1000)
            #
            expected_result = datetime.utcfromtimestamp(float(value))
        else:
            value = d.strftime(pattern)
            expected_result = datetime.strptime(value, pattern)
            # Deal with time formats that don't define a year explicitly
            if "%y" not in pattern.lower():
                expected_year = get_sensible_year(*expected_result.timetuple()[1:-3])
                expected_result = expected_result.replace(year = expected_year)
        log = {}
        instance.cb[cb](value, log)
        instance.assertTrue("date" in log.keys())
        instance.assertEqual(log['date'], expected_result)


class TestGenericLibrary(unittest.TestCase):
    """Unit testing for the generic libraries"""
    normalizer_path = os.environ['NORMALIZERS_PATH']
    tagTypes = get_generic_tagTypes(os.path.join(normalizer_path,
                                                 'common_tagTypes.xml'))

# Path: logsparser/normalizer.py
# def get_generic_tagTypes(path = 'normalizers/common_tagTypes.xml'):
#     """Imports the common tag types.
#     
#     @return: a dictionary of tag types."""
#     generic = {}
#     try:
#         tagTypes = parse(open(path, 'r')).getroot()
#         for tagType in tagTypes:
#             tt_name = tagType.get('name')
#             tt_type = tagType.get('ttype') or 'basestring'
#             tt_desc = {}
#             for child in tagType:
#                 if child.tag == 'description':
#                     for desc in child:
#                         lang = desc.get('language') or 'en'
#                         tt_desc[lang] = child.text
#                 elif child.tag == 'regexp':
#                     tt_regexp = child.text
#             generic[tt_name] = TagType(tt_name, tt_type, tt_regexp, tt_desc)
#         return generic
#     except StandardError, err:
#         warnings.warn("Could not load generic tags definition file : %s \
#                        - generic tags will not be available." % err)
#         return {}
# 
# Path: logsparser/normalizer.py
# def get_generic_callBacks(path = 'normalizers/common_callBacks.xml'):
#     """Imports the common callbacks.
# 
#     @return a dictionnary of callbacks."""
#     generic = {}
#     try:
#         callBacks = parse(open(path, 'r')).getroot()
#         for callBack in callBacks:
#             cb_name = callBack.get('name')
#             # cb_desc = {}
#             for child in callBack:
#                 if child.tag == 'code':
#                     cb_code = child.text
# 		# descriptions are not used yet but implemented in xml and dtd files for later use
#                 # elif child.tag == 'description':
#                 #     for desc in child:
#                 #         lang = desc.get('language')
#                 #         cb_desc[lang] = desc.text
#             generic[cb_name] = CallbackFunction(cb_code, cb_name)
#         return generic
#     except StandardError, err:
#         warnings.warn("Could not load generic callbacks definition file : %s \
#                        - generic callbacks will not be available." % err)
#         return {}


# Path: tests/test_commonElements.py
import os
import unittest
from datetime import datetime, timedelta
from logsparser.normalizer import get_generic_tagTypes
from logsparser.normalizer import get_generic_callBacks

    # special cases
    if pattern == "ISO8601":
        pattern = "%Y-%m-%dT%H:%M:%SZ"
    for d in DATES_TO_TEST:
        if pattern == "EPOCH":
            #value = d.strftime('%s') + ".%i" % (d.microsecond/1000)
            # Fix for windows strftime('%s'), and python timedelta total_seconds not exists in 2.6
            td = d - datetime(1970, 1, 1)
            total_seconds_since_epoch = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
            value = str(total_seconds_since_epoch) + ".%i" % (d.microsecond/1000)
            #
            expected_result = datetime.utcfromtimestamp(float(value))
        else:
            value = d.strftime(pattern)
            expected_result = datetime.strptime(value, pattern)
            # Deal with time formats that don't define a year explicitly
            if "%y" not in pattern.lower():
                expected_year = get_sensible_year(*expected_result.timetuple()[1:-3])
                expected_result = expected_result.replace(year = expected_year)
        log = {}
        instance.cb[cb](value, log)
        instance.assertTrue("date" in log.keys())
        instance.assertEqual(log['date'], expected_result)


class TestGenericLibrary(unittest.TestCase):
    """Unit testing for the generic libraries"""
    normalizer_path = os.environ['NORMALIZERS_PATH']
    tagTypes = get_generic_tagTypes(os.path.join(normalizer_path,
                                                 'common_tagTypes.xml'))

    cb = get_generic_callBacks(os.path.join(normalizer_path,