orange / Orange / orng / fileutil.py

import xml.dom.minidom, re

def _zip_open(zipfile, file, mode='r'):
    if hasattr(zipfile, "open"):
        return zipfile.open(file, mode)
    else:
        from cStringIO import StringIO
        return StringIO(zipfile.read(file))

def create_text_element(tag_name, value):
    result = xml.dom.minidom.Element(tag_name)
    textNode = xml.dom.minidom.Text()
    textNode.data = value
    result.appendChild(textNode)
    return result

def xml_set(parent, node_name, value):
    child = get_element_nonrecursive(parent, node_name)
    if not child:
        if value:
            parent.appendChild(create_text_element(node_name, value))
    else:
        if not value:
            parent.removeChild(child)
        else:
            for text in child.childNodes:
                child.removeChild(text)
            textNode = xml.dom.minidom.Text()
            textNode.data = value
            child.appendChild(textNode)

def xml_text_of(node, parent=None, multiline=False):
    if node.__class__ is str:
        node = get_element_nonrecursive(parent, node)
    t = ""
    if node==None:
        return t
    for n in node.childNodes:
        if n.nodeType == n.COMMENT_NODE:
            continue
        if n.nodeType != n.TEXT_NODE:
            break
        t += n.data
    if multiline:
        t = "\n\n".join( map(lambda x: x.strip(), re.split("\n[ \t]*\n", t.strip())) )
    else:
        t = re.sub("\s+", " ", t.strip())
    return t

def get_element_nonrecursive(parent, elementname, create=False):
    for node in [n for n in parent.childNodes if n.nodeType==n.ELEMENT_NODE]:
        if node.tagName == elementname:
            return node
    if create:
        node = xml.dom.minidom.Element(elementname)
        parent.appendChild(node)
        return node
    return None
    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.