about summary refs log tree commit diff
path: root/wqflask/utility
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/utility')
-rw-r--r--[-rwxr-xr-x]wqflask/utility/AJAX_table.py2
-rw-r--r--wqflask/utility/Plot.py15
-rw-r--r--[-rwxr-xr-x]wqflask/utility/TDCell.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/THCell.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/__init__.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/after.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/benchmark.py31
-rw-r--r--[-rwxr-xr-x]wqflask/utility/corr_result_helpers.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/db_tools.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/formatting.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/helper_functions.py0
-rw-r--r--wqflask/utility/logger.py137
-rw-r--r--[-rwxr-xr-x]wqflask/utility/svg.py0
-rw-r--r--[-rwxr-xr-x]wqflask/utility/temp_data.py0
-rw-r--r--wqflask/utility/tools.py86
-rw-r--r--[-rwxr-xr-x]wqflask/utility/webqtlUtil.py0
16 files changed, 234 insertions, 37 deletions
diff --git a/wqflask/utility/AJAX_table.py b/wqflask/utility/AJAX_table.py
index 083d1c0d..d70acfcd 100755..100644
--- a/wqflask/utility/AJAX_table.py
+++ b/wqflask/utility/AJAX_table.py
@@ -68,6 +68,8 @@ class AJAX_table:
                 workbook = xl.Writer('%s.xls' % (webqtlConfig.TMPDIR+file))
                 worksheet = workbook.add_worksheet()
 
+                logger.warning("Creating new MySQLdb cursor (this method is OBSOLETE!)")
+
                 con = MySQLdb.Connect(db=webqtlConfig.DB_NAME,host=webqtlConfig.MYSQL_SERVER, user=webqtlConfig.DB_USER,passwd=webqtlConfig.DB_PASSWD)
                 cursor = con.cursor()
 
diff --git a/wqflask/utility/Plot.py b/wqflask/utility/Plot.py
index 063de387..d60e2bb2 100644
--- a/wqflask/utility/Plot.py
+++ b/wqflask/utility/Plot.py
@@ -29,8 +29,6 @@ from __future__ import print_function
 import piddle as pid
 from pprint import pformat as pf
 
-print("Lysol")
-
 from math import *
 import random
 import sys, os
@@ -38,14 +36,15 @@ from numarray import linear_algebra as la
 from numarray import ones, array, dot, swapaxes
 
 import reaper
-sys.path.append("..")
-print(sys.path)
+# sys.path.append("..") Never in a running webserver
 from basicStatistics import corestats
 
 import svg
 import webqtlUtil
 from base import webqtlConfig
 
+import utility.logger
+logger = utility.logger.getLogger(__name__ )
 
 def cformat(d, rank=0):
     'custom string format'
@@ -297,10 +296,10 @@ def find_outliers(vals):
 
     """
 
-    print("xerxes vals is:", pf(vals))
+    logger.debug("xerxes vals is:", pf(vals))
 
     if vals:
-        #print("vals is:", pf(vals))
+        #logger.debug("vals is:", pf(vals))
         stats = corestats.Stats(vals)
         low_hinge = stats.percentile(25)
         up_hinge = stats.percentile(75)
@@ -313,7 +312,7 @@ def find_outliers(vals):
         upper_bound = None
         lower_bound = None
 
-    print(pf(locals()))
+    logger.debug(pf(locals()))
     return upper_bound, lower_bound
 
 
@@ -436,7 +435,7 @@ def plotBoxPlot(canvas, data, offset= (40, 40, 40, 40), XLabel="Category", YLabe
 
             canvas.drawCross(XCoord, plotHeight + yTopOffset - (catMean-Yll)*plotHeight/(Yur - Yll), \
                     color=pid.blue,size=3)
-            #print (catMean, catMedian, cat25per, cat75per)
+            #print(catMean, catMedian, cat25per, cat75per)
             pass
 
         XCoord += stepX
diff --git a/wqflask/utility/TDCell.py b/wqflask/utility/TDCell.py
index 8de8e050..8de8e050 100755..100644
--- a/wqflask/utility/TDCell.py
+++ b/wqflask/utility/TDCell.py
diff --git a/wqflask/utility/THCell.py b/wqflask/utility/THCell.py
index dde221b5..dde221b5 100755..100644
--- a/wqflask/utility/THCell.py
+++ b/wqflask/utility/THCell.py
diff --git a/wqflask/utility/__init__.py b/wqflask/utility/__init__.py
index d9856eed..d9856eed 100755..100644
--- a/wqflask/utility/__init__.py
+++ b/wqflask/utility/__init__.py
diff --git a/wqflask/utility/after.py b/wqflask/utility/after.py
index a3bb85e9..a3bb85e9 100755..100644
--- a/wqflask/utility/after.py
+++ b/wqflask/utility/after.py
diff --git a/wqflask/utility/benchmark.py b/wqflask/utility/benchmark.py
index d5b32703..8c97370d 100755..100644
--- a/wqflask/utility/benchmark.py
+++ b/wqflask/utility/benchmark.py
@@ -3,34 +3,39 @@ from __future__ import print_function, division, absolute_import
 import collections
 import inspect
 import time
+from utility.tools import LOG_BENCH
 
+from utility.logger import getLogger
+logger = getLogger(__name__ )
 
 class Bench(object):
     entries = collections.OrderedDict()
 
-    def __init__(self, name=None):
+    def __init__(self, name=None, write_output=True):
         self.name = name
+        self.write_output = write_output
 
     def __enter__(self):
-        if self.name:
-            print("Starting benchmark: %s" % (self.name))
-        else:
-            print("Starting benchmark at: %s [%i]" % (inspect.stack()[1][3], inspect.stack()[1][2]))
+        if self.write_output:
+            if self.name:
+                logger.debug("Starting benchmark: %s" % (self.name))
+            else:
+                logger.debug("Starting benchmark at: %s [%i]" % (inspect.stack()[1][3], inspect.stack()[1][2]))
         self.start_time = time.time()
 
     def __exit__(self, type, value, traceback):
-        if self.name:
-            name = self.name
-        else:
-            name = "That"
-
         time_taken = time.time() - self.start_time
-        print("  %s took: %f seconds" % (name, (time_taken)))
+        if self.write_output:
+            if self.name:
+                name = self.name
+            else:
+                name = "That"
+
+            logger.info("  %s took: %f seconds" % (name, (time_taken)))
 
         if self.name:
             Bench.entries[self.name] = Bench.entries.get(self.name, 0) + time_taken
 
-
     @classmethod
     def report(cls):
         total_time = sum((time_taken for time_taken in cls.entries.itervalues()))
@@ -42,4 +47,4 @@ class Bench(object):
 
     def reset(cls):
         """Reset the entries"""
-        cls.entries = collections.OrderedDict()
\ No newline at end of file
+        cls.entries = collections.OrderedDict()
diff --git a/wqflask/utility/corr_result_helpers.py b/wqflask/utility/corr_result_helpers.py
index ef644d85..ef644d85 100755..100644
--- a/wqflask/utility/corr_result_helpers.py
+++ b/wqflask/utility/corr_result_helpers.py
diff --git a/wqflask/utility/db_tools.py b/wqflask/utility/db_tools.py
index 4034f39c..4034f39c 100755..100644
--- a/wqflask/utility/db_tools.py
+++ b/wqflask/utility/db_tools.py
diff --git a/wqflask/utility/formatting.py b/wqflask/utility/formatting.py
index e53dda22..e53dda22 100755..100644
--- a/wqflask/utility/formatting.py
+++ b/wqflask/utility/formatting.py
diff --git a/wqflask/utility/helper_functions.py b/wqflask/utility/helper_functions.py
index 15f60765..15f60765 100755..100644
--- a/wqflask/utility/helper_functions.py
+++ b/wqflask/utility/helper_functions.py
diff --git a/wqflask/utility/logger.py b/wqflask/utility/logger.py
new file mode 100644
index 00000000..86ee1c52
--- /dev/null
+++ b/wqflask/utility/logger.py
@@ -0,0 +1,137 @@
+# GeneNetwork logger
+#
+# The standard python logging module is very good. This logger adds a
+# few facilities on top of that. Main one being that it picks up
+# settings for log levels (global and by module) and (potentially)
+# offers some fine grained log levels for the standard levels.
+#
+# All behaviour is defined here.  Global settings (defined in
+# default_settings.py).
+#
+# To use logging and settings put this at the top of a module:
+#
+#   import utility.logger
+#   logger = utility.logger.getLogger(__name__ )
+#
+# To override global behaviour set the LOG_LEVEL in default_settings.py
+# or use an environment variable, e.g.
+#
+#    env LOG_LEVEL=INFO ./bin/genenetwork2
+#
+# To override log level for a module replace that with, for example,
+#
+#   import logging
+#   import utility.logger
+#   logger = utility.logger.getLogger(__name__,level=logging.DEBUG)
+#
+# We'll add more overrides soon.
+
+import logging
+import string
+from inspect import isfunction
+from pprint import pformat as pf
+from inspect import stack
+
+from utility.tools import LOG_LEVEL, LOG_LEVEL_DEBUG, LOG_SQL, LOG_FORMAT
+
+class GNLogger:
+    """A logger class with some additional functionality, such as
+    multiple parameter logging, SQL logging, timing, colors, and lazy
+    functions.
+
+    """
+
+    def __init__(self,name):
+        self.logger = logging.getLogger(name)
+
+    def setLevel(self,value):
+        """Set the undelying log level"""
+        self.logger.setLevel(value)
+
+    def debug(self,level=0,*args):
+        """Call logging.debug for multiple args. Use level=num to filter on
+LOG_LEVEL_DEBUG.
+
+        """
+        if level <= LOG_LEVEL_DEBUG:
+            self.collect(self.logger.debug,*args)
+
+    def info(self,*args):
+        """Call logging.info for multiple args"""
+        self.collect(self.logger.info,*args)
+
+    def warning(self,*args):
+        """Call logging.warning for multiple args"""
+        self.collect(self.logger.warning,*args)
+        self.logger.warning(self.collect(*args))
+
+    def error(self,*args):
+        """Call logging.error for multiple args"""
+        self.collect(self.logger.error,*args)
+
+    def infof(self,*args):
+        """Call logging.info for multiple args lazily"""
+        # only evaluate function when logging
+        if self.logger.getEffectiveLevel() < 30:
+            self.collectf(self.logger.debug,*args)
+
+    def debugf(self,level=0,*args):
+        """Call logging.debug for multiple args lazily"""
+        # only evaluate function when logging
+        if level <= LOG_LEVEL_DEBUG:
+            if self.logger.getEffectiveLevel() < 20:
+                self.collectf(self.logger.debug,*args)
+
+    def sql(self, sqlcommand, fun = None):
+        """Log SQL command, optionally invoking a timed fun"""
+        if LOG_SQL:
+            caller = stack()[1][3]
+            if caller in ['fetchone','fetch1','fetchall']:
+                caller = stack()[2][3]
+            self.info(caller,sqlcommand)
+        if fun:
+            result = fun(sqlcommand)
+            if LOG_SQL:
+                self.info(result)
+            return result
+
+    def collect(self,fun,*args):
+        """Collect arguments and use fun to output"""
+        out = "."+stack()[2][3]
+        for a in args:
+            if len(out)>1:
+                out += ": "
+            if isinstance(a, str):
+                out = out + a
+            else:
+                out = out + pf(a,width=160)
+        fun(out)
+
+    def collectf(self,fun,*args):
+        """Collect arguments and use fun to output one by one"""
+        out = "."+stack()[2][3]
+        for a in args:
+            if len(out)>1:
+                out += ": "
+                if isfunction(a):
+                    out += a()
+                else:
+                    if isinstance(a, str):
+                        out = out + a
+                    else:
+                        out = out + pf(a,width=160)
+        fun(out)
+
+# Get the module logger. You can override log levels at the
+# module level
+def getLogger(name, level = None):
+    gnlogger = GNLogger(name)
+    logger = gnlogger.logger
+
+    if level:
+        logger.setLevel(level)
+    else:
+        logger.setLevel(LOG_LEVEL)
+
+    logger.info("Log level of "+name+" set to "+logging.getLevelName(logger.getEffectiveLevel()))
+    return gnlogger
diff --git a/wqflask/utility/svg.py b/wqflask/utility/svg.py
index 512bc9e6..512bc9e6 100755..100644
--- a/wqflask/utility/svg.py
+++ b/wqflask/utility/svg.py
diff --git a/wqflask/utility/temp_data.py b/wqflask/utility/temp_data.py
index 5bf700c9..5bf700c9 100755..100644
--- a/wqflask/utility/temp_data.py
+++ b/wqflask/utility/temp_data.py
diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py
index 11441d7a..bb8241f5 100644
--- a/wqflask/utility/tools.py
+++ b/wqflask/utility/tools.py
@@ -5,14 +5,18 @@ import os
 import sys
 from wqflask import app
 
+# Use the standard logger here to avoid a circular dependency
+import logging
+logger = logging.getLogger(__name__ )
+
 def get_setting(command_id,guess=None):
     """Resolve a setting from the environment or the global settings in
-    app.config, with get_valid_path is a function checking whether the
+    app.config, with valid_path is a function checking whether the
     path points to an expected directory and returns the full path to
     the binary command
 
       guess = os.environ.get('HOME')+'/pylmm'
-      get_setting('PYLMM_PATH',guess)
+      valid_path(get_setting('PYLMM_PATH',guess))
 
     first tries the environment variable in +id+, next gets the Flask
     app setting for the same +id+ and finally does an educated
@@ -31,23 +35,39 @@ def get_setting(command_id,guess=None):
     """
     def value(command):
         if command:
-            sys.stderr.write("Found path "+command+"\n")
+            # sys.stderr.write("Found "+command+"\n")
             return command
         else:
             return None
 
     # ---- Check whether environment exists
-    sys.stderr.write("Looking for "+command_id+"\n")
+    logger.debug("Looking for "+command_id+"\n")
     command = value(os.environ.get(command_id))
-    if not command:
+    if command is None or command == "":
         # ---- Check whether setting exists in app
         command = value(app.config.get(command_id))
-        if not command:
+        if command is None:
             command = value(guess)
-            if not command:
-                raise Exception(command_id+' path unknown or faulty (update settings.py?). '+command_id+' should point to the path')
+            if command is None or command == "":
+                print command
+                raise Exception(command_id+' setting unknown or faulty (update default_settings.py?).')
+    logger.debug("Set "+command_id+"="+str(command))
     return command
 
+def get_setting_bool(id):
+    v = get_setting(id)
+    if v not in [0,False,'False','FALSE',None]:
+      return True
+    return False
+
+def get_setting_int(id):
+    v = get_setting(id)
+    if isinstance(v, str):
+        return int(v)
+    if v is None:
+        return 0
+    return v
+
 def valid_bin(bin):
     if os.path.islink(bin) or valid_file(bin):
         return bin
@@ -100,7 +120,7 @@ def locate(name, subdir=None):
     if valid_path(base):
         lookfor = base + "/" + name
         if valid_file(lookfor):
-            print("Found: file "+lookfor+"\n")
+            logger.info("Found: file "+lookfor+"\n")
             return lookfor
         else:
             raise Exception("Can not locate "+lookfor)
@@ -120,18 +140,52 @@ def locate_ignore_error(name, subdir=None):
     if valid_path(base):
         lookfor = base + "/" + name
         if valid_file(lookfor):
-            print("Found: file "+name+"\n")
+            logger.debug("Found: file "+name+"\n")
             return lookfor
-    sys.stderr.write("WARNING: file "+name+" not found\n")
+    logger.info("WARNING: file "+name+" not found\n")
     return None
 
 def tempdir():
     return valid_path(get_setting("TEMPDIR","/tmp"))
 
+BLUE  = '\033[94m'
+GREEN = '\033[92m'
+BOLD  = '\033[1m'
+ENDC  = '\033[0m'
+
+def show_settings():
+    from utility.tools import LOG_LEVEL
+
+    print("Set global log level to "+BLUE+LOG_LEVEL+ENDC)
+    log_level = getattr(logging, LOG_LEVEL.upper())
+    logging.basicConfig(level=log_level)
+
+    logger.info(BLUE+"Mr. Mojo Risin 2"+ENDC)
+    print "runserver.py: ****** The webserver has the following configuration ******"
+    keylist = app.config.keys()
+    keylist.sort()
+    for k in keylist:
+        try:
+            print("%s %s%s%s%s" % (k,BLUE,BOLD,get_setting(k),ENDC))
+        except:
+            print("%s %s%s%s%s" % (k,GREEN,BOLD,app.config[k],ENDC))
+
 
 # Cached values
-PYLMM_COMMAND = pylmm_command()
-GEMMA_COMMAND = gemma_command()
-PLINK_COMMAND = plink_command()
-FLAT_FILES    = flat_files()
-TEMPDIR       = tempdir()
+WEBSERVER_MODE     = get_setting('WEBSERVER_MODE')
+GN_SERVER_URL      = get_setting('GN_SERVER_URL')
+SQL_URI            = get_setting('SQL_URI')
+LOG_LEVEL          = get_setting('LOG_LEVEL')
+LOG_LEVEL_DEBUG    = get_setting_int('LOG_LEVEL_DEBUG')
+LOG_SQL            = get_setting_bool('LOG_SQL')
+LOG_SQLALCHEMY     = get_setting_bool('LOG_SQLALCHEMY')
+LOG_BENCH          = get_setting_bool('LOG_BENCH')
+LOG_FORMAT         = "%(message)s"    # not yet in use
+USE_REDIS          = get_setting_bool('USE_REDIS')
+USE_GN_SERVER      = get_setting_bool('USE_GN_SERVER')
+
+PYLMM_COMMAND      = pylmm_command()
+GEMMA_COMMAND      = gemma_command()
+PLINK_COMMAND      = plink_command()
+FLAT_FILES         = flat_files()
+TEMPDIR            = tempdir()
diff --git a/wqflask/utility/webqtlUtil.py b/wqflask/utility/webqtlUtil.py
index 4fc978f5..4fc978f5 100755..100644
--- a/wqflask/utility/webqtlUtil.py
+++ b/wqflask/utility/webqtlUtil.py