# XML "function" nodes that transform their contents

import datetime
import operator
import os
import re
import sre_constants
import warnings

import angles
import dates
import exc
import format
import lists
import maps
import scalar
import sql
import sql_io
import strings
import term
import units
import util
import xml_dom
import xpath

##### Exceptions

class SyntaxError(exc.ExceptionWithCause):
    def __init__(self, cause):
        exc.ExceptionWithCause.__init__(self, 'Invalid XML function syntax',
            cause)

class FormatException(exc.ExceptionWithCause):
    def __init__(self, cause):
        exc.ExceptionWithCause.__init__(self, 'Invalid input value', cause)

##### Helper functions

def map_names(func, params):
    return [(func(name), value) for name, value in params]

def variadic_args(node):
    args = map_names(float, xml_dom.NodeEntryIter(node))
    args.sort()
    return [value for name, value in args]

def map_items(func, items):
    return [(name, func(value)) for name, value in items]

def cast(type_, val):
    '''Throws FormatException if can't cast'''
    try: return type_(val)
    except ValueError, e: raise FormatException(e)

def conv_items(type_, items):
    return map_items(lambda val: cast(type_, val),
        xml_dom.TextEntryOnlyIter(items))

def pop_value(items, name='value'):
    '''@param name Name of value param, or None to accept any name'''
    try: last = items.pop() # last entry contains value
    except IndexError: return None # input is empty and no actions
    if name != None and last[0] != name: return None # input is empty
    return last[1]

def merge_tagged(root):
    '''Merges siblings in root that are marked as mergeable.
    Used to recombine pieces of nodes that were split apart in the mappings.
    '''
    for name in set((c.tagName for c in xpath.get(root, '*[@merge=1]'))):
        xml_dom.merge_by_name(root, name)
    
    # Recurse
    for child in xml_dom.NodeElemIter(root): merge_tagged(child)

funcs = {}
simplifying_funcs = {}

##### Public functions

var_name_prefix = '$'

def is_var_name(str_): return str_.startswith(var_name_prefix)

def is_var(node):
    return xml_dom.is_text_node(node) and is_var_name(xml_dom.value(node))

def is_func_name(name):
    return name.startswith('_') and name != '_' # '_' is default root node name

def is_func(node): return is_func_name(node.tagName)

def is_xml_func_name(name): return is_func_name(name) and name in funcs

def is_xml_func(node): return is_xml_func_name(node.tagName)

def is_scalar(value):
    return scalar.is_scalar(value) and not (util.is_str(value)
        and is_var_name(value))

def passthru(node):
    '''Passes through single child node. First prunes the node.'''
    xml_dom.prune(node)
    children = list(xml_dom.NodeEntryIter(node))
    if len(children) == 1: xml_dom.replace(node, children[0][1])

def simplify(node):
    '''Simplifies an XML tree.
    * Merges nodes tagged as mergable
    * Runs simplifying functions
    '''
    for child in xml_dom.NodeElemIter(node): simplify(child)
    merge_tagged(node)
    
    name = node.tagName
    
    # Pass-through optimizations
    if is_func_name(name):
        try: func = simplifying_funcs[name]
        except KeyError: xml_dom.prune_empty(node)
        else: func(node)
    # Pruning optimizations
    else: # these should not run on functions because they would remove args
        xml_dom.prune_children(node)

def process(node, on_error=exc.reraise, is_rel_func=None, db=None):
    '''Evaluates the XML functions in an XML tree.
    @param is_rel_func None|f(str) Tests if a name is a relational function.
        * If != None: Non-relational functions are removed, or relational
          functions are treated specially, depending on the db param (below).
    @param db
        * If None: Non-relational functions other than structural functions are
          replaced with their last parameter (usually the value), not evaluated.
          This is used in column-based mode to remove XML-only functions.
        * If != None: Relational functions are evaluated directly. This is used
          in row-based mode to combine relational and XML functions.
    '''
    has_rel_funcs = is_rel_func != None
    assert db == None or has_rel_funcs # rel_funcs required if db set
    
    for child in xml_dom.NodeElemIter(node):
        process(child, on_error, is_rel_func, db)
    merge_tagged(node)
    
    name = node.tagName
    if not is_func_name(name): return node # not any kind of function
    
    row_mode = has_rel_funcs and db != None
    column_mode = has_rel_funcs and db == None
    func = funcs.get(name, None)
    items = list(xml_dom.NodeTextEntryIter(node))
    
    # Parse function
    if len(items) == 1 and items[0][0].isdigit(): # has single numeric param
        # pass-through optimization for aggregating functions with one arg
        value = items[0][1] # pass through first arg
    elif row_mode and (is_rel_func(name) or func == None): # row-based mode
        if items and reduce(operator.or_, (xml_dom.is_node(v)
            for n, v in items)): return # preserve complex funcs
        # Evaluate using DB
        try: value = sql_io.put(db, name, dict(items), on_error=on_error)
        except sql.DoesNotExistException: return # preserve unknown funcs
            # possibly a built-in function of db_xml.put()
    elif column_mode or func == None:
        # local XML function can't be used or does not exist
        if column_mode and is_rel_func(name): return # preserve relational funcs
        # otherwise XML-only in column mode, or DB-only in XML output mode
        value = pop_value(items, None) # just replace with last param
    else: # local XML function
        try: value = func(items, node)
        except Exception, e: # also catch non-wrapped exceptions (XML func bugs)
            # Save in case another exception raised, overwriting sys.exc_info()
            exc.add_traceback(e)
            str_ = strings.ustr(node)
            exc.add_msg(e, 'function:\n'+str_)
            xml_dom.replace(node, xml_dom.mk_comment(node.ownerDocument,
                '\n'+term.emph_multiline(str_)))
                
            on_error(e)
            return # in case on_error() returns
    
    xml_dom.replace_with_text(node, value)

##### Simplifying functions

# Function names must start with _ to avoid collisions with real tags
# Functions take params (node) and have no return value

#### Logic

def _and(node):
    values = [v for k, v in xml_dom.NodeTextEntryIter(node)]
    
    if lists.and_(map(is_scalar, values)): # all constants
        xml_dom.replace_with_text(node, lists.and_(values))
    else: passthru(node)
simplifying_funcs['_and'] = _and

def _or(node):
    values = [v for k, v in xml_dom.NodeTextEntryIter(node)]
    
    if lists.and_(map(is_scalar, values)): # all constants
        xml_dom.replace_with_text(node, lists.or_(values))
    else: passthru(node)
simplifying_funcs['_or'] = _or

def _exists(node):
    '''Returns whether its node is non-empty'''
    xml_dom.replace_with_text(node, not xml_dom.is_empty(node))
simplifying_funcs['_exists'] = _exists

def _if(node):
    '''
    *Must* be run to remove conditions that functions._if() can't handle.
    Note: Can add `@name` attr to distinguish separate _if statements.
    '''
    params = dict(xml_dom.NodeEntryIter(node))
    then = params.get('then', None)
    cond = params.get('cond', None)
    else_ = params.get('else', None)
    
    if cond == None: xml_dom.replace(node, else_) # always False
    elif then == else_: xml_dom.replace(node, then) # always same value
    elif is_var(cond): pass # can't simplify variable conditions
    elif xml_dom.is_text_node(cond) and bool(xml_dom.value(cond)): # always True
        xml_dom.replace(node, then)
simplifying_funcs['_if'] = _if

def _nullIf(node):
    '''
    *Must* be run to remove conditions that functions._nullIf() can't handle.
    '''
    params = dict(xml_dom.NodeEntryIter(node))
    null = params.get('null', None)
    value = params.get('value', None)
    
    if value == None: xml_dom.prune_parent(node) # empty
    elif null == None: xml_dom.replace(node, value) # nothing to null out
simplifying_funcs['_nullIf'] = _nullIf

#### Comparison

def _eq(node):
    params = dict(xml_dom.NodeTextEntryIter(node))
    left = params.get('left', None)
    right = params.get('right', None)
    
    if is_scalar(left) and is_scalar(right): # constant
        xml_dom.replace_with_text(node, left == right)
    elif left == right: xml_dom.replace_with_text(node, True) # always True
simplifying_funcs['_eq'] = _eq

#### Merging

simplifying_funcs['_alt'] = passthru
simplifying_funcs['_join'] = passthru
simplifying_funcs['_join_words'] = passthru
simplifying_funcs['_merge_prefix'] = passthru
simplifying_funcs['_merge'] = passthru
simplifying_funcs['_min'] = passthru
simplifying_funcs['_max'] = passthru
simplifying_funcs['_avg'] = passthru

def _first(node):
    '''Chooses the first non-empty param (sorting by numeric param name)'''
    xml_dom.prune_children(node)
    args = variadic_args(node)
    try: first = args[0]
    except IndexError: first = None
    xml_dom.replace(node, first)
simplifying_funcs['_first'] = _first

#### Environment access

def _env(node):
    params = dict(xml_dom.NodeTextEntryIter(node))
    try: name = params['name']
    except KeyError, e: raise SyntaxError(e)
    
    xml_dom.replace_with_text(node, os.environ[name])
simplifying_funcs['_env'] = _env

##### XML functions

# Function names must start with _ to avoid collisions with real tags
# Functions take arguments (items, node)

#### Transforming values

def _replace(items, node):
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
    value = pop_value(items)
    if value == None: return None # input is empty
    try:
        for repl, with_ in items:
            if re.match(r'^\w+$', repl):
                repl = r'(?<![^\W_])'+repl+r'(?![^\W_])' # match whole word
            value = re.sub(repl, with_, value)
    except sre_constants.error, e: raise SyntaxError(e)
    return util.none_if(value.strip(), u'') # empty strings always mean None
funcs['_replace'] = _replace

#### Quantities

def _units(items, node):
    value = pop_value(items)
    if value == None: return None # input is empty
    
    quantity = units.str2quantity(value)
    try:
        for action, units_ in items:
            units_ = util.none_if(units_, u'')
            if action == 'default': units.set_default_units(quantity, units_)
            elif action == 'to':
                try: quantity = units.convert(quantity, units_)
                except ValueError, e: raise FormatException(e)
            else: raise SyntaxError(ValueError('Invalid action: '+action))
    except units.MissingUnitsException, e: raise FormatException(e)
    return units.quantity2str(quantity)
funcs['_units'] = _units

def _rangeStart(items, node):
    items = dict(conv_items(strings.ustr, items))
    try: value = items['value']
    except KeyError: return None # input is empty
    return units.parse_range(value)[0]
funcs['_rangeStart'] = _rangeStart

def _rangeEnd(items, node):
    items = dict(conv_items(strings.ustr, items))
    try: value = items['value']
    except KeyError: return None # input is empty
    return units.parse_range(value)[1]
funcs['_rangeEnd'] = _rangeEnd

class CvException(Exception):
    def __init__(self):
        Exception.__init__(self, 'CV (coefficient of variation) values are only'
            ' allowed for ratio scale data '
            '(see <http://en.wikipedia.org/wiki/Coefficient_of_variation>)')

def _noCV(items, node):
    items = list(conv_items(strings.ustr, items))
    try: name, value = items.pop() # last entry contains value
    except IndexError: return None # input is empty
    if re.match('^(?i)CV *\d+$', value): raise FormatException(CvException())
    return value
funcs['_noCV'] = _noCV

#### Angles

def _compass(items, node):
    '''Converts a compass direction (N, NE, NNE, etc.) into a degree heading'''
    items = dict(conv_items(strings.ustr, items))
    try: value = items['value']
    except KeyError: return None # input is empty
    
    if not value.isupper(): return value # pass through other coordinate formats
    try: return util.cast(str, angles.compass2heading(value)) # ignore None
    except KeyError, e: raise FormatException(e)
funcs['_compass'] = _compass
