summaryrefslogtreecommitdiff
path: root/qface/helper/generic.py
blob: 713952dc815020d57b11d4d341dbcae2bdde4a8b (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
import json
import hashlib


def jsonify(symbol):
    """ returns json format for symbol """
    try:
        # all symbols have a toJson method, try it
        return json.dumps(symbol.toJson(), indent='  ')
    except AttributeError:
        pass
    return json.dumps(symbol, indent='  ')


def upper_first(s):
    """ uppercase first letter """
    s = str(s)
    return s[0].upper() + s[1:]


def lower_first(s):
    s = str(s)
    return s[0].lower() + s[1:]


def hash(symbol, hash_type='sha1'):
    """ create a hash code from symbol """
    code = hashlib.new(hash_type)
    code.update(str(symbol).encode('utf-8'))
    return code.hexdigest()


def path(symbol):
    """ replaces '.' with '/' """
    return str(symbol).replace('.', '/')


def identifier(s):
    return str(s).lower().replace('.', '_')


def get_filters():
    return {
        'jsonify': jsonify,
        'upper_first': upper_first,
        'lower_first': lower_first,
        'upperfirst': upper_first,
        'lowerfirst': lower_first,
        'hash': hash,
        'path': path,
        'identifier': identifier,
    }