def iter_from_xml_buffer( buff: IO[bytes], filter_attrs: Optional[Iterable[str]] = None, read_chunk_size: int = 8192) -> Iterator[Dict[str, Any]]: """Yields all items inside a given OSM XML buffer. `filter_attrs` is explained in osmiter.iter_from_osm documentation. """ # Create helper objects handler = OSMContentHandler(filter_attrs) parser = ExpatParser() parser.setContentHandler(handler) # Read data in chunks data = buff.read(read_chunk_size) while data: # Parse XML parser.feed(data) # Check if some features are available - # if so _move_ them to the user (so that we can discard them). if handler.features: yield from handler.features handler.features = [] # Read next chunk data = buff.read(read_chunk_size) # Finalize the parser parser.close() # Final check if some features are left if handler.features: yield from handler.features
class XMLFeeder: def __init__(self, handler=None): if handler is None: handler = XMLHandler() self.parser = ExpatParser() self.parser.setContentHandler(handler) # we need to fake a root element self.parser.feed("<root>") def write_message(self, data): self.parser.feed(data)
def feed(self, data, isFinal = 0): # Error messages returned by the ScanProsite server are formatted as # as plain text instead of an XML document. To catch such error # messages, we override the feed method of the Expat parser. # The error message is (hopefully) contained in the data that was just # fed to the parser. if self.firsttime: if data[:5].decode('utf-8') != "<?xml": raise ValueError(data) self.firsttime = False return ExpatParser.feed(self, data, isFinal)
def feed(self, data, isFinal=0): # Error messages returned by the ScanProsite server are formatted as # as plain text instead of an XML document. To catch such error # messages, we override the feed method of the Expat parser. # The error message is (hopefully) contained in the data that was just # fed to the parser. if self.firsttime: if data[:5].decode('utf-8') != "<?xml": raise ValueError(data) self.firsttime = False return ExpatParser.feed(self, data, isFinal)
class BlobClient: """Sometimes it is necessary to understand the incoming INDI information on the serverside as opposed to sending the raw information to the httpclient. Here we would like to extract BLOB data so that it can be saved to disk. To do this we feed the xml to the ExpatParser which uses BlobHandler to pull out the base64 data and convert it to binary. """ def __init__(self): self.parser = ExpatParser() self.parser.setContentHandler(BlobHandler()) # we need to fake a root element self.parser.feed("<root>") def write_message(self, data): self.parser.feed(data)