about summary refs log tree commit diff
path: root/wqflask/utility
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/utility')
-rw-r--r--wqflask/utility/helper_functions.py24
-rw-r--r--wqflask/utility/logger.py22
-rw-r--r--wqflask/utility/tools.py70
3 files changed, 92 insertions, 24 deletions
diff --git a/wqflask/utility/helper_functions.py b/wqflask/utility/helper_functions.py
index 15f60765..377f6b26 100644
--- a/wqflask/utility/helper_functions.py
+++ b/wqflask/utility/helper_functions.py
@@ -4,18 +4,22 @@ from base.trait import GeneralTrait
 from base import data_set
 from base.species import TheSpecies
 
+from wqflask import user_manager
+import logging
+logger = logging.getLogger(__name__ )
+
 
 def get_species_dataset_trait(self, start_vars):
     #assert type(read_genotype) == type(bool()), "Expecting boolean value for read_genotype"
     self.dataset = data_set.create_dataset(start_vars['dataset'])
-    print("After creating dataset")
+    logger.debug("After creating dataset")
     self.species = TheSpecies(dataset=self.dataset)
-    print("After creating species")
+    logger.debug("After creating species")
     self.this_trait = GeneralTrait(dataset=self.dataset,
                                    name=start_vars['trait_id'],
                                    cellid=None,
                                    get_qtl_info=True)
-    print("After creating trait")
+    logger.debug("After creating trait")
 
     #if read_genotype:
     #self.dataset.group.read_genotype_file()
@@ -23,15 +27,17 @@ def get_species_dataset_trait(self, start_vars):
 
 
 def get_trait_db_obs(self, trait_db_list):
+    if isinstance(trait_db_list, basestring):
+        trait_db_list = trait_db_list.split(",")
 
     self.trait_list = []
-    for i, trait_db in enumerate(trait_db_list):
-        if i == (len(trait_db_list) - 1):
-            break
-        trait_name, dataset_name = trait_db.split(":")
-        #print("dataset_name:", dataset_name)
+    for trait in trait_db_list:
+        data, _separator, hmac = trait.rpartition(':')
+        data = data.strip()
+        assert hmac==user_manager.actual_hmac_creation(data), "Data tampering?"
+        trait_name, dataset_name = data.split(":")
         dataset_ob = data_set.create_dataset(dataset_name)
         trait_ob = GeneralTrait(dataset=dataset_ob,
                                name=trait_name,
                                cellid=None)
-        self.trait_list.append((trait_ob, dataset_ob))
\ No newline at end of file
+        self.trait_list.append((trait_ob, dataset_ob))
diff --git a/wqflask/utility/logger.py b/wqflask/utility/logger.py
index ddc0ea82..bacb0aa4 100644
--- a/wqflask/utility/logger.py
+++ b/wqflask/utility/logger.py
@@ -31,6 +31,7 @@ import string
 from inspect import isfunction
 from pprint import pformat as pf
 from inspect import stack
+import datetime
 
 from utility.tools import LOG_LEVEL, LOG_LEVEL_DEBUG, LOG_SQL, LOG_FORMAT
 
@@ -49,11 +50,20 @@ class GNLogger:
         self.logger.setLevel(value)
 
     def debug(self,*args):
+        """Call logging.debug for multiple args. Use (lazy) debugf and
+level=num to filter on LOG_LEVEL_DEBUG.
+
+        """
+        self.collect(self.logger.debug,*args)
+
+    def debug20(self,*args):
         """Call logging.debug for multiple args. Use level=num to filter on
 LOG_LEVEL_DEBUG (NYI).
 
         """
-        self.collect(self.logger.debug,*args)
+        if level <= LOG_LEVEL_DEBUG:
+            if self.logger.getEffectiveLevel() < 20:
+                self.collect(self.logger.debug,*args)
 
     def info(self,*args):
         """Call logging.info for multiple args"""
@@ -66,7 +76,10 @@ LOG_LEVEL_DEBUG (NYI).
 
     def error(self,*args):
         """Call logging.error for multiple args"""
-        self.collect(self.logger.error,*args)
+        now = datetime.datetime.utcnow()
+        time_str = now.strftime('%H:%M:%S UTC %Y%m%d')
+        l = [time_str]+list(args)
+        self.collect(self.logger.error,*l)
 
     def infof(self,*args):
         """Call logging.info for multiple args lazily"""
@@ -75,7 +88,10 @@ LOG_LEVEL_DEBUG (NYI).
             self.collectf(self.logger.debug,*args)
 
     def debugf(self,level=0,*args):
-        """Call logging.debug for multiple args lazily"""
+        """Call logging.debug for multiple args lazily and handle
+        LOG_LEVEL_DEBUG correctly
+
+        """
         # only evaluate function when logging
         if level <= LOG_LEVEL_DEBUG:
             if self.logger.getEffectiveLevel() < 20:
diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py
index bb8241f5..77cb6ac5 100644
--- a/wqflask/utility/tools.py
+++ b/wqflask/utility/tools.py
@@ -3,12 +3,16 @@
 
 import os
 import sys
+import json
+
 from wqflask import app
 
 # Use the standard logger here to avoid a circular dependency
 import logging
 logger = logging.getLogger(__name__ )
 
+OVERRIDES = {}
+
 def get_setting(command_id,guess=None):
     """Resolve a setting from the environment or the global settings in
     app.config, with valid_path is a function checking whether the
@@ -44,13 +48,15 @@ def get_setting(command_id,guess=None):
     logger.debug("Looking for "+command_id+"\n")
     command = value(os.environ.get(command_id))
     if command is None or command == "":
-        # ---- Check whether setting exists in app
-        command = value(app.config.get(command_id))
+        command = OVERRIDES.get(command_id)
         if command is None:
-            command = value(guess)
-            if command is None or command == "":
-                print command
-                raise Exception(command_id+' setting unknown or faulty (update default_settings.py?).')
+            # ---- Check whether setting exists in app
+            command = value(app.config.get(command_id))
+            if command is None:
+                command = value(guess)
+                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
 
@@ -92,6 +98,10 @@ def gemma_command(guess=None):
 def plink_command(guess=None):
     return valid_bin(get_setting("PLINK_COMMAND",guess))
 
+def flat_file_exists(subdir):
+    base = get_setting("GENENETWORK_FILES")
+    return valid_path(base+"/"+subdir)
+
 def flat_files(subdir=None):
     base = get_setting("GENENETWORK_FILES")
     if subdir:
@@ -103,6 +113,17 @@ def assert_dir(dir):
         raise Exception("ERROR: can not find directory "+dir)
     return dir
 
+def assert_writable_dir(dir):
+    try:
+        fn = dir + "/test.txt"
+        fh = open( fn, 'w' )
+        fh.write("I am writing this text to the file\n")
+        fh.close()
+        os.remove(fn)
+    except IOError:
+        raise Exception('Unable to write test.txt to directory ' + dir )
+    return dir
+
 def mk_dir(dir):
     if not valid_path(dir):
         os.makedirs(dir)
@@ -146,7 +167,10 @@ def locate_ignore_error(name, subdir=None):
     return None
 
 def tempdir():
-    return valid_path(get_setting("TEMPDIR","/tmp"))
+    """
+    Get UNIX TMPDIR by default
+    """
+    return valid_path(get_setting("TMPDIR","/tmp"))
 
 BLUE  = '\033[94m'
 GREEN = '\033[92m'
@@ -160,18 +184,21 @@ def show_settings():
     log_level = getattr(logging, LOG_LEVEL.upper())
     logging.basicConfig(level=log_level)
 
+    logger.info(OVERRIDES)
     logger.info(BLUE+"Mr. Mojo Risin 2"+ENDC)
-    print "runserver.py: ****** The webserver has the following configuration ******"
+    print "runserver.py: ****** Webserver 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))
+            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))
+            print("%s: %s%s%s%s" % (k,GREEN,BOLD,app.config[k],ENDC))
 
 
 # Cached values
+GN_VERSION         = get_setting('GN_VERSION')
+HOME               = get_setting('HOME')
 WEBSERVER_MODE     = get_setting('WEBSERVER_MODE')
 GN_SERVER_URL      = get_setting('GN_SERVER_URL')
 SQL_URI            = get_setting('SQL_URI')
@@ -184,8 +211,27 @@ LOG_FORMAT         = "%(message)s"    # not yet in use
 USE_REDIS          = get_setting_bool('USE_REDIS')
 USE_GN_SERVER      = get_setting_bool('USE_GN_SERVER')
 
+GENENETWORK_FILES  = get_setting('GENENETWORK_FILES')
+
 PYLMM_COMMAND      = pylmm_command()
 GEMMA_COMMAND      = gemma_command()
 PLINK_COMMAND      = plink_command()
-FLAT_FILES         = flat_files()
-TEMPDIR            = tempdir()
+TEMPDIR            = tempdir() # defaults to UNIX TMPDIR
+
+from six import string_types
+
+if os.environ.get('WQFLASK_OVERRIDES'):
+    jsonfn = get_setting('WQFLASK_OVERRIDES')
+    logger.error("WQFLASK_OVERRIDES: %s" % jsonfn)
+    with open(jsonfn) as data_file:
+        overrides = json.load(data_file)
+        for k in overrides:
+            cmd = overrides[k]
+            if isinstance(cmd, string_types):
+                OVERRIDES[k] = eval(cmd)
+            else:
+                OVERRIDES[k] = cmd
+            logger.debug(OVERRIDES)
+
+assert_dir(get_setting("JS_BIODALLIANCE"))
+assert_dir(get_setting("JS_TWITTER_POST_FETCHER"))