Example #1
0
    def test_postXMLEndpoint(self):
        testURL = "https://api.eveonline.com/server/ServerStatus.xml.aspx"

        res_cache = api.postXMLEndpoint(testURL)
        self.assertIsInstance(res_cache, ET.Element)
        # Test cached response
        self.assertEqual(ET.tostring(api.postXMLEndpoint(testURL)), ET.tostring(res_cache))
Example #2
0
def set_owner(conn, uuid, owner):
    """Set ON_OWNER by uuid.
    @param uuid: uuid of the VM
    @param owner: string representing owner
    """
    vmid = get_id_by_uuid(conn, uuid)

    if not vmid:
        return

    vm = conn.lookupByID(vmid)
    domain = ET.fromstring(vm.XMLDesc(0))
    metadata = domain.find('./metadata') or ET.SubElement(domain, 'metadata')
    owner_e = ET.SubElement(metadata, ET.QName('urn:opennode-tui', 'owner'))
    owner_e.text = owner

    ## TODO: cleanup
    open('/etc/libvirt/qemu/%s.xml' % (vm.name()), 'w').write(ET.tostring(domain))

    data = open('/etc/libvirt/qemu/%s.xml' % (vm.name()), 'r').read()
    domain_n = ET.fromstring(data)
    owner_e = domain_n.find('./metadata/{urn:opennode-tui}owner')
    assert owner_e is not None
    assert owner_e.text == owner
    return owner_e.text
    def test_affiliate_with_supported_seats(self):
        """ Verify that endpoint returns course runs for verified and professional seats only. """
        response = self.client.get(self.affiliate_url)
        self.assertEqual(response.status_code, 200)
        root = ET.fromstring(response.content)
        self.assertEqual(1, len(root.findall('product')))
        self.assert_product_xml(
            root.findall('product/[pid="{}-{}"]'.format(self.course_run.key, self.seat_verified.type))[0],
            self.seat_verified
        )

        # Add professional seat.
        seat_professional = SeatFactory(course_run=self.course_run, type=Seat.PROFESSIONAL)

        response = self.client.get(self.affiliate_url)
        root = ET.fromstring(response.content)
        self.assertEqual(2, len(root.findall('product')))

        self.assert_product_xml(
            root.findall('product/[pid="{}-{}"]'.format(self.course_run.key, self.seat_verified.type))[0],
            self.seat_verified
        )
        self.assert_product_xml(
            root.findall('product/[pid="{}-{}"]'.format(self.course_run.key, seat_professional.type))[0],
            seat_professional
        )
Example #4
0
    def fix_graphml_format_better(cls, name, gridfs):
        cls._logger.info("Reformatting file to fix GDO graphml formatting using element tree")
        replacements = {}
        with gridfs.get_last_version(name) as f:
            tree = ElementTree.parse(f)
            f_id = f._id
            for key_entry in tree.findall("{http://graphml.graphdrawing.org/xmlns}key"):
                key_name = key_entry.attrib['attr.name']
                id = key_entry.attrib['id']
                key_entry.attrib['id'] = key_name
                replacements[id] = key_name
                # cls._logger.info("Name: %s, Key %s", key_name, key_entry)
            # cls._logger.info("Dict %s", replacements)
            root=tree.getroot()
            for data_entry in root.iter("{http://graphml.graphdrawing.org/xmlns}data"):
                found_key = data_entry.attrib['key']
                data_entry.set('key', replacements[found_key])

            ElementTree.register_namespace('', "http://graphml.graphdrawing.org/xmlns")

        with gridfs.new_file(filename=name, content_type="text/xml") as des:
            tree.write(des, encoding='utf-8', method='xml')

        cls._logger.info("Deleting old version of file %s", f_id)
        gridfs.delete(f_id)
 def callback(results):
     reactor.stop()
     for success, result in results:
         if success:
             print ElementTree.dump(result)
         else:
             print result.printTraceback()
Example #6
0
def parse_soap_enveloped_saml_thingy(text, expected_tags):
    """Parses a SOAP enveloped SAML thing and returns the thing as
    a string.
    
    :param text: The SOAP object as XML string
    :param expected_tags: What the tag of the SAML thingy is expected to be.
    :return: SAML thingy as a string
    """
    envelope = ElementTree.fromstring(text)

    # Make sure it's a SOAP message
    assert envelope.tag == '{%s}Envelope' % soapenv.NAMESPACE
    
    assert len(envelope) >= 1
    body = None
    for part in envelope:
        if part.tag == '{%s}Body' % soapenv.NAMESPACE:
            assert len(part) == 1
            body = part
            break

    if body is None:
        return ""
    
    saml_part = body[0]
    if saml_part.tag in expected_tags:
        return ElementTree.tostring(saml_part, encoding="UTF-8")
    else:
        raise WrongMessageType("Was '%s' expected one of %s" % (saml_part.tag,
                                                                expected_tags))
Example #7
0
def add_to_manifest(repositories, fallback_branch = None):
    try:
        lm = ElementTree.parse(".repo/local_manifests/du_manifest.xml")
        lm = lm.getroot()
    except:
        lm = ElementTree.Element("manifest")

    for repository in repositories:
        repo_name = repository['repository']
        repo_target = repository['target_path']
        if exists_in_tree(lm, repo_name):
            print('DirtyUnicorns/%s already exists' % (repo_name))
            continue

        print('Adding dependency: DirtyUnicorns/%s -> %s' % (repo_name, repo_target))
        project = ElementTree.Element("project", attrib = { "path": repo_target,
            "remote": "du", "name": "%s" % repo_name })

        if 'branch' in repository:
            project.set('revision',repository['branch'])
        elif fallback_branch:
            print("Using fallback branch %s for %s" % (fallback_branch, repo_name))
            project.set('revision', fallback_branch)
        else:
            print("Using default branch for %s" % repo_name)

        lm.append(project)

    indent(lm, 0)
    raw_xml = ElementTree.tostring(lm).decode()
    raw_xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + raw_xml

    f = open('.repo/local_manifests/du_manifest.xml', 'w')
    f.write(raw_xml)
    f.close()
Example #8
0
 def multicopy():    
     try:
         tree = ET.parse('filecopylocations.xml')
         xroot = tree.getroot()
         print "\nWould you like to edit the following copy desitinations?\n"
         ET.dump(xroot)
         edit = raw_input("\ny=yes : n=no\n")
         if edit == 'y':
             initMulticopy()
         else:
             pass
     except:
         initMulticopy()
         tree = ET.parse('filecopylocations.xml')
         xroot = tree.getroot()
 
     print "\nPlease select the file you wish to have copied."
 
     try:
         
         fcpyfrom = getFileLocation()
         fcpyname = getFileNameFromFilePath(fcpyfrom)
         fcpyfrom = open(fcpyfrom, 'r').read()
         for loc in xroot.findall('Location'):
             f = open(loc.text + '/' + fcpyname, 'w')
             f.write(fcpyfrom)
             f.close()
         print "\nFile was successfully copied!"
     except:
         print "\nCould not copy file!"
         pass
    def test_get_xml(self):
        self.resModelProgram.metadata.create_element('Description', abstract="test abstract")
        self.resModelProgram.metadata.create_element('Subject', value="test subject")
        release_date = '2016-10-24T21:05:00.315907+00:00'
        self.resModelProgram.metadata.create_element('MpMetadata',
                                                     modelVersion='5.1.011',
                                                     modelProgramLanguage='Fortran',
                                                     modelOperatingSystem='Windows',
                                                     modelReleaseDate=release_date,
                                                     modelWebsite='http://www.hydroshare.org',
                                                     modelCodeRepository='http://www.github.com',
                                                     modelReleaseNotes='releaseNote.pdf',
                                                     modelDocumentation='manual.pdf',
                                                     modelSoftware='utilities.exe',
                                                     modelEngine='sourceCode.zip')

        # test if xml from get_xml() is well formed
        ET.fromstring(self.resModelProgram.metadata.get_xml())
        xml_doc = self.resModelProgram.metadata.get_xml()
        # check to see if the specific metadata are in the xml doc
        self.assertTrue('5.1.011' in xml_doc)
        self.assertTrue('Fortran' in xml_doc)
        self.assertTrue('Windows' in xml_doc)
        self.assertTrue(release_date in xml_doc)
        self.assertTrue('http://www.hydroshare.org' in xml_doc)
        self.assertTrue('http://www.github.com' in xml_doc)
        self.assertTrue('releaseNote.pdf' in xml_doc)
        self.assertTrue('manual.pdf' in xml_doc)
        self.assertTrue('utilities.exe' in xml_doc)
        self.assertTrue('sourceCode.zip' in xml_doc)
Example #10
0
    def run(self):
        """Runner"""
        service_path = rel_path(os.path.join("build",
            "Add to BibDesk.workflow", "Contents", "document.wflow"))
        app_path = rel_path(os.path.join("build", "ADS to BibDesk.app",
            "Contents", "document.wflow"))
        py_path = rel_path("adsbibdesk.py")

        for workflow in (service_path, app_path):
            with open(workflow, 'r') as fh:
                xml = ElementTree.fromstring(fh.read())
            for arr in xml.find('dict').find('array').getchildren():

                # fetch Python code inside the xml
                py = [c for c in arr.find('dict').getchildren()
                      if c.tag == 'dict' and
                      any([i.text and '/usr/bin/env' in i.text
                           for i in c.getchildren()])]

                # rewrite with current file
                if py:
                    logger.info("Inserting {0} into {1}".format(py_path,
                                                                workflow))
                    py[0].find('string').text = open(py_path).read()

            logger.info("Saving {0}".format(workflow))
            with open(workflow, 'wb') as fh:
                fh.write(ElementTree.tostring(xml))
        logger.info("Completed ADS to BibDesk build step")
Example #11
0
 def intiMulticopy(self):
     fio = fIO()
     
     fio.filePrompt("folder", "Please select folder locations to copy to.\n"
                    + "These folder locations will be saved.\n"
                    + "When you are done loading in locations, simply\n" 
                    + "press the cancel button in the file explorer.\n")
     
     try:
         tree = ET.parse('filecopylocations.xml')
         xroot = tree.getroot()
         for locs in xroot.findall('Location'):
             xroot.remove(locs)
     except:
         froot = ET.Element('Directories')
         tree = ET.ElementTree(froot)
         tree.write('filecopylocations.xml')
         tree = ET.parse('filecopylocations.xml')
         xroot = tree.getroot()
 
     locnum = 1
     fio.folderLocation()
     floc = fio.getFolderLocation()
     while(floc != ''):
         try:
             loc = ET.SubElement(xroot, 'Location'.format(locnum))
             loc.set('index', '{0}'.format(locnum))
             locnum = locnum + 1
             loc.text = floc
             floc = fio.getFolderLocation()
         except:
             floc = ''
     
     tree.write('filecopylocations.xml')
     ET.dump(xroot)
Example #12
0
    def _create_html_file(self, sourcefile, htmlfile, errors):
        name = self.generator.get_name()
        root = ElementTree.fromstring(CPPCHECK_HTML_FILE)
        title = root.find("head/title")
        title.text = "cppcheck - report - %s" % name

        body = root.find("body")
        for div in body.findall("div"):
            if div.get("id") == "page":
                page = div
                break
        for div in page.findall("div"):
            if div.get("id") == "header":
                h1 = div.find("h1")
                h1.text = "cppcheck report - %s" % name
            if div.get("id") == "content":
                content = div
                srcnode = self.generator.bld.root.find_node(sourcefile)
                hl_lines = [e["line"] for e in errors if e.has_key("line")]
                formatter = CppcheckHtmlFormatter(linenos=True, style="colorful", hl_lines=hl_lines, lineanchors="line")
                formatter.errors = [e for e in errors if e.has_key("line")]
                css_style_defs = formatter.get_style_defs(".highlight")
                lexer = pygments.lexers.guess_lexer_for_filename(sourcefile, "")
                s = pygments.highlight(srcnode.read(), lexer, formatter)
                table = ElementTree.fromstring(s)
                content.append(table)

        s = ElementTree.tostring(root, method="html")
        s = CCPCHECK_HTML_TYPE + s
        node = self.generator.path.get_bld().find_or_declare(htmlfile)
        node.write(s)
        return css_style_defs
def globalVars():
    global _home_dir, _desktop_folder, _script_dir, xml_cache_root, xml_options_root, _cache_dir, _options_dir, qualityPresets
    _home_dir = QtCore.QDir.homePath()
    _desktop_folder = _home_dir + "/Desktop/"
    _script_dir = QtCore.QDir.currentPath()
    _cache_dir =  _script_dir + "/cache.xml"
    _options_dir = _script_dir + "/options.xml"
    external_dir = _home_dir + "/.audiobookEncoder"

    if not os.path.exists(external_dir):
        os.makedirs(external_dir)

    if not os.path.exists(external_dir + "/cache.xml"):
        shutil.copy2(_cache_dir, external_dir)

    if not os.path.exists(external_dir + "/options.xml"):
        shutil.copy2(_options_dir, external_dir)

    _cache_dir =  external_dir + "/cache.xml"
    _options_dir = external_dir + "/options.xml"

    # xml table auslesen
    xml_cache_root = ET.parse(_cache_dir).getroot()
    xml_options_root = ET.parse(_options_dir).getroot()

    qualityPresets = ["96 Kbps, Stereo, 48 kHz", "128 Kbps, Stereo, 48 kHz", "256 Kbps, Stereo, 48 kHz", "320 Kbps, Stereo, 48 kHz"]
Example #14
0
def get_product_pages(static,url,logger):
	logger.debug("In get_product_pages: " + url)
	# Get the svr first (it's global)
	lines = themortgagemeter_utils.get_page(False,'',url,logger,True).split('\n')
	# Now get the mortgage data
	if static:
		tree = ET.parse('static_html/tesco/Products.xml')
		root = tree.getroot()
	else:
		root = ET.fromstring(themortgagemeter_utils.get_page(False,'',url,logger,True))
	term = str(25 * 12)
	for purchase in ('HousePurchase','Remortgage'):
		if purchase == 'HousePurchase':
			eligibilities = ['NFTB','NMH']
		elif purchase == 'Remortgage':
			eligibilities = ['NRM']
		for rate_type in ('FixedRate','TrackerRate'):
			if rate_type == 'FixedRate':
				mortgage_type = 'F'
			elif rate_type == 'TrackerRate':
				mortgage_type = 'T'
			rate_set = root.find(purchase).find(rate_type)
			for rate in rate_set.findall('LTV'):
				ltv_percent = rate.get('max')
				for mortgage in rate.findall('Mortgage'):
					#ET.dump(mortgage)
					#print "--------------------"
					rate_percent = mortgage.find('initialRate').text
					apr_percent = mortgage.find('APR').text
					svr_percent = mortgage.find('variableRate').text
					name = mortgage.find('name').text.split('\n')[0]
					initial_period = themortgagemeter_utils.get_months(name,logger)
					booking_fee = str(int(mortgage.find('bookingFee').text) + int(mortgage.find('productFee').text))
					for eligibility in eligibilities:
						mc_util.handle_mortgage_insert(institution_code,mortgage_type,rate_percent,svr_percent,apr_percent,ltv_percent,initial_period,booking_fee,term,'http://www.tescobank.com/personal/finance/mortgages',eligibility,logger)
Example #15
0
def ensure_elementtree_imported(verbosity, logfile):
    global ET, ET_has_iterparse
    if ET is not None:
        return
    if "IronPython" in sys.version:
        import xml.etree.ElementTree as ET
        #### 2.7.2.1: fails later with
        #### NotImplementedError: iterparse is not supported on IronPython. (CP #31923)
    else:
        try: import xml.etree.cElementTree as ET
        except ImportError:
            try: import cElementTree as ET
            except ImportError:
                try: import lxml.etree as ET
                except ImportError:
                    try: import xml.etree.ElementTree as ET
                    except ImportError:
                        try: import elementtree.ElementTree as ET
                        except ImportError:
                            raise Exception("Failed to import an ElementTree implementation")
    if hasattr(ET, 'iterparse'):
        _dummy_stream = BYTES_IO(b'')
        try:
            ET.iterparse(_dummy_stream)
            ET_has_iterparse = True
        except NotImplementedError:
            pass
    if verbosity:
        etree_version = repr([
            (item, getattr(ET, item))
            for item in ET.__dict__.keys()
            if item.lower().replace('_', '') == 'version'
            ])
        print(ET.__file__, ET.__name__, etree_version, ET_has_iterparse, file=logfile)
Example #16
0
    def _get_game_xml(self, steam_id, mode):
        """Get the XML list of games"""

        fname = "games_{}.xml".format(steam_id)

        if mode == "read":
            log.info("Reading from {}".format(fname))
            with open(fname, 'rb') as f:
                content = f.read()
        else:
            url = self.userUrl.format(steam_id=steam_id)
            log.debug("Requesting {}".format(url))

            f = urlopen(url)
            content = f.read()

        if mode == "write":
            log.info("Writing to {}".format(fname))
            with open(fname, 'wb') as f:
                f.write(content)

        try:
            root = ET.fromstring(content)
        except UnicodeEncodeError:
            root = ET.fromstring(content.encode('utf-8'))

        return root
Example #17
0
    def test_remove_doubletheme(self):
        context = ET.fromstring("""
            <contexte xmlns="http://olst.ling.umontreal.ca/dicoenviro/">
                <contexte-texte>I compare apples and oranges</contexte-texte>
                <participant type="Act" role="Agent">
                    <fonction-syntaxique nom="Subject">
                        <groupe-syntaxique nom="NP">I</groupe-syntaxique>
                    </fonction-syntaxique>
                </participant>
                 <lexie-att>compare</lexie-att> 
                <participant type="Act" role="Theme">
                    <fonction-syntaxique nom="Object">
                        <groupe-syntaxique nom="NP">apples</groupe-syntaxique>
                    </fonction-syntaxique>
                </participant>
                 and 
                <participant type="Act" role="Theme">
                    <fonction-syntaxique nom="Object">
                        <groupe-syntaxique nom="NP">oranges</groupe-syntaxique>
                    </fonction-syntaxique>
                </participant>
            </contexte>
            """)
        sentence_text, frame = xmlcontext_to_frame(
            "http://olst.ling.umontreal.ca/dicoenviro/", 'compare', context)

        vn_frame = ET.fromstring(
            """<SYNTAX><NP value="Agent" /><VERB /><NP value="Theme" /></SYNTAX>""")
        self.assertEqual(syntax_to_str(frame), syntax_to_str(vn_frame))
Example #18
0
 def _clone_menu(self, xml, name, owner=None):
     """
     Hopefully temporary hack for converting UIManager XML describing the
     main menubar into a rebindable popup menu. UIManager by itself doesn't
     let you do this, by design, but we need a bigger menu than the little
     things it allows you to build.
     """
     ui_elt = ET.fromstring(xml)
     rootmenu_elt = ui_elt.find("menubar")
     rootmenu_elt.attrib["name"] = name
     xml = ET.tostring(ui_elt)
     self.app.ui_manager.add_ui_from_string(xml)
     tmp_menubar = self.app.ui_manager.get_widget('/' + name)
     popupmenu = gtk.Menu()
     for item in tmp_menubar.get_children():
         tmp_menubar.remove(item)
         popupmenu.append(item)
     if owner is not None:
         popupmenu.attach_to_widget(owner, None)
     popupmenu.set_title("MyPaint")
     popupmenu.connect("selection-done", self.popupmenu_done_cb)
     popupmenu.connect("deactivate", self.popupmenu_done_cb)
     popupmenu.connect("cancel", self.popupmenu_done_cb)
     self.popupmenu_last_active = None
     return popupmenu
Example #19
0
def open_soap_envelope(text):
    """

    :param text: SOAP message
    :return: dictionary with two keys "body"/"header"
    """
    try:
        envelope = ElementTree.fromstring(text)
    except Exception as exc:
        raise XmlParseError("%s" % exc)

    assert envelope.tag == '{%s}Envelope' % soapenv.NAMESPACE
    assert len(envelope) >= 1
    content = {"header": [], "body": None}

    for part in envelope:
        if part.tag == '{%s}Body' % soapenv.NAMESPACE:
            assert len(part) == 1
            content["body"] = ElementTree.tostring(part[0], encoding="UTF-8")
        elif part.tag == "{%s}Header" % soapenv.NAMESPACE:
            for item in part:
                _str = ElementTree.tostring(item, encoding="UTF-8")
                content["header"].append(_str)

    return content
Example #20
0
    def to_string(self, endpoints):
        """
        Converts the given endpoint description beans into a string

        :param endpoints: A list of EndpointDescription beans
        :return: A string containing an XML document
        """
        # Make the ElementTree
        root = self._make_xml(endpoints)
        tree = ElementTree.ElementTree(root)

        # Force the default name space
        ElementTree.register_namespace("", EDEF_NAMESPACE)

        # Make the XML
        for encoding in ('unicode', 'UTF-8'):
            # Prepare a StringIO output
            output = StringIO()

            try:
                # Try to write with a correct encoding
                tree.write(output, encoding=encoding, xml_declaration=True,
                           method="xml")
                break

            except LookupError:
                # 'unicode' is needed in Python 3, but unknown in Python 2...
                continue

        else:
            raise LookupError("Couldn't find a valid encoding")

        return output.getvalue()
    def data_import(self, odm):
        """
        Return result of request to import data to an instance.

        Required params:
        :param odm: odm xml data for import, must be utf-8 encoded and unescaped.
        :type odm: str.

        :returns: contents of the method response in an OrderedDict, or None.
        """
        envelope_copy = et.fromstring(self.envelope)
        body = [i for i in envelope_copy.iterfind(
            './/se:Body', {'se': self.ns_se})][0]

        method_name = 'importRequest'
        import_request = et.Element(et.QName(self.ns_data, method_name))
        body.append(import_request)

        odm_node = et.Element('odm')
        odm_node.append(et.fromstring(odm))
        import_request.append(odm_node)

        envelope = et.tostring(envelope_copy)
        response = self.request(
            self.ocws_url, envelope, method_name, self.ns_data)
        return response
Example #22
0
	def hilight(self, buttons):
		""" Hilights specified button, if same ID is found in svg """
		cache_id = "|".join([ "%s:%s" % (x, buttons[x]) for x in buttons ])
		if not cache_id in self.cache:
			# Ok, this is close to madness, but probably better than drawing
			# 200 images by hand;
			if len(buttons) == 0:
				# Quick way out - changes are not needed
				svg = Rsvg.Handle.new_from_data(self.current_svg.encode("utf-8"))
			else:
				# 1st, parse source as XML
				tree = ET.fromstring(self.current_svg.encode("utf-8"))
				# 2nd, change colors of some elements
				for button in buttons:
					el = SVGEditor.find_by_id(tree, button)
					if el is not None:
						SVGEditor.recolor(el, buttons[button])
					
				# 3rd, turn it back into XML string......
				xml = ET.tostring(tree)
				
				# ... and now, parse that as XML again......
				svg = Rsvg.Handle.new_from_data(xml.encode("utf-8"))
			while len(self.cache) >= self.CACHE_SIZE:
				self.cache.popitem(False)
			if self.size_override:
				w, h = self.size_override
				self.cache[cache_id] = svg.get_pixbuf().scale_simple(
						w, h, GdkPixbuf.InterpType.BILINEAR)
			else:
				self.cache[cache_id] = svg.get_pixbuf()
		
		self.image.set_from_pixbuf(self.cache[cache_id])
Example #23
0
    def mount_iso(self, dev, image):
        def attach_iso(dev, disk, vol):
            if disk.get('device') == 'cdrom':
                for elm in disk:
                    if elm.tag == 'target':
                        if elm.get('dev') == dev:
                            src_media = ElementTree.Element('source')
                            src_media.set('file', vol.path())
                            disk.insert(2, src_media)
                            return True

        storages = self.get_storages()
        for storage in storages:
            stg = self.get_storage(storage)
            if stg.info()[0] != 0:
                for img in stg.listVolumes():
                    if image == img:
                        vol = stg.storageVolLookupByName(image)
        tree = ElementTree.fromstring(self._XMLDesc(0))
        for disk in tree.findall('devices/disk'):
            if attach_iso(dev, disk, vol):
                break
        if self.get_status() == 1:
            xml = ElementTree.tostring(disk)
            self.instance.attachDevice(xml)
            xmldom = self._XMLDesc(VIR_DOMAIN_XML_SECURE)
        if self.get_status() == 5:
            xmldom = ElementTree.tostring(tree)
        self._defineXML(xmldom)
    def xml(self):
        """
        xml representation of the metadata.

        :return: xml representation of the metadata
        :rtype: ElementTree.Element
        """

        root = super(ImpactLayerMetadata, self).xml
        provenance_path = self._special_properties['provenance']
        provenance_element = root.find(provenance_path, XML_NS)

        # find the provenance parent tag
        if provenance_element is not None:
            # there is already a provenance tag so we remove it
            provenance_parent = provenance_element.getparent()
            provenance_parent.remove(provenance_element)
        else:
            # find the parent using the provenance path minus one level
            provenance_parent = '/'.join(provenance_path.split('/')[:-1])
            provenance_parent = root.find(provenance_parent, XML_NS)

        # generate the provenance xml element
        provenance_element = ElementTree.fromstring(self.provenance.xml)
        provenance_parent.append(provenance_element)
        return prettify_xml(ElementTree.tostring(root))
Example #25
0
def isIgnored(myMedia):
    ignored = False
    
    msg = u'File: {0}'.format(myMedia.oStatus.fileName)
    Debug(msg)
    pchtrakt.logger.info(msg)
    
    ignored = isKeywordIgnored(myMedia.oStatus.fileName)
    
    if not ignored and ignored_repertory[0] != '':
        for el in myMedia.oStatus.fullPath.split('/'):
            if el <> '' and el in ignored_repertory:
                msg = 'This video is in a ignored repertory: {0}'.format(el)
                ignored = True
                break

    if not ignored and YamjIgnoredCategory[0] != '':
        if isinstance(myMedia.parsedInfo, mp.MediaParserResultTVShow):
            files = listdir(YamjPath)
            for file in files:
                if file.endswith('xml'):
                    file = unicode(file, errors='replace')
                    if file.find(myMedia.parsedInfo.name) >= 0:
                        oXml = ElementTree.parse(YamjPath + file)
                        ignored = isGenreIgnored(oXml.findall('.//genre'))
                        if ignored:
                            break
        else:
            file = unicode(myMedia.oStatus.fileName.rsplit('.',1)[0] + '.xml', errors='replace')
            oXml = ElementTree.parse(YamjPath + file)
            genres = oXml.findall('.//genre')
            
            ignored = isGenreIgnored(genres)
    return ignored
Example #26
0
    def setUp(self):
        JSBSimTestCase.setUp(self)

        self.fdm = CreateFDM(self.sandbox)
        self.script_path = self.sandbox.path_to_jsbsim_file('scripts',
                                                            'c1722.xml')

        # Read the time step 'dt' from the script file
        self.tree = et.parse(self.script_path)
        root = self.tree.getroot()
        use_tag = root.find('use')
        aircraft_name = use_tag.attrib['aircraft']
        self.run_tag = root.find('run')
        self.dt = float(self.run_tag.attrib['dt'])

        # Read the date at which the trim will be run
        for event in root.findall('run/event'):
            if event.attrib['name'] == 'Trim':
                cond_tag = event.find('condition')
                self.trim_date = float(cond_tag.text.split()[-1])
                break

        # Read the output rate and the output file from the aircraft file
        aircraft_path = self.sandbox.path_to_jsbsim_file('aircraft', aircraft_name,
                                                         append_xml(aircraft_name))
        tree = et.parse(aircraft_path)
        output_tag = tree.getroot().find('output')
        self.output_file = output_tag.attrib['name']
        self.rateHz = float(output_tag.attrib['rate'])
        self.rate = int(1.0 / (self.rateHz * self.dt))
Example #27
0
def get_bib_string(bib_xml):
   """
      this function extracts the bibliographic record from
      the XML string returned by ALMA's API. ALMA's 
      MARCXML record is enclosed by markup
       <collection xmlns="http://www.loc.gov/MARC21/slim">
         <record></record>
       </collection>
   """
   bib_string=""
   outcome=1
   in_string=bib_xml.replace("\n","")
   try:
      tree=elementTree.fromstring(in_string)
   except:
      sys.stderr.write("parse failed."+"\n")
      return bib_string,outcome

   try:
      bib_element=tree.find("record")
   except:
      sys.stderr.write("xml parsing failed. couldn't find record element"+"\n")
      return bib_string,outcome
   try:
       bib_string=elementTree.tostring(bib_element,encoding="utf-8",method="xml")
       #sys.stderr.write(str(bib_string)+"\n"))
       bib_string=bib_string.replace("<?xml version='1.0' encoding='utf-8'?>","")
       #bib_string=elementTree.tostring(bib_element,method="xml")
   except:
      sys.stderr.write("xml parsing failed. couldn't find record element"+"\n")
      return "",outcome
   bib_string=bib_string.replace(" schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"","")
   bib_string=bib_string.replace("</record>","")
   bib_string=bib_string.replace("<leader>     ","<leader>12345")
   return bib_string,0
Example #28
0
def main():
    count = 1
    start_title = parse_args()
    print "%i. %s" % (count, start_title)
    count += 1
    next_page = start_title
    link_list = []
    next_page = next_page.replace(" ", "+").encode('utf-8')
    xml = urllib2.urlopen(
        'https://en.wikipedia.org/w/api.php?action=query&format=xml&prop=links&indexpageids=1&titles=' + next_page + '&plnamespace=&pllimit=5000')
    root = ET.fromstring(xml.read())
    for child in root.iter('pl'):
        if ':' not in child.get('title'):
            link_list.append(child.get('title'))
    next_page = random.choice(link_list)
    while next_page:
        last_list = link_list
        link_list = []

        while not link_list:
            next_page = random.choice(last_list)
            print "%i. %s" % (count, next_page)
            count += 1
            next_page = next_page.replace(" ", "+").encode('utf-8')
            xml = urllib2.urlopen(
                'https://en.wikipedia.org/w/api.php?action=query&format=xml&prop=links&indexpageids=1&titles=' + next_page + '&plnamespace=&pllimit=5000')
            root = ET.fromstring(xml.read())
            for child in root.iter('pl'):
                if ':' not in child.get('title'):
                    link_list.append(child.get('title'))
Example #29
0
def limit_rdb(services_rdb, full_factory_map, full_constructor_map):
    ET.register_namespace('','http://openoffice.org/2010/uno-components')
    tree = ET.parse(services_rdb[0])
    root = tree.getroot()

    for component in root.findall('{http://openoffice.org/2010/uno-components}component'):
        # direct
        uri = component.get('uri')
        component_name = None
        if uri != None:
            component_name = re.sub('^vnd.sun.star.expand:\$LO_LIB_DIR/([^.]*).so$', '\\1.a', uri)
        if component_name in full_factory_map:
            continue

        # via a constructor - limit only to those we have
        has_constructor = False
        for implementation in component.findall('{http://openoffice.org/2010/uno-components}implementation'):
            constructor = implementation.get('constructor')
            if constructor in full_constructor_map:
                has_constructor = True
            else:
                component.remove(implementation)

        if not has_constructor:
            root.remove(component)

    tree.write(services_rdb[0] + '.out', xml_declaration = True, method = 'xml')
Example #30
0
def save_element(elem, path):
    if elem is None:
        return
    
    if not path or path is None:
        return
    
    parent = os.path.dirname(path)
    if not os.path.isdir(parent):
        os.makedirs(parent, mode=0o755, exist_ok=True)
        os.chmod(parent, 0o0755)
        
    try:
        # Get the elements default namespace
        namespace = getNamespace(elem)
        # Pretty up the element
        indent(elem)
        # Register the discovered namespace as the default
        ET.register_namespace("", namespace)
        # Create a new ElementTree with this element as the root
        elem_tree = ET.ElementTree(element=elem)
        # Write the full tree to a file
        elem_tree.write(path, xml_declaration=False, encoding="UTF-8", method="xml")
        os.chmod(path, 0o0664)
        return True
    except:
        print(" *** Error writing new element to path %s" % path)
        print("     Exception Type: ", sys.exc_info()[0])
        print("    Exception Value: ", sys.exc_info()[1])
        return False