This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutup.py
114 lines (91 loc) · 2.16 KB
/
shutup.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
shutup.py v. 1.0
Shuts up stupid console output when you want some peace and quiet using all the
wrong tricks in the book.
"""
import sys
class ShutUpWrite(object):
"""
Mock sys.stdout object with a nuked write method. Probably not healthy.
"""
def write(self, text):
pass
class shutup(object):
def __init__(self, fn=None):
self._save = sys.stdout
self._function = fn
def __call__(self):
"""
Used for calling on string
"""
if self._function is not None:
try:
sys.stdout = ShutUpWrite()
output = self._function()
finally:
sys.stdout = self._save
return output
else:
raise Exception("Requires a function to call")
def __enter__(self):
sys.stdout = ShutUpWrite()
return True
def __exit__(self, type, value, traceback):
sys.stdout = self._save
return False
def function(fn):
"""
decorator use case
@shutup.function
def some_function():
do some stuff
"""
def new_fn():
stdout_save = sys.stdout
sys.stdout= ShutUpWrite()
output = fn()
sys.stdout = stdout_save
return(output)
return(new_fn)
def method(fn):
"""
class method usecase
@shutup.method
def some_function():
do some stuff
"""
def new_fn(self):
stdout_save = sys.stdout
sys.stdout= ShutUpWrite()
output = fn(self)
sys.stdout = stdout_save
return(output)
return(new_fn)
def context():
"""
Alt for with shutup.shutup()
usage:
with shutup.context() as shutit:
do something
"""
return shutup()
_save_stdout = sys.stdout # Oh yeah, suck on my global.
def mute():
"""
Mutes... fucking... everything.
"""
sys.stdout= ShutUpWrite()
return True # why not?
def unmute():
"""
I hope this works.
"""
sys.stdout = _save_stdout
return True # momma always said to give back
def string(annoying):
mute()
try:
output = exec(annoying)
finally:
unmute()
return output