globals.py 3.02 KB
Newer Older
Matt Traudt's avatar
Matt Traudt committed
1
import os
Matt Traudt's avatar
Matt Traudt committed
2
from sbws.lib.pastlylogger import PastlyLogger
Matt Traudt's avatar
Matt Traudt committed
3
4
5
6


G_PKG_DIR = os.path.abspath(os.path.dirname(__file__))
G_INIT_FILE_MAP = [
7
8
9
10
11
12
13
14
    # Specified as:
    #     (source, destination, type)
    # Where:
    #     - source is relative to the sbws/ directory
    #     - destination is relative to $HOME/.sbws/ directory (or whatever the
    #     user specified as their directory with --directory)
    #     - type is 'file', and ideally type 'dir' will be supported in the
    #     future as needed
Matt Traudt's avatar
Matt Traudt committed
15
16
]

17
18
19
20
# Minimum and maximum number of bytes a client is allowed to request from a
# server. If these are changed, a WIRE_PROTO_VER bump is required, which also
# happens to require an sbws major version bump.
MIN_REQ_BYTES = 1
21
MAX_REQ_BYTES = 1 * 1024 * 1024 * 1024  # 1 GiB
22
SOCKET_TIMEOUT = 60  # seconds
23

Matt Traudt's avatar
Matt Traudt committed
24
25

def is_initted(d):
26
    if not os.path.isdir(d):
Matt Traudt's avatar
Matt Traudt committed
27
28
        return False
    for _, fname, _ in G_INIT_FILE_MAP:
29
        fname = os.path.join(d, fname)
Matt Traudt's avatar
Matt Traudt committed
30
31
        if not os.path.exists(fname):
            return False
32
33
34
    conf_fname = os.path.join(d, 'config.ini')
    if not os.path.exists(conf_fname):
        return False
Matt Traudt's avatar
Matt Traudt committed
35
    return True
36
37


Matt Traudt's avatar
Matt Traudt committed
38
def fail_hard(*s, log=None):
39
40
41
    ''' Optionally log something to stdout ... and then exit as fast as
    possible '''
    if s:
Matt Traudt's avatar
Matt Traudt committed
42
43
44
45
        if log:
            log.error(*s)
        else:
            print(*s)
46
    exit(1)
Matt Traudt's avatar
Matt Traudt committed
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
def _log_level_string_to_int(s):
    if s == 'debug':
        return 4
    elif s == 'info':
        return 3
    elif s == 'notice':
        return 2
    elif s == 'warn':
        return 1
    elif s == 'error':
        return 0
    fail_hard('Unknown log level:', s)


def _log_level_int_to_string(i):
    if i >= 4:
        return 'debug'
    elif i == 3:
        return 'info'
    elif i == 2:
        return 'notice'
    elif i == 1:
        return 'warn'
    else:
        return 'error'

Matt Traudt's avatar
Matt Traudt committed
75

76
77
def make_logger(args, conf):
    def get_logger(level, default):
Matt Traudt's avatar
Matt Traudt committed
78
        def_file = '/dev/stdout'
79
        common_kwargs = {'log_threads': True, 'default': default}
Matt Traudt's avatar
Matt Traudt committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        if level == 'debug':
            return PastlyLogger(debug=def_file, overwrite=['debug'],
                                **common_kwargs)
        if level == 'info':
            return PastlyLogger(info=def_file, overwrite=['info'],
                                **common_kwargs)
        if level == 'notice':
            return PastlyLogger(notice=def_file, overwrite=['notice'],
                                **common_kwargs)
        if level == 'warn':
            return PastlyLogger(warn=def_file, overwrite=['warn'],
                                **common_kwargs)
        if level == 'error':
            return PastlyLogger(error=def_file, overwrite=['error'],
                                **common_kwargs)
        else:
            fail_hard('Unknown log level', level)
97
98
99
100
101
102
    level_str = conf.get('general', 'log_level')
    default_level_str = level_str
    level = _log_level_string_to_int(level_str)
    level = level + args.verbose - args.quiet
    level_str = _log_level_int_to_string(level)
    return get_logger(level_str, default_level_str)