<commit_before>
from datetime import datetime, timedelta
import time
"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
# Import helpers as desired, or define your own, ie:
#from webhelpers.html.tags import checkbox, password
from iso8601.iso8601 import ISO8601_REGEX
import re

class ParseError(Exception):
    """Raised when there is a problem parsing a date string"""

def importModuleFromFile(fullpath):
    """Loads  and returns module defined by the file path. Returns None if file could 
    not be loaded"""
    import os
    import sys
    import logging
    log = logging.getLogger(__name__)
    
    sys.path.append(os.path.dirname(fullpath))
    
    module = None
    try:
        module = __import__(os.path.splitext(os.path.basename(fullpath))[0])
 
    except Exception as ex:
        log.exception("Failed to load module:\n"+ex)
    finally:
        del sys.path[-1]
        return module

def convertToISO8601UTC (dateTimeArg):
    """This method assumes that the datetime is local naive time."""
    if isinstance(dateTimeArg, datetime) == True:
        dateUTC = datetime.utcfromtimestamp(time.mktime(dateTimeArg.timetuple()))
        #Add the macroseconds back since hte mktime conversion loses it
        return (dateUTC + timedelta(0, 0, dateTimeArg.microsecond))
    return dateTimeArg
        
def convertToISO8601Zformat(dateTimeArg):
    if isinstance(dateTimeArg, datetime) ==True:
        return convertToISO8601UTC (dateTimeArg).isoformat()+ "Z" 
    return dateTimeArg

def harvestTimeFormat(dateTimeArg):
    if isinstance(dateTimeArg, datetime) ==True:
        fmtStr = getHarvestDatetimeFormatString()
        utcdate = convertToISO8601UTC (dateTimeArg)
        import logging
        log = logging.getLogger(__name__)
        log.exception(utcdate.utctimetuple())
        return time.strftime(fmtStr,utcdate.utctimetuple())
    return dateTimeArg
    
def nowToISO8601Zformat():
    return datetime.utcnow().isoformat()+"Z"

def getISO8601Granularity(iso8601string):
    '''Determine the precision of an ISO8601 datetime string'''
    
    def _getPrecision(gran, granule):
        if gran.precision == None:
            return len(granule)
        else:
            return gran.precision

    if not isinstance(iso8601string, basestring):
        raise ParseError("Expecting a string %r" % iso8601string)
    m = ISO8601_REGEX.match(iso8601string)
    if not m:
        raise ParseError("Unable to parse date string %r" % iso8601string)
    groups = m.groupdict()
    sandsOfTime = [
                   Granularity("year",  4, 1), 
                   Granularity("month", 2, 2), 
                   Granularity("day",   2, 3), 
                   Granularity("hour",  2, 4),
                   Granularity("minute",2, 5),<commit_msg>removed some needless logging<commit_after>
from datetime import datetime, timedelta
import time
"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
# Import helpers as desired, or define your own, ie:
#from webhelpers.html.tags import checkbox, password
from iso8601.iso8601 import ISO8601_REGEX
import re

class ParseError(Exception):
    """Raised when there is a problem parsing a date string"""

def importModuleFromFile(fullpath):
    """Loads  and returns module defined by the file path. Returns None if file could 
    not be loaded"""
    import os
    import sys
    import logging
    log = logging.getLogger(__name__)
    
    sys.path.append(os.path.dirname(fullpath))
    
    module = None
    try:
        module = __import__(os.path.splitext(os.path.basename(fullpath))[0])
 
    except Exception as ex:
        log.exception("Failed to load module:\n"+ex)
    finally:
        del sys.path[-1]
        return module

def convertToISO8601UTC (dateTimeArg):
    """This method assumes that the datetime is local naive time."""
    if isinstance(dateTimeArg, datetime) == True:
        dateUTC = datetime.utcfromtimestamp(time.mktime(dateTimeArg.timetuple()))
        #Add the macroseconds back since hte mktime conversion loses it
        return (dateUTC + timedelta(0, 0, dateTimeArg.microsecond))
    return dateTimeArg
        
def convertToISO8601Zformat(dateTimeArg):
    if isinstance(dateTimeArg, datetime) ==True:
        return convertToISO8601UTC (dateTimeArg).isoformat()+ "Z" 
    return dateTimeArg

def harvestTimeFormat(dateTimeArg):
    if isinstance(dateTimeArg, datetime) ==True:
        fmtStr = getHarvestDatetimeFormatString()
        utcdate = convertToISO8601UTC (dateTimeArg)
#        import logging
#        log = logging.getLogger(__name__)
#        log.exception(utcdate.utctimetuple())
        return time.strftime(fmtStr,utcdate.utctimetuple())
    return dateTimeArg
    
def nowToISO8601Zformat():
    return datetime.utcnow().isoformat()+"Z"

def getISO8601Granularity(iso8601string):
    '''Determine the precision of an ISO8601 datetime string'''
    
    def _getPrecision(gran, granule):
        if gran.precision == None:
            return len(granule)
        else:
            return gran.precision

    if not isinstance(iso8601string, basestring):
        raise ParseError("Expecting a string %r" % iso8601string)
    m = ISO8601_REGEX.match(iso8601string)
    if not m:
        raise ParseError("Unable to parse date string %r" % iso8601string)
    groups = m.groupdict()
    sandsOfTime = [
                   Granularity("year",  4, 1), 
                   Granularity("month", 2, 2), 
                   Granularity("day",   2, 3), 
                   Granularity("hour",  2, 4),
                   Granularity("minute",2, 5),
{'fraction_non_alphanumeric': 0.09138020383485922, 'fraction_numerical': 0.02694765935394714, 'mean_word_length': 3.0376569037656904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4078143', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1662, 'n_words': 488}