def RegisterForTriuiEvents(self, msgIDlst, function, *args, **kw): if type(msgIDlst) == int: msgIDlst = [msgIDlst] cookie = uthread.uniqueId() or uthread.uniqueId() self._triuiRegs[cookie] = msgIDlst ref = weakrefutil.CallableWeakRef(function) for id_ in msgIDlst: self._triuiRegsByMsgID.setdefault(id_, {})[cookie] = (ref, args, kw) log.LogInfo('RegisterForTriuiEvents', cookie, msgIDlst, function, args, kw) return cookie
def AddNewSlot(self, slotData, slotIdx, refresh=True): name, cmdstring, iconNo = slotData slotID = uthread.uniqueId() if not uicore.cmd.HasCommand(cmdstring): print 'Command not available', cmdstring slotDataWithID = (slotID, name, cmdstring, iconNo) if name in self.toolSlots: return if slotIdx in self.slots: newSlots = {} for k, v in self.slots.iteritems(): if k >= slotIdx: newSlots[k + 1] = v else: newSlots[k] = v self.slots = newSlots self.slots[slotIdx] = slotDataWithID self.toolSlots[name] = uicls.ToolSlot( parent=self.sr.slotParent, pos=(self._slotMarginX + self._pageMargin, self._slotMarginY + self._pageMargin, self._slotSize, self._slotSize), name='slot_%s' % name, slotData=slotDataWithID, slotIdx=slotIdx, slotID=slotID, showLabel=self.showLabel) if refresh: self.RearrangeSlots()
def BeginSnapshot(self, name): key = uthread.uniqueId() snapshot = self.snapshotDict[key] = Snapshot(name) tasklets = blue.pyos.taskletTimer.GetTasklets().values() snapshot.taskletStart = map(lambda i: (str(i.context), i.switches, i.time), filter(lambda i: i.context != 'Idle Thread' and i.time, tasklets)) snapshot.memoryStart = blue.pyos.ProbeStuff()[1][7] return key
def AddNewSlot(self, slotData, slotIdx, refresh=True): name, cmdstring, iconNo = slotData slotID = uthread.uniqueId() if not uicore.cmd.HasCommand(cmdstring): print "Command not available", cmdstring slotDataWithID = (slotID, name, cmdstring, iconNo) if name in self.toolSlots: return if slotIdx in self.slots: newSlots = {} for k, v in self.slots.iteritems(): if k >= slotIdx: newSlots[k + 1] = v else: newSlots[k] = v self.slots = newSlots self.slots[slotIdx] = slotDataWithID self.toolSlots[name] = uicls.ToolSlot( parent=self.sr.slotParent, pos=( self._slotMarginX + self._pageMargin, self._slotMarginY + self._pageMargin, self._slotSize, self._slotSize, ), name="slot_%s" % name, slotData=slotDataWithID, slotIdx=slotIdx, slotID=slotID, showLabel=self.showLabel, ) if refresh: self.RearrangeSlots()
def Register(self, callbackObj): """ 'checkFn' is a function that takes an item and returns true iff the item is inside this container. 'callbackObj' must define this function: IsMine(item) must return true iff the item is considered to be inside the container Additionally, these callback functions will called if present: OnInvChange(item, change) some item has changed somehow this is cumulative with the following callbacks AddItem(item) item has moved into this container UpdateItem(item, change) item has changed inside this container RemoveItem(itemID) item was removed from this container Callbacks won't get executed in this callbackObj until it has an attribute invReady with a true value. It is suggested that you turn invReady to true as soon as you have your initial listing. For convenience, callbacks on deco windows are not executed, and the registration is killed, if the window is dead. (Note that a callback object doesn't need be a window.) # Return a (cookie, list) tuple, where 'cookie' is a value that can # be passed to Unregister to cancel this registration, and list is # the list of items that meet the conditions at this time. Currently return the cookie alone, as explained in the commented out paragraph above :). For correctness, it is recommended that you 1) register 2) get the initial set of items as normally 3) set self.invReady = 1 so you'll get updates as soon as you have the initial state. """ cookie = uthread.uniqueId() or uthread.uniqueId() self.LogInfo('Registering', cookie, callbackObj) self.regs[cookie] = _weakref.ref(callbackObj) return cookie
def GeneratePKForLine(self, line): primaryKey = [] if not self.primaryKeys: primaryKey = uthread.uniqueId() else: for columnIndex in self.primaryKeys: primaryKey.append(line[columnIndex]) if len(primaryKey) == 1: return primaryKey[0] return primaryKey
def GetTextureFromURL(self, path, currentURL=None, ignoreCache=0, dontcache=0, fromWhere=None, sizeonly=0, retry=1): if path.endswith('.blue'): return self.GetPic_blue(path) fullPath = corebrowserutil.ParseURL(path, currentURL)[0] if path.startswith('res:'): try: surface = trinity.Tr2HostBitmap() surface.CreateFromFile(path) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) if sizeonly: return (path, w, h, bw, bh) return self.ReturnTexture(path, w, h, bw, bh) except: self.LogError('Failed to load image', path) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly) if ignoreCache: sm.GetService('browserCache').InvalidateImage(fullPath) while self.urlloading.has_key(fullPath): blue.pyos.BeNice() if not dontcache: cacheData = sm.GetService('browserCache').GetFromCache(fullPath) if cacheData and os.path.exists(cacheData[0].replace( 'cache:/', blue.paths.ResolvePath(u'cache:/'))): if sizeonly: return cacheData return self.ReturnTexture(*cacheData) try: self.urlloading[fullPath] = 1 ret = corebrowserutil.GetStringFromURL(fullPath) cacheID = int( str(blue.os.GetWallclockTime()) + str(uthread.uniqueId() or uthread.uniqueId())) imagestream = ret.read() ext = None if 'content-type' in ret.headers.keys( ) and ret.headers['content-type'].startswith('image/'): ext = ret.headers['content-type'][6:] if ext == None or ext == 'png': header = imagestream[:16] for sig, sext in [('PNG', 'PNG'), ('GIF', 'GIF'), ('JFI', 'JPEG'), ('BM8', 'BMP')]: for i in xrange(0, 12): if header[i:i + 3] == sig: ext = sext break if not ext: header = imagestream[-16:] for sig, sext in [('XFILE', 'TGA')]: for i in xrange(0, 10): if header[i:i + 5] == sig: ext = sext break if ext: filename = '%sBrowser/Img/%s.%s' % ( blue.paths.ResolvePath(u'cache:/'), cacheID, ext) resfile = blue.classes.CreateInstance('blue.ResFile') if not resfile.Open(filename, 0): resfile.Create(filename) resfile.Write(imagestream) resfile.Close() if ext.upper() == 'GIF': g = DrawArea() g.setBgColor(Transparent) g.loadGIF(filename.replace(u'/', u'\\').encode('utf8')) ext = 'PNG' filename = u'%sBrowser/Img/%s.%s' % ( blue.paths.ResolvePath(u'cache:/'), cacheID, ext) g.outPNG(filename.replace(u'/', u'\\').encode('utf8')) surface = trinity.Tr2HostBitmap() surface.CreateFromFile(filename) w, h = surface.width, surface.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) cachePath = 'cache:/Browser/Img/%s.%s' % (cacheID, ext) if 'pragma' not in ret.headers.keys( ) or ret.headers['Pragma'].find('no-cache') == -1: sm.GetService('browserCache').Cache( fullPath, (cachePath, w, h, bw, bh)) del self.urlloading[fullPath] if sizeonly: return (cachePath, w, h, bw, bh) return self.ReturnTexture(cachePath, w, h, bw, bh) del self.urlloading[fullPath] return self.ErrorPic(sizeonly) except Exception as e: if retry: sys.exc_clear() if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] return self.GetTextureFromURL(path, currentURL, ignoreCache, dontcache, fromWhere, sizeonly, 0) self.LogError(e, 'Failed to load image', repr(path)) if self.urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self.ErrorPic(sizeonly)
def GetTextureFromURL(self, path, currentURL = None, ignoreCache = 0, dontcache = 0, fromWhere = None, sizeonly = 0, retry = 1): if path.endswith('.blue'): return self._GetPic_blue(path) dev = trinity.device fullPath = corebrowserutil.ParseURL(path, currentURL)[0] if path.startswith('res:'): try: bmp = trinity.Tr2HostBitmap() bmp.CreateFromFile(path) w, h = bmp.width, bmp.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) if sizeonly: return (path, w, h, bw, bh) return self._ReturnTexture(path, w, h, bw, bh) except: self.LogError('Failed to load image', path) if self._urlloading.has_key(fullPath): del self.urlloading[fullPath] sys.exc_clear() return self._ErrorPic(sizeonly) if ignoreCache: sm.GetService('browserCache').InvalidateImage(fullPath) while self._urlloading.has_key(fullPath): blue.pyos.BeNice() if not dontcache: cacheData = sm.GetService('browserCache').GetFromCache(fullPath) if cacheData and os.path.exists(cacheData[0].replace('cache:/', blue.paths.ResolvePath(u'cache:/'))): if sizeonly: return cacheData return self._ReturnTexture(*cacheData) try: self._urlloading[fullPath] = 1 ret = corebrowserutil.GetStringFromURL(fullPath) cacheID = int(str(blue.os.GetWallclockTime()) + str(uthread.uniqueId() or uthread.uniqueId())) imagestream = ret.read() ext = None if 'content-type' in ret.headers.keys() and ret.headers['content-type'].startswith('image/'): ext = ret.headers['content-type'][6:] if ext == None or ext == 'png': header = imagestream[:16] for sig, sext in [('PNG', 'PNG'), ('GIF', 'GIF'), ('JFI', 'JPEG'), ('BM8', 'BMP')]: for i in xrange(0, 12): if header[i:i + 3] == sig: ext = sext break if not ext: header = imagestream[-16:] for sig, sext in [('XFILE', 'TGA')]: for i in xrange(0, 10): if header[i:i + 5] == sig: ext = sext break if ext: filename = '%sBrowser/Img/%s.%s' % (blue.paths.ResolvePath(u'cache:/'), cacheID, ext) resfile = blue.classes.CreateInstance('blue.ResFile') if not resfile.Open(filename, 0): resfile.Create(filename) resfile.Write(imagestream) resfile.Close() if ext.upper() == 'GIF': g = pychartdir.DrawArea() g.setBgColor(pychartdir.Transparent) g.loadGIF(filename.replace(u'/', u'\\').encode('utf8')) ext = 'PNG' filename = u'%sBrowser/Img/%s.%s' % (blue.paths.ResolvePath(u'cache:/'), cacheID, ext) g.outPNG(filename.replace(u'/', u'\\').encode('utf8')) bmp = trinity.Tr2HostBitmap() bmp.CreateFromFile(filename) w, h = bmp.width, bmp.height bw, bh = uiutil.GetBuffersize(w), uiutil.GetBuffersize(h) cachePath = 'cache:/Browser/Img/%s.%s' % (cacheID, ext) if 'pragma' not in ret.headers.keys() or ret.headers['Pragma'].find('no-cache') == -1: sm.GetService('browserCache').Cache(fullPath, (cachePath, w, h, bw, bh)) del self._urlloading[fullPath] if sizeonly: return (cachePath, w, h, bw, bh) return self._ReturnTexture(cachePath, w, h, bw, bh) del self._urlloading[fullPath] return self._ErrorPic(sizeonly) except Exception as e: if retry: sys.exc_clear() if fullPath in self._urlloading: del self._urlloading[fullPath] return self.GetTextureFromURL(path, currentURL, ignoreCache, dontcache, fromWhere, sizeonly, 0) self.LogError(type(e), 'on line', sys.exc_traceback.tb_lineno, 'in browserImage service') self.LogError(e, 'Failed to load image', path) if fullPath in self._urlloading: del self._urlloading[fullPath] sys.exc_clear() return self._ErrorPic(sizeonly)
def Register(self, callbackObj): cookie = uthread.uniqueId() or uthread.uniqueId() self.LogInfo('Registering', cookie, callbackObj) self.regs[cookie] = _weakref.ref(callbackObj) return cookie