def testEncodeDecode(self): session = Session() s = StringIO() self._store.encoder()(session, s) output = s.getvalue() s.close() self.assertTrue(isinstance(output, str)) s = StringIO(output) output = self._store.decoder()(s) s.close() self.assertTrue(type(output) is type(session)) self.assertEqual(output._data, session._data)
def createPyDocs(self, filename, dir): """Create an HTML module documentation using pydoc.""" import pydoc package, module = os.path.split(filename) module = os.path.splitext(module)[0] if package: module = package.replace('/', '.') + '.' + module targetName = '%s/%s.html' % (dir, module) self.printMsg('Creating %s...' % targetName) saveDir = os.getcwd() os.chdir(dir) try: stdout = sys.stdout sys.stdout = StringIO() try: try: pydoc.writedoc(module) except Exception: pass msg = sys.stdout.getvalue() finally: sys.stdout = stdout finally: os.chdir(saveDir) if msg: self.printMsg(msg)
def respond(self, transaction): """Write out a syntax hilighted version of the file. The filename is an attribute of the request object. """ res = transaction._response req = self.request() if not req.hasField('filename'): res.write('<h3 style="color:red">Error</h3><p>' 'No filename given to syntax color!</p>') return filename = req.field('filename') filename = self.request().serverSidePath(os.path.basename(filename)) if not os.path.exists(filename): res.write('<h3 style="color:red">Error</h3><p>' 'The requested file %r does not exist' ' in the proper directory.</p>' % os.path.basename(filename)) return from DocSupport import py2html myout = StringIO() stdout = sys.stdout sys.stdout = myout py2html.main((None, '-stdout', '-files', filename)) results = myout.getvalue() results = results.replace('\t', ' ') # 4 spaces per tab res.write(results) sys.stdout = stdout
def jsonCall(self): """Execute method with arguments on the server side. Returns Javascript function to be executed by the client immediately. """ request = self.request() data = json.loads(request.rawInput().read()) self._id, call, params = data['id'], data['method'], data['params'] if call == 'system.listMethods': self.writeResult(self.exposedMethods()) elif call in self.exposedMethods(): try: method = getattr(self, call) except AttributeError: self.writeError('%s, although an approved method, ' 'was not found' % call) else: try: if self._debug: self.log("json call %s(%s)" % (call, params)) self.writeResult(method(*params)) except Exception: err = StringIO() traceback.print_exc(file=err) e = err.getvalue() self.writeError('%s was called, ' 'but encountered an error: %s' % (call, e)) err.close() else: self.writeError('%s is not an approved method' % call)
def compileAndRun(self, pspSource, classname): pspClass = self.compileString(pspSource, classname) pspInstance = pspClass() outStream = StringIO() pspInstance._writeHTML(outStream) output = outStream.getvalue() return output
def validateHTML(html): """Validate the given html. Validate the input using Web Design Group's HTML validator available at http://www.htmlhelp.com/tools/validator/ Make sure you install the offline validator (called "validate") which can be called from the command-line. The "validate" script must be in your path. If no errors are found, an empty string is returned. Otherwise, the HTML with the error messages is returned. """ input, output = os.popen4('validate') input.write(html) input.close() out = output.readlines() output.close() errorLines = {} for line in out: if line[0:5] == 'Line ': i = line.find(',') if i >= 0: linenum = int(line[5:i]) errorLines[linenum] = line # Be quiet if all's well if not errorLines: return '' result = StringIO() result.write('<table style="background-color: #ffffff">' '<tr><td colspan="2">\n') result.write("<pre>%s</pre>" % "".join(out)) result.write('</td></tr>\n') goodColors = ['#d0d0d0', '#e0e0e0'] badColor = '#ffd0d0' lines = html.splitlines(True) i = 1 for line in lines: if i in errorLines: result.write('<tr style="background-color: %s">' '<td rowspan="2">%d</td><td>%s</td></tr>\n' % (badColor, i, encodeWithIndentation(errorLines[i]))) result.write('<tr style="background-color: %s">' '<td>%s</td></tr>\n' % (badColor, encodeWithIndentation(line))) else: color = goodColors[i % 2] result.write('<tr style="background-color: %s">' '<td>%d</td><td>%s</td></tr>\n' % (color, i, encodeWithIndentation(line))) i += 1 result.write('</table>\n') return result.getvalue()
def checkBasics(self): reader = HTMLReader() tag = reader.readString('<html> </html>') self.assertEqual(tag.name(), 'html') self.assertEqual(reader.rootTag(), tag) self.assertTrue(reader.filename() is None) out = StringIO() tag.pprint(out) self.assertEqual(out.getvalue(), '<html>\n</html>\n')
def ajaxCall(self): """Execute method with arguments on the server side. The method name is passed in the field _call_, the unique request number in the field _req_ and the arguments in the field _ (single underscore). Returns JavaScript function to be executed by the client immediately. """ req = self.request() startTime = None if req.hasField('_call_'): call = req.field('_call_') args = req.field('_', []) if not isinstance(args, list): args = [args] if self._clientPolling and self._responseTimeout: startTime = time() if call in self.exposedMethods(): try: method = getattr(self, call) except AttributeError: cmd = self.alert('%s, although an approved method, ' 'was not found' % call) else: try: if self._debug: self.log("Ajax call %s(%s)" % (call, args)) cmd = str(method(*args)) except Exception: err = StringIO() print_exc(file=err) e = err.getvalue() cmd = self.alert('%s was called, ' 'but encountered an error: %s' % (call, e)) err.close() else: cmd = self.alert('%s is not an approved method' % call) else: cmd = self.alert('Ajax call missing call parameter.') inTime = startTime is None or (time() - startTime < self._responseTimeout) if inTime: # If the computation of the method did not last very long, # deliver it immediately back to the client with this response: if self._debug: self.log("Ajax returns immediately: %s" % cmd) self.write(cmd) else: # If the client request might have already timed out, # put the result in the queue and let client poll it: if self._debug: self.log("Ajax puts in queue: %s" % cmd) sid = self.session().identifier() self._responseQueue.setdefault(sid, []).append(cmd)
def parse_response_gzip(self, f): """Workaround M2Crypto issue mentioned above.""" sio = StringIO() while 1: chunk = f.read() if not chunk: break sio.write(chunk) sio.seek(0) return Transport.parse_response_gzip(self, sio)
def createHighlightedSource(self, filename, dir): """Create highlighted HTML source code using py2html.""" from DocSupport import py2html module = os.path.splitext(os.path.basename(filename))[0] targetName = '%s/%s.html' % (dir, module) self.printMsg('Creating %s...' % targetName) stdout = sys.stdout sys.stdout = StringIO() try: py2html.main((None, '-stdout', '-files', filename)) result = sys.stdout.getvalue() finally: sys.stdout = stdout open(targetName, 'w').write(result)
def loads(self, str): """Unpickle a string.""" return self.load(StringIO(str))
def png(self): s = StringIO() self._image.save(s, 'png') return s.getvalue()
def png(self): s = StringIO() self._image.writePng(s) return s.getvalue()
def _testBasic(self): """Simple tests...""" # Create table t = DataTable() # Headings 1 t = DataTable() t.setHeadings([TableColumn('name'), TableColumn('age:int'), TableColumn('rating:float')]) # Headings 2 t = DataTable() t.setHeadings(['name', 'age:int', 'rating:float']) # Adding and accessing data a = ['John', '26', '7.25'] b = ['Mary', 32, 8.5] c = dict(name='Fred', age=28, rating=9.0) d = Record(name='Wilma', age=27, rating=9.5) t.append(a) t.append(b) t.append(c) t.append(d) self.assertEqual(t[-4]['name'], 'John') self.assertEqual(t[-3]['name'], 'Mary') self.assertEqual(t[-2]['name'], 'Fred') self.assertEqual(t[-1]['name'], 'Wilma') self.assertEqual(t[-4].asDict(), {'name': 'John', 'age': 26, 'rating': 7.25}) self.assertEqual(t[-3].asList(), b) self.assertEqual(t[-2].asDict(), c) self.assertEqual(t[-1].asList(), ['Wilma', 27, 9.5]) # Printing # print t # Writing file (CSV) answer = '''\ name,age,rating John,26,7.25 Mary,32,8.5 Fred,28,9.0 Wilma,27,9.5 ''' out = StringIO() t.writeFile(out) results = out.getvalue() self.assertEqual(results, answer, '\n%r\n%r\n' % (results, answer)) # Accessing rows for row in t: self.assertEqual(row['name'], row[0]) self.assertEqual(row['age'], row[1]) self.assertEqual(row['rating'], row[2]) for item in row: pass # Default type t = DataTable(defaultType='int') t.setHeadings(list('xyz')) t.append([1, 2, 3]) t.append([4, 5, 6]) self.assertEqual(t[0]['x'] - t[1]['z'], -5)
def emailException(self, htmlErrMsg): """Email the exception. Send the exception via mail, either as an attachment, or as the body of the mail. """ message = Message() # Construct the message headers headers = self.setting('ErrorEmailHeaders').copy() headers['Date'] = formatdate() headers['Mime-Version'] = '1.0' headers['Subject'] = headers.get( 'Subject', '[WebKit Error]') + ' %s: %s' % sys.exc_info()[:2] for h, v in headers.items(): if isinstance(v, (list, tuple)): v = ','.join(v) message.add_header(h, v) # Construct the message body if self.setting('EmailErrorReportAsAttachment'): # start off with a text/plain part text = ( 'WebKit caught an exception while processing' ' a request for "%s" at %s (timestamp: %s).' ' The plain text traceback from Python is printed below and' ' the full HTML error report from WebKit is attached.\n\n' % (self.servletPathname(), asclocaltime(self._time), self._time)) message.set_type('multipart/mixed') part = Message() part.set_type('text/plain') body = StringIO() body.write(text) traceback.print_exc(file=body) part.set_payload(body.getvalue()) body.close() message.attach(part) part = Message() # now add htmlErrMsg part.add_header('Content-Transfer-Encoding', '7bit') part.add_header('Content-Description', 'HTML version of WebKit error message') part.add_header('Content-Disposition', 'attachment', filename='WebKitErrorMsg.html') part.set_type('text/html') part.set_payload(htmlErrMsg) message.attach(part) else: message.set_type('text/html') message.set_payload(htmlErrMsg, 'us-ascii') # Send the message server = self.setting('ErrorEmailServer') # This setting can be: server, server:port, server:port:user:password # or server:port:user:password:popserver:popport for "smtp after pop". parts = server.split(':', 5) server = port = user = passwd = None popserver = popssl = popport = None try: server = parts[0] try: port = int(parts[1]) except ValueError: pass user = parts[2] passwd = parts[3] popserver = parts[4] try: popport = int(parts[5]) except ValueError: popport = None if parts[6].lower() == 'ssl': popssl = True except IndexError: pass if user and passwd and popserver: # SMTP after POP if popssl is None and popport == 995: popssl = True popssl = poplib.POP3_SSL if popssl else poplib.POP3 if popport: popserver = popssl(popserver, popport) else: popserver = popssl(popserver) popserver.set_debuglevel(0) popserver.user(user) popserver.pass_(passwd) try: popserver.quit() except Exception: pass if port: server = smtplib.SMTP(server, port) else: server = smtplib.SMTP(server) try: server.set_debuglevel(0) if user and passwd and not popserver: # SMTP-AUTH server.ehlo() if server.has_extn('starttls'): server.starttls() server.ehlo() server.login(user, passwd) body = message.as_string() server.sendmail(headers['From'], headers['To'], body) finally: try: server.quit() except Exception: pass