from __future__ import print_function
from warnings import warn
-import select
+from select import poll, POLLIN, POLLOUT, POLLPRI, POLLHUP, POLLERR, POLLNVAL
from subprocess import Popen, PIPE
+from socket import socket as Socket
+
from type_helpers import isstr
# #############################################################################
# CONSTANTS
# #############################################################################
-DEFAULT_POLL_FLAGS = select.POLLIN | select.POLLPRI | select.POLLHUP
-# | select.POLLERR
+DEFAULT_POLL_FLAGS = POLLIN | POLLPRI | POLLHUP | POLLERR
DEFAULT_POLL_TIMEOUT = 10
def flags_to_str(flags):
""" return short string representation for poll flags """
text_parts = []
- if flags & select.POLLIN:
+ if flags & POLLIN:
text_parts.append('IN')
- if flags & select.POLLOUT:
+ if flags & POLLOUT:
text_parts.append('OUT')
- if flags & select.POLLPRI:
+ if flags & POLLPRI:
text_parts.append('PRI')
- if flags & select.POLLERR:
+ if flags & POLLERR:
text_parts.append('ERR')
- if flags & select.POLLHUP:
+ if flags & POLLHUP:
text_parts.append('HUP')
- if flags & select.POLLNVAL:
+ if flags & POLLNVAL:
text_parts.append('INVALID')
return ','.join(text_parts)
if key not in ('flags', 'timeout'):
raise ValueError('argument not recognized: {0}'.format(key))
- self._poller = select.poll()
+ self._poller = poll()
sources = []
file_nos = []
def next(self):
""" main function to poll next line from sources
- returns (source, desc, line_stripped)
+ returns (source, desc, line_stripped, flags)
"""
while True:
if not flags & self._flags:
raise PollFlagsException(flags, desc)
- if flags & select.POLLHUP:
- raise PollHupException(desc)
+ if flags & POLLHUP:
+ #raise PollHupException(desc)
+ warn('received a hangup during polling for {0}'
+ .format(desc))
+
+ if flags & POLLERR:
+ warn('received an err during polling for {0}'
+ .format(desc))
+ if flags & POLLNVAL:
+ warn('poll replied "invalid request" err during polling '
+ 'for {0}'
+ .format(desc))
# read line from source
line = source.readline()
continue
# if reach here, we have a new line of text
- return source, desc, line.strip()
+ return source, desc, line.strip(), flags
# for poll results
# end: inf loop
# end: Follower._next_line
with open(syslog_file, 'r') as file_handle:
flwr = Follower(file_handle, 'syslog file', proc.stdout, 'tail syslog',
proc.stderr, 'tail stderr')
- for n_lines, (source, desc, line) in enumerate(flwr):
- test_syslog_line(n_lines, source, desc, line, start_time)
- # until user aborts
- # end: with open syslog
-# end: function test_follower_syslog
+ for n_lines, (source, desc, line, flags) in enumerate(flwr):
+ test_syslog_line(n_lines, source, desc, line,
+ today_str, start_time)
def test_follower_context():
start_time = dt.now() - td(seconds=time_diff_seconds)
with follow(syslog_file) as flwr:
- for n_lines, (source, desc, line) in enumerate(flwr):
- test_syslog_line(n_lines, source, desc, line, start_time)
+ for n_lines, (source, desc, line, flags) in enumerate(flwr):
+ test_syslog_line(n_lines, source, desc, line,
+ today_str, start_time)
def test():