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 __init__(self, xmlFile): self.servers = [] # Read in XML _ignore_etree, servers_node = xmlutil.readXML(xmlFile.path, ELEMENT_SERVERS) self._parseXML(servers_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: 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 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 __init__(self, xmlFile): self.servers = [] # Read in XML _ignore_etree, servers_node = xmlutil.readXML(xmlFile.path, ELEMENT_SERVERS) self._parseXML(servers_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: 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 parse(xmlFile, ignoreIPLookupFailures=False): results = {} # Read in XML try: _ignore_tree, servers_node = readXML(xmlFile, ELEMENT_SERVERS) except ValueError, e: log.error("XML parse error for '%s' because: %s" % (xmlFile, e,), raiseException=RuntimeError)
def parse(xmlFile, ignoreIPLookupFailures=False): results = {} # Read in XML try: _ignore_tree, servers_node = readXML(xmlFile, ELEMENT_SERVERS) except ValueError, e: raise RuntimeError("XML parse error for '{}' because: {}".format(xmlFile, e,))
def parse(xmlFile, ignoreIPLookupFailures=False): results = {} # Read in XML try: _ignore_tree, servers_node = readXML(xmlFile, ELEMENT_SERVERS) except ValueError, e: raise RuntimeError("XML parse error for '{}' because: {}".format(xmlFile, e,))
def __init__(self, xmlFile, items): self.items = items self.xmlFile = xmlFile # Read in XML try: _ignore_tree, augments_node = readXML(self.xmlFile, ELEMENT_AUGMENTS) except ValueError, e: raise RuntimeError("XML parse error for '%s' because: %s" % (self.xmlFile, e,))
def __init__(self, xmlFile): self.items = [] self.xmlFile = fullServerPath(config.DataRoot, xmlFile) # Read in XML try: _ignore_tree, proxies_node = readXML(self.xmlFile, ELEMENT_PROXIES) except ValueError, e: log.error("XML parse error for '%s' because: %s" % (self.xmlFile, e,), raiseException=RuntimeError)
def __init__(self, xmlFile, items): self.items = items self.xmlFile = xmlFile # Read in XML try: _ignore_tree, augments_node = readXML(self.xmlFile, ELEMENT_AUGMENTS) except ValueError, e: raise RuntimeError("XML parse error for '%s' because: %s" % (self.xmlFile, e,))
def uidsFromFile(self, filename): """ Return all uids from the augments xml file """ _ignore_etree, augments_node = readXML(filename) for record_node in augments_node: if record_node.tag != xmlaugmentsparser.ELEMENT_RECORD: continue uid = record_node.find(xmlaugmentsparser.ELEMENT_UID).text yield uid
def uidsFromFile(self, filename): """ Return all uids from the augments xml file """ _ignore_etree, augments_node = readXML(filename) for record_node in augments_node: if record_node.tag != xmlaugmentsparser.ELEMENT_RECORD: continue uid = record_node.find(xmlaugmentsparser.ELEMENT_UID).text yield uid
def readDatabase(self): """ Read in XML data. """ _ignore, root = xmlutil.readXML(self.xmlfile, "timezones") self.dtstamp = root.findtext("dtstamp") for child in root: if child.tag == "timezone": tz = TimezoneInfo.readXML(child) if tz: self.timezones[tz.tzid] = tz for alias in tz.aliases: self.aliases[alias] = tz.tzid
def readDatabase(self): """ Read in XML data. """ _ignore, root = xmlutil.readXML(self.xmlfile, "timezones") self.dtstamp = root.findtext("dtstamp") for child in root: if child.tag == "timezone": tz = TimezoneInfo.readXML(child) if tz: self.timezones[tz.tzid] = tz for alias in tz.aliases: self.aliases[alias] = tz.tzid
def __init__(self, xmlFile): self.items = [] self.xmlFile = fullServerPath(config.DataRoot, xmlFile) # Read in XML try: _ignore_tree, proxies_node = readXML(self.xmlFile, ELEMENT_PROXIES) except ValueError: log.failure("XML parse error for proxy data file {xmlfile}", xmlfile=self.xmlFile) # FIXME: RuntimeError is dumb. self._parseXML(proxies_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 __init__(self, xmlFile): self.items = [] self.xmlFile = fullServerPath(config.DataRoot, xmlFile) # Read in XML try: _ignore_tree, proxies_node = readXML(self.xmlFile, ELEMENT_PROXIES) except ValueError: log.failure("XML parse error for proxy data file {xmlfile}", xmlfile=self.xmlFile) # FIXME: RuntimeError is dumb. self._parseXML(proxies_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 __init__(self, xmlFile, externalUpdate=True): if type(xmlFile) is str: xmlFile = FilePath(xmlFile) self.xmlFile = xmlFile self.realm = None self.items = {} for recordType in RECORD_TYPES.values(): self.items[recordType] = {} # Read in XML try: _ignore_tree, accounts_node = readXML(self.xmlFile.path, ELEMENT_ACCOUNTS) except ValueError, e: log.error("XML parse error for '%s' because: %s" % (self.xmlFile, e,), raiseException=RuntimeError)
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 _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 test_addElement(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) addSubElement(root, "added", "added text") self._checkXML(root, XMLUtil.data4)
def test_writeXML(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) self._checkXML(root, XMLUtil.data3)
def test_changeElement_existing(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) changeSubElementText(root, "help", "changed text") self._checkXML(root, XMLUtil.data5)
def test_readXML_noverify(self): io = StringIO(XMLUtil.data1) etree, root = readXML(io) self.assertEqual(etree.getroot(), root) self.assertEqual(root.tag, "test")
def test_changeElement_existing(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) changeSubElementText(root, "help", "changed text") self._checkXML(root, XMLUtil.data5)
def test_readXML_verify_ok(self): io = StringIO(XMLUtil.data1) etree, root = readXML(io, expectedRootTag="test") self.assertEqual(etree.getroot(), root) self.assertEqual(root.tag, "test")
def test_readXML_noverify(self): io = StringIO(XMLUtil.data1) etree, root = readXML(io) self.assertEqual(etree.getroot(), root) self.assertEqual(root.tag, "test")
def test_changeElement_new(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) changeSubElementText(root, "new", "new text") self._checkXML(root, XMLUtil.data6)
def test_changeElement_new(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) changeSubElementText(root, "new", "new text") self._checkXML(root, XMLUtil.data6)
def test_writeXML(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) self._checkXML(root, XMLUtil.data3)
def test_readXML_verify_ok(self): io = StringIO(XMLUtil.data1) etree, root = readXML(io, expectedRootTag="test") self.assertEqual(etree.getroot(), root) self.assertEqual(root.tag, "test")
def test_addElement(self): io = StringIO(XMLUtil.data1) _ignore_etree, root = readXML(io) addSubElement(root, "added", "added text") self._checkXML(root, XMLUtil.data4)