<s> import os <EOL> import socket <EOL> import datetime <EOL> import copy <EOL> import gluon . contenttype <EOL> import gluon . fileutils <EOL> response . subtitle = '<STR_LIT>' <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>" ) : <EOL> raise HTTP ( <NUM_LIT:200> , T ( '<STR_LIT>' ) ) <EOL> if ( 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> ignore_rw = True <EOL> response . view = '<STR_LIT>' <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> 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> 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> stop = start + <NUM_LIT:100> <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 ) . count ( ) <EOL> if form . vars . update_check and form . vars . update_fields : <EOL> db ( query ) . update ( ** eval_in_global_env ( '<STR_LIT>' <EOL> % form . vars . update_fields ) ) <EOL> response . flash = T ( '<STR_LIT>' , nrows ) <EOL> elif form . vars . delete_check : <EOL> db ( query ) . delete ( ) <EOL> response . flash = T ( '<STR_LIT>' , nrows ) <EOL> nrows = db ( query ) . count ( ) <EOL> if orderby : <EOL> rows = db ( query , ignore_common_filters = True ) . select ( limitby = ( <EOL> start , stop ) , orderby = eval_in_global_env ( orderby ) ) <EOL> else : <EOL> rows = db ( query , ignore_common_filters = True ) . select ( <EOL> 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> 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> 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> ] ] , ignore_common_filters = True ) . select ( ) . first ( ) <EOL> else : <EOL> record = db ( db [ table ] . id == request . args ( <EOL> <NUM_LIT:2> ) , ignore_common_filters = True ) . 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> 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> clear_ram = False <EOL> clear_disk = False <EOL> session . flash = "<STR_LIT>" <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> for key , value in cache . ram . storage . items ( ) : <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> 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 ) </s>
