示例#1
0
    def _dumpTZs(self):

        _ignore, root = xmlutil.newElementTreeWithRoot("timezones")
        addSubElement(root, "dtstamp", self.dtstamp)
        for _ignore, v in sorted(self.timezones.items(), key=lambda x: x[0]):
            v.generateXML(root)
        xmlutil.writeXML(self.xmlfile, root)
    def _dumpTZs(self):

        _ignore, root = xmlutil.newElementTreeWithRoot("timezones")
        addSubElement(root, "dtstamp", self.dtstamp)
        for _ignore, v in sorted(self.timezones.items(), key=lambda x: x[0]):
            v.generateXML(root)
        xmlutil.writeXML(self.xmlfile, root)
 def generateXML(self, parent):
     """
     Generate the XML element for this timezone info.
     """
     node = xmlutil.addSubElement(parent, "timezone")
     xmlutil.addSubElement(node, "tzid", self.tzid)
     xmlutil.addSubElement(node, "dtstamp", self.dtstamp)
     for alias in self.aliases:
         xmlutil.addSubElement(node, "alias", alias)
     xmlutil.addSubElement(node, "md5", self.md5)
示例#4
0
 def generateXML(self, parent):
     """
     Generate the XML element for this timezone info.
     """
     node = xmlutil.addSubElement(parent, "timezone")
     xmlutil.addSubElement(node, "tzid", self.tzid)
     xmlutil.addSubElement(node, "dtstamp", self.dtstamp)
     for alias in self.aliases:
         xmlutil.addSubElement(node, "alias", alias)
     xmlutil.addSubElement(node, "md5", self.md5)
示例#5
0
def upgradeAugmentsXML(augmentsFilePath):
    """
    Convert the old augments XML auto-schedule related elements to the twext.who.xml format

    @param augmentsFilePath: the file to convert
    @type augmentsFilePath: L{FilePath}
    """
    # Read in XML
    try:
        _ignore_tree, augments_node = readXML(augmentsFilePath.path,
                                              ELEMENT_AUGMENTS)
    except ValueError:
        log.error("Cannot parse {path}", path=augmentsFilePath.path)
        return

    log.info("Converting augments.xml")
    changed = False
    for recordNode in augments_node:

        autoScheduleElement = recordNode.find("auto-schedule")
        if autoScheduleElement is not None:
            # This is a legacy augment record; if auto-schedule was true we
            # need to set mode to "automatic", if false then set mode to "none".
            # If auto-schedule was true and auto-schedule-mode existed, keep mode.

            autoScheduleModeElement = recordNode.find("auto-schedule-mode")
            if autoScheduleModeElement is None:
                autoScheduleModeElement = addSubElement(recordNode,
                                                        "auto-schedule-mode",
                                                        text="automatic")

            if autoScheduleElement.text == "false":
                autoScheduleModeElement.text = "none"

            recordNode.remove(autoScheduleElement)
            changed = True

        enableElement = recordNode.find("enable")
        if enableElement is not None:
            recordNode.remove(enableElement)
            changed = True

    if changed:
        writeXML(augmentsFilePath.path, augments_node)
示例#6
0
    def test_addElement(self):

        io = StringIO(XMLUtil.data1)
        _ignore_etree, root = readXML(io)
        addSubElement(root, "added", "added text")
        self._checkXML(root, XMLUtil.data4)
 def test_addElement(self):
     
     io = StringIO(XMLUtil.data1)
     _ignore_etree, root = readXML(io)
     addSubElement(root, "added", "added text")
     self._checkXML(root, XMLUtil.data4)
示例#8
0
 def _updateRecordInXMLDB(self, record, recordNode):
     del recordNode[:]
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_UID, record.uid)
     if record.serverID:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_SERVERID, record.serverID)
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLELOGIN, "true" if record.enabledForLogin else "false")
     if record.autoScheduleMode:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE_MODE, record.autoScheduleMode)
     if record.autoAcceptGroup:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_AUTOACCEPTGROUP, record.autoAcceptGroup)
示例#9
0
 def _addRecordToXMLDB(self, record, parentNode):
     record_node = addSubElement(parentNode, xmlaugmentsparser.ELEMENT_RECORD)
     self._updateRecordInXMLDB(record, record_node)
示例#10
0
    def _addElement(self, parent, principal):
        """
        Create an XML element from principal and add it as a child of parent
        """

        # TODO: derive this from xmlaccountsparser.py
        xmlTypes = {
            'users'     : 'user',
            'groups'    : 'group',
            'locations' : 'location',
            'resources' : 'resource',
            'addresses' : 'address',
        }
        xmlType = xmlTypes[principal.recordType]

        element = addSubElement(parent, xmlType)
        for value in principal.shortNames:
            addSubElement(element, "uid", text=value.decode("utf-8"))
        addSubElement(element, "guid", text=principal.guid)
        if principal.fullName is not None:
            addSubElement(element, "name", text=principal.fullName.decode("utf-8"))
        if principal.firstName is not None:
            addSubElement(element, "first-name", text=principal.firstName.decode("utf-8"))
        if principal.lastName is not None:
            addSubElement(element, "last-name", text=principal.lastName.decode("utf-8"))
        for value in principal.emailAddresses:
            addSubElement(element, "email-address", text=value.decode("utf-8"))
        if principal.extras:
            extrasElement = addSubElement(element, "extras")
            for key, value in principal.extras.iteritems():
                addSubElement(extrasElement, key, text=value.decode("utf-8"))

        return element
示例#11
0
 def _updateRecordInXMLDB(self, record, recordNode):
     del recordNode[:]
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_UID, record.uid)
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
     if record.serverID:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_SERVERID, record.serverID)
     if record.partitionID:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_PARTITIONID, record.partitionID)
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_ENABLELOGIN, "true" if record.enabledForLogin else "false")
     addSubElement(recordNode, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")
     if record.autoScheduleMode:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE_MODE, record.autoScheduleMode)
     if record.autoAcceptGroup:
         addSubElement(recordNode, xmlaugmentsparser.ELEMENT_AUTOACCEPTGROUP, record.autoAcceptGroup)
示例#12
0
 def _addRecordToXMLDB(self, record, parentNode):
     record_node = addSubElement(parentNode, xmlaugmentsparser.ELEMENT_RECORD)
     self._updateRecordInXMLDB(record, record_node)
示例#13
0
    def _doModifyInFile(self, xmlfile, records):
    
        if not os.path.exists(xmlfile):
            return

        _ignore_etree, augments_node = readXML(xmlfile)
    
        # Map uid->record for fast lookup
        recordMap = dict([(record.uid, record) for record in records])

        # Make sure UID is present
        changed = False
        for record_node in augments_node.getchildren():
            
            if record_node.tag != xmlaugmentsparser.ELEMENT_RECORD:
                continue
    
            uid = record_node.find(xmlaugmentsparser.ELEMENT_UID).text
            if uid in recordMap:
                # Modify record
                record = recordMap[uid]
                del record_node.getchildren()[:]
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_UID, record.uid)
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_HOSTEDAT, record.hostedAt)
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")
                changed = True
        
        
        # Modify xmlfile
        if changed:
            writeXML(xmlfile, augments_node)
示例#14
0
    def _doAddToFile(self, xmlfile, records):

        if not os.path.exists(xmlfile):

            # File doesn't yet exist.  Create it with items in self.db, and
            # set file permissions.

            _ignore_etree, augments_node = newElementTreeWithRoot(xmlaugmentsparser.ELEMENT_AUGMENTS)
            for record in self.db.itervalues():
                record_node = addSubElement(augments_node, xmlaugmentsparser.ELEMENT_RECORD)
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_UID, record.uid)
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_HOSTEDAT, record.hostedAt)
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
                addSubElement(record_node, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")


            writeXML(xmlfile, augments_node)

            # Set permissions
            uid = -1
            if config.UserName:
                try:
                    uid = pwd.getpwnam(config.UserName).pw_uid
                except KeyError:
                    log.error("User not found: %s" % (config.UserName,))
            gid = -1
            if config.GroupName:
                try:
                    gid = grp.getgrnam(config.GroupName).gr_gid
                except KeyError:
                    log.error("Group not found: %s" % (config.GroupName,))
            if uid != -1 and gid != -1:
                os.chown(xmlfile, uid, gid)


        _ignore_etree, augments_node = readXML(xmlfile)

        # Create new record
        for record in records:
            record_node = addSubElement(augments_node, xmlaugmentsparser.ELEMENT_RECORD)
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_UID, record.uid)
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLE, "true" if record.enabled else "false")
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_HOSTEDAT, record.hostedAt)
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLECALENDAR, "true" if record.enabledForCalendaring else "false")
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_ENABLEADDRESSBOOK, "true" if record.enabledForAddressBooks else "false")
            addSubElement(record_node, xmlaugmentsparser.ELEMENT_AUTOSCHEDULE, "true" if record.autoSchedule else "false")
        
        # Modify xmlfile
        writeXML(xmlfile, augments_node)