Beispiel #1
0
def get_rss2(url):
    try:
        with XmlReader.Create(url) as reader:
            return [
                RSSItem(
                    i.Title.Text, i.Summary.Text,
                    i.Links[0].Uri.AbsoluteUri if i.Links.Count > 0 else "")
                for i in SyndicationFeed.Load(reader).Items
            ]
    except XmlException:
        wc = WebClient()
        wc.Encoding = UTF8
        xmlstr = wc.DownloadString(url)
        xdoc = XmlDocument()
        xdoc.LoadXml(xmlstr)
        xelem = xdoc.DocumentElement

        titles = [
            i.InnerText.Replace("\n", "").Replace("\r", "")
            for i in xelem.SelectNodes("//item//title")
        ]
        links = [i.InnerText for i in xelem.SelectNodes("//item//link")]
        descriptions = [
            i.InnerText for i in xelem.SelectNodes("//item//description")
        ]
        return [
            RSSItem(t, d, l) for t, d, l in zip(titles, descriptions, links)
        ]
Beispiel #2
0
def prettify_string(rough_string):
    """Return an XML string with added whitespace for legibility,
    using .NET infrastructure.

    Parameters
    ----------
    rough_string : str
        XML string
    """
    mStream = MemoryStream()
    writer = XmlTextWriter(mStream, Encoding.UTF8)
    document = XmlDocument()

    document.LoadXml(rough_string)

    writer.Formatting = Formatting.Indented

    writer.WriteStartDocument()
    document.WriteContentTo(writer)
    writer.Flush()
    mStream.Flush()

    mStream.Position = 0

    sReader = StreamReader(mStream)

    formattedXml = sReader.ReadToEnd()

    return formattedXml
Beispiel #3
0
    def serialize(self,
                  document,
                  indent=False,
                  encoding=bridge.ENCODING,
                  prefixes=None,
                  omit_declaration=False):
        doc = XmlDocument()
        doc.LoadXml(self.__start_document(document))
        if document.xml_text:
            doc.DocumentElement.AppendChild(
                doc.CreateTextNode(document.xml_text))
        self.__serialize_element(doc, doc.DocumentElement, document)

        settings = XmlWriterSettings()
        settings.Indent = indent
        settings.Encoding = Encoding.GetEncoding(encoding)
        settings.OmitXmlDeclaration = omit_declaration

        ms = MemoryStream()
        xw = XmlWriter.Create(ms, settings)
        doc.Save(xw)
        sr = StreamReader(ms)
        ms.Seek(0, SeekOrigin.Begin)
        content = sr.ReadToEnd()
        ms.Close()

        return content
Beispiel #4
0
def FWConfigureManifest():
    """
    Configures python manifest file according to the current version of 
    FieldWorks.
    Returns True if FlexTools needs to be restarted (necessary if
    the manifest file has been changed)
    """
    # Nov2014:
    # There are different versions of python32.exe.manifest for different versions of FieldWorks
    # FW 7: this one is stable and distributed in the PythonXX.NET\FW7 directories
    # FW 8.0: distributed in the PythonXX.NET\FW8 directories
    # FW 8.1: reference FwKernel.X.manifest from a created python32.exe.manifest
    #         so we can pick up updates automatically.

    restartRequired = False

    FwKernelManifestName = "FwKernel.X.manifest"
    Python32ManifestName = "python32.exe.manifest"
    # For FW 8.1+
    FwKernelManifestPath = os.path.join(FWCodeDir, FwKernelManifestName)
    if os.access(FwKernelManifestPath, os.F_OK):        # Doesn't exist in FW 7
        # FwKernel.X.manifest will have been copied by FWConfigureDLLs()

        # Find version number of FwKernel.X.manifest
        FwKernelXML = XmlDocument()
        FwKernelXML.Load(FwKernelManifestPath)

        # <assemblyIdentity name="FwKernel.X" version="8.1.2.41947" type="win32" />
        FwKernelVersion = FwKernelXML.DocumentElement.FirstChild.GetAttribute("version")

        Python32ManifestXML = XmlDocument()
        Python32ManifestXML.LoadXml(Python32Manifest)

        Python32ManifestXML.DocumentElement.LastChild.FirstChild.FirstChild.SetAttribute("version", FwKernelVersion)
        # print Python32ManifestXML.DocumentElement.LastChild.FirstChild.FirstChild.GetAttribute("version")

        py_net_folders = glob.glob("..\Python*.NET\FW%s" % FWMajorVersion)

        # Compare with version number in each python32.exe.manifest
        for folder in py_net_folders:
            manifestFilename = os.path.join(folder, Python32ManifestName)
            ToCheckXML = XmlDocument()
            ToCheckXML.Load(manifestFilename)

            try:
                ver = ToCheckXML.DocumentElement.LastChild.FirstChild.FirstChild.GetAttribute("version")
            except AttributeError:
                # Arrives here with the default python32.exe.manifest for earlier versions of FW.
                ver = ""

            # If different then write a new python32.exe.manifest, and force restart
            if ver <> FwKernelVersion:
                restartRequired = True
                Python32ManifestXML.Save(manifestFilename)  # Overwrite the manifest
                print "Startup: Manifest updated:", manifestFilename

    return restartRequired
    def _GetWsdlLocation(wsilXmlString, key):
        '''Parse location of wsdl from the wsil xml string, the key should correspond to the value in the wsil xml document'''

        xmlDoc = XmlDocument()
        xmlDoc.LoadXml(wsilXmlString)

        manager = XmlNamespaceManager(xmlDoc.NameTable)
        manager.AddNamespace("wsil", "http://schemas.xmlsoap.org/ws/2001/10/inspection/")

        # example key: Query Sales Quotes
        condition = 'wsil:abstract[contains(text(),"objname={0}")]'.format(key)
        xpathQuery = '/wsil:inspection/wsil:service[{0}]/wsil:description/@location'.format(condition)

        locationAttribute = xmlDoc.SelectSingleNode(xpathQuery, manager)
        return locationAttribute.Value