Changeset 242


Ignore:
Timestamp:
Oct 23, 2009, 11:54:30 AM (10 years ago)
Author:
pvkouteren
Message:

Added documentation and removed duplicate and old code which isn't needed any more.

Files:
11 edited

Legend:

Unmodified
Added
Removed
  • IbidasSOAP.py

    r213 r242  
    55import config
    66from container import postgres
     7
     8"""
     9The IbidasSOAPServer class allows starting the WSGI server and handling SOAP requests.
     10The SOAP methods are defined in the IbidasAtomicServices class by Soaplib.
     11@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     12@version: 1.0
     13"""
    714
    815class IbidasSOAPServer(SimpleWSGISoapApp, IbidasAtomicServices):
  • container/postgres.py

    r221 r242  
    2929   Logging cursor. Extension to the pyscopg2 cursor.
    3030   '''
    31 
    32    def __init__(self, *kargs, **kwargs):
    33      psycopg2.extensions.cursor.__init__(self, *kargs, **kwargs)
    34      logger = logging.getLogger('ibidas_sql')
    35      ''' Use os.getcwd() here because sys.path[0] is not guaranteed to be the right path '''
    36      logdir = os.getcwd() + '/logs'
    37      if (not os.path.isdir(logdir)):
    38          os.mkdir(logdir)
    39      handler = logging.FileHandler(logdir + '/ibidas_sql.log')
    40      formatter = logging.Formatter()
    41      handler.setFormatter(formatter)
    42      logger.addHandler(handler)
    43      logger.setLevel(logging.INFO)
    44      self.logger = logger
    45 
    46    def execute(self, sql, args=None, dontlog=None):
    47        '''Executes the query in sql.
    48 
    49         args: arguments to place in the query
    50         dontlog: exception class which should not be logged
    51        '''
    52        dontlog=1
    53        if(args != None and len(args) > 0):
    54            psycopg2.extensions.cursor.execute(self, sql, args)
    55        else:
    56            psycopg2.extensions.cursor.execute(self, sql)
    57 
    58 class PatrickLogCursor(psycopg2.extensions.cursor):
    59    '''
    60    Logging cursor. Extension to the pyscopg2 cursor.
    61    '''
    6231   
    6332   def __init__(self, *kargs, **kwargs):
     
    6534     self.sqllogger = Logger('ibidas_sql')
    6635     self.sqllogger.setLevel('INFO')
    67      #self.sqllogger = logger.getInstance()
    6836       
    6937   def execute(self, sql, args=None, dontlog=None):
     
    8351           for res in results:
    8452               self.views[res[0].rstrip("_view")] = res[0]
    85        '''
    86        psycopg2.extensions.cursor.execute(self, "SELECT viewname \
    87                               FROM pg_views \
    88                               WHERE schemaname = 'public'")
    89        results= psycopg2.extensions.cursor.fetchall(self)
    90        self.views = {}
    91        for res in results:
    92            self.views[res[0].rstrip("_view")] = res[0]
    93        '''
    9453     
    9554       """
     
    9857       do logging of the used tables here as well (if requested).
    9958       """
    100 
    101        # TEST
    102        #query = self._switch_to_views(sql, dontlog)
    10359       
    10460       import config
     
    10662       
    10763       if (authenticated_mode == 'off' and self._getLoggedInUser() == 'ibidas'):
    108            #print "Not using views"
    10964           query = sql
    11065       else:
    111            #print "Using views"
    11266           query = self._switch_to_views(sql, dontlog)
    113        
    114        '''   
    115        if (not self._getLoggedInUser() == 'ibidas'):
    116            query = self._switch_to_views(sql, dontlog)
    117            #print "Using views =("
    118        else:
    119            query = sql
    120            #print "Not using views =)"
    121         '''
    12267
    12368       t1 = time.time()
     
    13075       t2 = time.time()
    13176       totalt = t2 - t1
    132        #if (dontlog is None):
    13377       
    13478       query_logging = config.getValue('ibidas.ini', { "ibidas.query_logging": 'on'}, "ibidas.query_logging")
     
    13882   def _switch_to_views(self, query, dontlog=None):
    13983       for table,view in self.views.iteritems():
    140            #query = query.replace(table, view)
    14184           pattern = re.compile(r"[\s,]" + table + "[\s\t,.;]")
    14285           query = re.sub(pattern, " " + view + " ", query)
     
    229172   
    230173    def __init__(self, args):       
    231        #il = IbidasLogger()
    232        #setattr(self, 'logger', il.getInstance())
    233174       if not(hasattr(self, 'logger')):
    234175           setattr(self, 'logger', IbidasLogger.getIbidasLogger())
     
    239180       """Returns a logging database cursor"""
    240181       return psycopg2.extensions.connection.cursor(self, \
    241                                            cursor_factory=PatrickLogCursor)
     182                                           cursor_factory=LogCursor)
    242183 
    243184    #def close(self):
  • log/IbidasLogger.py

    r221 r242  
    88    return logger
    99
     10"""
     11A child logger class defining a core logger for Ibidas.
     12@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     13@version: 1.0
     14"""
     15
    1016class IbidasLogger(Logger):
    1117
  • log/Logger.py

    r199 r242  
    22import logging
    33import os
     4
     5"""
     6A base logger class defining a uniform way of logging for usage in Ibidas.
     7@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     8@version: 1.0
     9"""
    410
    511class Logger():
     
    2733   
    2834    def log(self, message, level=None):
    29        level = None
    30        self.logger.log(50, "log " + message)
    3135       # if level is None, we take self.logginglevel to be the level
    3236       if level is None:
     
    3438       self.logger.log(level, message)
    3539
     40    """ Default log methods """
    3641    def debug(self, message):
    3742        self.log(message, logging._levelNames['DEBUG'])
    3843       
    3944    def info(self, message):
    40         #self.log(message, logging._levelNames['INFO'])
    41         self.logger.log(logging._levelNames['INFO'], message)
     45        self.log(message, logging._levelNames['INFO'])
     46        #self.logger.log(logging._levelNames['INFO'], message)
    4247
    4348    def warning(self, message):
  • modules/calc/arrays.py

    r234 r242  
    1 """ Array operations """
     1"""
     2Some array operations more or less named like commonly used PHP functions
     3@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     4@version: 1.0
     5"""
    26
    37""" Array difference """
     
    2933"""
    3034def array_remove_duplicates(array):
    31     return set(array)
     35    return list(set(array))
  • modules/statistics/Hypergeometric.py

    r234 r242  
    33import modules.calc.arrays
    44
     5"""
     6The Hypergeometric test implementation.
     7@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     8@version: 1.0
     9"""
     10
    511class Hypergeometric():
    612   
    7     def __init__(self, top, background, reference):
    8         self.debug = False
     13    def __init__(self, top, background, reference, debug=False):
     14        self.debug = debug
    915        a = modules.calc.arrays.array_overlap(top, background)
    1016        b = modules.calc.arrays.array_diff(background, top)
     
    2026        self.n = self.len_a + self.len_c
    2127   
     28    """ Do the calculation and possibly output the calculation server-sided """
    2229    def calculate(self):           
    2330        result = scipy.stats.hypergeom.pmf(self.k,self.t,self.m,self.n)
    2431        if self.debug == True:
    2532            self._prettyPrintHypergeometricTable()
    26             print "Hypergeometric result: " + str(result)
     33            print "Hypergeometric test result: " + str(result)
    2734        return result
    2835   
     36    """ Make a pretty table of the calculation """
    2937    def _prettyPrintHypergeometricTable(self):
    3038        column1width = max(len(str(self.k)), len(str(self.len_c)), len(str(self.n)))
     
    3846        print "| " + (" " * (column1width - len(str(self.n)))) + str(self.n) + " | " + (" " * (column2width - len(str((self.len_b + self.len_d))))) + str((self.len_b + self.len_d)) + " | " + (" " * (column3width - len(str(self.t)))) + str(self.t)
    3947   
    40     def _setDebug(self, value):
     48    def setDebug(self, value):
    4149        if value == True:
    4250            this.debug = True
  • setup_db.py

    r229 r242  
    2121        self.nosoap = nosoap
    2222   
     23    """ Return the arguments which this script accepts """
    2324    def getPossibleVars(self):
    2425        return {'debug': "For debug mode. It installs a unittest database and removes it.",
     
    2627                'nosoap': "Will prevent the system from regenerating IbidasSerializers."
    2728        }
    28        
     29     
     30    """ Print the values of the arguments given """   
    2931    def vars(self):
    3032        print "debug: " + str(self.debug)
     
    3234        print "nosoap: " + str(self.nosoap)
    3335   
     36    """ Start the installation and output """
    3437    def start(self):
    3538        self._output("=== Database setup script overview ===", False)
     
    3841        self._startSetup()
    3942   
     43    """ Print the variables to output """
    4044    def _printVars(self):
    4145        self._output("Initial setup variables:")
     
    5660                append = ""
    5761            self._output("  * Generate IbidasSerializers: YES" + append)
    58                
    59        
     62                   
    6063    def _output(self, string, showtime=True):
    6164        try:
     
    6770        else:
    6871            print string   
    69        
     72     
     73    """ Check the database connection and load the configuration """   
    7074    def _check_and_load(self):
    7175        self._loadConfig()
     
    7781            self._output("=== End of database setup script ===", False)
    7882            sys.exit()
    79        
     83   
     84    """ Load the configuration """   
    8085    def _loadConfig(self):
    8186        _ConfigDefault = {
     
    99104            sys.exit()
    100105   
     106    """ Test the connection to the database """
    101107    def _testConnection(self):
    102108        try:
     
    111117            return False
    112118
     119    """ Clear the database, user and usergroups (if existing) and create the database again """
    113120    def _startSetup(self):
    114121        self._openConnection("template1")
     
    128135        self._openConnection(self.setupconfig['setup.db_name'])
    129136        self._fillDatabase()
    130        
     137     
     138    """ Create the tables, users and usergroups """   
    131139    def _fillDatabase(self):
    132140        self._output("Creating tables..")
     
    159167        '''
    160168   
     169    """ Create the serializers by calling the setup_serializers.py script """
    161170    def _createSerializers(self):
    162171        try:
     
    172181            sys.exit()
    173182   
     183    """ Execute the SQL defined in the preimport.sql file """
    174184    def _executeIbidasSQL(self):
    175185        filename = "sql/preimport.sql" # The file which contains the sql to create the db schema
     
    185195        file.close()       
    186196       
     197    """ Attempt to open a connection to the database """
    187198    def _openConnection(self, dbname='template1'):
    188199        try:
     
    195206            sys.exit()
    196207   
     208    """ Attempt to drop the database """
    197209    def _dropDatabase(self):
    198210        try:
     
    207219            sys.exit()
    208220   
     221    """ Attempt to drop the user 'ibidas' """
    209222    def _dropUser(self):
    210223        try:
     
    215228            pass
    216229       
     230    """ Attempt to create the user 'ibidas' """
    217231    def _createUser(self):
    218232        ibidas_user_sql = "CREATE USER ibidas WITH PASSWORD 'ibidas' NOCREATEDB CREATEUSER CREATEROLE;"
     
    223237            self._output("Oops: Unable to create superuser ibidas. Please run this script again!")
    224238            sys.exit()
    225        
     239   
     240    """ Attempt to drop the usergroup 'ibidas_admins' """   
    226241    def _dropUserGroup(self):
    227242        try:
     
    231246            ''' The database did not exist '''
    232247            pass
    233        
     248     
     249    """ Attempt to create the usergroup 'ibidas_admins' """   
    234250    def _createUserGroup(self):
    235251        ibidas_usergroup_sql = "CREATE GROUP ibidas_admins WITH USER ibidas;"
     
    241257            sys.exit()
    242258       
     259    """ Attempt to create the database """
    243260    def _createDatabase(self):
    244261        create_sql = "CREATE DATABASE " + self.setupconfig['setup.db_name'] + " WITH ENCODING 'UTF8';"
     
    250267            sys.exit()
    251268   
     269    """ Apply the user restrictions. All your rights are belong to postgres ;-)"""
    252270    def _applyRestrictions(self):
    253271        ''' Apply user restrictions on the tables '''
     
    271289            sys.exit()
    272290       
     291    """ Define and apply the views according to the tables present """
    273292    def _defineViews(self):
    274293        ''' Create views and apply restrictions on them '''
     
    332351        self._output("Created " + str(len(existing) + 1) + " views..")
    333352
     353    """ Close the database connection """
    334354    def _closeConnection(self):
    335355        ''' Close connections '''
     
    338358        self.cd.close()   
    339359   
     360    """ Write the initial configuration """
    340361    def _writeInitConfig(self):
    341362        ''' Create the config file based on the authenticated_mode variable '''
  • web/IbidasAtomicServicesTemp.py

    r241 r242  
    22from soaplib.service import soapmethod
    33from soaplib.serializers.primitive import *
    4 #from model.ibidasitem import IbidasItem
    54from model.IbidasSerializers import *
    65from IbidasMessage import IbidasMessage
     
    1110from modules.statistics.Hypergeometric import Hypergeometric
    1211
     12"""
     13The IbidasAtomicServices class provides a number of services which can be
     14called from either SOAP or XMLRPC without the need for any session
     15information.
     16@author: Jan Bot <J.J.Bot@tudelft.nl>
     17@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     18@version: 1.0
     19"""
     20
    1321class IbidasAtomicServices:
    14     """
    15     The IbidasAtomicServices class provides a number of services which can be
    16     called from either SOAP or XMLRPC without the need for any session
    17     information.
    18     """
     22
    1923    def __init__(self):
    2024        _ConfigDefault = {
     
    8185            tfp.parse(toCont = True)
    8286            self.transfac['factor'] = tfp
    83            
     87            # load genes
    8488            tgp = TransfacGeneParser(transfac_dir + 'gene.dat')
    8589            tgp.parse(toCont = True)
    8690            self.transfac['gene'] = tgp
    87            
     91            # load binding sites
    8892            tsp = TransfacSiteParser(transfac_dir + 'site.dat')
    8993            tsp.parse(toCont = True)
     
    9296   
    9397    """
    94     Test
     98    Test the connection. Calling foo results in returning bar after five seconds.
    9599    """
    96100
  • web/IbidasMessage.py

    r222 r242  
    66This file defines the IbidasMessage serializer.
    77This is the actual communication class which results in a SOAP message.
    8 @author Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
    9 @version 1.0
     8@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     9@version: 1.0
    1010'''
    1111 
     
    176176                if var is not None:
    177177                        current = getattr(self, var)
    178                         current.append(something)
     178                        current.append(var)
    179179                        setattr(self, var, current)
     180
  • web/model/IbidasSerializers.py

    r222 r242  
    55This file defines all possible IbidasSerializers
    66These are used in the IbidasMessage serializer
    7 @author Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
    8 @version 1.0
     7@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     8@version: 1.0
    99'''
    1010 
     
    1313        def __init__(self, **values):
    1414                for key,value in values.items():
    15                         print "Setting " + str(key) + " to " + str(value)
    1615                        setattr(self, key, value)
    1716
  • web/model/setup_serializers.py

    r222 r242  
    1 """ This class generates IbidasSerializers.py """
     1"""
     2This class generates IbidasSerializers.py
     3@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>
     4@version: 1.0
     5"""
    26from psycopg2 import ProgrammingError, OperationalError
    37import psycopg2
     
    1317        self.excludetables = ['set_pg_group']
    1418   
     19    """ Start generating """
    1520    def generate(self, cursor, tables):
    1621        classtypes = self._getClassTypesMapping(cursor, tables)
     
    2126        self._generateIbidasMessageSerializer(classtypes)
    2227        self._closeFile()
    23        
     28     
     29    """ Open a file. Method is here just to make it look nicer and better understandable. """   
    2430    def _openFile(self):
    2531        self.file = open(self.filepath, "w+")
    26        
     32     
     33    """ Get a mapping of classes to class types """ 
    2734    def _getClassTypesMapping(self, cursor, tables):
    2835        classtypes = {}
     
    3845                pass
    3946        return classtypes
    40        
     47     
     48    """ Generate the classes based on the classtypes mapping """   
    4149    def _generateClasses(self, classtypes):
    4250        self._generateSuperClass()
     
    4452            self._generateClass(classname, classtypes[classname])
    4553        #self._generateIbidasMessageSerializer(classtypes.keys())
    46            
     54   
     55    """ Generate the IbidasSerializer superclass """       
    4756    def _generateSuperClass(self):
    4857        lines = []
     
    5362        lines.append("This file defines all possible IbidasSerializers")
    5463        lines.append("These are used in the IbidasMessage serializer")
    55         lines.append("@author Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>")
    56         lines.append("@version 1.0")
     64        lines.append("@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>")
     65        lines.append("@version: 1.0")
    5766        lines.append("'''")
    5867        lines.append(" ")
     
    6372        lines.append("\t\t\tsetattr(self, key, value)")
    6473        self._writeLines(lines)
    65        
     74     
     75    """ Generate the IbidasMessageSerializer class """   
    6676    def _generateIbidasMessageSerializer(self, classnames):
    6777        self.folder = self.folder[:self.folder.rfind(u"/")]
     
    7787        lines.append("This file defines the IbidasMessage serializer.")
    7888        lines.append("This is the actual communication class which results in a SOAP message.")
    79         lines.append("@author Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>")
    80         lines.append("@version 1.0")
     89        lines.append("@author: Patrick van Kouteren <H.J.W.vanKouteren@student.tudelft.nl>")
     90        lines.append("@version: 1.0")
    8191        lines.append("'''")
    8292        lines.append(" ")
     
    124134        lines.append("\t\t\tsetattr(self, var, current)")
    125135        self._writeLines(lines)
    126        
     136   
     137    """
     138    Join lines and insert empty lines. Method is just here to make it look nicer and better
     139    understandable.
     140    """   
    127141    def _writeLines(self, lines):
    128142        self.file.write(("\n".join(lines))+"\n\n")
    129            
     143   
     144    """ Generate a class serializer """       
    130145    def _generateClass(self, name, items):
    131146        #subnames = name.split("_")
     
    142157        self._writeLines(lines)
    143158   
     159    """ Generate a classname """
    144160    def _generateClassname(self, name):
    145161        subnames = name.split("_")
     
    148164        return "Ibidas" + "".join(subnames)
    149165   
     166    """ Close a file. Just here to make it look nicer and better understandable. """
    150167    def _closeFile(self):
    151168        self.file.close()
    152169   
     170    """ Load a mapping from PostgreSQL to Soaplib types """
    153171    def _loadMapping(self, fromtypes, totypes):
    154172        self.mapping = self._PostgreSQLToSoaplibMapping()
Note: See TracChangeset for help on using the changeset viewer.