diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-03-05 14:50:54 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-03-05 14:50:54 +0100 |
commit | 94c053866bd1bf4dec8cbb78461e3f6ec617270c (patch) | |
tree | 6c17fdbe65673bd6d44fe431bc1262c9cd28e03e /src | |
parent | add Bar.invalidate (diff) | |
download | xpybar-94c053866bd1bf4dec8cbb78461e3f6ec617270c.tar.gz xpybar-94c053866bd1bf4dec8cbb78461e3f6ec617270c.tar.bz2 xpybar-94c053866bd1bf4dec8cbb78461e3f6ec617270c.tar.xz |
add linereader
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r-- | src/plugins/linereader.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/plugins/linereader.py b/src/plugins/linereader.py new file mode 100644 index 0000000..643d7f9 --- /dev/null +++ b/src/plugins/linereader.py @@ -0,0 +1,94 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +''' + +import os +from util import * + + +class LineReader: + ''' + Line reader + ''' + + + def __init__(self, channel = None): + ''' + Constructor + + @param channel:str|int|istream? The pathname, file descriptor or stream + of the file to read, `None` for stdin + ''' + self.__channel = None + if channel is None: + self.__next = lambda : input() + else: + next__ = lambda : channel.read(1) + if isinstance(channel, str): + self.__channel = channel = os.open(channel, os.O_RDONLY) + if isinstance(channel, int): + next__ = lambda : channel.read() + buffer = '' + def next_(): + while True: + got = next__() + if (got is None) or (len(got) == 0): + return None + if not isinstance(got, str): + got = got.decode('utf-8', 'replace') + buffer += got + if '\n' in buffer: + got = buffer.find('\n') + got, buffer = buffer[:got], buffer[got + 1:] + return got + self.__next = next_ + + + def next(self): + ''' + Reads the next line + + @return :str? The next line, `None` if stream has closed + ''' + try: + return self.__next() + except: + return None + + + def close(self): + ''' + Close any file this object has opened + ''' + if self.__channel is not None: + os.close(self.__channel) + + + def __enter__(self): + ''' + Invoked when `with` enters + ''' + return self + + + def __exit__(self, _type, _value, _trace): + ''' + Invoked when `with` exits + ''' + self.close() + |