def parse_xml(response): stream = QXmlStreamReader(response) document = XmlNode() current_node = document path = [] while not stream.atEnd(): stream.readNext() if stream.isStartElement(): node = XmlNode() attrs = stream.attributes() for i in range(attrs.count()): attr = attrs.at(i) node.attribs[_node_name(attr.name())] = attr.value() current_node.append_child(_node_name(stream.name()), node) path.append(current_node) current_node = node elif stream.isEndElement(): current_node = path.pop() elif stream.isCharacters(): current_node.text += stream.text() return document
def parse_xml(response): stream = QXmlStreamReader(response) document = XmlNode() current_node = document path = [] while not stream.atEnd(): stream.readNext() if stream.isStartElement(): node = XmlNode() attrs = stream.attributes() for i in range(attrs.count()): attr = attrs.at(i) node.attribs[_node_name(attr.name())] = string_(attr.value()) current_node.append_child(_node_name(stream.name()), node) path.append(current_node) current_node = node elif stream.isEndElement(): current_node = path.pop() elif stream.isCharacters(): current_node.text += stream.text() return document
def __feedLoaded(self, reply): """ Private slot to extract the loaded feed data. @param reply reference to the network reply @type QNetworkReply """ if id(reply) not in self.__replies: return topItem = self.__replies[id(reply)][1] del self.__replies[id(reply)] if reply.error() == QNetworkReply.NoError: linkString = "" titleString = "" xml = QXmlStreamReader() xmlData = reply.readAll() xml.addData(xmlData) while not xml.atEnd(): xml.readNext() if xml.isStartElement(): if xml.name() == "item": linkString = xml.attributes().value("rss:about") elif xml.name() == "link": linkString = xml.attributes().value("href") currentTag = xml.name() elif xml.isEndElement(): if xml.name() in ["item", "entry"]: itm = QTreeWidgetItem(topItem) itm.setText(0, titleString) itm.setData(0, FeedsManager.UrlStringRole, linkString) itm.setIcon(0, UI.PixmapCache.getIcon("rss16.png")) linkString = "" titleString = "" elif xml.isCharacters() and not xml.isWhitespace(): if currentTag == "title": titleString = xml.text() elif currentTag == "link": linkString += xml.text() if topItem.childCount() == 0: itm = QTreeWidgetItem(topItem) itm.setText(0, self.tr("Error fetching feed")) itm.setData(0, FeedsManager.UrlStringRole, "") itm.setData(0, FeedsManager.ErrorDataRole, str(xmlData, encoding="utf-8")) topItem.setExpanded(True) else: linkString = "" titleString = reply.errorString() itm = QTreeWidgetItem(topItem) itm.setText(0, titleString) itm.setData(0, FeedsManager.UrlStringRole, linkString) topItem.setExpanded(True)
def __feedLoaded(self): """ Private slot to extract the loaded feed data. """ reply = self.sender() if id(reply) not in self.__replies: return topItem = self.__replies[id(reply)][1] del self.__replies[id(reply)] if reply.error() == QNetworkReply.NoError: linkString = "" titleString = "" xml = QXmlStreamReader() xmlData = reply.readAll() xml.addData(xmlData) while not xml.atEnd(): xml.readNext() if xml.isStartElement(): if xml.name() == "item": linkString = xml.attributes().value("rss:about") elif xml.name() == "link": linkString = xml.attributes().value("href") currentTag = xml.name() elif xml.isEndElement(): if xml.name() in ["item", "entry"]: itm = QTreeWidgetItem(topItem) itm.setText(0, titleString) itm.setData(0, FeedsManager.UrlStringRole, linkString) itm.setIcon(0, UI.PixmapCache.getIcon("rss16.png")) linkString = "" titleString = "" elif xml.isCharacters() and not xml.isWhitespace(): if currentTag == "title": titleString = xml.text() elif currentTag == "link": linkString += xml.text() if topItem.childCount() == 0: itm = QTreeWidgetItem(topItem) itm.setText(0, self.tr("Error fetching feed")) itm.setData(0, FeedsManager.UrlStringRole, "") itm.setData(0, FeedsManager.ErrorDataRole, str(xmlData, encoding="utf-8")) topItem.setExpanded(True) else: linkString = "" titleString = reply.errorString() itm = QTreeWidgetItem(topItem) itm.setText(0, titleString) itm.setData(0, FeedsManager.UrlStringRole, linkString) topItem.setExpanded(True)
def handleNetworkData(self, networkReply: QNetworkReply): if networkReply.error() == QNetworkReply.NoError: choices = [] response = networkReply.readAll() xml = QXmlStreamReader(response) while not xml.atEnd(): xml.readNext() if xml.tokenType() == QXmlStreamReader.StartElement: if xml.name() == 'suggestion': string = xml.attributes().value('data') choices.append(string) self.showCompletion(choices) networkReply.deleteLater()
def _parseResult(self, reply): xml = reply.readAll() reader = QXmlStreamReader(xml) while not reader.atEnd(): reader.readNext() if reader.name() != "geometry": continue reader.readNextStartElement() if reader.name() != "location": continue reader.readNextStartElement() if reader.name() != "lat": continue latitude = float(reader.readElementText()) reader.readNextStartElement() if reader.name() != "lng": continue longitude = float(reader.readElementText()) return latitude, longitude raise GeoCoder.NotFoundError
def handleResponse(self, reply): er = reply.error() if er == QtNetwork.QNetworkReply.NoError: data = reply.readAll() r = QXmlStreamReader(str(data, 'UTF-8')) while not r.atEnd(): r.readNext() if (r.qualifiedName() == "yweather:location"): if r.isStartElement(): atr = r.attributes() print("City:", atr.value("city")) print("Country:", atr.value("country")) if (r.qualifiedName() == "yweather:condition"): if r.isStartElement(): atr = r.attributes() print("Date:", atr.value("date")) print("Condition:", atr.value("text")) print("Temperature:", \ atr.value("temp"), "deg C") if r.hasError(): print("Error reading feed") else: print("Error occured: ", er) print(er) print(reply.errorString()) QCoreApplication.quit()
class MapReaderPrivate(): def tr(self, sourceText, disambiguation = '', n = -1): return QCoreApplication.translate('MapReader', sourceText, disambiguation, n) def trUtf8(self, sourceText, disambiguation = '', n = -1): return QCoreApplication.translate('MapReader', sourceText, disambiguation, n) def __init__(self, mapReader): self.p = mapReader self.mMap = None self.mError = QString('') self.mReadingExternalTileset = False self.xml = QXmlStreamReader() self.mGidMapper = GidMapper() def readMap(self, device, path): self.mError = QString('') self.mPath = path map = None self.xml.setDevice(device) if (self.xml.readNextStartElement() and self.xml.name() == "map"): map = self.__readMap() else: self.xml.raiseError(self.tr("Not a map file.")) self.mGidMapper.clear() return map def readTileset(self, device, path): self.mError = '' self.mPath = path tileset = None self.mReadingExternalTileset = True self.xml.setDevice(device) if (self.xml.readNextStartElement() and self.xml.name() == "tileset"): tileset = self.__readTileset() else: self.xml.raiseError(self.tr("Not a tileset file.")) self.mReadingExternalTileset = False return tileset def openFile(self, file): if (not file.exists()): self.mError = self.tr("File not found: %s"%file.fileName()) return False elif (not file.open(QFile.ReadOnly | QFile.Text)): self.mError = self.tr("Unable to read file: %s"%file.fileName()) return False return True def errorString(self): if self.mError != '': return self.mError else: return self.tr("%d\n\nLine %d, column %s"%(self.xml.lineNumber(), self.xml.columnNumber(), self.xml.errorString())) def __readUnknownElement(self): qDebug("Unknown element (fixme): "+self.xml.name()+" at line "+self.xml.lineNumber()+", column "+self.xml.columnNumber()) self.xml.skipCurrentElement() def __readMap(self): atts = self.xml.attributes() mapWidth = Int(atts.value("width")) mapHeight = Int(atts.value("height")) tileWidth = Int(atts.value("tilewidth")) tileHeight = Int(atts.value("tileheight")) hexSideLength = Int(atts.value("hexsidelength")) orientationString = atts.value("orientation") orientation = orientationFromString(orientationString) if (orientation == Map.Orientation.Unknown): self.xml.raiseError(self.tr("Unsupported map orientation: \"%s\""%orientationString)) staggerAxisString = atts.value("staggeraxis") staggerAxis = staggerAxisFromString(staggerAxisString) staggerIndexString = atts.value("staggerindex") staggerIndex = staggerIndexFromString(staggerIndexString) renderOrderString = atts.value("renderorder") renderOrder = renderOrderFromString(renderOrderString) nextObjectId = Int(atts.value("nextobjectid")) self.mMap = Map(orientation, mapWidth, mapHeight, tileWidth, tileHeight) self.mMap.setHexSideLength(hexSideLength) self.mMap.setStaggerAxis(staggerAxis) self.mMap.setStaggerIndex(staggerIndex) self.mMap.setRenderOrder(renderOrder) if (nextObjectId): self.mMap.setNextObjectId(nextObjectId) bgColorString = atts.value("backgroundcolor") if len(bgColorString)>0: self.mMap.setBackgroundColor(QColor(bgColorString)) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): self.mMap.mergeProperties(self.__readProperties()) elif (self.xml.name() == "tileset"): self.mMap.addTileset(self.__readTileset()) elif (self.xml.name() == "layer"): self.mMap.addLayer(self.__readLayer()) elif (self.xml.name() == "objectgroup"): self.mMap.addLayer(self.__readObjectGroup()) elif (self.xml.name() == "imagelayer"): self.mMap.addLayer(self.__readImageLayer()) else: self.__readUnknownElement() # Clean up in case of error if (self.xml.hasError()): self.mMap = None return self.mMap def __readTileset(self): atts = self.xml.attributes() source = atts.value("source") firstGid = Int(atts.value("firstgid")) tileset = None if source == '': # Not an external tileset name = atts.value("name") tileWidth = Int(atts.value("tilewidth")) tileHeight = Int(atts.value("tileheight")) tileSpacing = Int(atts.value("spacing")) margin = Int(atts.value("margin")) if (tileWidth < 0 or tileHeight < 0 or (firstGid == 0 and not self.mReadingExternalTileset)): self.xml.raiseError(self.tr("Invalid tileset parameters for tileset '%s'"%name)) else: tileset = Tileset.create(name, tileWidth, tileHeight, tileSpacing, margin) while (self.xml.readNextStartElement()): if (self.xml.name() == "tile"): self.__readTilesetTile(tileset) elif (self.xml.name() == "tileoffset"): oa = self.xml.attributes() x = Int(oa.value("x")) y = Int(oa.value("y")) tileset.setTileOffset(QPoint(x, y)) self.xml.skipCurrentElement() elif (self.xml.name() == "properties"): tileset.mergeProperties(self.__readProperties()) elif (self.xml.name() == "image"): if (tileWidth == 0 or tileHeight == 0): self.xml.raiseError(self.tr("Invalid tileset parameters for tileset '%s'"%name)) tileset.clear() break else: self.__readTilesetImage(tileset) elif (self.xml.name() == "terraintypes"): self.__readTilesetTerrainTypes(tileset) else: self.__readUnknownElement() else: # External tileset absoluteSource = self.p.resolveReference(source, self.mPath) tileset, error = self.p.readExternalTileset(absoluteSource) if (not tileset): self.xml.raiseError(self.tr("Error while loading tileset '%s': %s"%(absoluteSource, error))) self.xml.skipCurrentElement() if (tileset and not self.mReadingExternalTileset): self.mGidMapper.insert(firstGid, tileset) return tileset def __readTilesetTile(self, tileset): atts = self.xml.attributes() id = Int(atts.value("id")) if (id < 0): self.xml.raiseError(self.tr("Invalid tile ID: %d"%id)) return hasImage = tileset.imageSource()!='' if (hasImage and id >= tileset.tileCount()): self.xml.raiseError(self.tr("Tile ID does not exist in tileset image: %d"%id)) return if (id > tileset.tileCount()): self.xml.raiseError(self.tr("Invalid (nonconsecutive) tile ID: %d"%id)) return # For tilesets without image source, consecutive tile IDs are allowed (for # tiles with individual images) if (id == tileset.tileCount()): tileset.addTile(QPixmap()) tile = tileset.tileAt(id) # Read tile quadrant terrain ids terrain = atts.value("terrain") if terrain != '': quadrants = terrain.split(",") if (len(quadrants) == 4): for i in range(4): if quadrants[i]=='': t = -1 else: t = Int(quadrants[i]) tile.setCornerTerrainId(i, t) # Read tile probability probability = atts.value("probability") if probability != '': tile.setProbability(Float(probability)) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): tile.mergeProperties(self.__readProperties()) elif (self.xml.name() == "image"): source = self.xml.attributes().value("source") if source != '': source = self.p.resolveReference(source, self.mPath) tileset.setTileImage(id, QPixmap.fromImage(self.__readImage()), source) elif (self.xml.name() == "objectgroup"): tile.setObjectGroup(self.__readObjectGroup()) elif (self.xml.name() == "animation"): tile.setFrames(self.__readAnimationFrames()) else: self.__readUnknownElement() # Temporary code to support TMW-style animation frame properties if (not tile.isAnimated() and tile.hasProperty("animation-frame0")): frames = QVector() i = 0 while(i>=0): frameName = "animation-frame" + str(i) delayName = "animation-delay" + str(i) if (tile.hasProperty(frameName) and tile.hasProperty(delayName)): frame = Frame() frame.tileId = tile.property(frameName) frame.duration = tile.property(delayName) * 10 frames.append(frame) else: break i += 1 tile.setFrames(frames) def __readTilesetImage(self, tileset): atts = self.xml.attributes() source = atts.value("source") trans = atts.value("trans") if len(trans)>0: if (not trans.startswith('#')): trans = '#' + trans tileset.setTransparentColor(QColor(trans)) if len(source)>0: source = self.p.resolveReference(source, self.mPath) # Set the width that the tileset had when the map was saved width = Int(atts.value("width")) self.mGidMapper.setTilesetWidth(tileset, width) if (not tileset.loadFromImage(self.__readImage(), source)): self.xml.raiseError(self.tr("Error loading tileset image:\n'%s'"%source)) def __readTilesetTerrainTypes(self, tileset): while (self.xml.readNextStartElement()): if (self.xml.name() == "terrain"): atts = self.xml.attributes() name = atts.value("name") tile = Int(atts.value("tile")) terrain = tileset.addTerrain(name, tile) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): terrain.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() else: self.__readUnknownElement() def __readImage(self): atts = self.xml.attributes() source = atts.value("source") format = atts.value("format") if len(source)==0: while (self.xml.readNextStartElement()): if (self.xml.name() == "data"): atts = self.xml.attributes() encoding = atts.value("encoding") data = self.xml.readElementText().toLatin1() if (encoding == "base64"): data = QByteArray.fromBase64(data) self.xml.skipCurrentElement() return QImage.fromData(data, format.toLatin1()) else: self.__readUnknownElement() else: self.xml.skipCurrentElement() source = self.p.resolveReference(source, self.mPath) image = self.p.readExternalImage(source) if (image.isNull()): self.xml.raiseError(self.tr("Error loading image:\n'%s'"%source)) return image return QImage() def __readLayer(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) tileLayer = TileLayer(name, x, y, width, height) readLayerAttributes(tileLayer, atts) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): tileLayer.mergeProperties(self.__readProperties()) elif (self.xml.name() == "data"): self.__readLayerData(tileLayer) else: self.__readUnknownElement() return tileLayer def __readLayerData(self, tileLayer): atts = self.xml.attributes() encoding = atts.value("encoding") compression = atts.value("compression") layerDataFormat = 0 if (encoding == ''): layerDataFormat = Map.LayerDataFormat.XML elif (encoding == "csv"): layerDataFormat = Map.LayerDataFormat.CSV elif (encoding == "base64"): if (compression == ''): layerDataFormat = Map.LayerDataFormat.Base64 elif (compression == "gzip"): layerDataFormat = Map.LayerDataFormat.Base64Gzip elif (compression == "zlib"): layerDataFormat = Map.LayerDataFormat.Base64Zlib else: self.xml.raiseError(self.tr("Compression method '%s' not supported"%compression)) return else: self.xml.raiseError(self.tr("Unknown encoding: %s"%encoding)) return self.mMap.setLayerDataFormat(layerDataFormat) x = 0 y = 0 while (self.xml.readNext() != QXmlStreamReader.Invalid): if (self.xml.isEndElement()): break elif (self.xml.isStartElement()): if (self.xml.name() == "tile"): if (y >= tileLayer.height()): self.xml.raiseError(self.tr("Too many <tile> elements")) continue atts = self.xml.attributes() gid = Int(atts.value("gid")) tileLayer.setCell(x, y, self.__cellForGid(gid)) x += 1 if (x >= tileLayer.width()): x = 0 y += 1 self.xml.skipCurrentElement() else: self.__readUnknownElement() elif (self.xml.isCharacters() and not self.xml.isWhitespace()): if (encoding == "base64"): self.__decodeBinaryLayerData(tileLayer, self.xml.text(), layerDataFormat) elif (encoding == "csv"): self.__decodeCSVLayerData(tileLayer, self.xml.text()) def __decodeBinaryLayerData(self, tileLayer, data, format): error = self.mGidMapper.decodeLayerData(tileLayer, data, format) if error==DecodeError.CorruptLayerData: self.xml.raiseError(self.tr("Corrupt layer data for layer '%s'"%tileLayer.name())) return elif error==DecodeError.TileButNoTilesets: self.xml.raiseError(self.tr("Tile used but no tilesets specified")) return elif error==DecodeError.InvalidTile: self.xml.raiseError(self.tr("Invalid tile: %d"%self.mGidMapper.invalidTile())) return elif error==DecodeError.NoError: pass def __decodeCSVLayerData(self, tileLayer, text): trimText = text.strip() tiles = trimText.split(',') if (len(tiles) != tileLayer.width() * tileLayer.height()): self.xml.raiseError(self.tr("Corrupt layer data for layer '%s'"%tileLayer.name())) return for y in range(tileLayer.height()): for x in range(tileLayer.width()): conversionOk = False gid, conversionOk = Int2(tiles[y * tileLayer.width() + x]) if (not conversionOk): self.xml.raiseError(self.tr("Unable to parse tile at (%d,%d) on layer '%s'"%(x + 1, y + 1, tileLayer.name()))) return tileLayer.setCell(x, y, self.__cellForGid(gid)) ## # Returns the cell for the given global tile ID. Errors are raised with # the QXmlStreamReader. # # @param gid the global tile ID # @return the cell data associated with the given global tile ID, or an # empty cell if not found ## def __cellForGid(self, gid): ok = False result, ok = self.mGidMapper.gidToCell(gid) if (not ok): if (self.mGidMapper.isEmpty()): self.xml.raiseError(self.tr("Tile used but no tilesets specified")) else: self.xml.raiseError(self.tr("Invalid tile: %d"%gid)) return result def __readImageLayer(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) imageLayer = ImageLayer(name, x, y, width, height) readLayerAttributes(imageLayer, atts) while (self.xml.readNextStartElement()): if (self.xml.name() == "image"): self.__readImageLayerImage(imageLayer) elif (self.xml.name() == "properties"): imageLayer.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() return imageLayer def __readImageLayerImage(self, imageLayer): atts = self.xml.attributes() source = atts.value("source") trans = atts.value("trans") if trans != '': if (not trans.startswith('#')): trans = '#' + trans imageLayer.setTransparentColor(QColor(trans)) source = self.p.resolveReference(source, self.mPath) imageLayerImage = self.p.readExternalImage(source) if (not imageLayer.loadFromImage(imageLayerImage, source)): self.xml.raiseError(self.tr("Error loading image layer image:\n'%s'"%source)) self.xml.skipCurrentElement() def __readObjectGroup(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) objectGroup = ObjectGroup(name, x, y, width, height) readLayerAttributes(objectGroup, atts) color = atts.value("color") if color != '': objectGroup.setColor(color) if (atts.hasAttribute("draworder")): value = atts.value("draworder") drawOrder = drawOrderFromString(value) if (drawOrder == ObjectGroup.DrawOrder.UnknownOrder): #del objectGroup self.xml.raiseError(self.tr("Invalid draw order: %s"%value)) return None objectGroup.setDrawOrder(drawOrder) while (self.xml.readNextStartElement()): if (self.xml.name() == "object"): objectGroup.addObject(self.__readObject()) elif (self.xml.name() == "properties"): objectGroup.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() return objectGroup def __readObject(self): atts = self.xml.attributes() id = Int(atts.value("id")) name = atts.value("name") gid = Int(atts.value("gid")) x = Float(atts.value("x")) y = Float(atts.value("y")) width = Float(atts.value("width")) height = Float(atts.value("height")) type = atts.value("type") visibleRef = atts.value("visible") pos = QPointF(x, y) size = QSizeF(width, height) object = MapObject(name, type, pos, size) object.setId(id) try: rotation = Float(atts.value("rotation")) ok = True except: ok = False if (ok): object.setRotation(rotation) if (gid): object.setCell(self.__cellForGid(gid)) if (not object.cell().isEmpty()): tileSize = object.cell().tile.size() if (width == 0): object.setWidth(tileSize.width()) if (height == 0): object.setHeight(tileSize.height()) try: visible = int(visibleRef) ok = True except: ok = False if ok: object.setVisible(visible) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): object.mergeProperties(self.__readProperties()) elif (self.xml.name() == "polygon"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polygon) elif (self.xml.name() == "polyline"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polyline) elif (self.xml.name() == "ellipse"): self.xml.skipCurrentElement() object.setShape(MapObject.Ellipse) else: self.__readUnknownElement() return object def __readPolygon(self): atts = self.xml.attributes() points = atts.value("points") pointsList = list(filter(lambda x:x.strip()!='', points.split(' '))) polygon = QPolygonF() ok = True for point in pointsList: try: x, y = point.split(',') except: ok = False break x, ok = Float2(x) if (not ok): break y, ok = Float2(y) if (not ok): break polygon.append(QPointF(x, y)) if (not ok): self.xml.raiseError(self.tr("Invalid points data for polygon")) self.xml.skipCurrentElement() return polygon def __readAnimationFrames(self): frames = QVector() while (self.xml.readNextStartElement()): if (self.xml.name() == "frame"): atts = self.xml.attributes() frame = Frame() frame.tileId = Int(atts.value("tileid")) frame.duration = Int(atts.value("duration")) frames.append(frame) self.xml.skipCurrentElement() else: self.__readUnknownElement() return frames def __readProperties(self): properties = Properties() while (self.xml.readNextStartElement()): if (self.xml.name() == "property"): self.__readProperty(properties) else: self.__readUnknownElement() return properties def __readProperty(self, properties): atts = self.xml.attributes() propertyName = atts.value("name") propertyValue = atts.value("value") while (self.xml.readNext() != QXmlStreamReader.Invalid): if (self.xml.isEndElement()): break elif (self.xml.isCharacters() and not self.xml.isWhitespace()): if (propertyValue.isEmpty()): propertyValue = self.xml.text() elif (self.xml.isStartElement()): self.__readUnknownElement() properties.insert(propertyName, propertyValue)
def __addDefaultActions(self): """ Private slot to add the default user agent entries. @return flag indicating that a user agent entry is checked (boolean) """ from . import UserAgentDefaults_rc # __IGNORE_WARNING__ defaultUserAgents = QFile(":/UserAgentDefaults.xml") defaultUserAgents.open(QIODevice.ReadOnly) menuStack = [] isChecked = False if self.__url: currentUserAgentString = self.__manager.userAgentForUrl(self.__url) else: from Helpviewer.HelpBrowserWV import HelpWebPage currentUserAgentString = HelpWebPage().userAgent() xml = QXmlStreamReader(defaultUserAgents) while not xml.atEnd(): xml.readNext() if xml.isStartElement() and xml.name() == "separator": if menuStack: menuStack[-1].addSeparator() else: self.addSeparator() continue if xml.isStartElement() and xml.name() == "useragent": attributes = xml.attributes() title = attributes.value("description") userAgent = attributes.value("useragent") act = QAction(self) act.setText(title) act.setData(userAgent) act.setToolTip(userAgent) act.setCheckable(True) act.setChecked(userAgent == currentUserAgentString) act.triggered.connect(self.__changeUserAgent) if menuStack: menuStack[-1].addAction(act) else: self.addAction(act) self.__actionGroup.addAction(act) isChecked = isChecked or act.isChecked() if xml.isStartElement() and xml.name() == "useragentmenu": attributes = xml.attributes() title = attributes.value("title") if title == "v_a_r_i_o_u_s": title = self.tr("Various") menu = QMenu(self) menu.setTitle(title) self.addMenu(menu) menuStack.append(menu) if xml.isEndElement() and xml.name() == "useragentmenu": menuStack.pop() if xml.hasError(): E5MessageBox.critical( self, self.tr("Parsing default user agents"), self.tr( """<p>Error parsing default user agents.</p><p>{0}</p>"""). format(xml.errorString())) return isChecked
class MapReaderPrivate(): def tr(self, sourceText, disambiguation='', n=-1): return QCoreApplication.translate('MapReader', sourceText, disambiguation, n) def trUtf8(self, sourceText, disambiguation='', n=-1): return QCoreApplication.translate('MapReader', sourceText, disambiguation, n) def __init__(self, mapReader): self.p = mapReader self.mMap = None self.mError = QString('') self.mReadingExternalTileset = False self.xml = QXmlStreamReader() self.mGidMapper = GidMapper() def readMap(self, device, path): self.mError = QString('') self.mPath = path map = None self.xml.setDevice(device) if (self.xml.readNextStartElement() and self.xml.name() == "map"): map = self.__readMap() else: self.xml.raiseError(self.tr("Not a map file.")) self.mGidMapper.clear() return map def readTileset(self, device, path): self.mError = '' self.mPath = path tileset = None self.mReadingExternalTileset = True self.xml.setDevice(device) if (self.xml.readNextStartElement() and self.xml.name() == "tileset"): tileset = self.__readTileset() else: self.xml.raiseError(self.tr("Not a tileset file.")) self.mReadingExternalTileset = False return tileset def openFile(self, file): if (not file.exists()): self.mError = self.tr("File not found: %s" % file.fileName()) return False elif (not file.open(QFile.ReadOnly | QFile.Text)): self.mError = self.tr("Unable to read file: %s" % file.fileName()) return False return True def errorString(self): if self.mError != '': return self.mError else: return self.tr("%d\n\nLine %d, column %s" % (self.xml.lineNumber(), self.xml.columnNumber(), self.xml.errorString())) def __readUnknownElement(self): qDebug("Unknown element (fixme): " + self.xml.name() + " at line " + self.xml.lineNumber() + ", column " + self.xml.columnNumber()) self.xml.skipCurrentElement() def __readMap(self): atts = self.xml.attributes() mapWidth = Int(atts.value("width")) mapHeight = Int(atts.value("height")) tileWidth = Int(atts.value("tilewidth")) tileHeight = Int(atts.value("tileheight")) hexSideLength = Int(atts.value("hexsidelength")) orientationString = atts.value("orientation") orientation = orientationFromString(orientationString) if (orientation == Map.Orientation.Unknown): self.xml.raiseError( self.tr("Unsupported map orientation: \"%s\"" % orientationString)) staggerAxisString = atts.value("staggeraxis") staggerAxis = staggerAxisFromString(staggerAxisString) staggerIndexString = atts.value("staggerindex") staggerIndex = staggerIndexFromString(staggerIndexString) renderOrderString = atts.value("renderorder") renderOrder = renderOrderFromString(renderOrderString) nextObjectId = Int(atts.value("nextobjectid")) self.mMap = Map(orientation, mapWidth, mapHeight, tileWidth, tileHeight) self.mMap.setHexSideLength(hexSideLength) self.mMap.setStaggerAxis(staggerAxis) self.mMap.setStaggerIndex(staggerIndex) self.mMap.setRenderOrder(renderOrder) if (nextObjectId): self.mMap.setNextObjectId(nextObjectId) bgColorString = atts.value("backgroundcolor") if len(bgColorString) > 0: self.mMap.setBackgroundColor(QColor(bgColorString)) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): self.mMap.mergeProperties(self.__readProperties()) elif (self.xml.name() == "tileset"): self.mMap.addTileset(self.__readTileset()) elif (self.xml.name() == "layer"): self.mMap.addLayer(self.__readLayer()) elif (self.xml.name() == "objectgroup"): self.mMap.addLayer(self.__readObjectGroup()) elif (self.xml.name() == "imagelayer"): self.mMap.addLayer(self.__readImageLayer()) else: self.__readUnknownElement() # Clean up in case of error if (self.xml.hasError()): self.mMap = None return self.mMap def __readTileset(self): atts = self.xml.attributes() source = atts.value("source") firstGid = Int(atts.value("firstgid")) tileset = None if source == '': # Not an external tileset name = atts.value("name") tileWidth = Int(atts.value("tilewidth")) tileHeight = Int(atts.value("tileheight")) tileSpacing = Int(atts.value("spacing")) margin = Int(atts.value("margin")) if (tileWidth < 0 or tileHeight < 0 or (firstGid == 0 and not self.mReadingExternalTileset)): self.xml.raiseError( self.tr("Invalid tileset parameters for tileset '%s'" % name)) else: tileset = Tileset.create(name, tileWidth, tileHeight, tileSpacing, margin) while (self.xml.readNextStartElement()): if (self.xml.name() == "tile"): self.__readTilesetTile(tileset) elif (self.xml.name() == "tileoffset"): oa = self.xml.attributes() x = Int(oa.value("x")) y = Int(oa.value("y")) tileset.setTileOffset(QPoint(x, y)) self.xml.skipCurrentElement() elif (self.xml.name() == "properties"): tileset.mergeProperties(self.__readProperties()) elif (self.xml.name() == "image"): if (tileWidth == 0 or tileHeight == 0): self.xml.raiseError( self. tr("Invalid tileset parameters for tileset '%s'" % name)) tileset.clear() break else: self.__readTilesetImage(tileset) elif (self.xml.name() == "terraintypes"): self.__readTilesetTerrainTypes(tileset) else: self.__readUnknownElement() else: # External tileset absoluteSource = self.p.resolveReference(source, self.mPath) tileset, error = self.p.readExternalTileset(absoluteSource) if (not tileset): self.xml.raiseError( self.tr("Error while loading tileset '%s': %s" % (absoluteSource, error))) self.xml.skipCurrentElement() if (tileset and not self.mReadingExternalTileset): self.mGidMapper.insert(firstGid, tileset) return tileset def __readTilesetTile(self, tileset): atts = self.xml.attributes() id = Int(atts.value("id")) if (id < 0): self.xml.raiseError(self.tr("Invalid tile ID: %d" % id)) return hasImage = tileset.imageSource() != '' if (hasImage and id >= tileset.tileCount()): self.xml.raiseError( self.tr("Tile ID does not exist in tileset image: %d" % id)) return if (id > tileset.tileCount()): self.xml.raiseError( self.tr("Invalid (nonconsecutive) tile ID: %d" % id)) return # For tilesets without image source, consecutive tile IDs are allowed (for # tiles with individual images) if (id == tileset.tileCount()): tileset.addTile(QPixmap()) tile = tileset.tileAt(id) # Read tile quadrant terrain ids terrain = atts.value("terrain") if terrain != '': quadrants = terrain.split(",") if (len(quadrants) == 4): for i in range(4): if quadrants[i] == '': t = -1 else: t = Int(quadrants[i]) tile.setCornerTerrainId(i, t) # Read tile probability probability = atts.value("probability") if probability != '': tile.setProbability(Float(probability)) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): tile.mergeProperties(self.__readProperties()) elif (self.xml.name() == "image"): source = self.xml.attributes().value("source") if source != '': source = self.p.resolveReference(source, self.mPath) tileset.setTileImage(id, QPixmap.fromImage(self.__readImage()), source) elif (self.xml.name() == "objectgroup"): tile.setObjectGroup(self.__readObjectGroup()) elif (self.xml.name() == "animation"): tile.setFrames(self.__readAnimationFrames()) else: self.__readUnknownElement() # Temporary code to support TMW-style animation frame properties if (not tile.isAnimated() and tile.hasProperty("animation-frame0")): frames = QVector() i = 0 while (i >= 0): frameName = "animation-frame" + str(i) delayName = "animation-delay" + str(i) if (tile.hasProperty(frameName) and tile.hasProperty(delayName)): frame = Frame() frame.tileId = tile.property(frameName) frame.duration = tile.property(delayName) * 10 frames.append(frame) else: break i += 1 tile.setFrames(frames) def __readTilesetImage(self, tileset): atts = self.xml.attributes() source = atts.value("source") trans = atts.value("trans") if len(trans) > 0: if (not trans.startswith('#')): trans = '#' + trans tileset.setTransparentColor(QColor(trans)) if len(source) > 0: source = self.p.resolveReference(source, self.mPath) # Set the width that the tileset had when the map was saved width = Int(atts.value("width")) self.mGidMapper.setTilesetWidth(tileset, width) if (not tileset.loadFromImage(self.__readImage(), source)): self.xml.raiseError( self.tr("Error loading tileset image:\n'%s'" % source)) def __readTilesetTerrainTypes(self, tileset): while (self.xml.readNextStartElement()): if (self.xml.name() == "terrain"): atts = self.xml.attributes() name = atts.value("name") tile = Int(atts.value("tile")) terrain = tileset.addTerrain(name, tile) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): terrain.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() else: self.__readUnknownElement() def __readImage(self): atts = self.xml.attributes() source = atts.value("source") format = atts.value("format") if len(source) == 0: while (self.xml.readNextStartElement()): if (self.xml.name() == "data"): atts = self.xml.attributes() encoding = atts.value("encoding") data = self.xml.readElementText().toLatin1() if (encoding == "base64"): data = QByteArray.fromBase64(data) self.xml.skipCurrentElement() return QImage.fromData(data, format.toLatin1()) else: self.__readUnknownElement() else: self.xml.skipCurrentElement() source = self.p.resolveReference(source, self.mPath) image = self.p.readExternalImage(source) if (image.isNull()): self.xml.raiseError( self.tr("Error loading image:\n'%s'" % source)) return image return QImage() def __readLayer(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) tileLayer = TileLayer(name, x, y, width, height) readLayerAttributes(tileLayer, atts) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): tileLayer.mergeProperties(self.__readProperties()) elif (self.xml.name() == "data"): self.__readLayerData(tileLayer) else: self.__readUnknownElement() return tileLayer def __readLayerData(self, tileLayer): atts = self.xml.attributes() encoding = atts.value("encoding") compression = atts.value("compression") layerDataFormat = 0 if (encoding == ''): layerDataFormat = Map.LayerDataFormat.XML elif (encoding == "csv"): layerDataFormat = Map.LayerDataFormat.CSV elif (encoding == "base64"): if (compression == ''): layerDataFormat = Map.LayerDataFormat.Base64 elif (compression == "gzip"): layerDataFormat = Map.LayerDataFormat.Base64Gzip elif (compression == "zlib"): layerDataFormat = Map.LayerDataFormat.Base64Zlib else: self.xml.raiseError( self.tr("Compression method '%s' not supported" % compression)) return else: self.xml.raiseError(self.tr("Unknown encoding: %s" % encoding)) return self.mMap.setLayerDataFormat(layerDataFormat) x = 0 y = 0 while (self.xml.readNext() != QXmlStreamReader.Invalid): if (self.xml.isEndElement()): break elif (self.xml.isStartElement()): if (self.xml.name() == "tile"): if (y >= tileLayer.height()): self.xml.raiseError( self.tr("Too many <tile> elements")) continue atts = self.xml.attributes() gid = Int(atts.value("gid")) tileLayer.setCell(x, y, self.__cellForGid(gid)) x += 1 if (x >= tileLayer.width()): x = 0 y += 1 self.xml.skipCurrentElement() else: self.__readUnknownElement() elif (self.xml.isCharacters() and not self.xml.isWhitespace()): if (encoding == "base64"): self.__decodeBinaryLayerData(tileLayer, self.xml.text(), layerDataFormat) elif (encoding == "csv"): self.__decodeCSVLayerData(tileLayer, self.xml.text()) def __decodeBinaryLayerData(self, tileLayer, data, format): error = self.mGidMapper.decodeLayerData(tileLayer, data, format) if error == DecodeError.CorruptLayerData: self.xml.raiseError( self.tr("Corrupt layer data for layer '%s'" % tileLayer.name())) return elif error == DecodeError.TileButNoTilesets: self.xml.raiseError(self.tr("Tile used but no tilesets specified")) return elif error == DecodeError.InvalidTile: self.xml.raiseError( self.tr("Invalid tile: %d" % self.mGidMapper.invalidTile())) return elif error == DecodeError.NoError: pass def __decodeCSVLayerData(self, tileLayer, text): trimText = text.strip() tiles = trimText.split(',') if (len(tiles) != tileLayer.width() * tileLayer.height()): self.xml.raiseError( self.tr("Corrupt layer data for layer '%s'" % tileLayer.name())) return for y in range(tileLayer.height()): for x in range(tileLayer.width()): conversionOk = False gid, conversionOk = Int2(tiles[y * tileLayer.width() + x]) if (not conversionOk): self.xml.raiseError( self.tr( "Unable to parse tile at (%d,%d) on layer '%s'" % (x + 1, y + 1, tileLayer.name()))) return tileLayer.setCell(x, y, self.__cellForGid(gid)) ## # Returns the cell for the given global tile ID. Errors are raised with # the QXmlStreamReader. # # @param gid the global tile ID # @return the cell data associated with the given global tile ID, or an # empty cell if not found ## def __cellForGid(self, gid): ok = False result, ok = self.mGidMapper.gidToCell(gid) if (not ok): if (self.mGidMapper.isEmpty()): self.xml.raiseError( self.tr("Tile used but no tilesets specified")) else: self.xml.raiseError(self.tr("Invalid tile: %d" % gid)) return result def __readImageLayer(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) imageLayer = ImageLayer(name, x, y, width, height) readLayerAttributes(imageLayer, atts) while (self.xml.readNextStartElement()): if (self.xml.name() == "image"): self.__readImageLayerImage(imageLayer) elif (self.xml.name() == "properties"): imageLayer.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() return imageLayer def __readImageLayerImage(self, imageLayer): atts = self.xml.attributes() source = atts.value("source") trans = atts.value("trans") if trans != '': if (not trans.startswith('#')): trans = '#' + trans imageLayer.setTransparentColor(QColor(trans)) source = self.p.resolveReference(source, self.mPath) imageLayerImage = self.p.readExternalImage(source) if (not imageLayer.loadFromImage(imageLayerImage, source)): self.xml.raiseError( self.tr("Error loading image layer image:\n'%s'" % source)) self.xml.skipCurrentElement() def __readObjectGroup(self): atts = self.xml.attributes() name = atts.value("name") x = Int(atts.value("x")) y = Int(atts.value("y")) width = Int(atts.value("width")) height = Int(atts.value("height")) objectGroup = ObjectGroup(name, x, y, width, height) readLayerAttributes(objectGroup, atts) color = atts.value("color") if color != '': objectGroup.setColor(color) if (atts.hasAttribute("draworder")): value = atts.value("draworder") drawOrder = drawOrderFromString(value) if (drawOrder == ObjectGroup.DrawOrder.UnknownOrder): #del objectGroup self.xml.raiseError(self.tr("Invalid draw order: %s" % value)) return None objectGroup.setDrawOrder(drawOrder) while (self.xml.readNextStartElement()): if (self.xml.name() == "object"): objectGroup.addObject(self.__readObject()) elif (self.xml.name() == "properties"): objectGroup.mergeProperties(self.__readProperties()) else: self.__readUnknownElement() return objectGroup def __readObject(self): atts = self.xml.attributes() id = Int(atts.value("id")) name = atts.value("name") gid = Int(atts.value("gid")) x = Float(atts.value("x")) y = Float(atts.value("y")) width = Float(atts.value("width")) height = Float(atts.value("height")) type = atts.value("type") visibleRef = atts.value("visible") pos = QPointF(x, y) size = QSizeF(width, height) object = MapObject(name, type, pos, size) object.setId(id) try: rotation = Float(atts.value("rotation")) ok = True except: ok = False if (ok): object.setRotation(rotation) if (gid): object.setCell(self.__cellForGid(gid)) if (not object.cell().isEmpty()): tileSize = object.cell().tile.size() if (width == 0): object.setWidth(tileSize.width()) if (height == 0): object.setHeight(tileSize.height()) try: visible = int(visibleRef) ok = True except: ok = False if ok: object.setVisible(visible) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): object.mergeProperties(self.__readProperties()) elif (self.xml.name() == "polygon"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polygon) elif (self.xml.name() == "polyline"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polyline) elif (self.xml.name() == "ellipse"): self.xml.skipCurrentElement() object.setShape(MapObject.Ellipse) else: self.__readUnknownElement() return object def __readPolygon(self): atts = self.xml.attributes() points = atts.value("points") pointsList = list(filter(lambda x: x.strip() != '', points.split(' '))) polygon = QPolygonF() ok = True for point in pointsList: try: x, y = point.split(',') except: ok = False break x, ok = Float2(x) if (not ok): break y, ok = Float2(y) if (not ok): break polygon.append(QPointF(x, y)) if (not ok): self.xml.raiseError(self.tr("Invalid points data for polygon")) self.xml.skipCurrentElement() return polygon def __readAnimationFrames(self): frames = QVector() while (self.xml.readNextStartElement()): if (self.xml.name() == "frame"): atts = self.xml.attributes() frame = Frame() frame.tileId = Int(atts.value("tileid")) frame.duration = Int(atts.value("duration")) frames.append(frame) self.xml.skipCurrentElement() else: self.__readUnknownElement() return frames def __readProperties(self): properties = Properties() while (self.xml.readNextStartElement()): if (self.xml.name() == "property"): self.__readProperty(properties) else: self.__readUnknownElement() return properties def __readProperty(self, properties): atts = self.xml.attributes() propertyName = atts.value("name") propertyValue = atts.value("value") while (self.xml.readNext() != QXmlStreamReader.Invalid): if (self.xml.isEndElement()): break elif (self.xml.isCharacters() and not self.xml.isWhitespace()): if (propertyValue.isEmpty()): propertyValue = self.xml.text() elif (self.xml.isStartElement()): self.__readUnknownElement() properties.insert(propertyName, propertyValue)
def __addDefaultActions(self): """ Private slot to add the default user agent entries. @return flag indicating that a user agent entry is checked (boolean) """ from . import UserAgentDefaults_rc # __IGNORE_WARNING__ defaultUserAgents = QFile(":/UserAgentDefaults.xml") defaultUserAgents.open(QIODevice.ReadOnly) menuStack = [] isChecked = False if self.__url: currentUserAgentString = self.__manager.userAgentForUrl(self.__url) else: from Helpviewer.HelpBrowserWV import HelpWebPage currentUserAgentString = HelpWebPage().userAgent() xml = QXmlStreamReader(defaultUserAgents) while not xml.atEnd(): xml.readNext() if xml.isStartElement() and xml.name() == "separator": if menuStack: menuStack[-1].addSeparator() else: self.addSeparator() continue if xml.isStartElement() and xml.name() == "useragent": attributes = xml.attributes() title = attributes.value("description") userAgent = attributes.value("useragent") act = QAction(self) act.setText(title) act.setData(userAgent) act.setToolTip(userAgent) act.setCheckable(True) act.setChecked(userAgent == currentUserAgentString) act.triggered.connect(self.__changeUserAgent) if menuStack: menuStack[-1].addAction(act) else: self.addAction(act) self.__actionGroup.addAction(act) isChecked = isChecked or act.isChecked() if xml.isStartElement() and xml.name() == "useragentmenu": attributes = xml.attributes() title = attributes.value("title") if title == "v_a_r_i_o_u_s": title = self.tr("Various") menu = QMenu(self) menu.setTitle(title) self.addMenu(menu) menuStack.append(menu) if xml.isEndElement() and xml.name() == "useragentmenu": menuStack.pop() if xml.hasError(): E5MessageBox.critical( self, self.tr("Parsing default user agents"), self.tr( """<p>Error parsing default user agents.</p><p>{0}</p>""") .format(xml.errorString())) return isChecked
def loadState(self, dev, stateParam, stateID = ""): curExecContext = ScExecContext() curExecContext.stateMachine = self.stateMachine curHistoryState = None debug = True r = QXmlStreamReader(dev) while not r.atEnd(): r.readNext() if r.isStartElement(): logger.info("Element :<%s>", r.name()) # # <scxml> # name = r.name().lower() if name == "scxml": if stateID == "": topLevelState = curState = stateParam self.stateInfo[curState] = r.attributes().value("initial") if curState == self.stateMachine: pass # # <state> || <parallel> # elif name == "state" or name == "parallel": inRoot = False stateId = r.attributes().value("id") newState = None # # Create state # if curState is not None: logger.info("Creating state [%s] child of [%s]", stateId, curState.objectName()) type = QState.ExclusiveStates if name == "state" else QState.ParallelStates newState = QState(type, curState) # # ??? # elif stateId == stateID: topLevelState = newState = stateParam if newState is not None: self.stateInfo[newState] = r.attributes().value("initial") newState.setObjectName(stateId) # # initial state # if stateId is not "" and self.stateInfo[curState] == stateId: if curState == self.stateMachine: logger.info("Setting [%s] initial state to [%s]", self.stateMachine.objectName(), newState.objectName()) self.stateMachine.setInitialState(newState) else: logger.info("Setting [%s] initial state to [%s]", curState.objectName(), newState.objectName()) curState.setInitialState(newState) # # TODO implement src attribute management in state element # initialID = r.attributes().value("initial") self.stateByID[stateId] = newState curState = newState curExecContext.state = newState if debug: # Add entry and exit log curExecContext.script = 'logger.debug("[scxml] [debug] > Entering state: [' + stateId + ']")' curExecContext.type = ScExecContext.StateEntry curExecContext.applyScript() curExecContext.script = 'logger.debug("[scxml] [debug] < Exiting state: [' + stateId + ']")' curExecContext.type = ScExecContext.StateExit curExecContext.applyScript() # # <initial> # elif name == "initial": if curState is not None and self.stateInfo[curState] == "": logger.info("Creating state [%s] child of [%s]", stateId, curState.objectName()) newState = QState(curState) curState.setInitialState(newState) # # <history> # elif name == "history": if curState is not None: stateId = r.attributes().value("id") type = r.attributes().value("type") type = QHistoryState.ShallowHistory if type == "shallow" else QHistoryState.DeepHistory curHistoryState = QHistoryState(type) curHistoryState.setObjectName(stateId) self.stateByID[stateId] = curHistoryState # # <final> # elif name == "final": if curState is not None: stateId = r.attributes().value("id") f = QFinalState(curState) f.setObjectName(stateId) curExecContext.state = f self.statesWithFinal.add(curState) gp = curState.parentState() if gp is not None: if gp.childMode() == QState.ParallelStates: self.statesWithFinal.add() self.stateByID[stateId] = f if debug: # Add entry and exit log curExecContext.script = 'logger.debug("[scxml] [debug] > Entering final state: [' + stateId + ']")' curExecContext.type = ScExecContext.StateEntry curExecContext.applyScript() curExecContext.script = 'logger.debug("[scxml] [debug] < Exiting final state: [' + stateId + ']")' curExecContext.type = ScExecContext.StateExit curExecContext.applyScript() # # <script> # elif name == "script": txt = r.readElementText() # # The SCXML Processor MUST evaluate any <script> element that is a child # of <scxml> at document load time # if curExecContext.type == ScExecContext.Unknown and curState == topLevelState: # TODO execute script pass else: curExecContext.script += txt # # <log> # elif name == "log": curExecContext.script += 'logger.' + r.attributes().value("level") + '("[' + \ r.attributes().value("label") + '] [' + \ r.attributes().value("level") + '] ' + r.attributes().value("expr") + '")' # # <assign> # elif name == "assign": pass # # <if> # elif name == "if": pass # # <elseif> # elif name == "elseif": pass # # <else> # elif name == "else": pass # # <cancel> # elif name == "cancel": pass # # <onentry> # elif name == "onentry": curExecContext.type = ScExecContext.StateEntry curExecContext.script = "" # # <onexit> # elif name == "onexit": curExecContext.type = ScExecContext.StateExit curExecContext.script = "" # # <raise> # elif name == "raise": pass # # <send> # elif name == "send": pass # # <invoke> # elif name == "invoke": pass # # <transition> # elif name == "transition": if curHistoryState is not None: inf = ScHistoryInfo() inf.hstate = curHistoryState inf.defaultStateID = r.attributes().value("target") self.historyInfo.append(inf) else: inf = ScTransitionInfo() inf.targets = r.attributes().value("target").split() # TODO split targets curExecContext.type = ScExecContext.Transition curExecContext.script = "" if debug: curExecContext.script = 'logger.debug("[scxml] [debug] = Transitioning to: [' \ + ', '.join(inf.targets) + ']")' + '\n' curTransitions = list() inf.transitions = list() self.transitionsInf.append(inf) for pfx in r.attributes().value("event").split(' '): sigTransition = None if pfx == '': # For eventless transition create QScxmlEventlessTransition transition sigTransition = QScxmlEventlessTransition(self.stateMachine) elif pfx.startswith("q-signal:"): # For all q-signal event, add a QSxcmlSignalTransition self.signalEvents.append(pfx) # get object name (a.b.c) => a objName = pfx[pfx.index(':')+1:pfx.index('.')] # get object reference obj = self.stateMachine.registeredObjects[objName] # get signal reference for attr in pfx[pfx.index('.')+1:].split('.'): sig = getattr(obj, attr) obj = sig # create Signal transition sigTransition = QScxmlSignalTransition(sig, self.stateMachine) if sigTransition is not None: # add condition to transition sigTransition.setConditionExpression(r.attributes().value("cond")) # add transition to current state curState.addTransition(sigTransition) # append sigTransition to transition info inf.transitions.append(sigTransition) # append sigTransition to curTransitions list curTransitions.append(sigTransition) else: logger.error("Transition creation error") # # End element # elif r.isEndElement(): name = r.name().lower() # # </state> or </parallel> # if name == "state" or name == "parallel": if curState == topLevelState: return else: curState = curState.parent() curExecContext.state = curState # # </history> # elif name == "history": curHistoryState = None # # </final> # elif name == "final": curExecContext.state = curExecContext.state.parentState() # # </if> # elif name == "if": pass # # </send> </raise> # elif name == "send" or name == "raise": pass # # </onentry> </onexit> </scxml> # elif name == "onentry" or name == "onexit" or name == "scxml": curExecContext.state = curState curExecContext.type = ScExecContext.StateExit if name == "onexit" else ScExecContext.StateEntry curExecContext.applyScript() curExecContext.type = ScExecContext.Unknown curExecContext.script = "" # # </transition> # elif name == "transition": if curHistoryState is None: curExecContext.transitions = curTransitions curExecContext.type = ScExecContext.Transition curExecContext.applyScript() curExecContext.script = "" curExecContext.type = ScExecContext.Unknown