def test_scan(self):
     #doc1 for only one txt file. 
     doc1 = amara.parse(self.parser1.scan().xml())
     #doc2 for all other 14 types of files. It also included two types of cmd files.
     doc2 = amara.parse(self.parser2.scan().xml())
     
     # doc1's test verifies all the information the xml comment provides.
     self.assertEquals(doc1.commentLog.branchInfo.originator, "sanummel")
     self.assertEquals(doc1.commentLog.branchInfo.category, "")
     self.assertEquals(doc1.commentLog.branchInfo.since, "07-03-22")
     self.assertEquals(doc1.commentLog.branchInfo.file, os.environ['TEST_DATA'] + '/data/comments_test.txt')
     self.assertEquals(doc1.commentLog.branchInfo.error, "kkk")
     self.assertEquals(str(doc1.commentLog.branchInfo).strip(),"Add rofsfiles for usage in paged images")
     # s = (str(doc1.commentLog.xml()))
     # print s
     # doc2's test only verifies the main comment content.
     self.assertEquals(str(doc2.commentLog.branchInfo[0]).strip(), "We need TwistOpen and TwistClose to cause display to change between\n landscape and portrait, but SysAp is consuming the key events.  Try\n treating them as Flip events are handled already by SysAp.")
     self.assertEquals(str(doc2.commentLog.branchInfo[1]).strip(), "Testing if it's good~~~")
     self.assertEquals(str(doc2.commentLog.branchInfo[2]).strip(), "We need TwistOpen and TwistClose to cause display to change between\n landscape and portrait, but SysAp is consuming the key events.  Try\n treating them as Flip events are handled already by SysAp.")
     self.assertEquals(str(doc2.commentLog.branchInfo[3]).strip(), "puikko ME SCD DeSW: wk21 Flag fix")
     self.assertEquals(str(doc2.commentLog.branchInfo[4]).strip(), "Since Catalogs is not compiling at this point, and we are not building it, don't try to\n pull it into the rom.  Also, tfxserver is crashing, so don't build or pull it in either.")
     self.assertEquals(str(doc2.commentLog.branchInfo[5]).strip(), "Fix target export, which cause issue when cleanexport.")
     self.assertEquals(str(doc2.commentLog.branchInfo[6]).strip(), "Move command to makmake as EBS does not call abld build")
     self.assertEquals(str(doc2.commentLog.branchInfo[7]).strip(), "Activate PCFW for Screensaver")
     self.assertEquals(str(doc2.commentLog.branchInfo[8]).strip(), "Support SPP mechanism for flags support.")
     self.assertEquals(str(doc2.commentLog.branchInfo[9]).strip(), "Enabling all the HWRM light zones target for product")
     self.assertEquals(str(doc2.commentLog.branchInfo[10]).strip(), "Again, it is just a test")
     self.assertEquals(str(doc2.commentLog.branchInfo[11]).strip(), "Test info, so whatever~")
     self.assertEquals(str(doc2.commentLog.branchInfo[12]).strip(), "k")
     self.assertEquals(str(doc2.commentLog.branchInfo[13]).strip(), "")
Beispiel #2
0
def test_attr_setnode4():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1"/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    doc2 = parse('<a xmlns:ns="urn:bogus:x"/>')
    attr_x = doc.xml_first_child.xml_attributes.getnode(BOGUS_X_NS, u'x')
    doc2.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc2.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #3
0
def test_attr_setnode4():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1"/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    doc2 = parse('<a xmlns:ns="urn:bogus:x"/>')
    attr_x = doc.xml_first_child.xml_attributes.getnode(BOGUS_X_NS, u'x')
    doc2.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc2.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #4
0
def test_attr_setnode1():
    EXPECTED = """<a x="1"/>"""
    doc = parse('<a x="1"/>')
    doc2 = parse('<a/>')
    #x = doc.xml_first_child.xml_attributes[None, u'x']
    attr_x = doc.xml_first_child.xml_attributes.getnode(None, u'x')
    #doc.xml_first_child.xml_attributes[None, u'y'] = attr_x
    doc2.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc2.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #5
0
def test_attr_setnode1():
    EXPECTED = """<a x="1"/>"""
    doc = parse('<a x="1"/>')
    doc2 = parse('<a/>')
    #x = doc.xml_first_child.xml_attributes[None, u'x']
    attr_x = doc.xml_first_child.xml_attributes.getnode(None, u'x')
    #doc.xml_first_child.xml_attributes[None, u'y'] = attr_x
    doc2.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc2.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #6
0
def amara_parse(source, uri=None, rules=None, binderobj=None,
                prefixes=None):
    """Convenience function for parsing XML.
    
    Works just as amara.parse, except that if source is '-' then
    it reads from standard input.
    """
    if source == '-':
        return amara.parse(sys.stdin, uri=uri, rules=rules,
                           binderobj=binderobj, prefixes=prefixes)
    else:
        return amara.parse(source, uri=uri, rules=rules,
                           binderobj=binderobj, prefixes=prefixes)
def amara_parse(source, uri=None, rules=None, binderobj=None,
                prefixes=None):
    """Convenience function for parsing XML.
    
    Works just as amara.parse, except that if source is '-' then
    it reads from standard input.
    """
    if source == '-':
        return amara.parse(sys.stdin, uri=uri, rules=rules,
                           binderobj=binderobj, prefixes=prefixes)
    else:
        return amara.parse(source, uri=uri, rules=rules,
                           binderobj=binderobj, prefixes=prefixes)
Beispiel #8
0
 def _capture_icd_icfs(self):
     prep_xml_path = self.config['prep.xml']
     if prep_xml_path is not None and os.path.exists(prep_xml_path):
         prep_doc = amara.parse(open(prep_xml_path,'r'))
         if hasattr(prep_doc.prepSpec, u'source'):
             for source in prep_doc.prepSpec.source:
                 if hasattr(source, u'unzipicds'):
                     for  unzipicds in source.unzipicds:
                         if hasattr(unzipicds, u'location'):
                             for location in unzipicds.location:
                                 excludes = []
                                 excluded = False
                                 if hasattr(location, 'exclude'):
                                     for exclude in location.exclude:
                                         _logger.debug('Exclude added: %s' % str(exclude.name))
                                         excludes.append(str(exclude.name))                                
                                         excluded = False
                                 path = str(location.name)                                    
                                 if os.path.exists(path):
                                     files = os.listdir(str(location.name))
                                     for file_ in files:
                                         for exclude in excludes:
                                             if file_.endswith(exclude):
                                                 excluded = True
                                         if file_.endswith('.zip') and not excluded:
                                             self._icd_icfs.append(file_)
                                             self._icd_icfs.sort(key=str)
Beispiel #9
0
    def test_tree(self):
        #Node types use string rather than numerical constants now
        #The root node type is called entity
        doc = amara.parse(self.MONTY_XML)

        self.assertEqual(doc.xml_type, tree.entity.xml_type)
        m = doc.xml_children[0] #xml_children is a sequence of child nodes
        self.assertEqual(m.xml_local, u'monty') #local name, i.e. without any prefix
        self.assertEqual (m.xml_qname, u'monty') #qualified name, e.g. includes prefix
        self.assertEqual(m.xml_prefix, None)
        self.assertEqual(m.xml_qname, u'monty') #qualified name, e.g. includes prefix
        self.assertEqual(m.xml_namespace, None)
        self.assertEqual(m.xml_name,  (None, u'monty')) #The "universal name" or "expanded name"
        self.assertEqual(m.xml_parent, doc)
        
        p1 = m.xml_children[1]
        XML_output = '<python spam="eggs">What do you mean "bleh"</python>'
        output = cStringIO.StringIO()
        xml_print(p1, stream=output)
        self.assertEqual(output.getvalue(), XML_output)
        
        self.assertEqual(p1.xml_attributes[(None, u'spam')], u"eggs")
        #Some manipulation
        p1.xml_attributes[(None, u'spam')] = u"greeneggs"
        self.assertEqual(p1.xml_attributes[(None, u'spam')], u"greeneggs")
        p1.xml_children[0].xml_value = u"Close to the edit"
        output = cStringIO.StringIO()
        xml_print(p1, stream=output)
        self.assertEqual(output.getvalue(),
                         '<python spam="greeneggs">Close to the edit</python>')
Beispiel #10
0
 def test_ids(self):
     DOC = '<!DOCTYPE xsa PUBLIC "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML" "http://www.garshol.priv.no/download/xsa/xsa.dtd">\n<xsa/>'
     doc = amara.parse(DOC, standalone=True)
     self.assertEqual(doc.xml_system_id, 
                      u'http://www.garshol.priv.no/download/xsa/xsa.dtd')
     self.assertEqual(doc.xml_public_id, 
                      u'-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML')        
Beispiel #11
0
def test_simple_attr_update3():
    import amara
    doc = amara.parse(PLACES)
    ns = doc.xml_select(
        u'/places/place[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(
        u'/places/*[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(
        u'/*/place[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(u'/*/*[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(
        u'places/place[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(
        u'places/*[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(
        u'*/place[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    ns = doc.xml_select(u'*/*[@name="Santiago, Regi\xf3n Metro De Santiago"]')
    assert len(ns) == 1, (len(ns), 1)
    return
Beispiel #12
0
def srewrite(source, **kwargs):
    doc = amara.parse(source)

    def handle_node(node, indent=0):
        empty = True
        if isinstance(node, tree.text):
            yield repr(node.xml_value)
        elif isinstance(node, tree.entity):
            yield INDENT_STR * indent + 'ROOT('
            for child in node.xml_children:
                empty = False
                yield '\n'
                for chunk in handle_node(child, indent + 1):
                    yield chunk
            yield (not empty) * INDENT_STR * indent + ')\n'
        elif isinstance(node, tree.element):
            yield INDENT_STR * indent + 'E('
            yield repr((node.xml_namespace,
                        node.xml_local)) if node.xml_namespace else repr(
                            node.xml_local)
            if node.xml_attributes:
                yield repr(dict(node.xml_attributes))
            for child in node.xml_children:
                empty = False
                yield '\n'
                for chunk in handle_node(child, indent + 1):
                    yield chunk
            yield (not empty) * INDENT_STR * indent + ')\n'

    for chunk in handle_node(doc):
        yield chunk
    return
Beispiel #13
0
 def test_nested_doc(self):
     'Input doc with text in nested elements'
     x = amara.parse('<a>one two <b>three four </b><c>five <d>six seven</d> eight</c> nine</a>')
     for i in range(1, 11):
         trimmed_tree = util.trim_word_count(x, i)
         word_count = len(trimmed_tree.xml_select(u'string(.)').split())
         self.assertEquals(word_count, min(i, 9))
Beispiel #14
0
 def test_flat_doc(self):
     'Input doc with just top-level text'
     x = amara.parse('<a>one two three four five six seven eight nine</a>')
     for i in range(1, 11):
         trimmed_tree = util.trim_word_count(x, i)
         word_count = len(trimmed_tree.xml_select(u'string(.)').split())
         self.assertEquals(word_count, min(i, 9))
Beispiel #15
0
 def test_flat_doc(self):
     'Input doc with just top-level text'
     x = amara.parse('<a>one two three four five six seven eight nine</a>')
     for i in range(1, 11):
         trimmed_tree = util.trim_word_count(x, i)
         word_count = len(trimmed_tree.xml_select(u'string(.)').split())
         self.assertEquals(word_count, min(i, 9))
    def test_bom_delta(self):
        """ Testing BOM delta creation... """
        delta = build.model.BOMDeltaXMLWriter(BOMMock(), os.path.join(os.environ['TEST_DATA'], 'data/bom/build_model_bom.xml'))
        (_, filename) = tempfile.mkstemp()
        delta.write(filename)
        xml = amara.parse(open(filename))
        self.assert_(xml.bomDelta[0].buildFrom[0] == "ido_raptor_mcl_abs_MCL.52.57")
        self.assert_(xml.bomDelta[0].buildTo[0] == "mock")

        print "baselines: ", len(xml.bomDelta.content.baseline)
        print "folders: ", len(xml.bomDelta.content.folder)
        print "tasks: ", len(xml.bomDelta.content.task)
        print "baseline[@overridden='false']:", len(xml.bomDelta.content.xml_xpath("baseline[@overridden='false']"))
        print "baseline[@overridden='true']: ", len(xml.bomDelta.content.xml_xpath("baseline[@overridden='true']"))
        print "folder[@status='deleted']: ", len(xml.bomDelta.content.xml_xpath("folder[@status='deleted']"))
        print "task[@status='deleted']: ", len(xml.bomDelta.content.xml_xpath("task[@status='deleted']"))

        self.assert_(len(xml.bomDelta.content.baseline) == 156)
        self.assert_(len(xml.bomDelta.content.folder) == 1)
        self.assert_(len(xml.bomDelta.content.task) == 1)
        self.assert_(len(xml.bomDelta.content.xml_xpath("baseline[@overridden='false']")) == 155)
        self.assert_(len(xml.bomDelta.content.xml_xpath("baseline[@overridden='true']")) == 1)

        self.assert_(len(xml.bomDelta.content.xml_xpath("folder[@status='deleted']")) == 1)

        self.assert_(len(xml.bomDelta.content.xml_xpath("task[@status='deleted']")) == 1)
Beispiel #17
0
def simple_xpath_index(self, content, patterns):
    results = {}
    doc = content if isinstance(content, node) else amara.parse(source)
    for pname, xpath in patterns.iteritems():
        result = doc.xml_select(pattern)
        results[pname] = simplify(result)
    return results
Beispiel #18
0
def test_templates():
    url = server()
    f = urlopen(url)
    tree = amara.parse(f)
    nodes = tree.xml_select("//service[@ident='urn:akara.test:template-1']")
    template = (tree.xml_select(
        "//service[@ident='urn:akara.test:template-1']/path/@template")
                [0].xml_value)
    assert template.endswith(
        "/test.template1?name={name}&language={lang?}"), template

    template = (tree.xml_select(
        "//service[@ident='urn:akara.test:template-2']/path/@template")
                [0].xml_value)
    assert template.endswith(
        "/test.template2?language={language?}&name={name}&os={os?}"), template

    nodes = (tree.xml_select(
        "//service[@ident='urn:akara.test:template-3']/path/@template"))
    assert len(nodes) == 0, nodes

    template = (tree.xml_select(
        "//service[@ident='urn:akara.test:template-4']/path/@template")
                [0].xml_value)
    assert template.endswith("/test.template4"), template
Beispiel #19
0
def srewrite(source, **kwargs):
    doc = amara.parse(source)
    def handle_node(node, indent=0):
        empty = True
        if isinstance(node, tree.text):
            yield repr(node.xml_value)
        elif isinstance(node, tree.entity):
            yield  INDENT_STR*indent + 'ROOT('
            for child in node.xml_children:
                empty = False
                yield '\n'
                for chunk in handle_node(child, indent+1):
                    yield chunk
            yield (not empty)*INDENT_STR*indent + ')\n'
        elif isinstance(node, tree.element):
            yield  INDENT_STR*indent + 'E('
            yield repr((node.xml_namespace, node.xml_local)) if node.xml_namespace else repr(node.xml_local)
            if node.xml_attributes:
                yield repr(dict(node.xml_attributes))
            for child in node.xml_children:
                empty = False
                yield '\n'
                for chunk in handle_node(child, indent+1):
                    yield chunk
            yield (not empty)*INDENT_STR*indent + ')\n'
    for chunk in handle_node(doc): yield chunk
    return
Beispiel #20
0
    def test_iterator(self):
        doc = amara.parse(self.XML)
        output = cStringIO.StringIO()
        XML_groupby="""\
<?xml version="1.0" encoding="utf-8"?>
<env>
  <a-wrapper>
    <a id="1"/>
    <a id="2"/>
    <a id="3"/>
  </a-wrapper>
  <b-wrapper>
    <b id="1.1"/>
    <b id="2.1"/>
    <b id="3.1"/>
  </b-wrapper>
  <c-wrapper>
    <c id="1.2"/>
    <c id="2.2"/>
    <c id="3.2"/>
  </c-wrapper>
</env>"""
        leaves = sorted(doc.xml_select(u'/env/*'), key=operator.attrgetter('xml_name'))
        
        w = structwriter(indent=u"yes", stream=output)
        w.feed(
            ROOT(
                E(u'env',
                  ( E(ename + u'-wrapper',
                      ( E(ename, e.xml_attributes.copy(), e.xml_children) for e in elems )
                      ) for ename, elems in itertools.groupby(leaves, lambda x: x.xml_qname) ),
                  )
            ))
        self.assertEqual(output.getvalue(), XML_groupby)
 def __init__(self, modelpath):
     """ Initialization. """
     doc = amara.parse(open(modelpath, 'r'))
     
     self.properties = {}
     self.required_props = []
     for property_ in doc.heliumDataModel.property:
         self.properties[str(property_.name)] = PropertyDef(property_)
                
     self.nongroupedproperties = copy.copy(self.properties)
     self.groups = {}
     for group in doc.heliumDataModel.group:
         groupobj = GroupDef(group)
         self.groups[str(group.name)] = groupobj
         
         for prop in groupobj.properties:
             groupobj.propref.append(self.properties[prop])
             if prop in self.nongroupedproperties:
                 del self.nongroupedproperties[prop]
                 
         groupobj.propref.sort()
         
         required_props_in_group = [p for p in group.propertyRef if p.usage == 'required']
         
         for required_prop in required_props_in_group:
             self.required_props.append(required_prop)
             if not self.properties.has_key(str(required_prop)):
                 raise Exception("Required property " + str(required_prop) + " is not defined!")
Beispiel #22
0
 def test_chardata_no_longer(self):
     doc = amara.parse('<A>Spam</A><!--eggs-->')
     A = doc.xml_children[0]
     spam = A.xml_children[0]
     for old_method in ['length', 'substringData', 'insertData', 'replaceData',
                        'appendData', 'deleteData']:
         self.assertRaises(AttributeError, getattr, spam, "%s" % old_method)
Beispiel #23
0
 def test_attr_no_longer(self):
     doc = amara.parse('<A a="b"/>')
     A = doc.xml_children[0]
     attr_a = A.xml_attributes.getnode(None, u'a')
     for old_method in ['nodeName', 'nodeValue', 'qualifiedName', 'value', 'namespaceURI', 
                        'localName', 'prefix', 'specified']:
         self.assertRaises(AttributeError, getattr, attr_a, "%s" % old_method)
Beispiel #24
0
 def test_auto_xml_namespace(self):
     '''Automatic recognition of XML namespace'''
     doc = amara.parse('<a/>')
     self.assertEqual(doc.xml_first_child.xml_namespaces[u'xml'], XML_NAMESPACE)
     doc.xml_first_child.xml_attributes[XML_NAMESPACE, u'xml:base'] = u'urn:bogus'
     self.assertEqual(doc.xml_first_child.xml_attributes[XML_NAMESPACE, u'xml:base'], u'urn:bogus')
     return
    def process_file(self, file):
        """Process a WXP file. Store results in our parser objects"""

        doc = amara.parse(file)

        items = []
        for item in doc.xml_xpath('*//item'):

            get_node = lambda x: item.xml_xpath(x).pop()

            info = _to_unicode({
                'id': get_node('wp:post_id'),
                'status': get_node('wp:status'),
                'slug': get_node('wp:post_name'),
                'title': item.title,
                'type': get_node('wp:post_type'),
                'link': item.link,
                'creator': get_node('dc:creator'),
                'pub_date': item.pubDate,
                'guid': item.guid,
                'content': get_node('content:encoded')})

            info['pub_date'] = self._parse_date(info['pub_date'])
            info['id'] = int(info['id'])

            cat_tags = item.xml_xpath('category')
            info['categories'] = self._get_categories(cat_tags)
            info['tags'] = self._get_tags(cat_tags)

            info['comments'] = self._get_comments(item)

            self.items.append(info)
Beispiel #26
0
def find_python_dependencies(setpath, dbPath, dbPrj):
    """ Search python dependencies """
    for root, dirs, files in os.walk(setpath, topdown=False):
        for fname in files:
            filePattern = re.compile('.ant.xml$')
            fileMatch = filePattern.search(fname)
            modulelist = []
            if (fileMatch):
                filePath = os.path.abspath(os.path.join(root, fname))
                with open(filePath) as f:
                    filePathAmara = 'file:///'+ filePath.replace('\\','/')
                    curPrj=amara.parse(filePathAmara)
                    for line in f:
                        linePattern = re.compile('^import')
                        lineMatch = linePattern.search(line)
                        if ((lineMatch) and (line.find('.')==-1)):
                            newLine = line.replace('import','')
                            newLine = newLine.replace(',','')
                            moduleArray = newLine.split()
                            for curModule in moduleArray:
                                try:
                                    importModule = __import__(curModule)
                                    if hasattr(importModule, '__file__'):
                                        modulePath=importModule.__file__
                                        if 'helium' in modulePath:
                                            for projectList in dbPrj.antDatabase.project:
                                                if (projectList.name == curPrj.project.name):
                                                    if not (curModule in modulelist):
                                                        print " Python module : " + curModule
                                                        moduleElement = projectList.pythonDependency.xml_create_element(u'module', content=u''+curModule)
                                                        projectList.pythonDependency.xml_append(moduleElement)
                                                    modulelist = modulelist + [curModule]
                                except ImportError, e:
                                    pass
def test_last_function_low_level():
    '''
    '''
    doc = amara.parse(XML)
    last_url = doc.xml_select(u'/listitem/para/ulink[position() = last()]/@url')
    EXPECTED = nodeset([u'http://www.ncbi.nlm.nih.gov/pubmed/16785838'])
    assert last_url == EXPECTED, (EXPECTED, last_url)
Beispiel #28
0
def simple_xpath_index(self, content, patterns):
    results = {}
    doc = content if isinstance(content, node) else amara.parse(source)
    for pname, xpath in patterns.iteritems():
        result = doc.xml_select(pattern)
        results[pname] = simplify(result)
    return results
Beispiel #29
0
 def test_no_longer(self):
     doc = amara.parse('<A/>')
     A = doc.xml_children[0]
     for old_method in ['nodeName', 'nodeValue', 'namespaceURI', 'localName', 
                        'prefix', 'childNodes', 'isSameNode', 'xpath', 'rootNode',
                        'baseURI', 'xmlBase', 'documentURI']:
         self.assertRaises(AttributeError, getattr, A, "%s" % old_method)
Beispiel #30
0
 def test_parse_with_file_path(self):
     """Parse with file path"""
     fname = tempfile.mktemp('.xml')
     fout = open(fname, 'w')
     fout.write(MONTY_XML)
     fout.close()
     doc = parse(fname)
     self.run_checks(doc)
Beispiel #31
0
 def Xtest_parse_with_url(self):
     doc = parse(TEST_URL)
     #Minimal node testing
     self.assertEqual(len(doc.xml_children), 1)
     self.assertEqual(doc.xml_children[0].xml_type, tree.element.xml_type)
     self.assertEqual(doc.xml_children[0].xml_qname, 'disclaimer')
     self.assertEqual(doc.xml_children[0].xml_namespace, None)
     self.assertEqual(doc.xml_children[0].xml_prefix, None)
Beispiel #32
0
 def test_parse_with_file_path(self):
     """Parse with file path"""
     fname = tempfile.mktemp('.xml')
     fout = open(fname, 'w')
     fout.write(MONTY_XML)
     fout.close()
     doc = parse(fname)
     self.run_checks(doc)
Beispiel #33
0
 def test_ids(self):
     DOC = '<!DOCTYPE xsa PUBLIC "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML" "http://www.garshol.priv.no/download/xsa/xsa.dtd">\n<xsa/>'
     doc = amara.parse(DOC, standalone=True)
     self.assertEqual(doc.xml_system_id,
                      u'http://www.garshol.priv.no/download/xsa/xsa.dtd')
     self.assertEqual(
         doc.xml_public_id,
         u'-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML')
Beispiel #34
0
def test_last_function_low_level():
    '''
    '''
    doc = amara.parse(XML)
    last_url = doc.xml_select(
        u'/listitem/para/ulink[position() = last()]/@url')
    EXPECTED = nodeset([u'http://www.ncbi.nlm.nih.gov/pubmed/16785838'])
    assert last_url == EXPECTED, (EXPECTED, last_url)
Beispiel #35
0
def _register_services(uri):
    new_templates = {}
    tree = amara.parse(uri)
    for path in tree.xml_select("//service[@ident]/path[@template]"):
        ident = path.xml_parent.xml_attributes["ident"]
        template = path.xml_attributes["template"]
        new_templates[ident] = opensearch.make_template(template)
    return new_templates
Beispiel #36
0
 def Xtest_parse_with_url(self):
     doc = parse(TEST_URL)
     #Minimal node testing
     self.assertEqual(len(doc.xml_children), 1)
     self.assertEqual(doc.xml_children[0].xml_type, tree.element.xml_type)
     self.assertEqual(doc.xml_children[0].xml_qname, 'disclaimer')
     self.assertEqual(doc.xml_children[0].xml_namespace, None)
     self.assertEqual(doc.xml_children[0].xml_prefix, None)
Beispiel #37
0
 def test_single_xpath(self):
     doc = amara.parse(XML)
     b_els = doc.xml_select(u"//b")
     self.assertEqual(len(b_els), 2)
     self.assertEqual(doc.xml_select(u"count(//b)"), 2)
     self.assertEqual(doc.xml_select(u"/a/@x")[0].xml_value , u'1')
     self.assertEqual(doc.xml_select(u'string(/a/b)'), u'i')
     return
Beispiel #38
0
    def _lookupCloudsXML(self):

        # Query the Redis DB for the complete, aggregated XML file
        cloudsXML = self.db.get(ConfigMapping[CLOUDS_KEY])
        # amDoc will contain a data structure that mirrors the public XML Schema, allowing for direct access via name.name2.name3 style
        amDoc = amara.parse(str(cloudsXML)) 

        return amDoc
Beispiel #39
0
def _register_services(uri):
    new_templates = {}
    tree = amara.parse(uri)
    for path in tree.xml_select("//service[@ident]/path[@template]"):
        ident = path.xml_parent.xml_attributes["ident"]
        template = path.xml_attributes["template"]
        new_templates[ident] = opensearch.make_template(template)
    return new_templates
Beispiel #40
0
 def test_root(self):
     doc = amara.parse('<A><B><C/></B></A>')
     C = doc.xml_select(u'//C').pop()
     self.assertEqual(C.xml_root, doc)
     mynode = C
     while mynode.xml_parent: 
         mynode = mynode.xml_parent
     self.assertEqual(mynode, doc)
Beispiel #41
0
 def test_single_xpath(self):
     doc = amara.parse(XML)
     b_els = doc.xml_select(u"//b")
     self.assertEqual(len(b_els), 2)
     self.assertEqual(doc.xml_select(u"count(//b)"), 2)
     self.assertEqual(doc.xml_select(u"/a/@x")[0].xml_value, u'1')
     self.assertEqual(doc.xml_select(u'string(/a/b)'), u'i')
     return
Beispiel #42
0
    def _lookupCloudsXML(self):

        # Query the Redis DB for the complete, aggregated XML file
        cloudsXML = self.db.get(ConfigMapping[CLOUDS_KEY])
        # amDoc will contain a data structure that mirrors the public XML Schema, allowing for direct access via name.name2.name3 style
        amDoc = amara.parse(str(cloudsXML))

        return amDoc
Beispiel #43
0
 def test_root(self):
     doc = amara.parse('<A><B><C/></B></A>')
     C = doc.xml_select(u'//C').pop()
     self.assertEqual(C.xml_root, doc)
     mynode = C
     while mynode.xml_parent:
         mynode = mynode.xml_parent
     self.assertEqual(mynode, doc)
Beispiel #44
0
 def test_chardata(self):
     doc = amara.parse('<A>Spam</A><!--eggs-->')
     A = doc.xml_children[0]
     spam = A.xml_children[0]
     self.assertEqual(spam.xml_value, u'Spam')
     self.assertEqual(len(spam.xml_value), 4)
     self.assertEqual(spam.xml_value[1:3], u'pa')
     spam.xml_value += u' eggs'
     self.assertEqual(spam.xml_value, u'Spam eggs')
Beispiel #45
0
def test_attr_setnode5():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1" ns:y="1" ns:z=""/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    attr_y = tree.attribute(BOGUS_X_NS, u'ns:y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(BOGUS_X_NS, u'ns:z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #46
0
 def test_no_longer(self):
     doc = amara.parse('<A/>')
     A = doc.xml_children[0]
     for old_method in [
             'nodeName', 'nodeValue', 'namespaceURI', 'localName', 'prefix',
             'childNodes', 'isSameNode', 'xpath', 'rootNode', 'baseURI',
             'xmlBase', 'documentURI'
     ]:
         self.assertRaises(AttributeError, getattr, A, "%s" % old_method)
Beispiel #47
0
 def test_chardata_no_longer(self):
     doc = amara.parse('<A>Spam</A><!--eggs-->')
     A = doc.xml_children[0]
     spam = A.xml_children[0]
     for old_method in [
             'length', 'substringData', 'insertData', 'replaceData',
             'appendData', 'deleteData'
     ]:
         self.assertRaises(AttributeError, getattr, spam, "%s" % old_method)
Beispiel #48
0
 def test_chardata(self):
     doc = amara.parse('<A>Spam</A><!--eggs-->')
     A = doc.xml_children[0]
     spam = A.xml_children[0]
     self.assertEqual(spam.xml_value, u'Spam')
     self.assertEqual(len(spam.xml_value), 4)
     self.assertEqual(spam.xml_value[1:3], u'pa')
     spam.xml_value += u' eggs'
     self.assertEqual(spam.xml_value, u'Spam eggs')
Beispiel #49
0
 def read_object_defs(self):
     self.logger.debug("==============================================================")
     objects = []
     for config in self.config_location:
         self.logger.debug("* Parsing %s" % config)
         doc = amara.parse(config)
         objects.extend([self._convert_component(component) for component in doc.components.component])
     self.logger.debug("==============================================================")
     return objects
Beispiel #50
0
 def test_nested_doc(self):
     'Input doc with text in nested elements'
     x = amara.parse(
         '<a>one two <b>three four </b><c>five <d>six seven</d> eight</c> nine</a>'
     )
     for i in range(1, 11):
         trimmed_tree = util.trim_word_count(x, i)
         word_count = len(trimmed_tree.xml_select(u'string(.)').split())
         self.assertEquals(word_count, min(i, 9))
Beispiel #51
0
	def parseServer(self):
		try:
			doc = amara.parse('file:%s/config.xml'%(C.YENCAP_MAN_CONF_HOME))
			self.host = ''
			self.port = int(str(doc.server.port))
			self.ip_version = str(doc.server.ip_version)
		except Exception, exp:
			print 'Check configuration file of the server : config.xml'
			print str(exp)
Beispiel #52
0
 def setUp(self):
     self.bigdoc1 = ["<A>"]
     self.bigdoc1.extend(["<B/>"]*(SCALE*100))
     self.bigdoc1.extend(["</A>"])
     self.bigdoc1 = ''.join(self.bigdoc1)
     #len(self.bigdoc1) is 400007 for SCALE = 1000
     self.doc = amara.parse(self.bigdoc1)
     gc.enable()
     return
Beispiel #53
0
def test_405_error_message():
    url = server()
    try:
        f = urlopen(url, "The server ignores this text")
        raise AssertionError("/ is not supposed to allow a POST")
    except urllib2.HTTPError, err:
        assert err.code == 405, err.code
        assert err.headers["Content-Type"] == "text/html", err.headers["Content-Type"]
        tree = amara.parse(err.fp, standalone=True)
Beispiel #54
0
def test_404_error_message():
    url = server() + "this_does_not_exist/I_mean_it/Anybody_want_a_peanut?"
    try:
        urlopen(url)
        raise AssertionError("that URL should not be present")
    except urllib2.HTTPError, err:
        assert err.code == 404
        assert err.headers["Content-Type"] == "text/html", err.headers["Content-Type"]
        tree = amara.parse(err.fp, standalone=True)
Beispiel #55
0
def test_attr_setnode5():
    EXPECTED = """<a xmlns:ns="urn:bogus:x" ns:x="1" ns:y="1" ns:z=""/>"""
    doc = parse('<a xmlns:ns="urn:bogus:x" ns:x="1"/>')
    attr_y = tree.attribute(BOGUS_X_NS, u'ns:y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(BOGUS_X_NS, u'ns:z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #56
0
def test_attr_setnode3():
    EXPECTED = """<a x="1"/>"""
    doc = parse('<a x="1"/>')
    #x = doc.xml_first_child.xml_attributes[None, u'x']
    attr_x = copy.copy(doc.xml_first_child.xml_attributes.getnode(None, u'x'))
    del doc.xml_first_child.xml_attributes[None, u'x']
    doc.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #57
0
def test_attr_setnode3():
    EXPECTED = """<a x="1"/>"""
    doc = parse('<a x="1"/>')
    #x = doc.xml_first_child.xml_attributes[None, u'x']
    attr_x = copy.copy(doc.xml_first_child.xml_attributes.getnode(None, u'x'))
    del doc.xml_first_child.xml_attributes[None, u'x']
    doc.xml_first_child.xml_attributes.setnode(attr_x)
    treecompare.check_xml(doc.xml_encode(), XMLDECL+EXPECTED)
    return
Beispiel #58
0
def test_attr_setnode2():
    EXPECTED = """<a x="1" y="1" z=""/>"""
    doc = parse('<a x="1"/>')
    attr_y = tree.attribute(None, u'y', u'1')
    doc.xml_first_child.xml_attributes.setnode(attr_y)
    attr_z = tree.attribute(None, u'z')
    doc.xml_first_child.xml_attributes.setnode(attr_z)
    treecompare.check_xml(doc.xml_encode(), XMLDECL + EXPECTED)
    return
Beispiel #59
0
    def readModules(self):
        moduleDescrList = []
        try:
            self.doc = amara.parse("file:" + C.YENCAP_CONF_HOME +
                                   "/modules.xml")

            for elem in self.doc.modules.module:

                yanguri = None

                try:
                    yanguri = str(elem.yanguri)
                except Exception, exp:
                    pass

                if yanguri != None:
                    i_nom = yanguri.find('?')
                    if i_nom != -1:
                        i_rev = yanguri.find('&')
                        if i_rev == -1:
                            i_rev = len(yanguri)

                        moduleDefName = yanguri[i_nom + 1:i_rev]
                        i_equal = moduleDefName.find('=')

                        name = moduleDefName[i_equal + 1:len(moduleDefName)]
                        xpath = '/'
                        pref = ''

                        namespace = yanguri[0:i_nom]
                    else:
                        raise Exception, "yang uri not conform : " + yanguri
                else:
                    name = str(elem.name)
                    xpath = str(elem.xpath)
                    namespace = str(elem.namespace)
                    pref = str(elem.namespace.pref)

                cachelifetime = int(str(elem.cachelifetime))
                parameters = {}
                try:
                    for param in elem.parameters.parameter:
                        parameters[param.name] = str(param.value)
                except Exception, exp:
                    pass

                moduleDescr = {
                    "yanguri": yanguri,
                    "name": name,
                    "xpath": xpath,
                    "namespace": namespace,
                    "pref": pref,
                    "cachelifetime": cachelifetime,
                    "parameters": parameters
                }
                moduleDescrList.append(moduleDescr)
Beispiel #60
0
def launch(*args, **kwargs):
    import pprint
    from amara.xpath.util import simplify
    source = args[0]
    expr = args[1]
    import amara
    doc = amara.parse(source)
    result = doc.xml_select(expr.decode('utf-8'))
    pprint.pprint(simplify(result))
    return