def toolbar(self): from html import DIV, SCRIPT, BEAUTIFY, TAG, URL, A BUTTON = TAG.button admin = URL("admin", "default", "design", extension='html', args=current.request.application) from gluon.dal import DAL dbstats = [] dbtables = {} infos = DAL.get_instances() for k,v in infos.iteritems(): dbstats.append(TABLE(*[TR(PRE(row[0]),'%.2fms' % (row[1]*1000)) for row in v['dbstats']])) dbtables[k] = dict(defined=v['dbtables']['defined'] or '[no defined tables]', lazy=v['dbtables']['lazy'] or '[no lazy tables]') u = web2py_uuid() backtotop = A('Back to top', _href="#totop-%s" % u) # Convert lazy request.vars from property to Storage so they # will be displayed in the toolbar. request = copy.copy(current.request) request.update(vars=current.request.vars, get_vars=current.request.get_vars, post_vars=current.request.post_vars) return DIV( BUTTON('design', _onclick="document.location='%s'" % admin), BUTTON('request', _onclick="jQuery('#request-%s').slideToggle()" % u), BUTTON('response', _onclick="jQuery('#response-%s').slideToggle()" % u), BUTTON('session', _onclick="jQuery('#session-%s').slideToggle()" % u), BUTTON('db tables', _onclick="jQuery('#db-tables-%s').slideToggle()" % u), BUTTON('db stats', _onclick="jQuery('#db-stats-%s').slideToggle()" % u), DIV(BEAUTIFY(request), backtotop, _class="hidden", _id="request-%s" % u), DIV(BEAUTIFY(current.session), backtotop, _class="hidden", _id="session-%s" % u), DIV(BEAUTIFY(current.response), backtotop, _class="hidden", _id="response-%s" % u), DIV(BEAUTIFY(dbtables), backtotop, _class="hidden", _id="db-tables-%s" % u), DIV(BEAUTIFY( dbstats), backtotop, _class="hidden", _id="db-stats-%s" % u), SCRIPT("jQuery('.hidden').hide()"), _id="totop-%s" % u )
def test_PRE(self): self.assertEqual( PRE('<>', _a='1', _b='2').xml(), b'<pre a="1" b="2"><></pre>')
if form.vars.update_check and form.vars.update_fields: db(query).update(**eval_in_global_env('dict(%s)' % form.vars.update_fields)) response.flash = T('%s rows updated', nrows) elif form.vars.delete_check: db(query).delete() response.flash = T('%s rows deleted', nrows) nrows = db(query).count() if orderby: rows = db(query).select(limitby=(start, stop), orderby=eval_in_global_env(orderby)) else: rows = db(query).select(limitby=(start, stop)) except Exception, e: (rows, nrows) = ([], 0) response.flash = DIV(T('Invalid Query'), PRE(str(e))) return dict( form=form, table=table, start=start, stop=stop, nrows=nrows, rows=rows, query=request.vars.query, ) # ########################################################## # ## edit delete one record # ###########################################################
def select(): import re db = get_database(request) dbname = request.args[0] regex = re.compile('(?P<table>\w+)\.(?P<field>\w+)=(?P<value>\d+)') if len(request.args) > 1 and hasattr(db[request.args[1]], '_primarykey'): regex = re.compile('(?P<table>\w+)\.(?P<field>\w+)=(?P<value>.+)') if request.vars.query: match = regex.match(request.vars.query) if match: request.vars.query = '%s.%s.%s==%s' % ( request.args[0], match.group('table'), match.group('field'), match.group('value')) else: request.vars.query = session.last_query query = get_query(request) if request.vars.start: start = int(request.vars.start) else: start = 0 nrows = 0 stop = start + 100 table = None rows = [] orderby = request.vars.orderby if orderby: orderby = dbname + '.' + orderby if orderby == session.last_orderby: if orderby[0] == '~': orderby = orderby[1:] else: orderby = '~' + orderby session.last_orderby = orderby session.last_query = request.vars.query form = FORM(TABLE( TR( T('Query:'), '', INPUT(_style='width:400px', _name='query', _value=request.vars.query or '', requires=IS_NOT_EMPTY(error_message=T("Cannot be empty")))), TR( T('Update:'), INPUT(_name='update_check', _type='checkbox', value=False), INPUT(_style='width:400px', _name='update_fields', _value=request.vars.update_fields or '')), TR( T('Delete:'), INPUT(_name='delete_check', _class='delete', _type='checkbox', value=False), ''), TR('', '', INPUT(_type='submit', _value='submit'))), _action=URL(r=request, args=request.args)) if request.vars.csvfile != None: try: import_csv(db[request.vars.table], request.vars.csvfile.file) response.flash = T('data uploaded') except Exception, e: response.flash = DIV(T('unable to parse csv file'), PRE(str(e)))
def hladapter(txt, clsid='', hlargs=dict(lexer='python', linenos=False)): """ Pygments highlight() adapter for Markmin. This function let you fully customize the syntax highlighting of a verbatim block (i.e. something like ``...``:class[id] which is generally used to display source code) at markup level. Pass this callable using the extra argument of a markmin2html call to enable processing of a custom class, for example: html = markmin2html('<markup text>', extra=dict(code=hladapter)) will enable syntax highlighting on ``...``:code markup through a call to Pygments highlight() function, to be precise to highlight('...', get_lexer_by_name('python'), HtmlFormatter(linenos=False)) see http://pygments.org/docs/index.html for Pygments documentation. Thanks to this function you can now choose the lexer to use, pass arguments to the lexer and to the HtmlFormatter instances with the following markup syntax: ``...``:code[lexer='alias', lexer_opt1=val1, lexer_opt2=val2, opt3=val3, ...] Options to be passed to the lexer have to be prefixed by 'lexer_' (the prefix is stripped off from the option name before use), all remaining arguments are used as options for the formatter, the example above will result in the following call highlight('...', get_lexer_by_name('alias', opt1=val1, opt2=val2), HtmlFormatter(linenos=False, opt3=val3)) Default arguments passed to highlight (i.e. when no [...] syntax is used in markup) are those found in hlargs argument of this function. You can override any of the default arguments, other arguments are simply added to defaults. See http://pygments.org/docs/lexers/index.html for an up-to-date list of available lexers and their aliases (short names) and options. See http://pygments.org/docs/formatters/index.html#HtmlFormatter for available formatter options and their meaning. For example, to have line counting starting at 8, lines 2nd and 23th highlighted, and no syntax highlighting on a code block, use: ...``:code[lexer=None, linenos=True, linenostart=8, hl_lines=(2, 23)] Notice that due to a limit of the regex_code in markmin2html.py currently you need to use a tuple instead of a list for hl_lines, moreover Pygments always counts the lines to highlight starting at 1, no matter if a linenostart != 1 is present. """ try: cidd = literal_eval(make_dict(clsid)) if clsid else {} kwargs = hlargs.copy() kwargs.update(cidd) # Pygments NULL lexer is TextLexer (text) lexer_alias = kwargs.pop('lexer', 'text') or 'text' lexer_opts = {} formatter_opts = {} for k in kwargs: if k.startswith('lexer_'): lexer_opts[k[6:]] = kwargs[k] elif k == 'hl_lines' and not isinstance(kwargs[k], tuple): # alllow single value (not a tuple) formatter_opts[k] = (kwargs[k], ) else: formatter_opts[k] = kwargs[k] lexer = get_lexer_by_name(lexer_alias, **lexer_opts) # NOTE: enabling linenos on HtmlFormatter lead to # <div class="highlight_wrapper"> # <table class="highlighttable"> # <tr> # <td class="linenos"><div class="linenodiv"><pre> # ... # </pre></div></td> # <td class="code"><div class="highlight"><pre> # ... # </pre></div></td> # </tr></table></div> # # otherwise (disabled linenos) # <div class="highlight_wrapper"> # <div class="highlight"><pre> # ... # </pre></div> formatter = HtmlFormatter(**formatter_opts) return ''.join(('<div class="highlight_wrapper">', to_native(highlight(txt, lexer, formatter)), '</div>')) except: return PRE(format_exception_only(*(sys.exc_info()[:2]))).xml()