ATLAS Offline Software
Functions | Variables
python.ext.silence Namespace Reference

Functions

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

Variables

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

Function Documentation

◆ fifo()

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

Definition at line 37 of file silence.py.

37 def fifo():
38  """
39  Create a fifo in a temporary place.
40  """
41  tmpdir = mkdtemp()
42  filename = pjoin(tmpdir, 'myfifo')
43  try:
44  mkfifo(filename)
45  except OSError as e:
46  print_("Failed to create FIFO: %s" % e, file=stderr)
47  raise
48  else:
49  try:
50  yield filename
51  finally:
52  remove(filename)
53  rmdir(tmpdir)
54 

◆ make_fn()

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

Definition at line 26 of file silence.py.

26 def make_fn(what, res, *args):
27  what.restype = res
28  what.argtypes = args
29  return what
30 

◆ reader_thread_func()

def 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 64 of file silence.py.

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

◆ set_blocking()

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

Definition at line 55 of file silence.py.

55 def set_blocking(fd):
56  """
57  Set FD to be blocking
58  """
59  flags = fcntl(fd, F_GETFL)
60  if flags & O_NONBLOCK:
61  flags ^= O_NONBLOCK
62  fcntl(fd, F_SETFL, flags)
63 

◆ silence()

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

Definition at line 96 of file silence.py.

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

◆ silence_sout_serr()

def python.ext.silence.silence_sout_serr (   filter_)

Definition at line 137 of file silence.py.

137 def silence_sout_serr(filter_):
138  from sys import stdout, stderr
139  with nested(silence(filter_, stdout), silence(filter_, stderr)) as (so, se):
140  yield so, se
141 

◆ test()

def python.ext.silence.test ( )

Definition at line 142 of file silence.py.

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

◆ test_with_exception()

def python.ext.silence.test_with_exception ( )

Definition at line 159 of file silence.py.

159 def test_with_exception():
160 
161  print_("Before silence.")
162  try:
163  with silence() as filt_content:
164  print_("Hmm.")
165  raise RuntimeError("Error.")
166  except Exception:
167  pass
168  print_("After silence")
169  print_("Stuff?", len(filt_content.getvalue()))
170 

◆ threaded_file_reader()

def 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 83 of file silence.py.

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

Variable Documentation

◆ FILE_p

python.ext.silence.FILE_p = c_void_p

Definition at line 31 of file silence.py.

◆ freopen

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

Definition at line 34 of file silence.py.

◆ pyapi

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

Definition at line 23 of file silence.py.

◆ PyFile_AsFile

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

Definition at line 33 of file silence.py.

◆ this_exe

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

Definition at line 24 of file silence.py.

python.ext.silence.threaded_file_reader
def threaded_file_reader(*args)
Definition: silence.py:83
python.ext.silence.set_blocking
def set_blocking(fd)
Definition: silence.py:55
python.ext.silence.fifo
def fifo()
Definition: silence.py:37
python.ext.silence.freopen
def freopen
Definition: silence.py:34
python.ext.silence.silence_sout_serr
def silence_sout_serr(filter_)
Definition: silence.py:137
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
python.ext.silence.test_with_exception
def test_with_exception()
Definition: silence.py:159
python.ext.silence.PyFile_AsFile
def PyFile_AsFile
Definition: silence.py:33
python.ext.silence.silence
def silence(filter_=lambda line:True, file_=stdout)
Definition: silence.py:96
python.ext.silence.test
def test()
Definition: silence.py:142
python.ext.silence.make_fn
def make_fn(what, res, *args)
Definition: silence.py:26
DerivationFramework::ClustersInCone::select
void select(const xAOD::IParticle *particle, const float coneSize, const xAOD::CaloClusterContainer *clusters, std::vector< bool > &mask)
Definition: ClustersInCone.cxx:14
python.ext.silence.reader_thread_func
def reader_thread_func(filename, filter_, real_stdout, filt_content)
Definition: silence.py:64