Completed
Push — master ( 031bf5...ff3ed3 )
by John
03:05
created

enter_to_exit()   B

Complexity

Conditions 5

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.125

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 12
ccs 3
cts 6
cp 0.5
crap 8.125
rs 8.5454
1
#!/usr/bin/env python3
2 4
"""This module is used for decorators."""
3
4 4
import time  # spinner delay, timer decorator
5 4
import math  # rounding
6 4
import os  # path check
7 4
import sys  # frozen
8 4
import sqlite3  # the sql library
9 4
from bbarchivist import dummy  # useless stdout
10 4
from bbarchivist import exceptions  # exceptions
11
12 4
__author__ = "Thurask"
13 4
__license__ = "WTFPL v2"
14 4
__copyright__ = "Copyright 2015-2016 Thurask"
15
16
17 4
def wrap_keyboard_except(method):
18
    """
19
    Decorator to absorb KeyboardInterrupt.
20
21
    :param method: Method to use.
22
    :type method: function
23
    """
24
    def wrapper(*args, **kwargs):
25
        """
26
        Try function, absorb KeyboardInterrupt and leave gracefully.
27
        """
28
        try:
29
            method(*args, **kwargs)
30
        except KeyboardInterrupt:
31
            dummy.UselessStdout.write("ASDASDASD")
32
    return wrapper
33
34
35 4
def timer(method):
36
    """
37
    Decorator to time a function.
38
39
    :param method: Method to time.
40
    :type method: function
41
    """
42 4
    def wrapper(*args, **kwargs):
43
        """
44
        Start clock, do function with args, print rounded elapsed time.
45
        """
46 4
        starttime = time.clock()
47 4
        method(*args, **kwargs)
48 4
        endtime = time.clock() - starttime
49 4
        endtime_proper = math.ceil(endtime * 100) / 100  # rounding
50 4
        mins, secs = divmod(endtime_proper, 60)
51 4
        hrs, mins = divmod(mins, 60)
52 4
        print("COMPLETED IN {0:02d}:{1:02d}:{2:02d}".format(int(hrs), int(mins), int(secs)))
53 4
    return wrapper
54
55
56 4
def sql_excepthandler(integrity):
57
    """
58
    Decorator to handle sqlite3.Error.
59
60
    :param integrity: Whether to allow sqlite3.IntegrityError.
61
    :type integrity: bool
62
    """
63 4
    def exceptdecorator(method):
64
        """
65
        Call function in sqlite3.Error try/except block.
66
67
        :param method: Method to use.
68
        :type method: function
69
        """
70 4
        def wrapper(*args, **kwargs):
71
            """
72
            Try function, handle sqlite3.Error, optionally pass sqlite3.IntegrityError.
73
            """
74 4
            try:
75 4
                result = method(*args, **kwargs)
76 4
                return result
77 4
            except sqlite3.IntegrityError if bool(integrity) else exceptions.DummyException:
78 4
                dummy.UselessStdout.write("ASDASDASD")  # DummyException never going to happen
79 4
            except sqlite3.Error as sqerror:
80 4
                print(sqerror)
81 4
        return wrapper
82 4
    return exceptdecorator
83
84
85 4
def sql_existhandler(sqlpath):
86
    """
87
    Decorator to check if SQL database exists.
88
89
    :param sqlpath: Path to SQL database.
90
    :type sqlpath: str
91
    """
92 4
    def existdecorator(method):
93
        """
94
        Call function if SQL database exists.
95
96
        :param method: Method to use.
97
        :type method: function
98
        """
99 4
        def wrapper(*args, **kwargs):
100
            """
101
            Check existence of database, leave if it doesn't.
102
            """
103 4
            if os.path.exists(sqlpath):
104 4
                result = method(*args, **kwargs)
105 4
                return result
106
            else:
107 4
                print("NO SQL DATABASE FOUND!")
108 4
                raise SystemExit
109 4
        return wrapper
110 4
    return existdecorator
111
112
113 4
def enter_to_exit(checkfreeze=True):
114
    """
115
    Press enter to exit a script.
116
117
    :param checkfreeze: If this triggers only in frozen executables. Default is true.
118
    :type checkfreeze: bool
119
    """
120 4
    greenlight = bool(getattr(sys, 'frozen', False)) if checkfreeze else True
121 4
    if greenlight:
122
        smeg = input("Press Enter to exit")
123
        if smeg or not smeg:
124
            raise SystemExit
125