vidjil_utils.py 3.66 KB
Newer Older
1
import math
2
import re
3
import defs
4
from gluon import current
5

6
def format_size(n, unit='B'):
7 8
    '''
    Takes an integer n, representing a filesize and returns a string
9 10
    where the size is formatted with the correct SI prefix and
    with a constant number of significant digits.
11 12

    Example:
13 14 15 16 17 18
    >>> format_size(42)
    '42 B'
    >>> format_size(123456)
    '123 kB'
    >>> format_size(1000*1000)
    '1.00 MB'
19
    >>> format_size(1024*1024*1024)
20 21 22
    '1.07 GB'
    >>> format_size(42*(2**40))
    '46.2 TB'
23
    '''
24 25 26 27

    if n == 0:
        return '0'

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    size = float(n)
    PREFIXES = ['', 'k', 'M', 'G', 'T', 'P']

    for prefix in PREFIXES:
        if size < 1000:
            break
        size /= 1000


    if size > 100 or not prefix:
        fmt = '%.0f'
    elif size > 10:
        fmt = '%.1f'
    else:
        fmt = '%.2f'

    return fmt % size + ' ' + prefix + unit

46 47 48 49 50 51 52 53 54

def anon(patient_id, user_id):
    db = current.db
    auth=current.auth
    
    last_name = db.patient[patient_id].last_name
    first_name = db.patient[patient_id].first_name
    
    if auth.has_permission("anon", "patient", patient_id, user_id):
55
        name = last_name + " " + first_name
56
    else:
57 58 59 60 61 62
        try:
            ln = unicode(last_name, 'utf-8')
        except UnicodeDecodeError:
            ln = last_name
        name = ln[:3]

63 64 65 66
    # Admins also see the patient id
    if auth.has_membership("admin"):
        name += ' (%s)' % patient_id

67
    return name
68 69 70 71 72 73 74 75 76


# take a string to check and a filter_str (list of word to find (or not)) 
# return true if the string respect the filter list 
def filter(str, filter_str):
    filter_list = filter_str.split(" ")
    
    for f in filter_list :
        if len(f) > 0 and f[0] == "-" :
77
            if f[1:].lower() in str.lower():
78 79
                return False
        else :
80
            if f.lower() not in str.lower():
81 82
                return False
    return True
83 84 85 86


log_patient = re.compile('\((\d+)\)')
log_config = re.compile(' c(\d+)')
87
log_task = re.compile('\[(\d+)\]')
88 89 90 91 92 93

def log_links(s):
    '''Add HTML links to a log string

    >>> log_links("abcdef")
    'abcdef'
94 95
    >>> log_links("[1234]abcdef")
    '[<a class="loglink pointer" onclick="db.call(\\'admin/log\\', {\\'file\\': \\'../../mnt/result/tmp/out-001234/\\'})">1234</a>]abcdef'
96
    >>> log_links("abcdef(234)")
97
    'abcdef(<a class="loglink pointer" onclick="db.call(\\'patient/info\\', {\\'id\\': \\'234\\'})">234</a>)'
98
    >>> log_links("abcdef(234)abcdef c11")
99
    'abcdef(234)abcdef <a class="loglink pointer" href="?patient=234&config=11">c11</a>'
100 101 102 103 104 105 106 107 108 109
    '''

    ### Parses the input string

    m_patient = log_patient.search(s)
    patient = m_patient.group(1) if m_patient else None

    m_config = log_config.search(s)
    config = m_config.group(1) if m_config else None

110 111 112
    m_task = log_task.search(s)
    task = int(m_task.group(1)) if m_task else None

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    ### Rules

    url = ''  # href link
    call = '' # call to db

    if patient and config:
        url = "?patient=%s&config=%s" % (patient, config)
        (start, end) = m_config.span()
        start += 1

    elif patient:
        call = "patient/info"
        args = {'id': patient}
        (start, end) = m_patient.span()
        start += 1
        end -= 1

130 131 132 133 134 135 136
    elif task:
        call = "admin/log"
        args = {'file': '../../' + defs.DIR_OUT_VIDJIL_ID % task + 'vidjil.log'}
        (start, end) = m_task.span()
        start += 1
        end -= 1

137 138 139 140 141 142 143 144 145
    ### Build final string

    link = ''
    if url:
        link = 'href="%s"' % url
    if call:
        link = '''onclick="db.call('%s', %s)"''' % (call, str(args))

    if link:
146
        s = '%s<a class="loglink pointer" %s>%s</a>%s' % (s[:start], link, s[start:end], s[end:])
147 148

    return s