aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/tools.py
blob: 1b31857ed6f4719cfc3b0b332e6288bba92b8257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Tools/paths finder resolves external paths from settings and/or environment
# variables

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 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'
      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
    +guess+.

    In all, the environment overrides the others, next is the flask
    setting, then the guess. A valid path to the binary command is
    returned. If none is resolved an exception is thrown.

    Note that we do not use the system path. This is on purpose
    because it will mess up controlled (reproducible) deployment. The
    proper way is to either use the GNU Guix defaults as listed in
    etc/default_settings.py or override them yourself by creating a
    different settings.py file (or setting the environment).

    """
    def value(command):
        if command:
            # sys.stderr.write("Found "+command+"\n")
            return command
        else:
            return None

    # ---- Check whether environment exists
    logger.debug("Looking for "+command_id+"\n")
    command = value(os.environ.get(command_id))
    if command == None or command == "":
        # ---- Check whether setting exists in app
        command = value(app.config.get(command_id))
        if command == None:
            command = value(guess)
            if command == None or command == "":
                raise Exception(command_id+' setting unknown or faulty (update default_settings.py?).')
    logger.info("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)
    return int(v)

def valid_bin(bin):
    if os.path.islink(bin) or valid_file(bin):
        return bin
    return None

def valid_file(fn):
    if os.path.isfile(fn):
        return fn
    return None

def valid_path(dir):
    if os.path.isdir(dir):
        return dir
    return None

def pylmm_command(guess=None):
    return valid_bin(get_setting("PYLMM_COMMAND",guess))

def gemma_command(guess=None):
    return valid_bin(get_setting("GEMMA_COMMAND",guess))

def plink_command(guess=None):
    return valid_bin(get_setting("PLINK_COMMAND",guess))

def flat_files(subdir=None):
    base = get_setting("GENENETWORK_FILES")
    if subdir:
        return assert_dir(base+"/"+subdir)
    return assert_dir(base)

def assert_dir(dir):
    if not valid_path(dir):
        raise Exception("ERROR: can not find directory "+dir)
    return dir

def mk_dir(dir):
    if not valid_path(dir):
        os.makedirs(dir)
    return assert_dir(dir)

def locate(name, subdir=None):
    """
    Locate a static flat file in the GENENETWORK_FILES environment.

    This function throws an error when the file is not found.
    """
    base = get_setting("GENENETWORK_FILES")
    if subdir:
        base = base+"/"+subdir
    if valid_path(base):
        lookfor = base + "/" + name
        if valid_file(lookfor):
            logger.info("Found: file "+lookfor+"\n")
            return lookfor
        else:
            raise Exception("Can not locate "+lookfor)
    if subdir: sys.stderr.write(subdir)
    raise Exception("Can not locate "+name+" in "+base)

def locate_ignore_error(name, subdir=None):
    """
    Locate a static flat file in the GENENETWORK_FILES environment.

    This function does not throw an error when the file is not found
    but returns None.
    """
    base = get_setting("GENENETWORK_FILES")
    if subdir:
        base = base+"/"+subdir
    if valid_path(base):
        lookfor = base + "/" + name
        if valid_file(lookfor):
            logger.debug("Found: file "+name+"\n")
            return lookfor
    logger.info("WARNING: file "+name+" not found\n")
    return None

def tempdir():
    return valid_path(get_setting("TEMPDIR","/tmp"))

# Cached values
WEBSERVER_MODE     = get_setting('WEBSERVER_MODE')
LOG_LEVEL          = get_setting('LOG_LEVEL')
DEBUG_LOG_LEVEL    = get_setting_int('DEBUG_LOG_LEVEL')
LOG_SQL            = get_setting_bool('LOG_SQL')
USE_REDIS          = get_setting_bool('USE_REDIS')

PYLMM_COMMAND      = pylmm_command()
GEMMA_COMMAND      = gemma_command()
PLINK_COMMAND      = plink_command()
FLAT_FILES         = flat_files()
TEMPDIR            = tempdir()