# XPath parsing

import copy
import warnings

from Parser import Parser
import strings
import util
import xml_dom

##### Path elements

class XpathElem:
    def __init__(self, name='', value=None, is_attr=False):
        self.name = name
        self.value = value
        self.is_attr = is_attr
        self.is_positive = True
        self.is_lookup_only = False
        self.is_ptr = False
        self.keys = []
        self.attrs = []
        self.other_branches = [] # temp implementation for split paths
    
    def __repr__(self):
        str_ = ''
        if not self.is_positive: str_ += '!'
        if self.is_attr: str_ += '@'
        if self.name == '': str_ += '""'
        else: str_ += self.name
        if self.is_lookup_only: str_ += '?'
        if self.keys != []: str_ += strings.urepr(self.keys)
        if self.attrs != []: str_ += ':'+strings.urepr(self.attrs)
        if self.is_ptr: str_ += '->'
        if self.other_branches != []:
            str_ += '{'+strings.urepr(self.other_branches)+'}'
        if self.value != None: str_ += '='+strings.urepr(self.value)
        return str_
    
    def __eq__(self, other): return self.__dict__ == other.__dict__

empty_elem = XpathElem()

def elem_is_empty(elem): return elem == empty_elem

def is_self(elem): return elem.name == '' or elem.name == '.'

def is_parent(elem): return elem.name == '..'

def is_all(elem): return elem.name == '*'

##### Paths

def is_xpath(value):
    return isinstance(value, list) and (len(value) == 0
        or isinstance(value[0], XpathElem))

def path_is_empty(path): return path == [] or path == [empty_elem]

def is_positive(path): return path[0].is_positive

def is_rooted(path): return elem_is_empty(path[0])

def value(path): return path[-1].value

def set_value(path, value):
    '''Caller must make a shallow copy of the path to prevent modifications from
    propagating to other copies of the path (a deep copy is not needed)'''
    path[-1] = copy.copy(path[-1]) # don't modify other copies of the path
    path[-1].value = value

def append(path, subpath, i=0):
    '''Recursively appends subpath to every leaf of a path tree'''
    if i >= len(path): path += subpath
    else:
        append(path, subpath, i+1)
        for branch in path[i].other_branches: append(branch, subpath)

def backward_id(elem):
    if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
    else: return None

instance_level = 1 # root's grandchildren

def obj(path):
    obj_path = copy.deepcopy(path[:instance_level+1])
    obj_path[-1].is_ptr = False # prevent pointer w/o target
    return obj_path

def set_id(path, id_, has_types=True):
    '''Caller must make a shallow copy of the path to prevent modifications from
    propagating to other copies of the path (a deep copy is not needed).
    @return The path to the ID attr, which can be used to change the ID
    '''
    if has_types: id_level = instance_level
    else: id_level = 0 # root's children
    if is_self(path[0]): id_level += 1 # explicit root element
    
    id_elem = path[id_level] = copy.copy(path[id_level])
        # don't modify other copies of the path
    id_elem.keys = id_elem.keys[:] # don't modify other copies of the elem
    id_attr = XpathElem('id', None, is_attr=True)
    
    # Save the path to the ID attr
    id_path = obj(path) # a copy of the path up through the ID level
    id_path.append(copy.copy(id_attr)) # this path's id_attr is independent
    
    # Set the ID attr on the provided XPath
    id_attr.value = id_
    id_elem.keys.append([id_attr])
    
    return id_path

def is_id(path): return path[0].is_attr and path[0].name == 'id'

def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0])

##### Parsing

import xpath_func

def expand_abbr(name, repl):
    before, abbr, after = name.partition('*')
    if abbr != '': name = before+repl+after
    return name

_cache = {}

def parse(str_):
    try: return _cache[str_]
    except KeyError: pass
    
    parser = Parser(str_)
    
    def _path():
        is_positive = not parser.str_('!')
        
        tree = []
        while True:
            # Split path
            if parser.str_('{'):
                try: last = tree[-1]
                except IndexError: parser.syntax_err('./{')
                last.other_branches = _paths()
                tree += last.other_branches.pop(0) # use first path for now
                parser.str_('}', required=True)
                if parser.str_('/'): append(tree, _path()) # subpath after {}
                break # anything after split path has already been parsed
            
            elem = XpathElem(is_attr=parser.str_('@'), name=_value())
            
            elem.is_lookup_only = parser.str_('?')
            
            # Keys used to match nodes
            if parser.str_('['):
                elem.keys = _paths()
                parser.str_(']', required=True)
            
            # Attrs created when no matching node exists
            if parser.str_(':'):
                parser.str_('[', required=True)
                elem.attrs = _paths()
                parser.str_(']', required=True)
            
            elem.is_ptr = parser.str_('->')
            tree.append(elem)
            
            # Lookahead assertion
            if parser.str_('('):
                parser.str_('/', required=True) # next / is inside ()
                path = _path()
                parser.str_(')', required=True)
                elem.keys.append(path)
                tree += path
            
            if not parser.str_('/'): break
        
        tree[0].is_positive = is_positive
        
        # Value
        if parser.str_('='):
            if parser.str_('$'): # reference (different from a pointer)
                value = _path()
            else: value = _value() # literal value
            set_value(tree, value)
        
        # Expand * abbrs
        for i in reversed(xrange(len(tree))):
            elem = tree[i]
            if elem.is_ptr: offset = 2
            else: offset = 1
            try: repl = tree[i+offset].name
            except IndexError: pass # no replacement elem
            else: elem.name = expand_abbr(elem.name, repl)
        
        return tree
    
    def _value():
        if parser.str_('"'):
            value = parser.re(r'[^"]*')
            parser.str_('"', required=True)
        else: value = parser.re(r'(?:(?:\w+:)*[\w.+*]+)?')
        return value
    
    def _paths():
        paths = []
        path = _path()
        if not path_is_empty(path): # not empty list
            paths.append(path)
            while parser.str_(','): paths.append(_path())
            if path_is_empty(paths[-1]): paths.pop() # remove trailing ","
        return paths
    
    path = _path()
    parser.end()
    path = xpath_func.process(path)
    _cache[str_] = path
    return path

##### Querying/creating XML trees

def get(root, xpath, create=False, last_only=None, limit=1, allow_rooted=True):
    '''Warning: The last_only optimization may put data that should be together
    into separate nodes'''
    if last_only == None: last_only = create
    if limit == None or limit > 1: last_only = False
    if util.is_str(xpath): xpath = parse(xpath)
    
    # Handle edge cases
    if xpath == []: return [root]
    if create and not is_positive(xpath): return []
    
    # Define vars
    doc = root.ownerDocument
    if allow_rooted and is_rooted(xpath): root = doc.documentElement
    elem = xpath[0]
    
    # Find possible matches
    children = []
    if elem.is_attr:
        child = root.getAttributeNode(elem.name)
        if child != None: children = [child]
    elif is_self(elem): children = [root]
    elif is_parent(elem):
        parent = xml_dom.parent(root)
        if parent == None: return [] # don't try to create doc root's parent
        root = parent
        children = [root]
    elif is_all(elem): children = list(xml_dom.NodeElemIter(root))
    else:
        children = xml_dom.by_tag_name(root, elem.name,
            last_only=(last_only and (elem.keys == [] or is_instance(elem))),
            ignore_namespace=False) # later set ignore_namespace to True?
    
    # Retrieve elem value
    value_ = elem.value
    if util.is_list(value_): # reference (different from a pointer)
        target = get_1(root, value_)
        if target != None: value_ = xml_dom.value(target)
        else:
            warnings.warn(UserWarning('XPath reference target missing: '
                +strings.ustr(value_)+'\nXPath: '+strings.ustr(xpath)))
            value_ = None
    
    # Check each match
    nodes = []
    for child in children:
        is_match = value_ == None or xml_dom.value(child) == value_
        for attr in elem.keys:
            if not is_match: break
            is_match = ((get(child, attr, False, last_only,
                allow_rooted=False) != []) == is_positive(attr))
        if is_match:
            nodes.append(child)
            if limit != None and len(nodes) >= limit: break
    
    # Create node
    if nodes == []:
        if not create or elem.is_lookup_only: return []
        if elem.is_attr:
            root.setAttribute(elem.name, '')
            node = root.getAttributeNode(elem.name)
        elif util.list_eq_is(children, [root]): node = root
        else: node = root.appendChild(doc.createElement(elem.name))
        if value_ != None: xml_dom.set_value(node, value_)
        nodes.append(node)
    
    path_value = value(xpath)
    xpath = xpath[1:] # rest of XPath
    
    next = []
    for node in nodes:
        # Create attrs
        if create:
            # Keys last so that any lookahead assertion's path will be created
            # last as it would have without the assertion
            for attr in elem.attrs + elem.keys:
                get(node, attr, create, last_only=False, allow_rooted=False)
        
        # Follow pointer
        if elem.is_ptr:
            root = doc.documentElement
            xpath = copy.deepcopy(xpath)
            id_path = backward_id(xpath[instance_level])
            if id_path != None: # backward (child-to-parent) pointer with ID key
                id_path[0].name = expand_abbr(id_path[0].name, node.tagName)
                set_value(id_path, xml_dom.get_id(node)) # modify xpath's id key
            else: # forward (parent-to-child) pointer
                id_ = xml_dom.value(node)
                obj_xpath = obj(xpath) # target object
                if id_ == None or get(root, obj_xpath, False, True) == []:
                    # no target or target keys don't match
                    if not create: continue
                    
                    # Use last target object's ID + 1
                    obj_xpath[-1].keys = [] # just get by tag name
                    last = get(root, obj_xpath, False, True)
                    if last != []: id_ = str(int(util.coalesce(
                        xml_dom.get_id(last[0]), 0)) + 1)
                    else: id_ = '0'
                    
                    # Will append if target keys didn't match.
                    # Use lookahead assertion to avoid this.
                    xml_dom.set_value(node, id_)
                else: last_only = False
                set_id(xpath, id_)
        else: root = node
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
        
        for branch in elem.other_branches:
            branch = branch[:] # don't modify input!
            set_value(branch, path_value)
            next += get(node, branch, create, last_only, limit,
                allow_rooted=False)
    
    return next

def get_1(*args, **kw_args):
    '''Runs get() and returns the first result'''
    return util.list_get(get(*args, **kw_args), 0)

def get_values(*args, **kw_args):
    '''Runs get() and returns the value node (first child) of each node'''
    return map(xml_dom.value_node, get(*args, **kw_args))

def get_value(*args, **kw_args):
    '''Runs get_1() and returns the value of any result node'''
    return util.do_ignore_none(xml_dom.value, get_1(*args, **kw_args))

def put_obj(root, xpath, id_, has_types, value=None):
    '''@return tuple(inserted_node, id_attr_node)'''
    if util.is_str(xpath): xpath = parse(xpath)
    
    xpath = xpath[:] # don't modify input!
    id_path = set_id(xpath, id_, has_types)
    if value != None: set_value(xpath, value)
    return (get_values(root, xpath, True), get_1(root, id_path))

def path2xml(xpath, first_branch=True):
    root = xml_dom.create_doc().documentElement
    get(root, xpath, True)
    return root.firstChild # skip to tree created inside root

def xml_set_id(root, id_): xml_dom.set_id(root.firstChild, id_)
