# XPath parsing

import copy
import warnings

from Parser import Parser
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_ += repr(self.keys)
        if self.attrs != []: str_ += ':'+repr(self.attrs)
        if self.is_ptr: str_ += '->'
        if self.other_branches != []: str_ += '{'+repr(self.other_branches)+'}'
        if self.value != None: str_ += '='+repr(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 == '..'

##### Paths

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): path[-1].value = value

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):
    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
    path[id_level].keys.append([XpathElem('id', id_, True)])

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

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_('{'):
                last = tree[-1]
                last.other_branches = _paths()
                parser.str_('}', required=True)
                if parser.str_('/'): # common subpath after {}
                    subpath = _path()
                    for branch in last.other_branches: branch += subpath
                tree += last.other_branches.pop(0) # use first path for now
                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 = []
        while True:
            paths.append(_path())
            if not parser.str_(','): break
        return paths
    
    path = _path()
    parser.end()
    _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]
    else:
        children = xml_dom.by_tag_name(root, elem.name,
            last_only and (elem.keys == [] or is_instance(elem)))
    
    # Retrieve elem value
    value_ = elem.value
    if util.is_list(value_): # reference (different from a pointer)
        targets = get(root, value_)
        try: target = targets[0]
        except IndexError:
            warnings.warn(UserWarning('XPath reference target missing: '
                +str(value_)+'\nXPath: '+str(xpath)))
            value_ = None
        else: value_ = xml_dom.value(target)
    
    # 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:
            for attr in elem.keys + elem.attrs:
                get(node, attr, create, last_only, 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(xml_dom.get_id(last[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 = copy.deepcopy(branch)
            set_value(branch, path_value)
            next += get(node, branch, create, last_only, limit,
                allow_rooted=False)
    
    return next

def put_obj(root, xpath, id_, has_types, value=None):
    if util.is_str(xpath): xpath = parse(xpath)
    
    xpath = copy.deepcopy(xpath) # don't modify input!
    set_id(xpath, id_, has_types)
    if value != None: set_value(xpath, value)
    get(root, xpath, True)

def path2xml(xpath, first_branch=True):
    root = xml_dom.create_doc().documentElement
    get(root, xpath, True)
    return root

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