-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (26 loc) · 776 Bytes
/
utils.py
File metadata and controls
35 lines (26 loc) · 776 Bytes
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
# -*- coding: UTF-8 -*-
import io
import sys
class Tee(object):
def __init__(self):
self.memory = io.StringIO()
self.origin_stdout = None
def start(self):
self.origin_stdout, sys.stdout = sys.stdout, self
def close(self):
if self.origin_stdout:
sys.stdout = self.origin_stdout
self.flush()
def getvalue(self, *args, **kwargs):
return self.memory.getvalue(*args, **kwargs)
def write(self, data):
self.memory.write(data)
if self.origin_stdout:
self.origin_stdout.write(data)
def flush(self):
self.memory.seek(0)
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()