blink 2.13 KB
Newer Older
1
#!/usr/bin/env python3
Dan Pascu's avatar
Dan Pascu committed
2 3 4 5

import os
import sys

Dan Pascu's avatar
Dan Pascu committed
6 7 8 9

def except_hook(exception_type, exception_value, traceback):
    sys.__excepthook__(exception_type, exception_value, traceback)

10

Dan Pascu's avatar
Dan Pascu committed
11 12
sys.excepthook = except_hook  # pyqt5 (>=5.5) will abort if an exception happens in python code when called from Qt, unless we define this

Dan Pascu's avatar
Dan Pascu committed
13

Dan Pascu's avatar
Dan Pascu committed
14 15
frozen = hasattr(sys, 'frozen')

Dan Pascu's avatar
Dan Pascu committed
16 17
# We need to mangle Python's import path in case blink is run directly from
# the bin/ directory.
Dan Pascu's avatar
Dan Pascu committed
18
script_dir = os.path.dirname(os.path.realpath(sys.executable if frozen else __file__))
Dan Pascu's avatar
Dan Pascu committed
19
parent_dir = os.path.dirname(script_dir)
Dan Pascu's avatar
Dan Pascu committed
20
if os.path.basename(script_dir) == 'bin' and os.path.exists(os.path.join(parent_dir, 'blink', '__init__.py')):
Dan Pascu's avatar
Dan Pascu committed
21 22 23 24 25 26 27 28 29 30
    # Insert the parent path just before the existing script's path. We need
    # to do this in order to work with debuggers which insert their own paths
    # at the beginning. The script's path is the last Python itself inserted
    # so we should insert just before that.
    try:
        position = sys.path.index(script_dir)
    except ValueError:
        position = 0
    sys.path.insert(position, parent_dir)

Dan Pascu's avatar
Dan Pascu committed
31
if frozen:
Saul Ibarra's avatar
Saul Ibarra committed
32
    from StringIO import StringIO
33

34
    class DivertedStdout(object):
35
        def __init__(self):
36
            self._output = StringIO()
37 38 39 40 41 42 43
            self._file = None

        @property
        def file(self):
            return self._file

        @file.setter
44 45 46 47 48 49 50 51 52
        def file(self, path):
            if self._file is not None:
                raise RuntimeError("output file was already set to: {0._file!r}".format(self))
            f = open(path, 'a', 0)
            # noinspection PyUnresolvedReferences
            f.write(self._output.getvalue())
            self._output.close()
            self._output = f
            self._file = path
53 54

        def __getattr__(self, name):
55
            return getattr(self._output, name)
56

57
    sys.stdout = sys.stderr = DivertedStdout()
58 59 60 61 62 63


# Import log last so the created StreamHandler instances have references
# to the replaced sys.std{out,err}
from application import log
log.level.current = log.level.WARNING
Saul Ibarra's avatar
Saul Ibarra committed
64 65


Dan Pascu's avatar
Dan Pascu committed
66 67 68 69
if __name__ == '__main__':
    from blink import Blink
    blink = Blink()
    blink.run()
70
    os._exit(0)