Beispiel #1
0
	def importXmlSrc(self, src):
		"""This will read in an XML source. The parameter can be a
		file path, an open file object, or the raw XML. It will look for
		a matching code file and, if found, import that code.
		"""
		parseCode = True
		if isinstance(src, file):
			xml = src.read()
			self._srcFile = src.name
		else:
			xml = src
			if not src.startswith("<"):
				xml = src = utils.resolvePathAndUpdate(src)
			if os.path.exists(src):
				self._srcFile = src
			else:
				parseCode = False
				self._srcFile = os.getcwd()
		return xtd.xmltodict(xml)
Beispiel #2
0
	def importJsonSource(self, src):
		"""This will read in a JSON source. The parameter can be a
		file path, an open file object, or the raw XML. It will look for
		a matching code file and, if found, import that code.
		"""
		parseCode = True
		try:
			# Try a file object
			jsonText = src.read()
			self._srcFile = src.name
		except AttributeError:
			if os.path.exists(src):
				self._srcFile = src = utils.resolvePathAndUpdate(src)
				jsonText = file(src).read()
			else:
				# It must be raw json
				jsonText = src
				self._srcFile = os.getcwd()
		return dabo.lib.jsonDecode(jsonText)
Beispiel #3
0
	def importXmlSrc(self, src):
		"""This will read in an XML source. The parameter can be a
		file path, an open file object, or the raw XML. It will look for
		a matching code file and, if found, import that code.
		"""
		parseCode = True
		if isinstance(src, file):
			xml = src.read()
			self._srcFile = src.name
		else:
			xml = src
			if not src.startswith("<"):
				xml = src = utils.resolvePathAndUpdate(src)
			if os.path.exists(src):
				self._srcFile = src
			else:
				parseCode = False
				self._srcFile = os.getcwd()
		return xtd.xmltodict(xml)
Beispiel #4
0
	def importJsonSource(self, src):
		"""This will read in a JSON source. The parameter can be a
		file path, an open file object, or the raw XML. It will look for
		a matching code file and, if found, import that code.
		"""
		parseCode = True
		try:
			# Try a file object
			jsonText = src.read()
			self._srcFile = src.name
		except AttributeError:
			if os.path.exists(src):
				self._srcFile = src = utils.resolvePathAndUpdate(src)
				jsonText = file(src).read()
			else:
				# It must be raw json
				jsonText = src
				self._srcFile = os.getcwd()
		return dabo.lib.jsonDecode(jsonText)
Beispiel #5
0
	def _setPicture(self, val):
		if not val:
			# Empty string passed; clear any current image
			self._picture = ""
			self._displayState = 1
			self._bmp = wx.EmptyBitmap(1, 1, 1)
			self.__image = wx.EmptyImage(1, 1)		# self._bmp.ConvertToImage()
			self._showPic()
			return
		elif isinstance(val, wx.Image):
			# An image stored as a stream is being used
			self.__image = val
			self._picture = "(stream)"
		elif isinstance(val, wx.Bitmap):
			# a raw bitmap is being supplied
			self._bmp = val
			self.__image = val.ConvertToImage()
			self._picture = "(stream)"
		elif isinstance(val, buffer):
			val = cStringIO.StringIO(val)
			img = wx.EmptyImage()
			img.LoadStream(val)
			self._setPicture(img)
			return
		else:
			if not os.path.isfile(val):
				origVal = val
				val = dabo.ui.getImagePath(val)
				if val is None or not os.path.isfile(val):
					# This will raise an IOError if it fails
					try:
						val = utils.resolvePathAndUpdate(origVal)
					except IOError:
						val = None
				if val is None or not os.path.isfile(val):
					# Bad image reference
					dabo.log.error(_("No file named '%s' exists.") % origVal)
					return
			self._picture = val
			self._displayState = 1
			idx = self.PictureIndex
			if idx != -1:
				# The image count is 1-based.
				maxIdx = self.FrameCount - 1
				if idx > maxIdx:
					dabo.log.error(_("Attempt to set PictureIndex (%(idx)s)to a value "
							"greater than the maximum index available (%(maxIdx)s).") % locals())
					idx = self.PictureIndex = maxIdx
			try:
				self._Image.LoadFile(val, index=idx)
			except IndexError:
				# Note: when I try to load an invalid index, I get a segfault, so I don't know
				# how useful this is.
				self._Image.LoadFile(val, index= -1)
			if _USE_PIL:
				try:
					pil_img = Image.open(val)
					# Only jpeg images support this
					exif = pil_img._getexif()
					orientation = exif.get(_ORIENTATION_TAG, 1)
					self._displayState = _exifToImg(orientation)
				except AttributeError:
					# Not a jpeg, or not a version with the _getexif() method
					pass
				except IOError:
					# Bad image, or no exif data available
					pass
		if self._Image.Ok():
			self._imgProp = float(self._Image.GetWidth()) / float(self._Image.GetHeight())
		else:
			self._imgProp = 1.0
		self._showPic()
Beispiel #6
0
 def _setPicture(self, val):
     if not val:
         # Empty string passed; clear any current image
         self._picture = ""
         self._displayState = 1
         self._bmp = wx.EmptyBitmap(1, 1, 1)
         self.__image = wx.EmptyImage(1, 1)  # self._bmp.ConvertToImage()
         self._showPic()
         return
     elif isinstance(val, wx.Image):
         # An image stored as a stream is being used
         self.__image = val
         self._picture = "(stream)"
     elif isinstance(val, wx.Bitmap):
         # a raw bitmap is being supplied
         self._bmp = val
         self.__image = val.ConvertToImage()
         self._picture = "(stream)"
     elif isinstance(val, buffer):
         val = cStringIO.StringIO(val)
         img = wx.EmptyImage()
         img.LoadStream(val)
         self._setPicture(img)
         return
     else:
         if not os.path.isfile(val):
             origVal = val
             val = dabo.ui.getImagePath(val)
             if val is None or not os.path.isfile(val):
                 # This will raise an IOError if it fails
                 try:
                     val = utils.resolvePathAndUpdate(origVal)
                 except IOError:
                     val = None
             if val is None or not os.path.isfile(val):
                 # Bad image reference
                 dabo.log.error(_("No file named '%s' exists.") % origVal)
                 return
         self._picture = val
         self._displayState = 1
         idx = self.PictureIndex
         if idx != -1:
             # The image count is 1-based.
             maxIdx = self.FrameCount - 1
             if idx > maxIdx:
                 dabo.log.error(
                     _("Attempt to set PictureIndex (%(idx)s)to a value "
                       "greater than the maximum index available (%(maxIdx)s)."
                       ) % locals())
                 idx = self.PictureIndex = maxIdx
         try:
             self._Image.LoadFile(val, index=idx)
         except IndexError:
             # Note: when I try to load an invalid index, I get a segfault, so I don't know
             # how useful this is.
             self._Image.LoadFile(val, index=-1)
         if _USE_PIL:
             try:
                 pil_img = Image.open(val)
                 # Only jpeg images support this
                 exif = pil_img._getexif()
                 orientation = exif.get(_ORIENTATION_TAG, 1)
                 self._displayState = _exifToImg(orientation)
             except AttributeError:
                 # Not a jpeg, or not a version with the _getexif() method
                 pass
             except IOError:
                 # Bad image, or no exif data available
                 pass
     if self._Image.Ok():
         self._imgProp = float(self._Image.GetWidth()) / float(
             self._Image.GetHeight())
     else:
         self._imgProp = 1.0
     self._showPic()