<s> import os <EOL> import socket <EOL> import datetime <EOL> import copy <EOL> import gluon . contenttype <EOL> import gluon . fileutils <EOL> try : <EOL> import pygraphviz as pgv <EOL> except ImportError : <EOL> pgv = None <EOL> is_gae = request . env . web2py_runtime_gae or False <EOL> global_env = copy . copy ( globals ( ) ) <EOL> global_env [ '<STR_LIT>' ] = datetime <EOL> http_host = request . env . http_host . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> remote_addr = request . env . remote_addr <EOL> try : <EOL> hosts = ( http_host , socket . gethostname ( ) , <EOL> socket . gethostbyname ( http_host ) , <EOL> '<STR_LIT>' , '<STR_LIT:127.0.0.1>' , '<STR_LIT>' ) <EOL> except : <EOL> hosts = ( http_host , ) <EOL> if request . env . http_x_forwarded_for or request . is_https : <EOL> session . secure ( ) <EOL> elif ( remote_addr not in hosts ) and ( remote_addr != "<STR_LIT:127.0.0.1>" ) and ( request . function != '<STR_LIT>' ) : <EOL> raise HTTP ( <NUM_LIT:200> , T ( '<STR_LIT>' ) ) <EOL> if request . function == '<STR_LIT>' : <EOL> if not '<STR_LIT>' in globals ( ) or not request . args : <EOL> redirect ( URL ( request . controller , '<STR_LIT:index>' ) ) <EOL> manager_action = auth . settings . manager_actions . get ( request . args ( <NUM_LIT:0> ) , None ) <EOL> if manager_action is None and request . args ( <NUM_LIT:0> ) == '<STR_LIT>' : <EOL> manager_action = dict ( role = auth . settings . auth_manager_role , <EOL> heading = T ( '<STR_LIT>' ) , <EOL> tables = [ auth . table_user ( ) , <EOL> auth . table_group ( ) , <EOL> auth . table_permission ( ) ] ) <EOL> manager_role = manager_action . get ( '<STR_LIT>' , None ) if manager_action else None <EOL> auth . requires_membership ( manager_role ) ( lambda : None ) ( ) <EOL> menu = False <EOL> elif ( request . application == '<STR_LIT>' and not session . authorized ) or ( request . application != '<STR_LIT>' and not gluon . fileutils . check_credentials ( request ) ) : <EOL> redirect ( URL ( '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT:index>' , <EOL> vars = dict ( send = URL ( args = request . args , vars = request . vars ) ) ) ) <EOL> else : <EOL> response . subtitle = T ( '<STR_LIT>' ) <EOL> menu = True <EOL> ignore_rw = True <EOL> response . view = '<STR_LIT>' <EOL> if menu : <EOL> response . menu = [ [ T ( '<STR_LIT>' ) , False , URL ( '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT>' , <EOL> args = [ request . application ] ) ] , [ T ( '<STR_LIT>' ) , False , <EOL> URL ( '<STR_LIT:index>' ) ] , [ T ( '<STR_LIT:state>' ) , False , <EOL> URL ( '<STR_LIT:state>' ) ] , [ T ( '<STR_LIT>' ) , False , <EOL> URL ( '<STR_LIT>' ) ] ] <EOL> if False and request . tickets_db : <EOL> from gluon . restricted import TicketStorage <EOL> ts = TicketStorage ( ) <EOL> ts . _get_table ( request . tickets_db , ts . tablename , request . application ) <EOL> def get_databases ( request ) : <EOL> dbs = { } <EOL> for ( key , value ) in global_env . items ( ) : <EOL> cond = False <EOL> try : <EOL> cond = isinstance ( value , GQLDB ) <EOL> except : <EOL> cond = isinstance ( value , SQLDB ) <EOL> if cond : <EOL> dbs [ key ] = value <EOL> return dbs <EOL> databases = get_databases ( None ) <EOL> def eval_in_global_env ( text ) : <EOL> exec ( '<STR_LIT>' % text , { } , global_env ) <EOL> return global_env [ '<STR_LIT>' ] <EOL> def get_database ( request ) : <EOL> if request . args and request . args [ <NUM_LIT:0> ] in databases : <EOL> return eval_in_global_env ( request . args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> session . flash = T ( '<STR_LIT>' ) <EOL> redirect ( URL ( '<STR_LIT:index>' ) ) <EOL> def get_table ( request ) : <EOL> db = get_database ( request ) <EOL> if len ( request . args ) > <NUM_LIT:1> and request . args [ <NUM_LIT:1> ] in db . tables : <EOL> return ( db , request . args [ <NUM_LIT:1> ] ) <EOL> else : <EOL> session . flash = T ( '<STR_LIT>' ) <EOL> redirect ( URL ( '<STR_LIT:index>' ) ) <EOL> def get_query ( request ) : <EOL> try : <EOL> return eval_in_global_env ( request . vars . query ) <EOL> except Exception : <EOL> return None <EOL> def query_by_table_type ( tablename , db , request = request ) : <EOL> keyed = hasattr ( db [ tablename ] , '<STR_LIT>' ) <EOL> if keyed : <EOL> firstkey = db [ tablename ] [ db [ tablename ] . _primarykey [ <NUM_LIT:0> ] ] <EOL> cond = '<STR_LIT>' <EOL> if firstkey . type in [ '<STR_LIT:string>' , '<STR_LIT:text>' ] : <EOL> cond = '<STR_LIT>' <EOL> qry = '<STR_LIT>' % ( <EOL> request . args [ <NUM_LIT:0> ] , request . args [ <NUM_LIT:1> ] , firstkey . name , cond ) <EOL> else : <EOL> qry = '<STR_LIT>' % tuple ( request . args [ : <NUM_LIT:2> ] ) <EOL> return qry <EOL> def index ( ) : <EOL> return dict ( databases = databases ) <EOL> def insert ( ) : <EOL> ( db , table ) = get_table ( request ) <EOL> form = SQLFORM ( db [ table ] , ignore_rw = ignore_rw ) <EOL> if form . accepts ( request . vars , session ) : <EOL> response . flash = T ( '<STR_LIT>' ) <EOL> return dict ( form = form , table = db [ table ] ) <EOL> def download ( ) : <EOL> import os <EOL> db = get_database ( request ) <EOL> return response . download ( request , db ) <EOL> def csv ( ) : <EOL> import gluon . contenttype <EOL> response . headers [ '<STR_LIT:Content-Type>' ] = gluon . contenttype . contenttype ( '<STR_LIT>' ) <EOL> db = get_database ( request ) <EOL> query = get_query ( request ) <EOL> if not query : <EOL> return None <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' % tuple ( request . vars . query . split ( '<STR_LIT:.>' ) [ : <NUM_LIT:2> ] ) <EOL> return str ( db ( query , ignore_common_filters = True ) . select ( ) ) <EOL> def import_csv ( table , file ) : <EOL> table . import_from_csv_file ( file ) <EOL> def select ( ) : <EOL> import re <EOL> db = get_database ( request ) <EOL> dbname = request . args [ <NUM_LIT:0> ] <EOL> try : <EOL> is_imap = db . _uri . startswith ( "<STR_LIT>" ) <EOL> except ( KeyError , AttributeError , TypeError ) : <EOL> is_imap = False <EOL> regex = re . compile ( '<STR_LIT>' ) <EOL> if len ( request . args ) > <NUM_LIT:1> and hasattr ( db [ request . args [ <NUM_LIT:1> ] ] , '<STR_LIT>' ) : <EOL> regex = re . compile ( '<STR_LIT>' ) <EOL> if request . vars . query : <EOL> match = regex . match ( request . vars . query ) <EOL> if match : <EOL> request . vars . query = '<STR_LIT>' % ( request . args [ <NUM_LIT:0> ] , <EOL> match . group ( '<STR_LIT>' ) , match . group ( '<STR_LIT>' ) , <EOL> match . group ( '<STR_LIT:value>' ) ) <EOL> else : <EOL> request . vars . query = session . last_query <EOL> query = get_query ( request ) <EOL> if request . vars . start : <EOL> start = int ( request . vars . start ) <EOL> else : <EOL> start = <NUM_LIT:0> <EOL> nrows = <NUM_LIT:0> <EOL> step = <NUM_LIT:100> <EOL> fields = [ ] <EOL> if is_imap : <EOL> step = <NUM_LIT:3> <EOL> stop = start + step <EOL> table = None <EOL> rows = [ ] <EOL> orderby = request . vars . orderby <EOL> if orderby : <EOL> orderby = dbname + '<STR_LIT:.>' + orderby <EOL> if orderby == session . last_orderby : <EOL> if orderby [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> orderby = orderby [ <NUM_LIT:1> : ] <EOL> else : <EOL> orderby = '<STR_LIT>' + orderby <EOL> session . last_orderby = orderby <EOL> session . last_query = request . vars . query <EOL> form = FORM ( TABLE ( TR ( T ( '<STR_LIT>' ) , '<STR_LIT>' , INPUT ( _style = '<STR_LIT>' , <EOL> _name = '<STR_LIT>' , _value = request . vars . query or '<STR_LIT>' , <EOL> requires = IS_NOT_EMPTY ( <EOL> error_message = T ( "<STR_LIT>" ) ) ) ) , TR ( T ( '<STR_LIT>' ) , <EOL> INPUT ( _name = '<STR_LIT>' , _type = '<STR_LIT>' , <EOL> value = False ) , INPUT ( _style = '<STR_LIT>' , <EOL> _name = '<STR_LIT>' , _value = request . vars . update_fields <EOL> or '<STR_LIT>' ) ) , TR ( T ( '<STR_LIT>' ) , INPUT ( _name = '<STR_LIT>' , <EOL> _class = '<STR_LIT>' , _type = '<STR_LIT>' , value = False ) , '<STR_LIT>' ) , <EOL> TR ( '<STR_LIT>' , '<STR_LIT>' , INPUT ( _type = '<STR_LIT>' , _value = T ( '<STR_LIT>' ) ) ) ) , <EOL> _action = URL ( r = request , args = request . args ) ) <EOL> tb = None <EOL> if form . accepts ( request . vars , formname = None ) : <EOL> regex = re . compile ( request . args [ <NUM_LIT:0> ] + '<STR_LIT>' ) <EOL> match = regex . match ( form . vars . query . strip ( ) ) <EOL> if match : <EOL> table = match . group ( '<STR_LIT>' ) <EOL> try : <EOL> nrows = db ( query , ignore_common_filters = True ) . count ( ) <EOL> if form . vars . update_check and form . vars . update_fields : <EOL> db ( query , ignore_common_filters = True ) . update ( <EOL> ** eval_in_global_env ( '<STR_LIT>' % form . vars . update_fields ) ) <EOL> response . flash = T ( '<STR_LIT>' , nrows ) <EOL> elif form . vars . delete_check : <EOL> db ( query , ignore_common_filters = True ) . delete ( ) <EOL> response . flash = T ( '<STR_LIT>' , nrows ) <EOL> nrows = db ( query , ignore_common_filters = True ) . count ( ) <EOL> if is_imap : <EOL> fields = [ db [ table ] [ name ] for name in <EOL> ( "<STR_LIT:id>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:to>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> if orderby : <EOL> rows = db ( query , ignore_common_filters = True ) . select ( <EOL> * fields , limitby = ( start , stop ) , <EOL> orderby = eval_in_global_env ( orderby ) ) <EOL> else : <EOL> rows = db ( query , ignore_common_filters = True ) . select ( <EOL> * fields , limitby = ( start , stop ) ) <EOL> except Exception , e : <EOL> import traceback <EOL> tb = traceback . format_exc ( ) <EOL> ( rows , nrows ) = ( [ ] , <NUM_LIT:0> ) <EOL> response . flash = DIV ( T ( '<STR_LIT>' ) , PRE ( str ( e ) ) ) <EOL> csv_table = table or request . vars . table <EOL> if csv_table : <EOL> formcsv = FORM ( str ( T ( '<STR_LIT>' ) ) + "<STR_LIT:U+0020>" , <EOL> INPUT ( _type = '<STR_LIT:file>' , _name = '<STR_LIT>' ) , <EOL> INPUT ( _type = '<STR_LIT>' , _value = csv_table , _name = '<STR_LIT>' ) , <EOL> INPUT ( _type = '<STR_LIT>' , _value = T ( '<STR_LIT>' ) ) ) <EOL> else : <EOL> formcsv = None <EOL> if formcsv and formcsv . process ( ) . accepted : <EOL> try : <EOL> import_csv ( db [ request . vars . table ] , <EOL> request . vars . csvfile . file ) <EOL> response . flash = T ( '<STR_LIT>' ) <EOL> except Exception , e : <EOL> response . flash = DIV ( T ( '<STR_LIT>' ) , PRE ( str ( e ) ) ) <EOL> return dict ( <EOL> form = form , <EOL> table = table , <EOL> start = start , <EOL> stop = stop , <EOL> step = step , <EOL> nrows = nrows , <EOL> rows = rows , <EOL> query = request . vars . query , <EOL> formcsv = formcsv , <EOL> tb = tb <EOL> ) <EOL> def update ( ) : <EOL> ( db , table ) = get_table ( request ) <EOL> keyed = hasattr ( db [ table ] , '<STR_LIT>' ) <EOL> record = None <EOL> db [ table ] . _common_filter = None <EOL> if keyed : <EOL> key = [ f for f in request . vars if f in db [ table ] . _primarykey ] <EOL> if key : <EOL> record = db ( db [ table ] [ key [ <NUM_LIT:0> ] ] == request . vars [ key [ <EOL> <NUM_LIT:0> ] ] ) . select ( ) . first ( ) <EOL> else : <EOL> record = db ( db [ table ] . id == request . args ( <EOL> <NUM_LIT:2> ) ) . select ( ) . first ( ) <EOL> if not record : <EOL> qry = query_by_table_type ( table , db ) <EOL> session . flash = T ( '<STR_LIT>' ) <EOL> redirect ( URL ( '<STR_LIT>' , args = request . args [ : <NUM_LIT:1> ] , <EOL> vars = dict ( query = qry ) ) ) <EOL> if keyed : <EOL> for k in db [ table ] . _primarykey : <EOL> db [ table ] [ k ] . writable = False <EOL> form = SQLFORM ( <EOL> db [ table ] , record , deletable = True , delete_label = T ( '<STR_LIT>' ) , <EOL> ignore_rw = ignore_rw and not keyed , <EOL> linkto = URL ( '<STR_LIT>' , <EOL> args = request . args [ : <NUM_LIT:1> ] ) , upload = URL ( r = request , <EOL> f = '<STR_LIT>' , args = request . args [ : <NUM_LIT:1> ] ) ) <EOL> if form . accepts ( request . vars , session ) : <EOL> session . flash = T ( '<STR_LIT>' ) <EOL> qry = query_by_table_type ( table , db ) <EOL> redirect ( URL ( '<STR_LIT>' , args = request . args [ : <NUM_LIT:1> ] , <EOL> vars = dict ( query = qry ) ) ) <EOL> return dict ( form = form , table = db [ table ] ) <EOL> def state ( ) : <EOL> return dict ( ) <EOL> def ccache ( ) : <EOL> if is_gae : <EOL> form = FORM ( <EOL> P ( TAG . BUTTON ( T ( "<STR_LIT>" ) , _type = "<STR_LIT>" , _name = "<STR_LIT:yes>" , _value = "<STR_LIT:yes>" ) ) ) <EOL> else : <EOL> cache . ram . initialize ( ) <EOL> cache . disk . initialize ( ) <EOL> form = FORM ( <EOL> P ( TAG . BUTTON ( <EOL> T ( "<STR_LIT>" ) , _type = "<STR_LIT>" , _name = "<STR_LIT:yes>" , _value = "<STR_LIT:yes>" ) ) , <EOL> P ( TAG . BUTTON ( <EOL> T ( "<STR_LIT>" ) , _type = "<STR_LIT>" , _name = "<STR_LIT>" , _value = "<STR_LIT>" ) ) , <EOL> P ( TAG . BUTTON ( <EOL> T ( "<STR_LIT>" ) , _type = "<STR_LIT>" , _name = "<STR_LIT>" , _value = "<STR_LIT>" ) ) , <EOL> ) <EOL> if form . accepts ( request . vars , session ) : <EOL> session . flash = "<STR_LIT>" <EOL> if is_gae : <EOL> if request . vars . yes : <EOL> cache . ram . clear ( ) <EOL> session . flash += T ( "<STR_LIT>" ) <EOL> else : <EOL> clear_ram = False <EOL> clear_disk = False <EOL> if request . vars . yes : <EOL> clear_ram = clear_disk = True <EOL> if request . vars . ram : <EOL> clear_ram = True <EOL> if request . vars . disk : <EOL> clear_disk = True <EOL> if clear_ram : <EOL> cache . ram . clear ( ) <EOL> session . flash += T ( "<STR_LIT>" ) <EOL> if clear_disk : <EOL> cache . disk . clear ( ) <EOL> session . flash += T ( "<STR_LIT>" ) <EOL> redirect ( URL ( r = request ) ) <EOL> try : <EOL> from guppy import hpy <EOL> hp = hpy ( ) <EOL> except ImportError : <EOL> hp = False <EOL> import shelve <EOL> import os <EOL> import copy <EOL> import time <EOL> import math <EOL> from gluon import portalocker <EOL> ram = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : time . time ( ) , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> disk = copy . copy ( ram ) <EOL> total = copy . copy ( ram ) <EOL> disk [ '<STR_LIT>' ] = [ ] <EOL> total [ '<STR_LIT>' ] = [ ] <EOL> def GetInHMS ( seconds ) : <EOL> hours = math . floor ( seconds / <NUM_LIT> ) <EOL> seconds -= hours * <NUM_LIT> <EOL> minutes = math . floor ( seconds / <NUM_LIT> ) <EOL> seconds -= minutes * <NUM_LIT> <EOL> seconds = math . floor ( seconds ) <EOL> return ( hours , minutes , seconds ) <EOL> if is_gae : <EOL> gae_stats = cache . ram . client . get_stats ( ) <EOL> try : <EOL> gae_stats [ '<STR_LIT>' ] = ( ( gae_stats [ '<STR_LIT>' ] * <NUM_LIT:100> ) / <EOL> ( gae_stats [ '<STR_LIT>' ] + gae_stats [ '<STR_LIT>' ] ) ) <EOL> except ZeroDivisionError : <EOL> gae_stats [ '<STR_LIT>' ] = T ( "<STR_LIT:?>" ) <EOL> gae_stats [ '<STR_LIT>' ] = GetInHMS ( time . time ( ) - gae_stats [ '<STR_LIT>' ] ) <EOL> total . update ( gae_stats ) <EOL> else : <EOL> for key , value in cache . ram . storage . iteritems ( ) : <EOL> if isinstance ( value , dict ) : <EOL> ram [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] - value [ '<STR_LIT>' ] <EOL> ram [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] <EOL> try : <EOL> ram [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] * <NUM_LIT:100> / value [ '<STR_LIT>' ] <EOL> except ( KeyError , ZeroDivisionError ) : <EOL> ram [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> else : <EOL> if hp : <EOL> ram [ '<STR_LIT>' ] += hp . iso ( value [ <NUM_LIT:1> ] ) . size <EOL> ram [ '<STR_LIT>' ] += hp . iso ( value [ <NUM_LIT:1> ] ) . count <EOL> ram [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if value [ <NUM_LIT:0> ] < ram [ '<STR_LIT>' ] : <EOL> ram [ '<STR_LIT>' ] = value [ <NUM_LIT:0> ] <EOL> ram [ '<STR_LIT>' ] . append ( ( key , GetInHMS ( time . time ( ) - value [ <NUM_LIT:0> ] ) ) ) <EOL> folder = os . path . join ( request . folder , '<STR_LIT>' ) <EOL> if not os . path . exists ( folder ) : <EOL> os . mkdir ( folder ) <EOL> locker = open ( os . path . join ( folder , '<STR_LIT>' ) , '<STR_LIT:a>' ) <EOL> portalocker . lock ( locker , portalocker . LOCK_EX ) <EOL> disk_storage = shelve . open ( <EOL> os . path . join ( folder , '<STR_LIT>' ) ) <EOL> try : <EOL> for key , value in disk_storage . items ( ) : <EOL> if isinstance ( value , dict ) : <EOL> disk [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] - value [ '<STR_LIT>' ] <EOL> disk [ '<STR_LIT>' ] = value [ '<STR_LIT>' ] <EOL> try : <EOL> disk [ '<STR_LIT>' ] = disk [ '<STR_LIT>' ] * <NUM_LIT:100> / value [ '<STR_LIT>' ] <EOL> except ( KeyError , ZeroDivisionError ) : <EOL> disk [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> else : <EOL> if hp : <EOL> disk [ '<STR_LIT>' ] += hp . iso ( value [ <NUM_LIT:1> ] ) . size <EOL> disk [ '<STR_LIT>' ] += hp . iso ( value [ <NUM_LIT:1> ] ) . count <EOL> disk [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if value [ <NUM_LIT:0> ] < disk [ '<STR_LIT>' ] : <EOL> disk [ '<STR_LIT>' ] = value [ <NUM_LIT:0> ] <EOL> disk [ '<STR_LIT>' ] . append ( ( key , GetInHMS ( time . time ( ) - value [ <NUM_LIT:0> ] ) ) ) <EOL> finally : <EOL> portalocker . unlock ( locker ) <EOL> locker . close ( ) <EOL> disk_storage . close ( ) <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] + disk [ '<STR_LIT>' ] <EOL> try : <EOL> total [ '<STR_LIT>' ] = total [ '<STR_LIT>' ] * <NUM_LIT:100> / ( total [ '<STR_LIT>' ] + <EOL> total [ '<STR_LIT>' ] ) <EOL> except ( KeyError , ZeroDivisionError ) : <EOL> total [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> if disk [ '<STR_LIT>' ] < ram [ '<STR_LIT>' ] : <EOL> total [ '<STR_LIT>' ] = disk [ '<STR_LIT>' ] <EOL> else : <EOL> total [ '<STR_LIT>' ] = ram [ '<STR_LIT>' ] <EOL> ram [ '<STR_LIT>' ] = GetInHMS ( time . time ( ) - ram [ '<STR_LIT>' ] ) <EOL> disk [ '<STR_LIT>' ] = GetInHMS ( time . time ( ) - disk [ '<STR_LIT>' ] ) <EOL> total [ '<STR_LIT>' ] = GetInHMS ( time . time ( ) - total [ '<STR_LIT>' ] ) <EOL> def key_table ( keys ) : <EOL> return TABLE ( <EOL> TR ( TD ( B ( T ( '<STR_LIT>' ) ) ) , TD ( B ( T ( '<STR_LIT>' ) ) ) ) , <EOL> * [ TR ( TD ( k [ <NUM_LIT:0> ] ) , TD ( '<STR_LIT>' % k [ <NUM_LIT:1> ] ) ) for k in keys ] , <EOL> ** dict ( _class = '<STR_LIT>' , <EOL> _style = "<STR_LIT>" ) ) <EOL> if not is_gae : <EOL> ram [ '<STR_LIT>' ] = key_table ( ram [ '<STR_LIT>' ] ) <EOL> disk [ '<STR_LIT>' ] = key_table ( disk [ '<STR_LIT>' ] ) <EOL> total [ '<STR_LIT>' ] = key_table ( total [ '<STR_LIT>' ] ) <EOL> return dict ( form = form , total = total , <EOL> ram = ram , disk = disk , object_stats = hp != False ) <EOL> def table_template ( table ) : <EOL> from gluon . html import TR , TD , TABLE , TAG <EOL> def FONT ( * args , ** kwargs ) : <EOL> return TAG . font ( * args , ** kwargs ) <EOL> def types ( field ) : <EOL> f_type = field . type <EOL> if not isinstance ( f_type , str ) : <EOL> return '<STR_LIT:U+0020>' <EOL> elif f_type == '<STR_LIT:string>' : <EOL> return field . length <EOL> elif f_type == '<STR_LIT:id>' : <EOL> return B ( '<STR_LIT>' ) <EOL> elif f_type . startswith ( '<STR_LIT>' ) or f_type . startswith ( '<STR_LIT>' ) : <EOL> return B ( '<STR_LIT>' ) <EOL> else : <EOL> return '<STR_LIT:U+0020>' <EOL> rows = [ ] <EOL> cellpadding = <NUM_LIT:4> <EOL> color = "<STR_LIT>" <EOL> bgcolor = "<STR_LIT>" <EOL> face = "<STR_LIT>" <EOL> face_bold = "<STR_LIT>" <EOL> border = <NUM_LIT:0> <EOL> rows . append ( TR ( TD ( FONT ( table , _face = face_bold , _color = bgcolor ) , <EOL> _colspan = <NUM_LIT:3> , _cellpadding = cellpadding , <EOL> _align = "<STR_LIT>" , _bgcolor = color ) ) ) <EOL> for row in db [ table ] : <EOL> rows . append ( TR ( TD ( FONT ( row . name , _color = color , _face = face_bold ) , <EOL> _align = "<STR_LIT:left>" , _cellpadding = cellpadding , <EOL> _border = border ) , <EOL> TD ( FONT ( row . type , _color = color , _face = face ) , <EOL> _align = "<STR_LIT:left>" , _cellpadding = cellpadding , <EOL> _border = border ) , <EOL> TD ( FONT ( types ( row ) , _color = color , _face = face ) , <EOL> _align = "<STR_LIT>" , _cellpadding = cellpadding , <EOL> _border = border ) ) ) <EOL> return "<STR_LIT>" % TABLE ( * rows , ** dict ( _bgcolor = bgcolor , _border = <NUM_LIT:1> , <EOL> _cellborder = <NUM_LIT:0> , _cellspacing = <NUM_LIT:0> ) <EOL> ) . xml ( ) <EOL> def bg_graph_model ( ) : <EOL> graph = pgv . AGraph ( layout = '<STR_LIT>' , directed = True , strict = False , rankdir = '<STR_LIT>' ) <EOL> subgraphs = dict ( ) <EOL> for tablename in db . tables : <EOL> if hasattr ( db [ tablename ] , '<STR_LIT>' ) : <EOL> meta_graphmodel = db [ tablename ] . _meta_graphmodel <EOL> else : <EOL> meta_graphmodel = dict ( group = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> group = meta_graphmodel [ '<STR_LIT>' ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> if not subgraphs . has_key ( group ) : <EOL> subgraphs [ group ] = dict ( meta = meta_graphmodel , tables = [ ] ) <EOL> subgraphs [ group ] [ '<STR_LIT>' ] . append ( tablename ) <EOL> else : <EOL> subgraphs [ group ] [ '<STR_LIT>' ] . append ( tablename ) <EOL> graph . add_node ( tablename , name = tablename , shape = '<STR_LIT>' , <EOL> label = table_template ( tablename ) ) <EOL> for n , key in enumerate ( subgraphs . iterkeys ( ) ) : <EOL> graph . subgraph ( nbunch = subgraphs [ key ] [ '<STR_LIT>' ] , <EOL> name = '<STR_LIT>' % n , <EOL> style = '<STR_LIT>' , <EOL> color = subgraphs [ key ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> label = subgraphs [ key ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> for tablename in db . tables : <EOL> for field in db [ tablename ] : <EOL> f_type = field . type <EOL> if isinstance ( f_type , str ) and ( <EOL> f_type . startswith ( '<STR_LIT>' ) or <EOL> f_type . startswith ( '<STR_LIT>' ) ) : <EOL> referenced_table = f_type . split ( ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> n1 = graph . get_node ( tablename ) <EOL> n2 = graph . get_node ( referenced_table ) <EOL> graph . add_edge ( n1 , n2 , color = "<STR_LIT>" , label = '<STR_LIT>' ) <EOL> graph . layout ( ) <EOL> if not request . args : <EOL> response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> return graph . draw ( format = '<STR_LIT>' , prog = '<STR_LIT>' ) <EOL> else : <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' % request . args ( <NUM_LIT:0> ) <EOL> if request . args ( <NUM_LIT:0> ) == '<STR_LIT>' : <EOL> return graph . string ( ) <EOL> else : <EOL> return graph . draw ( format = request . args ( <NUM_LIT:0> ) , prog = '<STR_LIT>' ) <EOL> def graph_model ( ) : <EOL> return dict ( databases = databases , pgv = pgv ) <EOL> def manage ( ) : <EOL> tables = manager_action [ '<STR_LIT>' ] <EOL> if isinstance ( tables [ <NUM_LIT:0> ] , str ) : <EOL> db = manager_action . get ( '<STR_LIT>' , auth . db ) <EOL> db = globals ( ) [ db ] if isinstance ( db , str ) else db <EOL> tables = [ db [ table ] for table in tables ] <EOL> if request . args ( <NUM_LIT:0> ) == '<STR_LIT>' : <EOL> auth . table_user ( ) . _plural = T ( '<STR_LIT>' ) <EOL> auth . table_group ( ) . _plural = T ( '<STR_LIT>' ) <EOL> auth . table_membership ( ) . _plural = T ( '<STR_LIT>' ) <EOL> auth . table_permission ( ) . _plural = T ( '<STR_LIT>' ) <EOL> if request . extension != '<STR_LIT>' : <EOL> return dict ( heading = manager_action . get ( '<STR_LIT>' , <EOL> T ( '<STR_LIT>' ) % dict ( action = request . args ( <NUM_LIT:0> ) . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) . title ( ) ) ) , <EOL> tablenames = [ table . _tablename for table in tables ] , <EOL> labels = [ table . _plural . title ( ) for table in tables ] ) <EOL> table = tables [ request . args ( <NUM_LIT:1> , cast = int ) ] <EOL> formname = '<STR_LIT>' % table . _tablename <EOL> linked_tables = orderby = None <EOL> if request . args ( <NUM_LIT:0> ) == '<STR_LIT>' : <EOL> auth . table_group ( ) . _id . readable = auth . table_membership ( ) . _id . readable = auth . table_permission ( ) . _id . readable = False <EOL> auth . table_membership ( ) . user_id . label = T ( '<STR_LIT>' ) <EOL> auth . table_membership ( ) . group_id . label = T ( '<STR_LIT>' ) <EOL> auth . table_permission ( ) . group_id . label = T ( '<STR_LIT>' ) <EOL> auth . table_permission ( ) . name . label = T ( '<STR_LIT>' ) <EOL> if table == auth . table_user ( ) : <EOL> linked_tables = [ auth . settings . table_membership_name ] <EOL> elif table == auth . table_group ( ) : <EOL> orderby = '<STR_LIT>' if not request . args ( <NUM_LIT:3> ) or '<STR_LIT>' not in request . args ( <NUM_LIT:3> ) else None <EOL> elif table == auth . table_permission ( ) : <EOL> orderby = '<STR_LIT>' <EOL> kwargs = dict ( user_signature = True , maxtextlength = <NUM_LIT:1000> , <EOL> orderby = orderby , linked_tables = linked_tables ) <EOL> smartgrid_args = manager_action . get ( '<STR_LIT>' , { } ) <EOL> kwargs . update ( ** smartgrid_args . get ( '<STR_LIT>' , { } ) ) <EOL> kwargs . update ( ** smartgrid_args . get ( table . _tablename , { } ) ) <EOL> grid = SQLFORM . smartgrid ( table , args = request . args [ : <NUM_LIT:2> ] , formname = formname , ** kwargs ) <EOL> return grid </s>
