def qvariant_encode(value): buffer = QBuffer() buffer.open(QIODevice.ReadWrite) stream = QDataStream(buffer) stream.writeQVariant(value) buffer.close() return buffer.buffer().toBase64().data().strip()
def __init__(self, name, screenArea, keymapFunc, detector, scale=0.8, threshLum=50, SFchar=None): self.name = name self.keymapFunc = keymapFunc self.keys = None self.listKey = [] # list of keyboard keys reserved for this player # look up table to map snes key to keyboard key self.lut = dict([ (sk, None) for sk in ['u', 'd', 'l', 'r', 'lk', 'mk', 'hk', 'lp', 'mp', 'hp'] ]) self.SFchar = SFchar self.threshLum = threshLum self.screenArea = screenArea self.detector = detector # morphological structuring element to clean the image self.kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) self.scale = scale self.bgmodel = None self.idFrame = 0 self.buffer = QBuffer() self.buffer.open(QIODevice.ReadWrite)
def qvariant_decode(data): byteArray = QByteArray.fromBase64(data) buffer = QBuffer(byteArray) buffer.open(QIODevice.ReadOnly) stream = QDataStream(buffer) result = stream.readQVariant() buffer.close() return result
def testRefCountOfTOutputDevice(self): generator = QSvgGenerator() iodevice1 = QBuffer() refcount1 = getrefcount(iodevice1) generator.setOutputDevice(iodevice1) self.assertEqual(generator.outputDevice(), iodevice1) self.assertEqual(getrefcount(generator.outputDevice()), refcount1 + 1) iodevice2 = QBuffer() refcount2 = getrefcount(iodevice2) generator.setOutputDevice(iodevice2) self.assertEqual(generator.outputDevice(), iodevice2) self.assertEqual(getrefcount(generator.outputDevice()), refcount2 + 1) self.assertEqual(getrefcount(iodevice1), refcount1) del generator self.assertEqual(getrefcount(iodevice2), refcount2)
def image(self,pixmap): if pixmap == None: self._image_as_pixmap = None self.picture_data = None else: byte_array = QByteArray() qbuffer = QBuffer(byte_array) qbuffer.open(QIODevice.WriteOnly) pixmap.toImage().save(qbuffer, "PNG") self._image_as_pixmap = None self.picture_data = memoryview(byte_array.data()) # memory view to support python 2 and 3
def mapBytes(self, kode): im = self.draw(kode) if isinstance(kode, UKode): qxm = pilfunc.pixmap2qt(im) elif isinstance(kode, (BKode, SKode)): qxm = pilfunc.bitmap2qt(im) else: raise ValueError bts = QByteArray() buffer = QBuffer(bts) buffer.open(QIODevice.WriteOnly) qxm.save(buffer, "png") buffer.close() return bts
def svgImage(svgfile, file=None): qs = QSvgWidget() qs.load(svgfile) qim = QPixmap.grabWidget(qs) bts = QByteArray() buffer = QBuffer(bts) buffer.open(QIODevice.WriteOnly) qim.save(buffer, "png") bts = buffer.data().data() buffer.close() if type(file) == str: assert os.path.splitext(file)[1].lower() == ".png" with open(file, "bw") as f: f.write(bts) elif hasattr(file, 'write'): file.write(bts) else: return bts
def getIcon(obj, disabled=False, path=None): if not path: path = iconPath if not getattr(obj, '_icon', None): obj._icon = addIconToFCAD(obj._iconName, path) if not disabled: return obj._icon if not getattr(obj, '_iconDisabled', None): name = getattr(obj, '_iconDisabledName', None) if name: obj._iconDisabled = addIconToFCAD(name, path) else: key = os.path.join(path, obj._iconName) + '.disabled' fmt = None try: if FreeCADGui.isIconCached(key): obj._iconDisabled = key return key else: fmt = 'PNG' except Exception: pass pixmap = FreeCADGui.getIcon(obj._icon).pixmap(*iconSize, mode=QIcon.Disabled) icon = QIcon(pixmapDisabled) icon.paint(QPainter(pixmap), 0, 0, iconSize[0], iconSize[1], Qt.AlignCenter) data = QByteArray() buf = QBuffer(data) buf.open(QIODevice.WriteOnly) if fmt: pixmap.save(buf, fmt) FreeCADGui.addIcon(key, data.data(), fmt) else: pixmap.save(buf, 'XPM') key = data.data().decode('latin1') obj._iconDisabled = key return obj._iconDisabled
def runparser(filelistdirs,logdir): print 'Start running the Translator' #os.chdir(logdir) filelistdirs=filter(None,filelistdirs) print 'D1',filelistdirs for dir in xrange(len(filelistdirs)): curdir=filelistdirs[dir] print 'D2',curdir newdir=os.path.join(logdir,os.path.basename(curdir)).replace('\\','/') if not os.path.exists(newdir): os.mkdir(newdir) filelist = os.listdir(curdir) print filelist os.chdir(curdir) for z in xrange(len(filelist)): ### Start the xml parser tree = ET.parse(filelist[z]) root = tree.getroot() '''create a html result file ''' filename= os.path.basename(filelist[z]) logfile=os.path.join(newdir,filename.replace('.onb','.html')).replace('\\','/') print logfile f=open(logfile,'w') headers=''' <!doctype html> <head> <title>OMWEBbook</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="../jquery.min1.10.2.js"></script> <script src='../dygraph-combined.js'></script> <link rel="stylesheet" href="../bootstrap.min.css"> <script src="../bootstrap.min.js"></script> <link rel="stylesheet" href="../codemirror.css"> <script src="../codemirror.js"></script> <script src="../modelica.js"></script> <link rel="stylesheet" href="../custom.css"> <script src="../autorefresh.js"></script> <script src="../evalnotebook.js"></script> </head> <body> <div class="navbar navbar-default navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">OMWebBook</a> </div> <button id="evaluate" type="button" class="btn btn-success navbar-btn">Evaluate Cell</button> <button id="evaluateall" type="button" class="btn btn-success navbar-btn">Eval All</button> <img id="progressbar" src="../ajax-loader.gif" class="img-rounded" alt="Cinque Terre"> </div> </div> <div class="container"> <br> <br> <br> ''' f.write(headers) count=0 check=0 sectioncount=1 subsectioncount=0.01 sectioncheck=False subsectioncheck=False currentlevel='' g=1 g1=1 for node in tree.iter(): if (node.tag=='TextCell'): imagelist=node.findall('Image') html=node.find('Text').text soup = BeautifulSoup(html) for a in soup.findAll('a'): staticlink="".join(['static','/',a['href']]).replace('.onb','.html') a['href']=a['href'].replace(a['href'], staticlink) #print staticlink findp=[] for p in soup.findAll('p'): checkempty=p['style'].replace(' ','').split(";") val="-qt-paragraph-type:empty" in checkempty if (val==False): # if (p.find('img')==None): # findp.append(p) # else: findp.append(p) for i in xrange(len(findp)): #x=findp[i].text x=findp[i] if(x.find('img')is None): if(x!=''): if(node.attrib['style']=='Text'): #partext='\n'.join(['<p align="justify" contenteditable=False>',str(x),'</p>']) #x["align"]='justify' x=re.sub(r'[^\x00-\x7F]+',' ', str(x)) partext='\n'.join([str(x)]) f.write(partext) f.write('<br>') f.write('\n') elif(node.attrib['style']=='Title'): print 'title' t=findp[i].text t=re.sub(r'[^\x00-\x7F]+',' ', str(t)) htmltext='\n'.join(['<h1>',t,'</h1>']) #htmltext='\n'.join([str(x)]) f.write(htmltext) f.write('\n') elif(node.attrib['style']=='Section'): print 'section' t=findp[i].text t=re.sub(r'[^\x00-\x7F]+',' ', str(t)) sectioncheck=True htmltext='\n'.join(['<h2>',str(sectioncount),t,'</h2>']) sectioncount+=1 #htmltext='\n'.join([str(x)]) f.write(htmltext) f.write('\n') elif(node.attrib['style']=='Subsection'): if(sectioncheck==True): g=1 g+=1 sectioncheck=False #print 'subsection',sectioncheck,(sectioncount-1),'.',(g-1) subsec=findp[i].text subsec=re.sub(r'[^\x00-\x7F]+',' ', str(subsec)) #scount=(sectioncount-1)+subsectioncount scount=str((sectioncount-1))+'.'+str((g-1)) subsectioncheck=True currentlevel=scount #print str((sectioncount-1)),'.',g #print 'subsection',scount htmltext='\n'.join(['<h3>',str(scount),subsec,'</h3>']) subsectioncount+=0.01 #htmltext='\n'.join([str(x)]) f.write(htmltext) f.write('\n') elif(node.attrib['style']=='Subsubsection'): if(subsectioncheck==True): g1=1 g1+=1 subsectioncheck=False #print 'subsection',sectioncheck,(sectioncount-1),'.',(g-1) subsec=findp[i].text subsec=re.sub(r'[^\x00-\x7F]+',' ', str(subsec)) #scount=(sectioncount-1)+subsectioncount scount=str(currentlevel)+'.'+str((g1-1)) print 'subsubsection',scount #print str((sectioncount-1)),'.',g #print 'subsection',scount htmltext='\n'.join(['<h4>',str(scount),subsec,'</h4>']) subsectioncount+=0.01 #htmltext='\n'.join([str(x)]) f.write(htmltext) f.write('\n') else: htmltext='\n'.join(['<h1>',str(x),'</h1>']) #htmltext='\n'.join([str(x)]) f.write(htmltext) f.write('\n') else: try: imagedir=os.path.join(newdir,"Images").replace('\\','/') if not os.path.exists(imagedir): os.mkdir(imagedir) os.chdir(imagedir) y=imagelist[0] image=y.text unique_filename = str(uuid.uuid4()) img = QtGui.QImage() #image=node.find('Image').text byteArray = QByteArray.fromBase64(image) buffer = QBuffer(byteArray) buffer.open(QIODevice.ReadOnly) data = QDataStream(buffer) data >> img buffer.close() filename=".".join([unique_filename,"png"]) imgpath="/".join(['static',os.path.basename(newdir),'Images/']) imgsrc=imgpath+filename img.save(filename) imgtag="".join(["<div align=\"center\">","<img src=",imgsrc,">","</div>"]) #print filename f.write(imgtag) del imagelist[0] except: pass if (node.tag=='GraphCell' or node.tag=='InputCell'): ## catch the input text inputtext=node.find('Input').text ''' if ('simulate' in inputtext): text='\n'.join(['<p> <b>',inputtext,'</b> </p>']) f.write(text) f.write('\n')''' #print 'arun', inputtext if(inputtext!=None): linecount=string.split(inputtext, '\n') ''' if ('plot(' in inputtext): text='\n'.join(['<p> <b>',inputtext,'</b> </p> <br>']) f.write(text) f.write('\n') ## code to automatically generate plot variable and button in html plotvar=inputtext.replace('plot','').replace('(','').replace(')','').replace('{','').replace('}','') listplotvar=plotvar.split(',') plotid='simulatebutton'+str(count)+'plot' buttonid='simulatebutton'+str(count) graphdivid='simulatebutton'+str(count)+'graphdiv' plotheader="\n".join(['<div>','<select id='+ plotid +' size=5 multiple>', '<option ><b>Select Plot Variables</b> </option>']) f.write(str(plotheader)) f.write('\n') for i in xrange(len(listplotvar)): varname='<option selected>'+ str(listplotvar[i]) + '</option>' f.write(varname) f.write('\n') closeoption="\n".join(['</select> <br>','<button id='+buttonid+'>Simulate</button> <br> <br>' ,'</div>']) f.write(closeoption) count=count+1; else:''' textid='check'+str(check)+'textarea' divid='check'+str(check)+'div' if ('plot(' in inputtext): text='\n'.join(['<textarea id='+ str(textid)+'>',inputtext,'</textarea> <br> <div id='+divid+'> </div> <br>']) else: text='\n'.join(['<textarea id='+ str(textid)+'>',inputtext,'</textarea> <div id='+divid+'> </div> <br>']) check=check+1 f.write(text) f.write('\n') ## catch the OMCPLOT datas curve=node.find('OMCPlot') if curve!=None: #count=count+1; print count try: #scriptdata=makeplot(curve,count) scriptdata=makeplot(curve,divid) f.write(scriptdata) f.write('\n') except: f.write("No data found") f.write('\n') else: textid='check'+str(check)+'textarea' divid='check'+str(check)+'div' inputtext='' text='\n'.join(['<textarea id='+ str(textid)+'>',inputtext,'</textarea> <div id='+divid+'> </div> <br>']) check=check+1 f.write(text) f.write('\n') print "Empty Graph cells" f.write('</div></body></html>') f.close() print 'Completed'
def main(url, output, option={}): result = { "error": [], "page": {}, "resources": [], "capture": None, } #savedir = appdir + "/artifacts/ghost/" + output #dump = savedir + "/ghost.pkl" savedir = os.path.join(appdir, "artifacts/ghost") dump = savedir + "/" + output try: #if os.path.exists(savedir): # shutil.rmtree(savedir) #os.makedirs(savedir) with open(dump, 'wb') as d: #umsgpack.dump(result, d) json.dump(result, d) except Exception as e: logger.error(str(e)) result["error"].append(str(e)) defaults = { "wait_timeout": 60, "display": False, "viewport_size": (800, 600), "plugins_enabled": True, "java_enabled": True, } proxy_url = None http_method = "GET" req_headers = None body = None if option: if "user_agent" in option: defaults["user_agent"] = str(option["user_agent"]) if "timeout" in option: defaults["wait_timeout"] = int(option["timeout"]) if "proxy" in option: proxy_url = option["proxy"] if "method" in option: http_method = option["method"] if "headers" in option: req_headers = option["headers"] if "post_data" in option: body = str(option["post_data"]) logger.info(defaults) ghost = None try: ghost = Ghost( #log_level=logging.DEBUG, log_level=logging.INFO, plugin_path=[ appdir + '/plugins', '/usr/lib/mozilla/plugins', ], defaults=defaults, ) except Exception as e: logger.error("ghost init failed. " + str(e)) result["error"].append(str(e)) with ghost.start() as session: if proxy_url: try: type = proxy_url.split(":")[0] server = proxy_url.split("/")[2] host = server.split(":")[0] port = server.split(":")[1] session.set_proxy( str(type), host=str(host), port=int(port), ) except Exception as e: logger.debug(e) headers = {} if req_headers: logger.debug(req_headers) for h in req_headers: headers[str(h)] = str(req_headers[h]) logger.debug(headers) if hasattr(ghost, "xvfb"): logger.info(ghost.xvfb) page = None resources = None try: page, resources = session.open(url.decode("utf-8"), method=http_method, headers=headers, body=body) except Exception as e: logger.error(str(e)) result["error"].append(str(e)) #if error: # result["error"] = error.spilt(".")[-1] if page: result["page"] = { "url": page.url, "http_status": page.http_status, "headers": page.headers, #"content":session.content.encode("utf-8"), "content": base64.b64encode(session.content.encode("utf-8")), "seq": 0, #"error":page.error.encode("utf-8").split(".")[-1], "error": page.error.split(".")[-1], } try: image = session.capture() ba = QByteArray() buffer = QBuffer(ba) buffer.open(QIODevice.WriteOnly) image.save(buffer, "PNG") bio = BytesIO(ba) bio.seek(0) #result["capture"] = bio.read() result["capture"] = base64.b64encode(bio.read()) bio.flush() bio.close() ba.clear() buffer.close() except Exception as e: logger.error(str(e)) result["error"].append(str(e)) if resources: seq = 0 for r in resources: seq += 1 #logger.debug(r.url) dict = { "url": r.url, "http_status": r.http_status, "headers": r.headers, #"content":r.content.encode("utf-8"), "content": base64.b64encode(r.content), "seq": seq, #"error":r.error.encode("utf-8").split(".")[-1], "error": r.error.split(".")[-1], } result["resources"].append(dict) with open(dump, 'wb') as d: #umsgpack.dump(result, d) json.dump(result, d) logger.debug(dump) ghost.exit() return dump