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 _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: 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] self._updateRecordInXMLDB(record, record_node) changed = True # Modify xmlfile if changed: writeXML(xmlfile, augments_node)
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)
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: if autoScheduleElement.text == "false": autoScheduleModeElement = recordNode.find("auto-schedule-mode") if autoScheduleModeElement is not None: 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)
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)
def _doRemoveFromFile(self, xmlfile, uids): _ignore_etree, augments_node = readXML(xmlfile) # Remove all UIDs present changed = False for child in augments_node: if child.tag != xmlaugmentsparser.ELEMENT_RECORD: continue if child.find(xmlaugmentsparser.ELEMENT_UID).text in uids: augments_node.remove(child) changed = True # Modify xmlfile if changed: writeXML(xmlfile, augments_node)
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)
def _doRemoveFromFile(self, xmlfile, uids): try: _ignore_etree, augments_node = readXML(xmlfile) except IOError: # If the file is missing, then removal is a no-op return # Remove all UIDs present changed = False for child in augments_node: if child.tag != xmlaugmentsparser.ELEMENT_RECORD: continue if child.find(xmlaugmentsparser.ELEMENT_UID).text in uids: augments_node.remove(child) changed = True # Modify xmlfile if changed: writeXML(xmlfile, augments_node)
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(): self._addRecordToXMLDB(record, augments_node) 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: self._addRecordToXMLDB(record, augments_node) # Modify xmlfile writeXML(xmlfile, augments_node)
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(): self._addRecordToXMLDB(record, augments_node) 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: {user}", user=config.UserName) gid = -1 if config.GroupName: try: gid = grp.getgrnam(config.GroupName).gr_gid except KeyError: log.error("Group not found: {grp}", grp=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: self._addRecordToXMLDB(record, augments_node) # Modify xmlfile writeXML(xmlfile, augments_node)
def _checkXML(self, node, data): xmlfile = self.mktemp() writeXML(xmlfile, node) with open(xmlfile) as f: newdata = f.read() self.assertEqual(newdata, data)
def _checkXML(self, node, data): xmlfile = self.mktemp() writeXML(xmlfile, node) newdata = open(xmlfile).read() self.assertEqual(newdata, data)