ATLAS Offline Software
Loading...
Searching...
No Matches
python.ext.silence Namespace Reference

Functions

 make_fn (what, res, *args)
 fifo ()
 set_blocking (fd)
 reader_thread_func (filename, filter_, real_stdout, filt_content)
 threaded_file_reader (*args)
 silence (filter_=lambda line:True, file_=stdout)
 silence_sout_serr (filter_)
 test ()
 test_with_exception ()

Variables

 pyapi = PyDLL(None)
 this_exe = CDLL(None)
 FILE_p = c_void_p
 PyFile_AsFile = make_fn(pyapi.PyFile_AsFile, FILE_p, py_object)
 freopen = make_fn(this_exe.freopen, FILE_p, c_char_p, c_char_p, FILE_p)

Function Documentation

◆ fifo()

python.ext.silence.fifo ( )
Create a fifo in a temporary place.

Definition at line 33 of file silence.py.

33def fifo():
34 """
35 Create a fifo in a temporary place.
36 """
37 tmpdir = mkdtemp()
38 filename = pjoin(tmpdir, 'myfifo')
39 try:
40 mkfifo(filename)
41 except OSError as e:
42 print("Failed to create FIFO: %s" % e, file=stderr)
43 raise
44 else:
45 try:
46 yield filename
47 finally:
48 remove(filename)
49 rmdir(tmpdir)
50
void print(char *figname, TCanvas *c1)

◆ make_fn()

python.ext.silence.make_fn ( what,
res,
* args )

Definition at line 22 of file silence.py.

22def make_fn(what, res, *args):
23 what.restype = res
24 what.argtypes = args
25 return what
26

◆ reader_thread_func()

python.ext.silence.reader_thread_func ( filename,
filter_,
real_stdout,
filt_content )
Sit there, reading lines from the pipe `filename`, sending those for which
`filter_()` returns False to `real_stdout`

Definition at line 60 of file silence.py.

60def reader_thread_func(filename, filter_, real_stdout, filt_content):
61 """
62 Sit there, reading lines from the pipe `filename`, sending those for which
63 `filter_()` returns False to `real_stdout`
64 """
65 with fdopen(osopen(filename, O_NONBLOCK | O_RDONLY)) as fd:
66 while True:
67 rlist, _, _ = select([fd], [], [])
68
69 line = fd.readline()
70 if not line:
71 break
72
73 elif filter_(line):
74 filt_content.write(line)
75 else:
76 real_stdout.write(line)
77
78@contextmanager

◆ set_blocking()

python.ext.silence.set_blocking ( fd)
Set FD to be blocking

Definition at line 51 of file silence.py.

51def set_blocking(fd):
52 """
53 Set FD to be blocking
54 """
55 flags = fcntl(fd, F_GETFL)
56 if flags & O_NONBLOCK:
57 flags ^= O_NONBLOCK
58 fcntl(fd, F_SETFL, flags)
59

◆ silence()

python.ext.silence.silence ( filter_ = lambda line: True,
file_ = stdout )
Prevent lines matching `filter_` ending up on `file_` (defaults to stdout)

Definition at line 92 of file silence.py.

92def silence(filter_=lambda line: True, file_=stdout):
93 """
94 Prevent lines matching `filter_` ending up on `file_` (defaults to stdout)
95 """
96 if not filter_:
97 yield
98 return
99
100 import io
101 file = io.IOBase
102 if not isinstance(file_, file):
103 # Unable to filter because it's not a file instance.
104 yield
105 return
106
107 saved_stdout = dup(file_.fileno())
108 stdout_file = PyFile_AsFile(file_)
109
110 from io import StringIO
111 filt_content = StringIO()
112
113 with nested(fdopen(saved_stdout, "w"), fifo()) as (real_stdout, filename):
114 try:
115 tfr = threaded_file_reader(filename, filter_,
116 real_stdout, filt_content)
117 with tfr:
118 # Redirect stdout to pipe
119 freopen(filename, "w", stdout_file)
120 try:
121 yield filt_content
122 finally:
123 # Redirect stdout back to it's original place
124 freopen("/dev/fd/%i" % saved_stdout, "w", stdout_file)
125
126 except Exception:
127 print("Hit an exception. Filtered content:")
128 print(filt_content.getvalue())
129 raise
130
131@contextmanager

◆ silence_sout_serr()

python.ext.silence.silence_sout_serr ( filter_)

Definition at line 132 of file silence.py.

132def silence_sout_serr(filter_):
133 from sys import stdout, stderr
134 with nested(silence(filter_, stdout), silence(filter_, stderr)) as (so, se):
135 yield so, se
136

◆ test()

python.ext.silence.test ( )

Definition at line 137 of file silence.py.

137def test():
138
139 def filter_hello(line):
140 if line.startswith("Data source lookup using"):
141 return True
142
143 print("Before with block..")
144
145 with silence(filter_hello):
146 from DQUtils.db import Databases
147 f = Databases.get_folder("DQMFONL")
148 print("Sensible stuff!")
149
150 print("f =", f)
151
152 print("I am after the silence block")
153

◆ test_with_exception()

python.ext.silence.test_with_exception ( )

Definition at line 154 of file silence.py.

154def test_with_exception():
155
156 print("Before silence.")
157 try:
158 with silence() as filt_content:
159 print("Hmm.")
160 raise RuntimeError("Error.")
161 except Exception:
162 pass
163 print("After silence")
164 print("Stuff?", len(filt_content.getvalue()))
165

◆ threaded_file_reader()

python.ext.silence.threaded_file_reader ( * args)
Operate a read_thread_func in another thread. Block with statement exit
until the function completes.

Definition at line 79 of file silence.py.

79def threaded_file_reader(*args):
80 """
81 Operate a read_thread_func in another thread. Block with statement exit
82 until the function completes.
83 """
84 reader_thread = Thread(target=reader_thread_func, args=args)
85 reader_thread.start()
86 try:
87 yield
88 finally:
89 reader_thread.join()
90
91@contextmanager

Variable Documentation

◆ FILE_p

python.ext.silence.FILE_p = c_void_p

Definition at line 27 of file silence.py.

◆ freopen

python.ext.silence.freopen = make_fn(this_exe.freopen, FILE_p, c_char_p, c_char_p, FILE_p)

Definition at line 30 of file silence.py.

◆ pyapi

python.ext.silence.pyapi = PyDLL(None)

Definition at line 19 of file silence.py.

◆ PyFile_AsFile

python.ext.silence.PyFile_AsFile = make_fn(pyapi.PyFile_AsFile, FILE_p, py_object)

Definition at line 29 of file silence.py.

◆ this_exe

python.ext.silence.this_exe = CDLL(None)

Definition at line 20 of file silence.py.