aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--TODO1
-rw-r--r--src/plugins/linereader.py94
2 files changed, 94 insertions, 1 deletions
diff --git a/TODO b/TODO
index 9103a22..95a760c 100644
--- a/TODO
+++ b/TODO
@@ -11,7 +11,6 @@ List of plugins to implement:
Taskbar (as in rarity)
Application menu
UPS
- Line reader
Blueshift integration
Backlight control
hdparm
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()
+