From 94c053866bd1bf4dec8cbb78461e3f6ec617270c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 5 Mar 2014 14:50:54 +0100 Subject: add linereader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/plugins/linereader.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/plugins/linereader.py (limited to 'src') 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 . +''' + +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() + -- cgit v1.2.3-70-g09d2