import re


def template_to_regex(template):
    """Convert a string template to a parsable regular expression.
    
    Given a data_path_format string template, parse the template into
    a parsable regular expression string for extracting each %VAR%
    variable.
    
    Supported %VAR% variables:
    
    * %EXPERIMENT_ID% - experiment ID (str)
    * %INSTRUMENT_SAT% - instrument/satellite ID (str)
    * %DATA_TYPE% - data type (str)
    * %YEAR% - full year (int, 4 digits)
    * %YEAR4% - full year (int, 4 digits)
    * %YEAR2% - last two digits of year (int, 2 digits)
    * %MONTH% - integer month (int, 2 digits)
    * %MONTH2% - integer month (int, 2 digits)
    * %DAY% - integer day (int, 2 digits)
    * %DAY2% - integer day (int, 2 digits)
    * %HOUR% - integer hour (int, 2 digits)
    * %HOUR2% - integer hour (int, 2 digits)
    
    Args:
        template (str): A string with the data_path_format template to
            be converted to a parsable regular expression.
    
    Returns:
        tuple: Tuple with the first element being a parsable regular 
        expression string, and the second element being a list of the 
        detected %VAR%s, in order found (from left to right).
        
    """
    # Initialize the final template regex string
    template_final = ""
    
    # Initialize the final matching group list
    matching_groups = []
    
    # Define the variables to replace, with their corresponding regex
    # capturing patterns.
    regex_replace_dict = {
                            "%EXPERIMENT_ID%"   : r'(.*)',
                            "%INSTRUMENT_SAT%"  : r'(.*)',
                            "%DATA_TYPE%"       : r'(.*)',
                            
                            "%YEAR%"            : r'(\d{4})',
                            "%YEAR4%"           : r'(\d{4})',
                            "%YEAR2%"           : r'(\d{2})',
                            
                            "%MONTH%"           : r'(\d{2})',
                            "%MONTH2%"          : r'(\d{2})',
                            
                            "%DAY%"             : r'(\d{2})',
                            "%DAY2%"            : r'(\d{2})',
                            
                            "%HOUR%"            : r'(\d{2})',
                            "%HOUR2%"           : r'(\d{2})',
                        }
    
    # Search for %VAR% variables with a %VAR% matching pattern
    matches = re.findall(r'(.*?)(%.*?%)(.*?)', template)
    
    # Loop through each match!
    for match in matches:
        # Grab the %VAR% part in the match.
        # (match returns a tuple with 3 elements - the misc string on
        # the left side, the %VAR% part in the middle, and the misc
        # string on the right side)
        template_part = match[1]
        
        # Check to see if this %VAR% is in our replacement table!
        if template_part in regex_replace_dict.keys():
            # Add it to the matching group list for future indexing
            # reference!
            matching_groups.append(template_part)
            
            # Then make the variable to regex replacement.
            template_part = template_part.replace(template_part, regex_replace_dict[template_part])
        
        # Finally, assemble the string back together.
        template_final += re.escape(match[0]) + template_part + re.escape(match[2])
    
    # Return the regex template and the list of matching groups!
    return (template_final, matching_groups)