def document(self): """Render the error document""" page = error_document_template % \ dict(prefix=request.environ.get('SCRIPT_NAME', ''), code=cgi.escape(request.params.get('code', '')), message=cgi.escape(request.params.get('message', ''))) return page
def input_field(self, name, value, sample_values): if value is not None: string_value = self.format(value) else: string_value = '' return '<input class="%s" name="%s" type="text" size="%d" value="%s"/>' % (cgi.escape(self.name()), cgi.escape(name), self.input_field_size(), cgi.escape(string_value, True))
def writedoc(key,top=False): """Write HTML documentation to a file in the current directory.""" if(type(key) == str and (key == "modules" or key == "/.")): heading = pydoc.html.heading( '<br><big><big><strong> ' 'Python: Index of Modules' '</strong></big></big>', '#ffffff', '#7799ee') builtins = [] for name in sys.builtin_module_names: builtins.append('<a href="%s">%s</a>' % (cgi.escape(name,quote=True), cgi.escape(name))) indices = ['<p>Built-in modules: ' + cgi.escape(join(builtins, ', '))] seen = {} for dir in pydoc.pathdirs(): indices.append(pydoc.html.index(dir, seen)) print cleanlinks(heading + join(indices)) return if(type(key) != types.ModuleType): object = pydoc.locate(key) if(object == None and top): print "could not locate module/object for key " + \ cgi.escape(key) + "<br><a href=\"pydoc:modules\">go to index</a>"; else: object = key if object: print cleanlinks(pydoc.html.page(pydoc.describe(object), pydoc.html.document(object)))
def compareDictsHTML(new, old, tid=None, indent=''): """Compare two state dictionaries, return HTML.""" html = [indent + '<div class="diff">\n'] diff = compareDicts(new, old) for key, (action, newvalue, oldvalue) in sorted(diff.items(), key=lambda (k, v): (str(type(k)), k)): what = action.split()[0] html.append(indent + ' <div class="diffitem %s">\n' % escape(what)) if isinstance(key, basestring) and isascii(key): html.append(indent + ' <strong>%s</strong>: ' % escape(key)) else: html.append(indent + ' <strong>%s</strong>: ' % IValueRenderer(key).render(tid)) if (action == CHANGED and isinstance(oldvalue, dict) and isinstance(newvalue, dict)): html.append('dictionary changed:\n') html.append(compareDictsHTML(newvalue, oldvalue, tid, indent=indent + ' ')) elif (action == CHANGED and isinstance(oldvalue, tuple) and isinstance(newvalue, tuple)): html.append('tuple changed:\n') html.append(compareTuplesHTML(newvalue, oldvalue, tid, indent=indent + ' ')) else: html.append(action) html.append(' ') if action == REMOVED: value = oldvalue else: value = newvalue html.append(IValueRenderer(value).render(tid)) html.append('\n') html.append(indent + ' </div>\n') html.append(indent + '</div>\n') return ''.join(html)
def main(j, args, params, tags, tasklet): page = args.page doc = args.doc page.addCSS('/jslib/old/breadcrumbs/breadcrumbs.css') data = "<ul class='breadcrumb'>%s</ul>" breadcrumbs = [] space = j.core.portal.active.getSpace(doc.getSpaceName()) if 'breadcrumbdata' in args.requestContext.params: for breadcrumb in args.requestContext.params['breadcrumbdata'][::-1]: for name, link in breadcrumb.iteritems(): breadcrumbs.insert(0, (link, name, {})) else: breadcrumbs.append((doc.original_name, cgi.escape(doc.title), {})) while doc.parent: doc = space.docprocessor.name2doc.get(doc.parent) if not doc: break args = {} for arg in doc.requiredargs: if arg in doc.appliedparams: args[arg] = doc.appliedparams[arg] breadcrumbs.insert(0, (doc.original_name, cgi.escape(doc.title), args)) innerdata = "" breadcrumbs.insert(0, ('/%s' % space.model.id, space.model.name, {})) for link, title, args in breadcrumbs[:-1]: if args: link = "%s?%s" % (link, urllib.urlencode(args)) innerdata += "<li><a href='%s'>%s</a><span style='opacity: 0.5; margin-right: 8px; margin-left: 2px;' class='icon-chevron-right'></span></li>" % (link, title) innerdata += "<li class='active'>%s</li>" % breadcrumbs[-1][1] page.addMessage(data % innerdata) params.result = page return params
def dump_data( self ): print "<h2>SQL Dump data:</h2>" print print "<pre>" for line in self.data.splitlines(): print cgi.escape( self._complete_prefix( line ) ) print "</pre>"
def writedocs(path, pkgpath='', depth=0, notprocessed=[]): if(path == "/."): writedoc(path) return depth+=1 if os.path.isdir(path): if(depth > __maxdepth): notprocessed.append(path) return dir = path for file in os.listdir(dir): path = os.path.join(dir, file) if os.path.isdir(path): writedocs(path, file + '.' + pkgpath, depth) if os.path.isfile(path): writedocs(path, pkgpath, depth) if os.path.isfile(path): modname = pydoc.inspect.getmodulename(path) if modname: writedoc(pkgpath + modname) if(depth == 1): if(len(notprocessed) != 0): print "<br> the following paths were not processed because they are deeper than the maximum depth of " + str(__maxdepth) + ":<br>" for x in notprocessed: print cgi.escape(x) + " <br>"
def post(self): response = {"error": False, "errtext": "", "newPage": ""} try: pageNum = int(self.request.get("pageNum")) newPageText = cgi.escape(self.request.get("pageText")) newPageLink = cgi.escape(self.request.get("pageLinkText")) imageLink = cgi.escape(self.request.get("imageLink")) # Make sure there are enough pages left? # Escape the text of invalid characters, trimming to 1700 newPageText = newPageText[:2500] acc = db.GqlQuery("SELECT * FROM CurHighPage").get() if acc == None: acc = CurHighPage(counter=1) acc.put() newNum = db.run_in_transaction(increment_counter, acc.key()) # Add the new page, and add the new page link np = Page(pageNum=newNum, content=newPageText, imageLink=imageLink) np.put() npl = PageLink(pageFrom=pageNum, pageTo=newNum, linkText=newPageLink) npl.put() response["newPage"] = newNum except Exception, inst: response["error"] = True response["errtext"] = "An Exception Has Occured " + str(inst)
def add_rss_item(status, msg, direction): if direction == "in": feed = status.feed_in title = "%s %s entered NEW" % (msg['Source'], msg['Version']) pubdate = msg['Date'] elif direction == "out": feed = status.feed_out if msg.has_key('Leave-Reason'): title = "%s %s left NEW (%s)" % (msg['Source'], msg['Version'], msg['Leave-Reason']) else: title = "%s %s left NEW" % (msg['Source'], msg['Version']) pubdate = datetime.utcnow() else: return False description = "<pre>Description: %s\nChanges: %s\n</pre>" % \ (cgi.escape(msg['Description']), cgi.escape(msg['Changes'])) link = "http://ftp-master.tanglu.org/new/%s_%s.html" % \ (msg['Source'], msg['Version']) feed.items.insert(0, PyRSS2Gen.RSSItem( title, pubDate = pubdate, description = description, author = cgi.escape(msg['Maintainer']), link = link, guid = link ) )
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) return nickname = user.nickname() book_id = cgi.escape(self.request.get("book_id")) book = models.Book.get(book_id) success= cgi.escape(self.request.get("success")) #price= cgi.escape(self.request.get("price")) try: price = float(cgi.escape(self.request.get("price"))) except BadValueError: price = 0.0 if(book): if (success=="True"): book.sold_date=datetime.datetime.now() book.price=price book.put() self.redirect("/user") else: book.delete() self.redirect("/user") else: self.redirect("/user")
def post(self): response = {"error": False, "errtext": "", "newPage": ""} try: pageNum = int(self.request.get("pageNum")) pageLinkPage = int(cgi.escape(self.request.get("pageLinkPage"))) newPageLink = cgi.escape(self.request.get("pageLinkText")) page = db.GqlQuery("SELECT * FROM Page WHERE pageNum = :1", pageLinkPage).get() if page == None: raise Exception("Target page does not exist") oldPageLink = db.GqlQuery( "SELECT * FROM PageLink WHERE pageTo = :1 AND pageFrom = :2", pageNum, pageLinkPage ).get() if oldPageLink != None: raise Exception("Target already has link to page " + str(pageLinkPage)) npl = PageLink(pageFrom=pageNum, pageTo=pageLinkPage, linkText=newPageLink) npl.put() response["newPage"] = pageLinkPage except Exception, inst: response["error"] = True response["errtext"] = "An Exception Has Occured " + str(inst)
def main(script_path): checker_bin, checker_opts, checker_ver = find_checker_program() version_string = "PyCheckMate %s – %s" % (__version__, checker_ver) warning_string = "" if not checker_bin: href_format = \ "<a href=\"javascript:TextMate.system('open %s', null)\">%s</a>" pychecker_url = href_format % (PYCHECKER_URL, "PyChecker") pyflakes_url = href_format % (PYFLAKES_URL, "PyFlakes") pylint_url = href_format % (PYLINT_URL, "Pylint") pep8_url = href_format % (PEP8_URL, "PEP 8") flake8_url = href_format % (FLAKE8_URL, "flake8") warning_string = \ "<p>Please install %s, %s, %s, %s or %s for more extensive code checking." \ "</p><br>" % (pychecker_url, pyflakes_url, pylint_url, pep8_url, flake8_url) basepath = os.getenv("TM_PROJECT_DIRECTORY") if basepath: project_dir = os.path.basename(basepath) script_name = os.path.basename(script_path) title = "%s — %s" % (escape(script_name), escape(project_dir)) else: title = escape(script_path) print HTML_HEADER_FORMAT % (title, version_string) if warning_string: print warning_string if checker_bin: run_checker_program(checker_bin, checker_opts, script_path) else: check_syntax(script_path) print HTML_FOOTER return 0
def post(self): user = users.get_current_user() if not user: nickname = user.nickname() self.redirect(users.create_login_url(self.request.uri)) return #Form data try: isbn = ISBN(str(cgi.escape(self.request.get("isbn")))) isbn.to_isbn13() text_isbn = isbn.format('') except ValueError: text_isbn = None title = cgi.escape(self.request.get("title")) try: price = float(cgi.escape(self.request.get("price"))) except BadValueError: price = 0.0 condition = cgi.escape(self.request.get("condition")) book_id = cgi.escape(self.request.get("book_id")) book = models.Book.get(book_id) # if the book is valid, update it. if(book): book.price=price book.condition=condition book.title=title book.put() self.redirect("/user") else: self.redirect('/edit?'+urllib.urlencode({'badisbn':True,'price':price,'title':title}))
def get(self): action = cgi.escape(self.request.get('action')) code = cgi.escape(self.request.get('code')) name = cgi.escape(self.request.get('name')) year = cgi.escape(self.request.get('year')) _credits = self.request.get_all('credit') credits = [0, 0, 0, 0, 0] if(code!="" and name!="" and year!=""): i = 0 for credit in _credits: if not credit: credits[i] = 0 else: credits[i] = int(cgi.escape(credit)) i += 1 if(action == "add"): Course(parent=ParentKeys.course, code=code, name=name, year=int(year), credits=credits).put() return webapp2.redirect('/{0}/{1}'.format(Index.adminUrl, Index.manageCourseUrl)) elif(action == "update"): course = Course.query(ancestor=ParentKeys.course) course = course.filter(Course.code==code).get() course.name = name course.year = int(year) course.credits = credits course.put() return webapp2.redirect('/{0}/{1}'.format(Index.adminUrl, Index.manageCourseUrl)) elif(action == "delete"): course = Course.query(ancestor=ParentKeys.course) course = course.filter(Course.code==code).get() cs = CourseSubject.query(ancestor=ParentKeys.courseSubject) cs = cs.filter(CourseSubject.courseCode==code) for c in cs: c.key.delete() course.key.delete() return webapp2.redirect('/{0}/{1}'.format(Index.adminUrl, Index.manageCourseUrl)) elif(action == "manage"): return webapp2.redirect('/{0}/{1}?code={2}'.format(Index.adminUrl, Index.manageCourseSubjectUrl, code)) courses = Course.query(ancestor=ParentKeys.course) templateValues = { 'title' : Index.title + ' - Admin - Manage Courses', 'navigator' : Index.adminNavigator, 'action' : Index.manageCourseUrl, 'method' : 'get', 'courses' : courses, } path = os.path.join(os.path.dirname(__file__), 'html/Course.html') self.response.out.write(template.render(path, templateValues))
def get_html_default(self, prefix="", disabled=False): if self.multiple: multiple = " multiple" else: multiple = "" if self.size: size = ' size="%s"' % str(self.size) else: size = '' rval = [] last_selected_value = "" for text, value, selected in self.options: if selected: selected_text = " selected" last_selected_value = value if not isinstance(last_selected_value, string_types): last_selected_value = str(last_selected_value) else: selected_text = "" rval.append('<option value="%s"%s>%s</option>' % (escape(unicodify(value), quote=True), selected_text, escape(unicodify(text), quote=True))) if last_selected_value: last_selected_value = ' last_selected_value="%s"' % escape(unicodify(last_selected_value), quote=True) if self.field_id is not None: id_string = ' id="%s"' % self.field_id else: id_string = '' rval.insert(0, '<select name="%s%s"%s%s%s%s%s%s%s>' % (prefix, self.name, multiple, size, self.refresh_on_change_text, last_selected_value, self.get_disabled_str(disabled), id_string, self.extra_attributes)) rval.append('</select>') return unicodify("\n".join(rval))
def get_config_edit(self): """Handles 'edit' property action.""" key = self.request.get('name') if not key: self.redirect('/admin?action=settings') item = config.Registry.registered[key] if not item: self.redirect('/admin?action=settings') template_values = {} template_values[ 'page_title'] = 'Course Builder - Edit Settings' exit_url = '/admin?action=settings#%s' % cgi.escape(key) rest_url = '/rest/config/item' delete_url = '/admin?%s' % urllib.urlencode({ 'action': 'config_reset', 'name': key, 'xsrf_token': cgi.escape(self.create_xsrf_token('config_reset'))}) template_values['main_content'] = oeditor.ObjectEditor.get_html_for( self, ConfigPropertyEditor.get_schema_json(item), ConfigPropertyEditor.get_schema_annotations(item), key, rest_url, exit_url, delete_url=delete_url) self.render_page(template_values)
def get(self): self.response.out.write('<html><body>') guestbook_name=self.request.get('guestbook_name') # Ancestor queries, as shown here, are strongly consistent; queries that # span entity groups are only eventually consistent. If we omitted the # ancestor from this query, there would be a slight chance that a greeting # that had just been written would not show up in a query. greetings = db.GqlQuery("SELECT * " "FROM Greeting " "WHERE ANCESTOR IS :1 " "ORDER BY date DESC LIMIT 10", guestbook_key(guestbook_name)) for greeting in greetings: if greeting.author: self.response.out.write( '<b>%s</b> wrote:' % greeting.author) else: self.response.out.write('An anonymous person wrote:') self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(greeting.content)) self.response.out.write(""" <form action="/sign?%s" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="Sign Guestbook"></div> </form> <hr> <form>Guestbook name: <input value="%s" name="guestbook_name"> <input type="submit" value="switch"></form> </body> </html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}), cgi.escape(guestbook_name)))
def post(self): user = cgi.escape(self.request.get('username'), quote= True) pwd = cgi.escape(self.request.get('password'), quote= True) verify = cgi.escape(self.request.get('verify'), quote= True) email = cgi.escape(self.request.get('email'), quote= True) erroru2 = "" errorp2 = "" errore2 = "" user2 = user email2 = email errorv2 = "" #4 conditions - valid username, valid password, verified = pwd, email is valid if not self.valid_username(user): erroru2 = ERROR_U user2 = "" # check if username exists in database if not self.valid_password(pwd): errorp2 = ERROR_P if not self.valid_verify(pwd, verify): errorv2 = ERROR_V if not self.valid_email(email) or email != "": errore2 = ERROR_E email2 = "" if (self.valid_username(user) and self.valid_password(pwd) and self.valid_verify(pwd, verify) and self.valid_email(email)): #set cookie self.redirect('/welcome') else: self.write_form2(erroru2, errorp2, errore2, user2, email2, errorv2)
def parse_new_user_post(self): """ Parse the input from the create user form. Returns: A dict that maps the form fields on the user creation page to None (if they pass our validation) or a str indicating why they fail our validation. """ users = {} error_msgs = {} users['email'] = cgi.escape(self.request.get('user_email')) if re.match(self.USER_EMAIL_REGEX, users['email']): error_msgs['email'] = None else: error_msgs['email'] = 'Format must be [email protected].' users['password'] = cgi.escape(self.request.get('user_password')) if len(users['password']) >= self.MIN_PASSWORD_LENGTH: error_msgs['password'] = None else: error_msgs['password'] = '******' \ 'long.'.format(self.MIN_PASSWORD_LENGTH) users['password_confirmation'] = cgi.escape( self.request.get('user_password_confirmation')) if users['password_confirmation'] == users['password']: error_msgs['password_confirmation'] = None else: error_msgs['password_confirmation'] = 'Passwords do not match.' return error_msgs
def post_newpost(): title = bottle.request.forms.get("subject") post = bottle.request.forms.get("body") tags = bottle.request.forms.get("tags") username = login_check() # see if user is logged in if (username is None): bottle.redirect("/login") if (title == "" or post == ""): errors="Post must contain a title and blog entry" return bottle.template("newpost_template", dict(subject=cgi.escape(title, quote=True), username=username, body=cgi.escape(post, quote=True), tags=tags, errors=errors)) # extract tags tags = cgi.escape(tags) tags_array = extract_tags(tags) # looks like a good entry, insert it escaped escaped_post = cgi.escape(post, quote=True) # substitute some <p> for the paragraph breaks newline = re.compile('\r?\n') formatted_post = newline.sub("<p>",escaped_post) permalink=insert_entry(title, formatted_post, tags_array, username) # now bottle.redirect to the blog permalink bottle.redirect("/post/" + permalink)
def process_signup(): connection = pymongo.Connection(connection_string, safe=True) email = bottle.request.forms.get("email") username = bottle.request.forms.get("username") password = bottle.request.forms.get("password") verify = bottle.request.forms.get("verify") # set these up in case we have an error case errors = {'username':cgi.escape(username), 'email':cgi.escape(email)} if (user.validate_signup(username, password, verify, email, errors)): if (not user.newuser(connection, username, password, email)): # this was a duplicate errors['username_error'] = "Username already in use. Please choose another" return bottle.template("signup", errors) session_id = user.start_session(connection, username) print session_id cookie= user.make_secure_val(session_id) bottle.response.set_cookie("session",cookie) bottle.redirect("/welcome") else: print "user did not validate" return bottle.template("signup", errors)
def manage_delProperties(self, ids=None, REQUEST=None): """Delete one or more properties specified by 'ids'.""" if REQUEST: # Bugfix for property named "ids" (Casey) if ids == self.getProperty('ids', None): ids = None ids = REQUEST.get('_ids', ids) if ids is None: return MessageDialog( title='No property specified', message='No properties were specified!', action ='./manage_propertiesForm',) propdict=self.propdict() nd=self._reserved_names for id in ids: if not hasattr(aq_base(self), id): raise BadRequest, ( 'The property <em>%s</em> does not exist' % escape(id)) if (not 'd' in propdict[id].get('mode', 'wd')) or (id in nd): return MessageDialog( title ='Cannot delete %s' % id, message='The property <em>%s</em> cannot be deleted.' % escape(id), action ='manage_propertiesForm') self._delProperty(id) if REQUEST is not None: return self.manage_propertiesForm(self, REQUEST)
def main(band): norm = config.CONF.tv.upper() filename = '/tmp/stationlist.xml' fp = open(filename,'w') fp.write('<?xml version="1.0"?>\n') fp.write('<!DOCTYPE stationlist PUBLIC "-//tvtime//DTD stationlist 1.0//EN" ' '"http://tvtime.sourceforge.net/DTD/stationlist1.dtd">\n') fp.write('<stationlist xmlns="http://tvtime.sourceforge.net/DTD/">\n') fp.write(' <list norm="%s" frequencies="%s">\n' % (norm, band)) c = 0 for m in config.TV_CHANNELS: if config.TV_FREQUENCY_TABLE.has_key(m[2]): channelfreq = float(config.TV_FREQUENCY_TABLE[m[2]]) / 1000.0 fp.write(' <station name="%s" active="1" position="%s" band="Custom" channel="%sMHz"/>\n' % \ (cgi.escape(m[1]), c, channelfreq)) else: fp.write(' <station name="%s" active="1" position="%s" band="%s" channel="%s"/>\n' % \ (cgi.escape(m[1]), c, band, m[2])) c = c + 1 fp.write(' </list>\n') fp.write('</stationlist>\n') fp.close() print 'written stationlist to %r' % (filename,)
def show_stored_data(self): self.response.out.write(''' <p><table border=1> <tr> <th>Key</th> <th>Value</th> <th>Created (GMT)</th> </tr>''') # This next line is replaced by the one under it, in order to help # protect against SQL injection attacks. Does it help enough? #entries = db.GqlQuery("SELECT * FROM StoredData ORDER BY tag") entries = StoredData.all().order("-tag") for e in entries: entry_key_string = str(e.key()) self.response.out.write('<tr>') self.response.out.write('<td>%s</td>' % escape(e.tag)) self.response.out.write('<td>%s</td>' % escape(e.value)) self.response.out.write('<td><font size="-1">%s</font></td>\n' % e.date.ctime()) self.response.out.write(''' <td><form action="/deleteentry" method="post" enctype=application/x-www-form-urlencoded> <input type="hidden" name="entry_key_string" value="%s"> <input type="hidden" name="tag" value="%s"> <input type="hidden" name="fmt" value="html"> <input type="submit" style="background-color: red" value="Delete"></form></td>\n''' % (entry_key_string, escape(e.tag))) self.response.out.write('</tr>') self.response.out.write('</table>')
def proxy(path, start_response, proxy_url): # pylint:disable=too-many-locals if "://" not in path: path = "http://" + path try: try: response = urllib2.urlopen(path) except urllib2.HTTPError as ex: response = ex print("%s: %s %s" % (path, response.code, response.msg)) headers = [(k, v) for (k, v) in response.headers.items() if k not in drop_headers] scheme, netloc, path, _params, _query, _fragment = urlparse(path) host = (scheme or "http") + "://" + netloc except Exception as ex: # pylint:disable=broad-except sys.stderr.write("error while reading %s:\n" % path) traceback.print_exc() tb = traceback.format_exc() start_response("502 Bad Gateway", [("Content-Type", "text/html")]) # pylint:disable=deprecated-method error_str = escape(str(ex) or ex.__class__.__name__ or "Error") error_str = "<h1>%s</h1><h2>%s</h2><pre>%s</pre>" % (error_str, escape(path), escape(tb)) return [_as_bytes(error_str)] else: start_response("%s %s" % (response.code, response.msg), headers) data = response.read() data = fix_links(data, proxy_url, host) return [data]
def application(environ, start_response): # the environment variable CONTENT_LENGTH may be empty or missing try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 # When the method is POST the query string will be sent # in the HTTP request body which is passed by the WSGI server # in the file like wsgi.input environment variable. request_body = environ['wsgi.input'].read(request_body_size) d = parse_qs(request_body) age = d.get('age', [''])[0] # Returns the first age value. hobbies = d.get('hobbies', []) # Returns a list of hobbies. # Always escape user input to avoid script injection age = escape(age) hobbies = [escape(hobby) for hobby in hobbies] response_body = html % (age or 'Empty', ', '.join(hobbies or ['No Hobbies'])) status = '200 OK' response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body]
def get_clean_body(mutant, response): ''' @see: Very similar to fingerprint_404.py get_clean_body() bug not quite the same maybe in the future I can merge both? Definition of clean in this method: - input: - response.get_url() == http://host.tld/aaaaaaa/?id=1 OR 23=23 - response.get_body() == '...<x>1 OR 23=23</x>...' - output: - self._clean_body( response ) == '...<x></x>...' All injected values are removed encoded and "as is". :param mutant: The mutant where I can get the value from. :param response: The HTTPResponse object to clean :return: A string that represents the "cleaned" response body. ''' body = response.body if response.is_text_or_html(): mod_value = mutant.get_mod_value() body = body.replace(mod_value, '') body = body.replace(urllib.unquote_plus(mod_value), '') body = body.replace(cgi.escape(mod_value), '') body = body.replace(cgi.escape(urllib.unquote_plus(mod_value)), '') return body
def sidebarRenderOwnSettings(self, body, site): title = cgi.escape(site.content_manager.contents.get("content.json", {}).get("title", ""), True) description = cgi.escape(site.content_manager.contents.get("content.json", {}).get("description", ""), True) privatekey = cgi.escape(self.user.getSiteData(site.address, create=False).get("privatekey", "")) body.append(u""" <li> <label for='settings-title'>Site title</label> <input type='text' class='text' value="{title}" id='settings-title'/> </li> <li> <label for='settings-description'>Site description</label> <input type='text' class='text' value="{description}" id='settings-description'/> </li> <li style='display: none'> <label>Private key</label> <input type='text' class='text long' value="{privatekey}" placeholder='[Ask on signing]'/> </li> <li> <a href='#Save' class='button' id='button-settings'>Save site settings</a> </li> """.format(**locals()))
def diagnose(): """ Check the environment for reasons libnotify isn't working. Return a user-readable message indicating possible issues. """ try: from gi.repository import Notify # @UnusedImport except ImportError: return (u"<p>Error: gir-notify isn't installed. On Ubuntu/Debian, install the " u"<a href=\"apt:gir1.2-notify-0.7\">gir1.2-notify-0.7</a> or " u"<a href=\"apt:gir1.0-notify-0.4\">gir1.0-notify-0.4</a> package.") if 'DISPLAY' not in os.environ and 'DBUS_SESSION_BUS_ADDRESS' not in os.environ: return (u"<p>Error: Environment variables DISPLAY and DBUS_SESSION_BUS_ADDRESS " u"aren't set. libnotify will only work when you run SickRage " u"from a desktop login.") try: import dbus except ImportError: pass else: try: bus = dbus.SessionBus() except dbus.DBusException, e: return (u"<p>Error: unable to connect to D-Bus session bus: <code>%s</code>." u"<p>Are you running SickRage in a desktop session?") % (cgi.escape(e),) try: bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications') except dbus.DBusException, e: return (u"<p>Error: there doesn't seem to be a notification daemon available: <code>%s</code> " u"<p>Try installing notification-daemon or notify-osd.") % (cgi.escape(e),)
def format_element(bfo, print_links="yes", serie_prefix="<br /><strong>Series: </strong>", separator="; "): """ Print series and volumes @param print_links Prints HTML links if "yes" @param serie_prefix A prefix printed before each serie @param separator a separator between series """ out = [] series = bfo.fields('490') for serie in series: serie_out = "" if serie.has_key('a'): serie_out += serie_prefix if print_links.lower() == "yes": serie_out += '<a href="http://cdsweb.cern.ch/search?f=490__a&p=' + \ serie['a'].replace(' ', '+') + '"' + \ cgi.escape(serie['a']) + '</a>' else: serie_out += cgi.escape(serie['a']) if serie.has_key('v'): serie_out += ', ' + serie['v'] if serie_out: out.append(serie_out) return separator.join(out)
def submit_request(): db = client.tudev_checkout data = dict(request.form) resp = {} resp['success'] = [] resp['failed'] = [] for item in data: item_name = data[item][0] item_quantity = int(data[item][1]) stored_item = db.inventory.find_one({'name': item_name}) if (stored_item): if (stored_item['quantity'] >= item_quantity): new_quant = stored_item['quantity'] - item_quantity db.inventory.update({'name': item_name}, {'$set': { 'quantity': new_quant }}) resp['success'].append({ 'name': item_name, 'quantity': item_quantity }) else: resp['failed'].append({ 'name': item_name, 'quantity': item_quantity }) request_id = str(uuid.uuid4())[:4] resp['id'] = request_id # if any items were checked out if (resp['success']): # send emails email_account = session['user'] full_account = db.users.find_one({'email': email_account}) if (full_account): user_name = full_account['name'] else: user_name = None email_server = smtplib.SMTP(app.config['SMTP'], 25) email_server.starttls() order_msg = email.message.Message() order_msg['Subject'] = 'TUDev Hardware - Item Request' order_msg['From'] = app.config['REQUEST_EMAIL_SEND'] order_msg['To'] = 'TUDev Orders' order_msg.add_header('Content-Type', 'text/html') items = [] for item in resp['success']: formatted_item = ''' <li>{quantity}x {name}<hr /></li> '''.format(quantity=item['quantity'], name=item['name']) items.append(formatted_item) items = ''.join(items) safe_name = cgi.escape(user_name).encode('ascii', 'xmlcharrefreplace') safe_email = cgi.escape(email_account).encode('ascii', 'xmlcharrefreplace') email_content = ''' <html> <body> <h1>Item Order</h1> <p><strong>From: </strong>{name}</p> <p><strong>Email: </strong>{email_account}</p> <p><strong>Order ID: </strong>{order_id}</p> <p><strong>Items ordered: </strong> <ul> {items} </ul> </p> </body> </html>'''.format(name=safe_name, email_account=safe_email, items=items, order_id=request_id) order_msg.set_payload(email_content) for account in app.config['REQUEST_EMAIL_ADMINS']: email_server.sendmail(app.config['REQUEST_EMAIL_SEND'], account, order_msg.as_string()) return jsonify(resp)
docFile = open(resultat, 'rb') contingut = docFile.read() docFile.close() os.remove(resultat) except Exception, e: excepcio = unicode(e) if not excepcio: response = http.HttpResponse( contingut, content_type='application/vnd.oasis.opendocument.text') response[ 'Content-Disposition'] = 'attachment; filename=qualitativa.odt' else: response = http.HttpResponse('''Gremlin's ate your pdf! %s''' % cgi.escape(excepcio)) return response #http://xhtml2pdf.appspot.com/static/pisa-en.html def reportQualitativa2(qualitativa, alumnes=[], grups=[], request=None): import locale locale.setlocale(locale.LC_TIME, 'ca_ES.utf8') reports = [] for nivell in Nivell.objects.all(): for curs in nivell.curs_set.all(): for grup in curs.grup_set.all():
def opener(self): return u'<span class="%s">' % cgi.escape(self.payload, True)
def fixfile(inpath, input_lines, outfile): from email.Utils import parseaddr basename = os.path.basename(inpath) infile = iter(input_lines) # convert plaintext psep to minimal XHTML markup print >> outfile, DTD print >> outfile, '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' print >> outfile, COMMENT print >> outfile, '<head>' # head header = [] psep = "" title = "" for line in infile: if not line.strip(): break if line[0].strip(): if ":" not in line: break key, value = line.split(":", 1) value = value.strip() header.append((key, value)) else: # continuation line key, value = header[-1] value = value + line header[-1] = key, value if key.lower() == "title": title = value elif key.lower() == "psep": psep = value if psep: title = "PSEP " + psep + " -- " + title if title: print >> outfile, ' <title>%s</title>' % cgi.escape(title) r = random.choice(range(64)) print >> outfile, ( ' <link rel="STYLESHEET" href="psep.css" type="text/css" />\n' '</head>\n' '<body>\n' '<div id="header">\n' '<a href="http://www.pyside.org/" title="Python Home Page">\n' '<img src="http://www.pyside.org/wp-content/themes/openbossa/images/logo.png" alt="[PySide]"\n' 'border="0" width="199" height="102" /></a>\n' '[<b><a href="http://www.pyside.org/">PySide Home</a></b>]\n' '</div>\n' '<div id="content">\n') if basename <> 'psep-0000.txt': print >> outfile, '[<b><a href=".">PSEP Index</a></b>]' print >> outfile, '<table border="0">' for k, v in header: if k.lower() in ('author', 'discussions-to'): mailtos = [] for part in re.split(',\s*', v): if '@' in part: realname, addr = parseaddr(part) if k.lower() == 'discussions-to': m = linkemail(addr, psep) else: m = fixemail(addr, psep) mailtos.append('%s <%s>' % (realname, m)) elif part.startswith('http:'): mailtos.append( '<a href="%s">%s</a>' % (part, part)) else: mailtos.append(part) v = COMMASPACE.join(mailtos) elif k.lower() in ('replaces', 'replaced-by', 'requires'): otherpseps = '' for otherpsep in re.split(',?\s+', v): otherpsep = int(otherpsep) otherpseps += '<a href="psep-%04d.html">%i</a> ' % (otherpsep, otherpsep) v = otherpseps elif k.lower() in ('last-modified',): date = time.strftime('%d-%b-%Y', time.localtime(os.stat(inpath)[8])) if int(psep) != 0: url = PSEPCVSURL % int(psep) v = '<a href="%s">%s</a> ' % (url, cgi.escape(date)) else: v = date elif k.lower() in ('content-type',): url = PSEPURL % 9 psep_type = v or 'text/plain' v = '<a href="%s">%s</a> ' % (url, cgi.escape(psep_type)) else: v = cgi.escape(v) print >> outfile, ' <tr class="field"><th class="field-name">%s: </th><td class="field-body">%s</td></tr>' \ % (cgi.escape(k), v) print >> outfile, '</table>' print >> outfile, '<hr />' need_pre = 1 for line in infile: if line[0] == '\f': continue if line.strip() == LOCALVARS: break if line[0].strip(): if not need_pre: print >> outfile, '</pre>' print >> outfile, '<h3>%s</h3>' % line.strip() need_pre = 1 elif not line.strip() and need_pre: continue else: # PSEP 0 has some special treatment if basename == 'psep-0000.txt': parts = line.split() if len(parts) > 1 and re.match(r'\s*\d{1,4}', parts[1]): # This is a PSEP summary line, which we need to hyperlink url = PSEPURL % int(parts[1]) if need_pre: print >> outfile, '<pre>' need_pre = 0 print >> outfile, re.sub( parts[1], '<a href="%s">%s</a>' % (url, parts[1]), line, 1), continue elif parts and '@' in parts[-1]: # This is a psep email address line, so filter it. url = fixemail(parts[-1], psep) if need_pre: print >> outfile, '<pre>' need_pre = 0 print >> outfile, re.sub( parts[-1], url, line, 1), continue line = fixpat.sub(lambda x, c=inpath: fixanchor(c, x), line) if need_pre: print >> outfile, '<pre>' need_pre = 0 outfile.write(line) if not need_pre: print >> outfile, '</pre>' print >> outfile, '</div>' print >> outfile, '</body>' print >> outfile, '</html>'
def editcount(user): if not confirmcount(user): return ns = dict() total = 0 try: db = MySQLdb.connect(db=dbname, host=dbserver, user=dbuser, passwd=dbpassword) except: print """ <p><strong>onoes!!</strong> some kind of database error occured... please try again later</p> """ return c = db.cursor() c.execute( "SELECT COUNT(DISTINCT page_id) FROM user, revision, page WHERE " "user_name=%s AND rev_user = user_id AND rev_page = page_id", user) t = c.fetchone() distinct = t[0] c = db.cursor() c.execute( "SELECT MIN(rev_timestamp) FROM user, revision, page WHERE " "user_name=%s AND rev_user = user_id AND rev_page = page_id", user) t = c.fetchone() firstedit = t[0] c = db.cursor() c.execute( "SELECT page_namespace, COUNT(*) FROM user, revision, page WHERE " "user_name=%s AND rev_user = user_id AND rev_page = page_id " "GROUP BY page_namespace", user) edits = {} t = c.fetchone() while t != None: total += t[1] edits[t[0]] = t[1] t = c.fetchone() if total == 0: print """<p><strong>this user does not seem to have any edits, or they don't exist</strong></p>""" return print """ <table class="edittable"> <tr><th>Username</th><td colspan='2'>%s</td></tr> <tr><th>Total edits</th><td colspan='2'>%s</td></tr> <tr><th>Distinct pages edited</th><td colspan='2'>%s</td></tr> <tr><th>Edits/page (avg)</th><td colspan='2'>%0.02f</td></tr> <tr><th>First edit</th><td colspan='2'>%s</td></tr> <tr> <th rowspan='%s'>Edits by namespace</th> <th>Namespace</th><th>Edits</th> </tr> """ % (cgi.escape(user), total, distinct, (float(total) / float(distinct)), fmttime(firstedit), len(edits) + 1) for nsn in edits.keys(): print "<tr class='nsn'><td class='nsname'>%s</td><td class='nedits'>%d</td></tr>" \ % (ns2name(nsn, dbname), edits[nsn]) print """ </table> <br clear='all'/> <p>* 'Project' and 'Project talk' refer to the project namespace: Wikipedia, Wiktionary, Meta, etc.</p> """ return
def f0(self, m): return '<!--' + escape(m.group(1), True) + '-->'
def request_item(): data = request.form try: name = data['name'] email_account = data['email'] item = data['item'] content = data['content'] confirm_msg = email.message.Message() confirm_msg['Subject'] = 'Request Item - Request Recieved' confirm_msg['From'] = app.config['REQUEST_EMAIL_SEND'] confirm_msg['To'] = email_account confirm_msg.add_header('Content-Type', 'text/html') safe_name = cgi.escape(name.split(' ')[0]).encode( 'ascii', 'xmlcharrefreplace') safe_item = cgi.escape(item).encode('ascii', 'xmlcharrefreplace') email_content = ''' <html> <body> <p> Hey {name}! <br> We recieved your request, we'll look into "{item}". <br> Happy Hacking, <br> The TUDev Team </p> </body> </html>'''.format(name=safe_name, item=safe_item) confirm_msg.set_payload(email_content) email_server = smtplib.SMTP(app.config['SMTP'], 25) email_server.starttls() # send email email_server.sendmail(app.config['REQUEST_EMAIL_SEND'], email_account, confirm_msg.as_string()) request_msg = email.message.Message() request_msg['Subject'] = 'TUDev Hardware - Item Request' request_msg['From'] = app.config['REQUEST_EMAIL_SEND'] request_msg['To'] = email_account request_msg.add_header('Content-Type', 'text/html') safe_email = cgi.escape(email_account).encode('ascii', 'xmlcharrefreplace') safe_content = cgi.escape(content).encode('ascii', 'xmlcharrefreplace') email_content = ''' <html> <body> <h1>Item Request</h1> <p><strong>From: </strong>{name}</p> <p><strong>Email: </strong>{email_account}</p> <p><strong>Item Requested: </strong>{item}</p> <p><strong>Reason for request</strong><br>{content}</p> </body> </html>'''.format(name=safe_name, email_account=safe_email, item=safe_item, content=safe_content) request_msg.set_payload(email_content) for account in app.config['REQUEST_EMAIL_ADMINS']: email_server.sendmail(app.config['REQUEST_EMAIL_SEND'], account, request_msg.as_string()) return jsonify({'status': 'request sent'}) except KeyError as e: abort(400)
def actionListobj(self): import gc import sys self.sendHeader() if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local: yield "This function is disabled on this proxy" raise StopIteration # No more if not in debug mode if not config.debug: yield "Not in debug mode" raise StopIteration type_filter = self.get.get("type") yield """ <style> * { font-family: monospace; white-space: pre } table * { text-align: right; padding: 0px 10px } </style> """ yield "Listing all %s objects in memory...<br>" % cgi.escape( type_filter) ref_count = {} objs = gc.get_objects() for obj in objs: obj_type = str(type(obj)) if obj_type != type_filter: continue refs = [ ref for ref in gc.get_referrers(obj) if hasattr(ref, "__class__") and ref.__class__.__name__ not in [ "list", "dict", "function", "type", "frame", "WeakSet", "tuple" ] ] if not refs: continue try: yield "%.1fkb <span title=\"%s\">%s</span>... " % ( float(sys.getsizeof(obj)) / 1024, cgi.escape( str(obj)), cgi.escape(str(obj)[0:100].ljust(100))) except: continue for ref in refs: yield " [" if "object at" in str(ref) or len(str(ref)) > 100: yield str(ref.__class__.__name__) else: yield str(ref.__class__.__name__) + ":" + cgi.escape( str(ref)) yield "] " ref_type = ref.__class__.__name__ if ref_type not in ref_count: ref_count[ref_type] = [0, 0] ref_count[ref_type][0] += 1 # Count ref_count[ref_type][1] += float( sys.getsizeof(obj)) / 1024 # Size yield "<br>" yield "<br>Object referrer (total: %s, %.2fkb):<br>" % ( len(ref_count), sum([stat[1] for stat in ref_count.values()])) for obj, stat in sorted(ref_count.items(), key=lambda x: x[1][0], reverse=True)[0:30]: # Sorted by count yield " - %.1fkb = %s x %s<br>" % (stat[1], stat[0], cgi.escape(str(obj))) gc.collect() # Implicit grabage collection
def html_escape(s, quote=True): return cgi.escape(s, quote=quote)
def note_link(self, note_or_guid, body): """Return a formatted note-link a-tag for note_or_guid.""" return ('<a href="%s" style="color: rgb(105, 170, 53);">%s</a>' % (self.get_evernote_url(note_or_guid), cgi.escape(body)))
def print_intent(out, id, i, cell): if cell: out.write("""<span class="intent" title="%s">shortcut</span>""" % ( cgi.escape(cell, True) ))
def actionStats(self): import gc import sys from Ui import UiRequest from Db import Db from Crypt import CryptConnection hpy = None if self.get.get("size") == "1": # Calc obj size try: import guppy hpy = guppy.hpy() except: pass self.sendHeader() if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local: yield "This function is disabled on this proxy" raise StopIteration s = time.time() main = sys.modules["main"] # Style yield """ <style> * { font-family: monospace } table td, table th { text-align: right; padding: 0px 10px } .connections td { white-space: nowrap } .serving-False { opacity: 0.3 } </style> """ # Memory try: yield "rev%s | " % config.rev yield "%s | " % config.ip_external yield "Opened: %s | " % main.file_server.port_opened yield "Crypt: %s | " % CryptConnection.manager.crypt_supported yield "In: %.2fMB, Out: %.2fMB | " % ( float(main.file_server.bytes_recv) / 1024 / 1024, float(main.file_server.bytes_sent) / 1024 / 1024) yield "Peerid: %s | " % main.file_server.peer_id import psutil process = psutil.Process(os.getpid()) mem = process.get_memory_info()[0] / float(2**20) yield "Mem: %.2fMB | " % mem yield "Threads: %s | " % len(process.threads()) yield "CPU: usr %.2fs sys %.2fs | " % process.cpu_times() yield "Files: %s | " % len(process.open_files()) yield "Sockets: %s | " % len(process.connections()) yield "Calc size <a href='?size=1'>on</a> <a href='?size=0'>off</a>" except Exception: pass yield "<br>" # Connections yield "<b>Connections</b> (%s, total made: %s):<br>" % (len( main.file_server.connections), main.file_server.last_connection_id) yield "<table class='connections'><tr> <th>id</th> <th>type</th> <th>ip</th> <th>open</th> <th>crypt</th> <th>ping</th>" yield "<th>buff</th> <th>bad</th> <th>idle</th> <th>open</th> <th>delay</th> <th>cpu</th> <th>out</th> <th>in</th> <th>last sent</th>" yield "<th>wait</th> <th>version</th> <th>sites</th> </tr>" for connection in main.file_server.connections: if "cipher" in dir(connection.sock): cipher = connection.sock.cipher()[0] else: cipher = connection.crypt yield self.formatTableRow([ ("%3d", connection.id), ("%s", connection.type), ("%s:%s", (connection.ip, connection.port)), ("%s", connection.handshake.get("port_opened")), ("<span title='%s'>%s</span>", (connection.crypt, cipher)), ("%6.3f", connection.last_ping_delay), ("%s", connection.incomplete_buff_recv), ("%s", connection.bad_actions), ("since", max(connection.last_send_time, connection.last_recv_time)), ("since", connection.start_time), ("%.3f", connection.last_sent_time - connection.last_send_time), ("%.3f", connection.cpu_time), ("%.0fkB", connection.bytes_sent / 1024), ("%.0fkB", connection.bytes_recv / 1024), ("%s", connection.last_cmd), ("%s", connection.waiting_requests.keys()), ("%s r%s", (connection.handshake.get("version"), connection.handshake.get("rev", "?"))), ("%s", connection.sites) ]) yield "</table>" # Tor hidden services yield "<br><br><b>Tor hidden services (status: %s):</b><br>" % main.file_server.tor_manager.status for site_address, onion in main.file_server.tor_manager.site_onions.items( ): yield "- %-34s: %s<br>" % (site_address, onion) # Db yield "<br><br><b>Db</b>:<br>" for db in sys.modules["Db.Db"].opened_dbs: yield "- %.3fs: %s<br>" % (time.time() - db.last_query_time, db.db_path.encode("utf8")) # Sites yield "<br><br><b>Sites</b>:" yield "<table>" yield "<tr><th>address</th> <th>connected</th> <th title='connected/good/total'>peers</th> <th>content.json</th> <th>out</th> <th>in</th> </tr>" for site in sorted(self.server.sites.values(), lambda a, b: cmp(a.address, b.address)): yield self.formatTableRow([ ("""<a href='#' onclick='document.getElementById("peers_%s").style.display="initial"; return false'>%s</a>""", (site.address, site.address)), ("%s", [ peer.connection.id for peer in site.peers.values() if peer.connection and peer.connection.connected ]), ("%s/%s/%s", (len([ peer for peer in site.peers.values() if peer.connection and peer.connection.connected ]), len(site.getConnectablePeers(100)), len(site.peers))), ("%s (loaded: %s)", (len(site.content_manager.contents), len([ key for key, val in dict( site.content_manager.contents).iteritems() if val ]))), ("%.0fkB", site.settings.get("bytes_sent", 0) / 1024), ("%.0fkB", site.settings.get("bytes_recv", 0) / 1024), ], "serving-%s" % site.settings["serving"]) yield "<tr><td id='peers_%s' style='display: none; white-space: pre' colspan=6>" % site.address for key, peer in site.peers.items(): if peer.time_found: time_found = int(time.time() - peer.time_found) / 60 else: time_found = "--" if peer.connection: connection_id = peer.connection.id else: connection_id = None if site.content_manager.hashfield: yield "Optional files: %4s " % len(peer.hashfield) time_added = (time.time() - peer.time_added) / (60 * 60 * 24) yield "(#%4s, err: %s, found: %3s min, add: %.1f day) %30s -<br>" % ( connection_id, peer.connection_error, time_found, time_added, key) yield "<br></td></tr>" yield "</table>" # No more if not in debug mode if not config.debug: raise StopIteration # Object types obj_count = {} for obj in gc.get_objects(): obj_type = str(type(obj)) if obj_type not in obj_count: obj_count[obj_type] = [0, 0] obj_count[obj_type][0] += 1 # Count obj_count[obj_type][1] += float(sys.getsizeof(obj)) / 1024 # Size yield "<br><br><b>Objects in memory (types: %s, total: %s, %.2fkb):</b><br>" % ( len(obj_count), sum([stat[0] for stat in obj_count.values()]), sum([stat[1] for stat in obj_count.values()])) for obj, stat in sorted(obj_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count yield " - %.1fkb = %s x <a href=\"/Listobj?type=%s\">%s</a><br>" % ( stat[1], stat[0], obj, cgi.escape(obj)) # Classes class_count = {} for obj in gc.get_objects(): obj_type = str(type(obj)) if obj_type != "<type 'instance'>": continue class_name = obj.__class__.__name__ if class_name not in class_count: class_count[class_name] = [0, 0] class_count[class_name][0] += 1 # Count class_count[class_name][1] += float( sys.getsizeof(obj)) / 1024 # Size yield "<br><br><b>Classes in memory (types: %s, total: %s, %.2fkb):</b><br>" % ( len(class_count), sum([stat[0] for stat in class_count.values()]), sum([stat[1] for stat in class_count.values()])) for obj, stat in sorted(class_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count yield " - %.1fkb = %s x <a href=\"/Dumpobj?class=%s\">%s</a><br>" % ( stat[1], stat[0], obj, cgi.escape(obj)) from greenlet import greenlet objs = [obj for obj in gc.get_objects() if isinstance(obj, greenlet)] yield "<br>Greenlets (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj).encode("utf8"))) from Worker import Worker objs = [obj for obj in gc.get_objects() if isinstance(obj, Worker)] yield "<br>Workers (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) from Connection import Connection objs = [obj for obj in gc.get_objects() if isinstance(obj, Connection)] yield "<br>Connections (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) from socket import socket objs = [obj for obj in gc.get_objects() if isinstance(obj, socket)] yield "<br>Sockets (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) from msgpack import Unpacker objs = [obj for obj in gc.get_objects() if isinstance(obj, Unpacker)] yield "<br>Msgpack unpacker (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) from Site import Site objs = [obj for obj in gc.get_objects() if isinstance(obj, Site)] yield "<br>Sites (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) objs = [ obj for obj in gc.get_objects() if isinstance(obj, self.server.log.__class__) ] yield "<br>Loggers (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj.name))) objs = [obj for obj in gc.get_objects() if isinstance(obj, UiRequest)] yield "<br>UiRequests (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) from Peer import Peer objs = [obj for obj in gc.get_objects() if isinstance(obj, Peer)] yield "<br>Peers (%s):<br>" % len(objs) for obj in objs: yield " - %.1fkb: %s<br>" % (self.getObjSize( obj, hpy), cgi.escape(repr(obj))) objs = [(key, val) for key, val in sys.modules.iteritems() if val is not None] objs.sort() yield "<br>Modules (%s):<br>" % len(objs) for module_name, module in objs: yield " - %.3fkb: %s %s<br>" % (self.getObjSize( module, hpy), module_name, cgi.escape(repr(module))) gc.collect() # Implicit grabage collection yield "Done in %.1f" % (time.time() - s)
def escape_html(string, quote=False): if six.PY2: import cgi return cgi.escape(string, quote=quote) import html return html.escape(string, quote=quote)
def print_cell(out, id, i, cell): if not cell is None: out.write(cgi.escape(str(cell)))
def _apply_index(self, request, resultset=None): """Apply the index to query parameters given in the request arg. The request argument should be a mapping object. If the request does not have a key which matches the "id" of the index instance, then None is returned. If the request *does* have a key which matches the "id" of the index instance, one of a few things can happen: - if the value is a blank string, None is returned (in order to support requests from web forms where you can't tell a blank string from empty). - if the value is a nonblank string, turn the value into a single-element sequence, and proceed. - if the value is a sequence, return a union search. - If the value is a dict and contains a key of the form '<index>_operator' this overrides the default method ('or') to combine search results. Valid values are "or" and "and". If None is not returned as a result of the abovementioned constraints, two objects are returned. The first object is a ResultSet containing the record numbers of the matching records. The second object is a tuple containing the names of all data fields used. FAQ answer: to search a Field Index for documents that have a blank string as their value, wrap the request value up in a tuple ala: request = {'id':('',)} """ record = parseIndexRequest(request, self.id, self.query_options) if record.keys is None: return None index = self._index r = None opr = None # experimental code for specifing the operator operator = record.get('operator',self.useOperator) if not operator in self.operators : raise RuntimeError("operator not valid: %s" % escape(operator)) # Range parameter range_parm = record.get('range',None) if range_parm: opr = "range" opr_args = [] if range_parm.find("min")>-1: opr_args.append("min") if range_parm.find("max")>-1: opr_args.append("max") if record.get('usage',None): # see if any usage params are sent to field opr = record.usage.lower().split(':') opr, opr_args=opr[0], opr[1:] if opr=="range": # range search if 'min' in opr_args: lo = min(record.keys) else: lo = None if 'max' in opr_args: hi = max(record.keys) else: hi = None if hi: setlist = index.values(lo,hi) else: setlist = index.values(lo) # If we only use one key, intersect and return immediately if len(setlist) == 1: result = setlist[0] if isinstance(result, int): result = IISet((result,)) return result, (self.id,) if operator == 'or': tmp = [] for s in setlist: if isinstance(s, int): s = IISet((s,)) tmp.append(s) r = multiunion(tmp) else: # For intersection, sort with smallest data set first tmp = [] for s in setlist: if isinstance(s, int): s = IISet((s,)) tmp.append(s) if len(tmp) > 2: setlist = sorted(tmp, key=len) else: setlist = tmp r = resultset for s in setlist: # the result is bound by the resultset r = intersection(r, s) else: # not a range search # Filter duplicates setlist = [] for k in record.keys: s = index.get(k, None) # If None, try to bail early if s is None: if operator == 'or': # If union, we can't possibly get a bigger result continue # If intersection, we can't possibly get a smaller result return IISet(), (self.id,) elif isinstance(s, int): s = IISet((s,)) setlist.append(s) # If we only use one key return immediately if len(setlist) == 1: result = setlist[0] if isinstance(result, int): result = IISet((result,)) return result, (self.id,) if operator == 'or': # If we already get a small result set passed in, intersecting # the various indexes with it and doing the union later is # faster than creating a multiunion first. if resultset is not None and len(resultset) < 200: smalllist = [] for s in setlist: smalllist.append(intersection(resultset, s)) r = multiunion(smalllist) else: r = multiunion(setlist) else: # For intersection, sort with smallest data set first if len(setlist) > 2: setlist = sorted(setlist, key=len) r = resultset for s in setlist: r = intersection(r, s) if isinstance(r, int): r = IISet((r, )) if r is None: return IISet(), (self.id,) else: return r, (self.id,)
def run(self): from bs4 import BeautifulSoup try: infile = open(os.path.join(COLLECTOR_ROOT, "malware", "malwr"), "r") data = infile.read() infile.close() soup = BeautifulSoup(data) ###Analysis Part try: self.genreport["status"] = True div = soup.find("div", {"class": "box-content"}) if div: table = div.find("table") static = parse(str(table)) for i in range(1, len(static)): if static[i] != []: self.info["Category"] = static[i][0] self.info["Started"] = static[i][1] self.info["Completed"] = static[i][2] self.info["Duration"] = static[i][3] self.genreport["analysisinfo"] = self.info except: log.error("Problem in malwr plugin in analysis section") ###Details Part try: div = soup.find("section", {"id": "file"}) if div: table = div.find("table") static = parse(str(table)) keys = list() values = list() keys = static[0] for i in range(1, len(static)): values.append("".join(str(static[i][0]))) dictionary = dict(zip(keys, values)) self.genreport["details"] = dictionary if "Download" in values: malware = soup.find( "a", {"class": "btn btn-primary btn-small"}) from auth.malwrauth import MalwrAuth from common.config import ConfigFile parser = ConfigFile() username = parser.get("MalwrAuth", "username") password = parser.get("MalwrAuth", "password") auth = MalwrAuth() auth.login(auth.urllogin, username, password) auth.download_malware(malware["href"]) except Exception as e: log.error("Problem in malwr plugin in details section") print e ###Signature Part try: sig = soup.find("section", {"id": "signatures"}) if sig: div = sig.findAll("div", {"class": re.compile("^alert")}) for i in div: self.signatures.append(i.text.strip()) self.genreport["signatures"] = self.signatures #print self.signatures except: log.error("Problem in malwr plugin in signature section") ###Screenshots try: scr = soup.find("section", {"id": "screenshots"}) href = scr.findAll("a", {"rel": "lightbox"}) if href: for link in href: self.screenshots.append("https://malwr.com/" + link["href"]) #print self.screenshots self.genreport["screenshots"] = self.screenshots tmpimg = list() count = 0 for screen in self.screenshots: img = requests.get(screen) tmpimg.append(base64.b64encode(img.content)) outfile = open( os.path.join(COLLECTOR_ROOT, "tmp", str(count) + ".png"), "wb") outfile.write(img.content) count += 1 outfile.close() self.genreport["images"] = tmpimg except Exception as error: log.debug(error) log.error("Problem in malwr plugin in screenshots section") ###Domains try: dm = soup.find("section", {"id": "domains"}) table = dm.find("table") if table: static = parse(str(table)) for i in range(1, len(static)): if static[i] != []: self.doms.append(static[i][0]) self.ips.append(static[i][1]) self.genreport["domains"] = self.doms self.genreport["ipaddress"] = self.ips #print self.doms #print self.ips except: log.error("problem with malwr plugin in section domain") ###IRC try: dm = soup.find("div", { "class": "tab-pane fade", "id": "network_irc_tab" }) if dm.pre: for line in dm.pre.contents: self.irc.append(line.extract()) #print cgi.escape(line.extract()) self.genreport["irc"] = self.irc except Exception as e: log.error("problem with malwr plugin in section IRC") #print e ###SMTP try: dm = soup.find("div", { "class": "tab-pane fade", "id": "network_smtp_tab" }) if dm.pre: for line in dm.pre.contents: self.smtp.append(line.extract()) print cgi.escape(line.extract()) self.genreport["smtp"] = self.smtp except Exception as e: log.error("problem with malwr plugin in section SMTP") #print e ###Section Summary try: sm = soup.find("section", {"id": "summary"}) fl = sm.find("div", {"id": "summary_files"}) tfiles = fl.find("div", {"class": "well mono"}) if tfiles: for files in tfiles.findAll(text=True): self.files.append(files.strip()) #print self.files self.genreport["files"] = self.files except: log.error("Problem with malwr plugin in summary files") ###Section Registry Keys try: rg = sm.find("div", {"id": "summary_keys"}) rkeys = rg.find("div", {"class": "well mono"}) if rkeys: for key in rkeys.findAll(text=True): self.regkeys.append(key.strip()) #print self.regkeys self.genreport["regkeys"] = self.regkeys except: log.error("Problem with malwr plugin in section regkeys") ###Section Summary Mutexex try: m = sm.find("div", {"id": "summary_mutexes"}) mutexes = m.find("div", {"class": "well mono"}) if mutexes: for mutex in mutexes.findAll(text=True): self.mutexes.append(mutex.strip()) #print self.mutexes self.genreport["mutexes"] = self.mutexes except: log.error("Problem in malwr plugin in mutex section") ###Section Static Analysis try: st = soup.find("div", {"id": "pe_sections"}) if st: table = st.find("table") static = parse(str(table)) for i in range(0, len(static)): if static[i] != []: self.pe_sections.append( (static[i][0], static[i][1], static[i][2], static[i][3], static[i][4])) #print self.pe_sections self.genreport["pe_sections"] = self.pe_sections except: log.error("Problem in malwr plugin in pe section") ###Section PE info try: div = soup.find("section", {"id": "static_analysis"}) if div: table = div.find("div", {"id": "pe_versioninfo"}) if table: static = parse(str(table)) keys = list() values = list() keys = static[0] for i in range(1, len(static)): values.append("".join(str(static[i][0]))) dictionary = dict(zip(keys, values)) self.genreport["versioninfo"] = dictionary except Exception as e: log.error("Problem in malwr plugin in PE info") print e ###PE Imports try: imp = soup.find("div", {"id": "pe_imports"}) if imp: Allimports = imp.findAll("div", {"class": "well"}) tmpdict = dict() if Allimports: for imports in Allimports: tmplist = list() library = imports.div.strong.text.split()[1] tmplist.append(("Library", "Offset", "Call")) for imps in imports.findAll( "span", {"class": "mono"}): tmpdict[imps.text.split() [0]] = imps.text.split()[1] tmplist.append((str(library.strip()), imps.text.split()[0], imps.text.split()[1])) self.pe_imports.append((library, tmpdict)) self.tmp.append(tmplist) #print self.pe_imports self.genreport["pe_imports"] = self.tmp except Exception as e: print e log.error("Problem in malwr plugin in pe imports") ###PE Strings try: strsection = soup.find("section", {"id": "static_strings"}) Allstr = strsection.find("div", {"class": "well"}) for string in Allstr.findAll(text=True): self.strings += "".join(string) tmpout = open( os.path.join(COLLECTOR_ROOT, "tmpannex", "strings.txt"), "w") tmpout.write(self.strings) tmpout.close() #print self.strings self.genreport["strings"] = self.strings except: log.error("Problem in malwr plugin in strings section") ####Behavioral Section try: script = soup.find('script', text=re.compile('graph_raw_data')) if script: json_text = script.text.split("=")[1].strip() jsonobject = json.loads(json_text[:-1]) self.behavior = jsonobject """for behav in self.behavior: for key,value in behav.items(): print key+" "+str(value)""" self.genreport["behavior"] = self.behavior except: log.error("Problem in malwr plugin in behavior javascript") ###Network Section try: ipnet = soup.find("div", {"id": "network_hosts_tab"}) table = ipnet.find("table") if table: static = parse(str(table)) for i in range(1, len(static)): if static[i] != []: self.ips.append(static[i][0]) except: log.error("Problem in malwr plugin in network host tab") ###Network Requests try: req = soup.find("div", {"id": "network_http_tab"}) table = req.find("table") if table: static = parse(str(table)) for i in range(0, len(static)): if static[i] != []: self.http_requests.append(static[i]) #print self.http_requests self.genreport["http_requests"] = self.http_requests except: log.error("Problem in malwr plugin in http requests") ###Dropped files try: dropfiles = soup.find("div", {"id": "dropped"}) dropAll = dropfiles.findAll("div", {"class": "box"}) if dropAll: for drp in dropAll: table = drp.find("table") static = parse(str(table)) keys = list() values = list() keys = static[0] for i in range(1, len(static)): if static[i] != []: values.append("".join(str(static[i][0]))) dictionary = dict(zip(keys, values)) self.dropped_files.append(dictionary) #print self.dropped_files self.genreport["dropped_files"] = self.dropped_files except: log.error("Problem in malwr plugin in dropped files") self.genreport["count"] = 10 return self.genreport except IOError: self.genreport["count"] = 0 return self.genreport log.error("Malwr file not found...")
def svg(self, nodecolor='blue', leafcolor='red', funccolor='green'): """ :return: SVG representation of a tree. """ fontsize = 12 hscale = 40 vscale = 25 hstart = vstart = 20 width = max(col for _, col in self.coords.values()) height = max(row for row, _ in self.coords.values()) result = [ '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" ' 'width="%dem" height="%dem" viewBox="%d %d %d %d">' % ( width * 3, height * 2.5, -hstart, -vstart, width * hscale + 3 * hstart, height * vscale + 3 * vstart, ) ] children = defaultdict(set) for n in self.nodes: if n: children[self.edges[n]].add(n) # horizontal branches from nodes to children for node in self.nodes: if not children[node]: continue y, x = self.coords[node] x *= hscale y *= vscale x += hstart y += vstart + fontsize // 2 childx = [self.coords[c][1] for c in children[node]] xmin = hstart + hscale * min(childx) xmax = hstart + hscale * max(childx) result.append( '\t<polyline style="stroke:black; stroke-width:1; fill:none;" ' 'points="%g,%g %g,%g" />' % (xmin, y, xmax, y)) result.append( '\t<polyline style="stroke:black; stroke-width:1; fill:none;" ' 'points="%g,%g %g,%g" />' % (x, y, x, y - fontsize // 3)) # vertical branches from children to parents for child, parent in self.edges.items(): y, _ = self.coords[parent] y *= vscale y += vstart + fontsize // 2 childy, childx = self.coords[child] childx *= hscale childy *= vscale childx += hstart childy += vstart - fontsize result += [ '\t<polyline style="stroke:white; stroke-width:10; fill:none;"' ' points="%g,%g %g,%g" />' % (childx, childy, childx, y + 5), '\t<polyline style="stroke:black; stroke-width:1; fill:none;"' ' points="%g,%g %g,%g" />' % (childx, childy, childx, y), ] # write nodes with coordinates for n, (row, column) in self.coords.items(): node = self.nodes[n] x = column * hscale + hstart y = row * vscale + vstart if n in self.highlight: color = nodecolor if isinstance(node, Tree) else leafcolor if isinstance(node, Tree) and node.label().startswith('-'): color = funccolor else: color = 'black' result += [ '\t<text style="text-anchor: middle; fill: %s; ' 'font-size: %dpx;" x="%g" y="%g">%s</text>' % ( color, fontsize, x, y, escape(node.label() if isinstance(node, Tree) else node), ) ] result += ['</svg>'] return '\n'.join(result)
def hook(ui, repo, hooktype, **args): #################### # get most recent changeset for value of some keywords: node = repo.lookup('tip') changes = repo.changelog.read(node) keywordmap = { 'U': cgi.escape(changes[1]), 'r': repo.changelog.rev(node), 's': short(node), 'h': hex(node), 'd': datetimestr(changes[2]), 't': cgi.escape(get_1stline(changes[4])), 'now': datetimestr([time.time(), None]), 'hgroot': repo.root, } if ui.verbose: for name, value in keywordmap.items(): ui.note(('keyword:%s=%s\n' % (name, value))) #################### # get properties from 'graphviz' section: section = 'graphviz' config = {} for name, default in configtable.items(): value = ui.config(section, name) if value is not None: config[name] = (default is False) or value elif default is None: raise util.Abort( _('"%s" is required in "%s" section') % (name, section)) else: config[name] = default ui.note(('config:%s=%s\n' % (name, config[name]))) key = 'limitspecs' limitspecs = (ui.config(section, key) or '') limits = filter(lambda s: 0 != len(s), string.split(limitspecs, ' ')) if ui.verbose: for limit in limits: ui.note(('limit:%s\n') % (limit)) opts = {} for c, opt, default, desc in graphviz_optlist: name = ('opt.%s' % opt) value = ui.config(section, name) key = opt.replace('-', '_') if value is not None: opts[key] = (default is None) or value else: opts[key] = default ui.note(('opt:%s=%s\n' % (opt, opts[key]))) opts['aliases'] = opts['aliases'] % keywordmap dirname, basename, suffix = split_path(config['imagefile']) #################### # invoke 'graphviz' extension ui.pushbuffer() graphviz(ui, repo, *limits, **opts) dot_source = ui.popbuffer() if config['dot_source']: dotfile = string.join([basename, 'dot'], '.') filename = (os.path.join(dirname, dotfile) % keywordmap) ui.note(('filename:%s\n') % (filename)) f = open(filename, 'wb') try: f.write(dot_source) f.flush() finally: f.close() #################### # invoke graphviz 'dot' command for image file cmd = string.join(['dot', ('-T%s' % suffix), config['dot_opts']], ' ') ui.note(('command:%s\n') % (cmd)) filename = (config['imagefile'] % keywordmap) ui.note(('filename:%s\n') % (filename)) filtercommand(ui, cmd, dot_source, filename) #################### # invoke graphviz 'dot' command for image map file if not config['imagemap']: return cmapfile = string.join([basename, 'html'], '.') imgtag = ('<img src="%s" usemap="_anonymous_0">' % (string.join([basename, suffix], '.'))) cmd = string.join(['dot', ('-T%s' % 'cmapx'), config['dot_opts']], ' ') ui.note(('command:%s\n') % (cmd)) filename = (os.path.join(dirname, cmapfile) % keywordmap) ui.note(('filename:%s\n') % (filename)) filtercommand(ui, cmd, dot_source, filename, o_prologue=((config['prologue'] % keywordmap) + imgtag), o_epilogue=(config['epilogue'] % keywordmap)) return False
def escape_html(s): return cgi.escape(s, quote="TRUE")
def register(): username = cgi.escape(request.form['username']) password = cgi.escape(request.form['password']) password2 = cgi.escape(request.form['password2']) email = cgi.escape(request.form['email']) usernameError = "" passwordError = "" password2Error = "" emailError = "" if not username: print("no username") usernameError = "Username is required" if not password: passwordError = "Password is required" if not email: emailError = "email is required" if len(email) <= 3 or len(email) >= 20: emailError = "email must be at between 3 and 20 characters long" counter = 0 for char in email: if char == ".": counter = counter + 1 if counter != 1: emailError = "email must include one '.'" counter = 0 for char in email: if char == "@": counter = counter + 1 if counter != 1: emailError = "email must include one '@'" else: hasSpace = False for char in email: if char == " ": emailError = "email can't contain spaces" if len(username) < 8: usernameError = "user name must be at least 8 characters long" elif len(username) > 20: usernameError = "username must be shorter then 20 characters" else: hasSpace = False for char in username: if char == " ": usernameError = "user name can't contain spaces" if len(password) < 8: passwordError = "Password must be at least 8 characters long" elif len(password) > 20: passwordError = "Password must be shorter then 20 characters" else: hasSpace = False for char in password: if char == " ": passwordError = "Password can't contain spaces" if password != password2: password2Error = "Password 2 must match password" if usernameError or passwordError or password2Error or emailError: return render_template("form.html", username=username, usernameError=usernameError, password=password, passwordError=passwordError, password2=password2, password2Error=password2Error, email=email, emailError=emailError) return render_template("welcome.html", username=username)
def _listing(self, env, start_response, prefix=None): """ Sends an HTML object listing to the remote client. :param env: The original WSGI environment dict. :param start_response: The original WSGI start_response hook. :param prefix: Any prefix desired for the container listing. """ if self._listings.lower() not in TRUE_VALUES: resp = HTTPNotFound()(env, self._start_response) return self._error_response(resp, env, start_response) tmp_env = self._get_escalated_env(env) tmp_env['REQUEST_METHOD'] = 'GET' tmp_env['PATH_INFO'] = \ '/%s/%s/%s' % (self.version, self.account, self.container) tmp_env['QUERY_STRING'] = 'delimiter=/&format=json' if prefix: tmp_env['QUERY_STRING'] += '&prefix=%s' % quote(prefix) else: prefix = '' resp = self.app(tmp_env, self._start_response) if self._get_status_int() // 100 != 2: return self._error_response(resp, env, start_response) listing = json.loads(''.join(resp)) if not listing: resp = HTTPNotFound()(env, self._start_response) return self._error_response(resp, env, start_response) headers = {'Content-Type': 'text/html; charset=UTF-8'} body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' \ 'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n' \ '<html>\n' \ ' <head>\n' \ ' <title>Listing of %s</title>\n' % \ cgi.escape(env['PATH_INFO']) if self._listings_css: body += ' <link rel="stylesheet" type="text/css" ' \ 'href="%s%s" />\n' % \ ('../' * prefix.count('/'), quote(self._listings_css)) else: body += ' <style type="text/css">\n' \ ' h1 {font-size: 1em; font-weight: bold;}\n' \ ' th {text-align: left; padding: 0px 1em 0px 1em;}\n' \ ' td {padding: 0px 1em 0px 1em;}\n' \ ' a {text-decoration: none;}\n' \ ' </style>\n' body += ' </head>\n' \ ' <body>\n' \ ' <h1 id="title">Listing of %s</h1>\n' \ ' <table id="listing">\n' \ ' <tr id="heading">\n' \ ' <th class="colname">Name</th>\n' \ ' <th class="colsize">Size</th>\n' \ ' <th class="coldate">Date</th>\n' \ ' </tr>\n' % \ cgi.escape(env['PATH_INFO']) if prefix: body += ' <tr id="parent" class="item">\n' \ ' <td class="colname"><a href="../">../</a></td>\n' \ ' <td class="colsize"> </td>\n' \ ' <td class="coldate"> </td>\n' \ ' </tr>\n' for item in listing: if 'subdir' in item: subdir = item['subdir'] if prefix: subdir = subdir[len(prefix):] body += ' <tr class="item subdir">\n' \ ' <td class="colname"><a href="%s">%s</a></td>\n' \ ' <td class="colsize"> </td>\n' \ ' <td class="coldate"> </td>\n' \ ' </tr>\n' % \ (quote(subdir), cgi.escape(subdir)) for item in listing: if 'name' in item: name = item['name'] if prefix: name = name[len(prefix):] body += ' <tr class="item %s">\n' \ ' <td class="colname"><a href="%s">%s</a></td>\n' \ ' <td class="colsize">%s</td>\n' \ ' <td class="coldate">%s</td>\n' \ ' </tr>\n' % \ (' '.join('type-' + cgi.escape(t.lower(), quote=True) for t in item['content_type'].split('/')), quote(name), cgi.escape(name), human_readable(item['bytes']), cgi.escape(item['last_modified']).split('.')[0]. replace('T', ' ')) body += ' </table>\n' \ ' </body>\n' \ '</html>\n' resp = Response(headers=headers, body=body) self._log_response(env, resp.status_int) return resp(env, start_response)
def text( self, nodedist=1, unicodelines=False, html=False, ansi=False, nodecolor='blue', leafcolor='red', funccolor='green', abbreviate=None, maxwidth=16, ): """ :return: ASCII art for a discontinuous tree. :param unicodelines: whether to use Unicode line drawing characters instead of plain (7-bit) ASCII. :param html: whether to wrap output in html code (default plain text). :param ansi: whether to produce colors with ANSI escape sequences (only effective when html==False). :param leafcolor, nodecolor: specify colors of leaves and phrasal nodes; effective when either html or ansi is True. :param abbreviate: if True, abbreviate labels longer than 5 characters. If integer, abbreviate labels longer than `abbr` characters. :param maxwidth: maximum number of characters before a label starts to wrap; pass None to disable. """ if abbreviate == True: abbreviate = 5 if unicodelines: horzline = '\u2500' leftcorner = '\u250c' rightcorner = '\u2510' vertline = ' \u2502 ' tee = horzline + '\u252C' + horzline bottom = horzline + '\u2534' + horzline cross = horzline + '\u253c' + horzline ellipsis = '\u2026' else: horzline = '_' leftcorner = rightcorner = ' ' vertline = ' | ' tee = 3 * horzline cross = bottom = '_|_' ellipsis = '.' def crosscell(cur, x=vertline): """Overwrite center of this cell with a vertical branch.""" splitl = len(cur) - len(cur) // 2 - len(x) // 2 - 1 lst = list(cur) lst[splitl:splitl + len(x)] = list(x) return ''.join(lst) result = [] matrix = defaultdict(dict) maxnodewith = defaultdict(lambda: 3) maxnodeheight = defaultdict(lambda: 1) maxcol = 0 minchildcol = {} maxchildcol = {} childcols = defaultdict(set) labels = {} wrapre = re.compile('(.{%d,%d}\\b\\W*|.{%d})' % (maxwidth - 4, maxwidth, maxwidth)) # collect labels and coordinates for a in self.nodes: row, column = self.coords[a] matrix[row][column] = a maxcol = max(maxcol, column) label = (self.nodes[a].label() if isinstance(self.nodes[a], Tree) else self.nodes[a]) if abbreviate and len(label) > abbreviate: label = label[:abbreviate] + ellipsis if maxwidth and len(label) > maxwidth: label = wrapre.sub(r'\1\n', label).strip() label = label.split('\n') maxnodeheight[row] = max(maxnodeheight[row], len(label)) maxnodewith[column] = max(maxnodewith[column], max(map(len, label))) labels[a] = label if a not in self.edges: continue # e.g., root parent = self.edges[a] childcols[parent].add((row, column)) minchildcol[parent] = min(minchildcol.get(parent, column), column) maxchildcol[parent] = max(maxchildcol.get(parent, column), column) # bottom up level order traversal for row in sorted(matrix, reverse=True): noderows = [[ ''.center(maxnodewith[col]) for col in range(maxcol + 1) ] for _ in range(maxnodeheight[row])] branchrow = [ ''.center(maxnodewith[col]) for col in range(maxcol + 1) ] for col in matrix[row]: n = matrix[row][col] node = self.nodes[n] text = labels[n] if isinstance(node, Tree): # draw horizontal branch towards children for this node if n in minchildcol and minchildcol[n] < maxchildcol[n]: i, j = minchildcol[n], maxchildcol[n] a, b = (maxnodewith[i] + 1) // 2 - 1, maxnodewith[j] // 2 branchrow[i] = ((' ' * a) + leftcorner).ljust( maxnodewith[i], horzline) branchrow[j] = (rightcorner + (' ' * b)).rjust( maxnodewith[j], horzline) for i in range(minchildcol[n] + 1, maxchildcol[n]): if i == col and any(a == i for _, a in childcols[n]): line = cross elif i == col: line = bottom elif any(a == i for _, a in childcols[n]): line = tee else: line = horzline branchrow[i] = line.center(maxnodewith[i], horzline) else: # if n and n in minchildcol: branchrow[col] = crosscell(branchrow[col]) text = [a.center(maxnodewith[col]) for a in text] color = nodecolor if isinstance(node, Tree) else leafcolor if isinstance(node, Tree) and node.label().startswith('-'): color = funccolor if html: text = [escape(a) for a in text] if n in self.highlight: text = [ '<font color=%s>%s</font>' % (color, a) for a in text ] elif ansi and n in self.highlight: text = [ '\x1b[%d;1m%s\x1b[0m' % (ANSICOLOR[color], a) for a in text ] for x in range(maxnodeheight[row]): # draw vertical lines in partially filled multiline node # labels, but only if it's not a frontier node. noderows[x][col] = (text[x] if x < len(text) else (vertline if childcols[n] else ' ').center(maxnodewith[col], ' ')) # for each column, if there is a node below us which has a parent # above us, draw a vertical branch in that column. if row != max(matrix): for n, (childrow, col) in self.coords.items(): if n > 0 and self.coords[ self.edges[n]][0] < row < childrow: branchrow[col] = crosscell(branchrow[col]) if col not in matrix[row]: for noderow in noderows: noderow[col] = crosscell(noderow[col]) branchrow = [ a + ((a[-1] if a[-1] != ' ' else b[0]) * nodedist) for a, b in zip(branchrow, branchrow[1:] + [' ']) ] result.append(''.join(branchrow)) result.extend((' ' * nodedist).join(noderow) for noderow in reversed(noderows)) return '\n'.join(reversed(result)) + '\n'
def to_xhtml_stream(stream): title_levels = [] last_title_level = None list_items = [] one_to_one = { 'paragraph': 'p', 'literal_block': 'pre', 'list_item': 'li', 'strong': 'strong', 'emphasis': 'em', 'literal': 'tt' } events = [] for event, value, line in stream: if event == TEXT: data = escape(value) events.append((event, data, line)) elif event == START_ELEMENT: _, name, attributes = value attr = {} if name == 'title': overline = attributes[(rest_uri, 'overline')] underline = attributes[(rest_uri, 'underline')] if (overline, underline) in title_levels: level = title_levels.index((overline, underline)) else: level = len(title_levels) title_levels.append((overline, underline)) # Add an Anchor to this section attr[(xhtml_uri, 'name')] = attributes[(rest_uri, 'target')] events.append((event, (xhtml_uri, 'a', attr), line)) events.append((END_ELEMENT, (xhtml_uri, 'a'), line)) # index 0 -> <h1> level += 1 tag = 'h%d' % level attr = {} last_title_level = level elif name == 'footnote': target = '#id%s' % str(attributes['target']) tag = 'a' attr[(xhtml_uri, 'href')] = target events.append((event, (xhtml_uri, tag), None)) tag = None # Add the character [ events.append((TEXT, '[', line)) elif name == 'reference': tag = 'a' target = attributes['target'] attr[(xhtml_uri, 'href')] = '#%s' % target elif name == 'list': item = attributes[(rest_uri, 'item')] if item == u'#': tag = 'ol' else: tag = 'ul' list_items.append(item) elif name in ('document', ): tag = None else: tag = one_to_one[name] if tag: events.append((event, (xhtml_uri, tag, attr), line)) elif event == END_ELEMENT: _, name = value if name == 'title': tag = 'h%d' % last_title_level elif name == 'footnote': events.append((TEXT, ']', line)) tag = 'a' elif name == 'reference': tag = 'a' elif name == 'list': if list_items.pop() == u'#': tag = 'ol' else: tag = 'ul' elif name in ('document', ): tag = None else: tag = one_to_one[name] if tag: events.append((event, (xhtml_uri, tag), None)) return events
def escape(s): return cgi.escape(s)
def add(self, text, style=None, color=None, highlight=None, size=None, subscript=None, superscript=None, bold=False, italic=False, underline=False, strike=False, font=None, url_id=None): if not isinstance(text, six.text_type): text = text.decode('utf-8', errors='ignore') text = escape(text).replace('\n', NEWLINE_XML).replace( '\a', NEWPARAGRAPH_XML).replace('\t', TAB_XML) prop = u'' if style: prop += u'<w:rStyle w:val="%s"/>' % style if color: if color[0] == '#': color = color[1:] prop += u'<w:color w:val="%s"/>' % color if highlight: if highlight[0] == '#': highlight = highlight[1:] prop += u'<w:highlight w:val="%s"/>' % highlight if size: prop += u'<w:sz w:val="%s"/>' % size prop += u'<w:szCs w:val="%s"/>' % size if subscript: prop += u'<w:vertAlign w:val="subscript"/>' if superscript: prop += u'<w:vertAlign w:val="superscript"/>' if bold: prop += u'<w:b/>' if italic: prop += u'<w:i/>' if underline: if underline not in ['single', 'double']: underline = 'single' prop += u'<w:u w:val="%s"/>' % underline if strike: prop += u'<w:strike/>' if font: prop += u'<w:rFonts w:ascii="{font}" w:hAnsi="{font}" w:cs="{font}"/>'.format( font=font) xml = u'<w:r>' if prop: xml += u'<w:rPr>%s</w:rPr>' % prop xml += u'<w:t xml:space="preserve">%s</w:t></w:r>' % text if url_id: xml = u'<w:hyperlink r:id="%s" w:tgtFrame="_blank">%s</w:hyperlink>' % ( url_id, xml) self.xml += xml
def ExpandLinks(s): """ Expand $xref:bash and so forth """ f = cStringIO.StringIO() out = html.Output(s, f) tag_lexer = html.TagLexer(s) pos = 0 it = html.ValidTokens(s) while True: try: tok_id, end_pos = next(it) except StopIteration: break if tok_id == html.StartTag: tag_lexer.Reset(pos, end_pos) if tag_lexer.TagName() == 'a': open_tag_right = end_pos href_start, href_end = tag_lexer.GetSpanForAttrValue('href') if href_start == -1: continue # TODO: Need to unescape like GetAttr() href = s[href_start:href_end] new = None m = _SHORTCUT_RE.match(href) if m: abbrev_name, arg = m.groups() if not arg: close_tag_left, _ = html.ReadUntilEndTag( it, tag_lexer, 'a') arg = s[open_tag_right:close_tag_left] # Hack to so we can write [Wiki Page]($wiki) and have the link look # like /Wiki-Page/ if abbrev_name == 'wiki': arg = arg.replace(' ', '-') func = _ABBREVIATIONS.get(abbrev_name) if not func: raise RuntimeError('Invalid abbreviation %r' % abbrev_name) new = func(arg) if new is not None: out.PrintUntil(href_start) f.write(cgi.escape(new)) out.SkipTo(href_end) pos = end_pos out.PrintTheRest() return f.getvalue()
def _dir2pi(option, argv): pkgdir = argv[1] if not os.path.isdir(pkgdir): raise ValueError("no such directory: %r" %(pkgdir, )) pkgdirpath = lambda *x: os.path.join(pkgdir, *x) shutil.rmtree(pkgdirpath("simple"), ignore_errors=True) os.mkdir(pkgdirpath("simple")) pkg_index = ("<html><head><title>Simple Index</title>" "<meta name='api-version' value='2' /></head><body>\n") processed_pkg = set() for file in os.listdir(pkgdir): pkg_filepath = os.path.join(pkgdir, file) if not os.path.isfile(pkg_filepath): continue pkg_basename = os.path.basename(file) if pkg_basename.startswith("."): continue pkg_name, pkg_rest = file_to_package(pkg_basename, pkgdir) pkg_dir_name = pkg_name if option.normalize_package_names: pkg_dir_name = normalize_pep503(pkg_dir_name) pkg_dir = pkgdirpath("simple", pkg_dir_name) if not os.path.exists(pkg_dir): os.mkdir(pkg_dir) if option.aggressive_normalization: try_symlink(option, pkg_dir_name, pkgdirpath("simple", normalize_pip67(pkg_name))) try_symlink(option, pkg_dir_name, pkgdirpath("simple", pkg_name)) symlink_target = os.path.join(pkg_dir, pkg_basename) symlink_source = os.path.join("../../", pkg_basename) if option.use_symlink: try_symlink(option, symlink_source, symlink_target) else: if option.verbose: print('copying %s to %s' % (symlink_target, pkg_filepath)) shutil.copy2(pkg_filepath, symlink_target) if pkg_name not in processed_pkg: pkg_index += "<a href='%s/'>%s</a><br />\n" %( cgi.escape(pkg_dir_name), cgi.escape(pkg_name), ) processed_pkg.add(pkg_name) if option.build_html: with open(os.path.join(pkg_dir, "index.html"), "a") as fp: fp.write("<a href='%(name)s'>%(name)s</a><br />\n" %{ "name": cgi.escape(pkg_basename), }) pkg_index += "</body></html>\n" if option.build_html: with open(pkgdirpath("simple/index.html"), "w") as fp: fp.write(pkg_index) return 0
def __init__(self, text): self.xml = escape(text).replace('\n', NEWLINE_XML).replace( '\a', NEWPARAGRAPH_XML)
def custom(obj): return escape(str(obj))
def escape(text): """:escape: Any text. Replaces the special XML/XHTML characters "&", "<" and ">" with XML entities, and filters out NUL characters. """ return cgi.escape(text.replace('\0', ''), True)