def openEntry(self): """ opens the data entry corresponding to a new XML settings :brief: It parse the XML settings, creates thread pools and runs the INIT pool. """ if self.xmlsettings: # flag for INIT mode self.__datasources.counter = -1 self.__datasources.nxroot = self.__nxRoot errorHandler = sax.ErrorHandler() parser = sax.make_parser() handler = NexusXMLHandler( self.__nxPath[-1] if self.__nxPath else self.__eFile, self.__datasources, self.__decoders, self.__fetcher.groupTypes, parser, json.loads(self.jsonrecord), self._streams, self.skipacquisition) parser.setContentHandler(handler) parser.setErrorHandler(errorHandler) inpsrc = sax.InputSource() inpsrc.setByteStream(StringIO(self.xmlsettings)) parser.parse(inpsrc) self.__initPool = handler.initPool self.__stepPool = handler.stepPool self.__finalPool = handler.finalPool self.__triggerPools = handler.triggerPools self.__entryAttrs = handler.entryAttrs self.__initPool.numberOfThreads = self.numberOfThreads self.__stepPool.numberOfThreads = self.numberOfThreads self.__finalPool.numberOfThreads = self.numberOfThreads for pool in self.__triggerPools.keys(): self.__triggerPools[pool].numberOfThreads = \ self.numberOfThreads self.__initPool.setJSON(json.loads(self.jsonrecord)) if not self.skipacquisition: self.__initPool.runAndWait() self.__initPool.checkErrors() self.skipacquisition = False if self.addingLogs: self.__entryCounter += 1 if not self.__logGroup.is_valid: self.__logGroup.reopen() lfield = self.__logGroup.create_field( "nexus__entry__%s_xml" % str(self.__entryCounter), "string") lfield.write(self.xmlsettings) lfield.close() if self.__nxFile and hasattr(self.__nxFile, "flush"): self.__nxFile.flush() if self.stepsperfile > 0: self.__filenames = [] self.__filetimes = {} self.__nextfile() elif "swmr" in self.__pars.keys() and self.__pars["swmr"]: self.__nxFile.reopen(readonly=False, **self.__pars)
def policy_reader(filename, path, no_check_name=False): policy = Policy() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) policy.name = filename[:-4] if not no_check_name: policy.check_name(policy.name) policy.filename = filename policy.path = path policy.builtin = False if path.startswith(config.ETC_FIREWALLD) else True policy.default = policy.builtin handler = policy_ContentHandler(policy) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_POLICY, "not a valid policy file: %s" % \ msg.getException()) del handler del parser return policy
def service_reader(filename, path): service = Service() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) service.name = filename[:-4] service.check_name(service.name) service.filename = filename service.path = path service.builtin = False if path.startswith(config.ETC_FIREWALLD) else True service.default = service.builtin handler = service_ContentHandler(service) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_SERVICE, "not a valid service file: %s" % \ msg.getException()) del handler del parser return service
def zone_reader(filename, path, no_check_name=False): zone = Zone() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) zone.name = filename[:-4] if not no_check_name: zone.check_name(zone.name) zone.filename = filename zone.path = path zone.builtin = False if path.startswith(config.ETC_FIREWALLD) else True zone.default = zone.builtin handler = zone_ContentHandler(zone) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_ZONE, "not a valid zone file: %s" % \ msg.getException()) del handler del parser if PY2: zone.encode_strings() return zone
def zone_reader(filename, path, no_check_name=False): zone = Zone() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) zone.name = filename[:-4] if not no_check_name: zone.check_name(zone.name) zone.filename = filename zone.path = path zone.builtin = False if path.startswith(config.ETC_FIREWALLD) else True zone.default = zone.builtin # new Zone() objects default this to True, but if reading on disk # configuration we have to assume False, because the absence of # <forward> element indicates False. Presence indicates True. zone.forward = False handler = zone_ContentHandler(zone) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_ZONE, "not a valid zone file: %s" % \ msg.getException()) del handler del parser return zone
def helper_reader(filename, path): helper = Helper() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) helper.name = filename[:-4] helper.check_name(helper.name) helper.filename = filename helper.path = path helper.builtin = False if path.startswith(config.ETC_FIREWALLD) else True helper.default = helper.builtin handler = helper_ContentHandler(helper) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_HELPER, "not a valid helper file: %s" % \ msg.getException()) del handler del parser if PY2: helper.encode_strings() return helper
def test_constructor(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) parser = sax.make_parser() handler = sax.ContentHandler() name = "datasource" attrs = {"type": "TANGO"} el = InnerXMLHandler(parser, handler, name, attrs) parser.setContentHandler(el) self.assertEqual(el.xml, None) inpsrc = sax.InputSource() inpsrc.setByteStream(StringIO('<device> Something</device>')) parser.parse(inpsrc) self.assertEqual(parser.getContentHandler(), el) self.assertEqual(el.endElement("datasource"), None) self.assertEqual(len(el.xml), 3) self.assertEqual(el.xml[0], '<datasource type="TANGO">') self.assertEqual(el.xml[1], '<device> Something</device>') self.assertEqual(el.xml[2], '</datasource>') self.assertEqual(parser.getContentHandler(), handler)
def test_XML_group_name(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) parser = sax.make_parser() handler = sax.ContentHandler() name = "datasource" el = InnerXMLHandler(parser, handler, name, {}) parser.setContentHandler(el) self.assertEqual(el.xml, None) inpsrc = sax.InputSource() inpsrc.setByteStream( StringIO('<group type="NXentry" name="entry"></group>')) parser.parse(inpsrc) self.assertEqual(parser.getContentHandler(), el) self.assertEqual(el.endElement("datasource"), None) self.assertTrue( (el.xml == ('<datasource>', '<group type="NXentry" name="entry"></group>', '</datasource>')) or (el.xml == ('<datasource>', '<group name="entry" type="NXentry"></group>', '</datasource>'))) self.assertEqual(parser.getContentHandler(), handler)
def icmptype_reader(filename, path): icmptype = IcmpType() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "%s is missing .xml suffix" % filename) icmptype.name = filename[:-4] icmptype.check_name(icmptype.name) icmptype.filename = filename icmptype.path = path icmptype.builtin = False if path.startswith(config.ETC_FIREWALLD) else True icmptype.default = icmptype.builtin handler = icmptype_ContentHandler(icmptype) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_ICMPTYPE, "not a valid icmptype file: %s" % \ msg.getException()) del handler del parser if PY2: icmptype.encode_strings() return icmptype
def _parse(self, data): inpsrc = sax.InputSource() inpsrc.setByteStream(BytesIO(data)) try: self._parser.parse(inpsrc) except sax.SAXParseException as exc: new_exc = AssertionError(u'invalid document: %s' % exc) new_exc.position = (exc._linenum, exc._colnum) raise new_exc return super(XMLSyntaxValidator, self)._parse(data)
def ipset_reader(filename, path): ipset = IPSet() if not filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % filename) ipset.name = filename[:-4] ipset.check_name(ipset.name) ipset.filename = filename ipset.path = path ipset.builtin = False if path.startswith(config.ETC_FIREWALLD) else True ipset.default = ipset.builtin handler = ipset_ContentHandler(ipset) parser = sax.make_parser() parser.setContentHandler(handler) name = "%s/%s" % (path, filename) with open(name, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_IPSET, "not a valid ipset file: %s" % \ msg.getException()) del handler del parser if "timeout" in ipset.options and ipset.options["timeout"] != "0" and \ len(ipset.entries) > 0: # no entries visible for ipsets with timeout log.warning("ipset '%s': timeout option is set, entries are ignored", ipset.name) del ipset.entries[:] i = 0 entries_set = set() while i < len(ipset.entries): if ipset.entries[i] in entries_set: log.warning("Entry %s already set, ignoring.", ipset.entries[i]) ipset.entries.pop(i) else: try: ipset.check_entry(ipset.entries[i], ipset.options, ipset.type) except FirewallError as e: log.warning("%s, ignoring.", e) ipset.entries.pop(i) else: entries_set.add(ipset.entries[i]) i += 1 del entries_set if PY2: ipset.encode_strings() return ipset
def parse_xmlstream(self, stream): """ Take a byte stream has input and parse the xml content. """ source = sax.InputSource() source.setByteStream(stream) try: self.sax_parser.parse(source) except Exception as e: raise Exception("Error " + self.current_state()) from e return self.to_delete
def parse_xmlstream(self, stream): """ Take a byte stream has input and parse the xml content. """ source = sax.InputSource() source.setByteStream(stream) try: self.sax_parser.parse(source) except Exception: logger.error("Error while parsing xml file:\n" + self.current_state(), exc_info=True) raise return self.to_delete
def read(self): self.cleanup() if not self.filename.endswith(".xml"): raise FirewallError(errors.INVALID_NAME, "'%s' is missing .xml suffix" % self.filename) handler = direct_ContentHandler(self) parser = sax.make_parser() parser.setContentHandler(handler) with open(self.filename, "rb") as f: source = sax.InputSource(None) source.setByteStream(f) try: parser.parse(source) except sax.SAXParseException as msg: raise FirewallError(errors.INVALID_TYPE, "Not a valid file: %s" % \ msg.getException())
def test_XML_group_group(self): fun = sys._getframe().f_code.co_name print("Run: %s.%s() " % (self.__class__.__name__, fun)) parser = sax.make_parser() handler = sax.ContentHandler() attrs = {"type": "NXentry"} name = "group" xml = '<group type="NXentry" name="entry1">' + \ '<group type="NXinstrument" name="instrument"></group></group>' el = InnerXMLHandler(parser, handler, name, attrs) parser.setContentHandler(el) self.assertEqual(el.xml, None) inpsrc = sax.InputSource() inpsrc.setByteStream(StringIO(xml)) parser.parse(inpsrc) self.assertEqual(parser.getContentHandler(), el) self.assertEqual(el.endElement("group"), None) self.assertEqual(len(el.xml), 3) self.assertEqual(el.xml[0], '<group type="NXentry">') self.assertTrue( (el.xml[1] == '<group type="NXentry" name="entry1">' '<group type="NXinstrument" name="instrument"></group></group>') or (el.xml[1] == '<group type="NXentry" name="entry1">' '<group name="instrument" type="NXinstrument"></group></group>') or (el.xml[1] == '<group name="entry1" type="NXentry">' '<group type="NXinstrument" name="instrument"></group></group>') or (el.xml[1] == '<group name="entry1" type="NXentry">' '<group name="instrument" type="NXinstrument"></group></group>')) self.assertEqual(el.xml[2], '</group>') self.assertEqual(parser.getContentHandler(), handler)
def resolveEntity(self, publicId, systemId): print publicId, systemId return _SAX.InputSource(systemId)