def fromXML(cls, svgImageElement): # Check if provided element is indeed an image tag. Stop if not. assert svgImageElement.tagName == cls._elementName,\ "Provided element cannot be parsed. Wrong tag name?" # Extract image link from the provided image tag try: imageLink = svgImageElement.getAttribute('xlink:href').encode( 'utf-8') except: raise AttributeError, \ "Provided element no xlink or provided xlink is invalid." # Extract element's id (it is required). While new barSlideBackgorundElement may have blank id, # the serialized one has contain a valid id. try: id = svgImageElement.getAttribute('id').encode('utf-8') except: raise AttributeError, \ "Provided element no id attribute or provided id is invalid." # Create empty background element instance with a given image link and extracted id newBackgroundElement = cls(imageLink, id=id) # Extract and validate other attributes. Handling some of them is easy # while some of the other are more complicated to parse: if svgImageElement.hasAttribute('height') and \ svgImageElement.hasAttribute('width'): height = float(svgImageElement.getAttribute('height')) width = float(svgImageElement.getAttribute('width')) newBackgroundElement.size = (width, height) if svgImageElement.hasAttribute('x') and \ svgImageElement.hasAttribute('y'): x = float(svgImageElement.getAttribute('x')) y = float(svgImageElement.getAttribute('y')) newBackgroundElement.position = (x, y) if svgImageElement.hasAttribute('style'): styleString = parseStyle(svgImageElement.getAttribute('style')) newBackgroundElement._style = styleString if styleString.has_key('opacity'): newBackgroundElement.opacity = float(styleString['opacity']) if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'modality'): newBackgroundElement.modality = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'modality') if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'preparation'): newBackgroundElement.preparation = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'preparation') if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'description'): newBackgroundElement.description = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'description') return newBackgroundElement
def fromXML(cls, svgImageElement): # Check if provided element is indeed an image tag. Stop if not. assert svgImageElement.tagName == cls._elementName,\ "Provided element cannot be parsed. Wrong tag name?" # Extract image link from the provided image tag try: imageLink = svgImageElement.getAttribute('xlink:href').encode('utf-8') except: raise AttributeError, \ "Provided element no xlink or provided xlink is invalid." # Extract element's id (it is required). While new barSlideBackgorundElement may have blank id, # the serialized one has contain a valid id. try: id = svgImageElement.getAttribute('id').encode('utf-8') except: raise AttributeError, \ "Provided element no id attribute or provided id is invalid." # Create empty background element instance with a given image link and extracted id newBackgroundElement = cls(imageLink, id = id) # Extract and validate other attributes. Handling some of them is easy # while some of the other are more complicated to parse: if svgImageElement.hasAttribute('height') and \ svgImageElement.hasAttribute('width'): height = float(svgImageElement.getAttribute('height')) width = float(svgImageElement.getAttribute('width')) newBackgroundElement.size = (width,height) if svgImageElement.hasAttribute('x') and \ svgImageElement.hasAttribute('y'): x = float(svgImageElement.getAttribute('x')) y = float(svgImageElement.getAttribute('y')) newBackgroundElement.position = (x,y) if svgImageElement.hasAttribute('style'): styleString = parseStyle(svgImageElement.getAttribute('style')) newBackgroundElement._style = styleString if styleString.has_key('opacity'): newBackgroundElement.opacity = float(styleString['opacity']) if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'modality'): newBackgroundElement.modality = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'modality') if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'preparation'): newBackgroundElement.preparation = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'preparation') if svgImageElement.hasAttributeNS(BAR_XML_NAMESPACE, 'description'): newBackgroundElement.description = \ svgImageElement.getAttributeNS(BAR_XML_NAMESPACE, 'description') return newBackgroundElement
def _setOpacity(self, value): assert type(value) == type(1.) or \ type(value) == type(0), \ "Opacity has to be float between 0 and 1" self._opacity = float(value) styleDict = parseStyle(self._attributes['style']) styleDict['opacity'] = str(self._opacity) self._attributes['style'] = formatStyle(styleDict)