From b3ec6d5c41ea3545eaee3d2b1ec6f72a5bb80312 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 25 Jul 2013 19:34:11 +0200 Subject: print a tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/featherweight.py | 38 +++++++++++++++++++++++++++++++++----- src/flocker.py | 6 +++--- 2 files changed, 36 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/featherweight.py b/src/featherweight.py index 3a6c790..38276ae 100755 --- a/src/featherweight.py +++ b/src/featherweight.py @@ -18,16 +18,44 @@ 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 import sys +from subprocess import Popen, PIPE -from parser import * +from flocker import * -feed = sys.argv[1] -with open(feed, 'r') as file: - feed = file.read() +height_width = Popen('stty size'.split(' '), stdout = PIPE, stderr = PIPE).communicate()[0] +(height, width) = height_width.decode('utf-8', 'error')[:-1].split(' ') -print(parse_feed(feed)) +home = os.environ['HOME'] +root = '%s/.featherweight' % home +if not os.path.exists(root): + os.makedirs(root) +islinux = ('TERM' not in os.environ) or (os.environ['TERM'] == 'linux') + +feeds = None +with touch('%s/feeds' % root) as feeds_flock: + flock(feeds_flock, False) + with open('%s/feeds' % root, 'rb') as file: + feeds = file.read().decode('utf-8', 'error') + if len(feeds) == 0: + feeds = '[]' + feeds = eval(feeds) + unflock(feeds_flock) + + +def print_node(feed, last, indent): + title = feed['title'] + print(indent + ('└' if last else '├') + ('── ' if islinux else '─╼ ') + title) + if 'inner' in feed: + inner = feed['inner'] + for feed in inner: + print_node(feed, feed is inner[-1], indent + (' ' if last else '│ ')) + +print('My Feeds') +for feed in feeds: + print_node(feed, feed is feeds[-1], '') diff --git a/src/flocker.py b/src/flocker.py index 4503354..ff1dccf 100644 --- a/src/flocker.py +++ b/src/flocker.py @@ -28,10 +28,10 @@ def touch(file): def flock(file, exclusive, nonblocking = False): - locktype = (fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) | (fcntl.LOCK_NB if nonblocking else 0) - fcntl(file.fileno(), locktype) + locktype = (LOCK_fcntl.EX if exclusive else fcntl.LOCK_SH) | (fcntl.LOCK_NB if nonblocking else 0) + fcntl.fcntl(file.fileno(), locktype) def unflock(file): - fcntl(file.fileno(), LOCK_UN) + fcntl.fcntl(file.fileno(), fcntl.LOCK_UN) -- cgit v1.2.3-70-g09d2