Beispiel #1
0
def upgradeResourcesXML(resourcesFilePath):
    """
    Convert the old XML format to the twext.who.xml format

    @param resourcesFilePath: the file to convert
    @type resourcesFilePath: L{FilePath}
    """
    try:
        with resourcesFilePath.open() as fh:
            try:
                etree = parseXML(fh)
            except XMLParseError:
                log.error("Cannot parse {path}", path=resourcesFilePath.path)
                return
    except (OSError, IOError):
        # Can't read the file
        log.error("Cannot read {path}", path=resourcesFilePath.path)
        return

    accountsNode = etree.getroot()
    if accountsNode.tag != "accounts":
        return

    tagMap = {
        "uid": ("short-name",),
        "guid": ("guid", "uid"),
        "name": ("full-name",),
    }
    log.info("Converting resources.xml")
    directoryNode = XMLElement("directory")
    directoryNode.set("realm", accountsNode.get("realm"))
    for sourceNode in accountsNode:
        recordType = sourceNode.tag
        destNode = XMLElement("record")
        destNode.set("type", recordType)
        for sourceFieldNode in sourceNode:
            tags = tagMap.get(sourceFieldNode.tag, None)
            if tags:
                for tag in tags:
                    destFieldNode = XMLElement(tag)
                    value = sourceFieldNode.text
                    try:
                        # Normalize UUID values to uppercase
                        value = str(uuid.UUID(value)).upper()
                    except ValueError:
                        pass
                    destFieldNode.text = value
                    destNode.append(destFieldNode)

        directoryNode.append(destNode)

    resourcesFilePath.setContent(etreeToString(directoryNode, "utf-8"))
Beispiel #2
0
def upgradeResourcesXML(resourcesFilePath):
    """
    Convert the old XML format to the twext.who.xml format

    @param resourcesFilePath: the file to convert
    @type resourcesFilePath: L{FilePath}
    """
    try:
        with resourcesFilePath.open() as fh:
            try:
                etree = parseXML(fh)
            except XMLParseError:
                log.error("Cannot parse {path}", path=resourcesFilePath.path)
                return
    except (OSError, IOError):
        # Can't read the file
        log.error("Cannot read {path}", path=resourcesFilePath.path)
        return

    accountsNode = etree.getroot()
    if accountsNode.tag != "accounts":
        return

    tagMap = {
        "uid": ("short-name",),
        "guid": ("guid", "uid"),
        "name": ("full-name",),
    }
    log.info("Converting resources.xml")
    directoryNode = XMLElement("directory")
    directoryNode.set("realm", accountsNode.get("realm"))
    for sourceNode in accountsNode:
        recordType = sourceNode.tag
        destNode = XMLElement("record")
        destNode.set("type", recordType)
        for sourceFieldNode in sourceNode:
            tags = tagMap.get(sourceFieldNode.tag, None)
            if tags:
                for tag in tags:
                    destFieldNode = XMLElement(tag)
                    destFieldNode.text = sourceFieldNode.text
                    destNode.append(destFieldNode)

        directoryNode.append(destNode)

    resourcesFilePath.setContent(etreeToString(directoryNode, "utf-8"))
Beispiel #3
0
 def _writeDirectoryNode(self, directoryNode):
     self.filePath.setContent(etreeToString(directoryNode))
     self.flush()
Beispiel #4
0
 def _writeDirectoryNode(self, directoryNode):
     self.filePath.setContent("{preamble}\n{xml}\n".format(preamble=self.filePreamble, xml=etreeToString(directoryNode, "utf-8")))
     self.flush()