diff options
| author | Mattias Andrée <maandree@operamail.com> | 2013-07-25 19:34:11 +0200 |
|---|---|---|
| committer | Mattias Andrée <maandree@operamail.com> | 2013-07-25 19:34:11 +0200 |
| commit | b3ec6d5c41ea3545eaee3d2b1ec6f72a5bb80312 (patch) | |
| tree | 5971ab8feac11f235bb7c9dcce51eb2bb69d2738 | |
| parent | add touch to flocker (diff) | |
| download | pytagomacs-b3ec6d5c41ea3545eaee3d2b1ec6f72a5bb80312.tar.gz pytagomacs-b3ec6d5c41ea3545eaee3d2b1ec6f72a5bb80312.tar.bz2 pytagomacs-b3ec6d5c41ea3545eaee3d2b1ec6f72a5bb80312.tar.xz | |
print a tree
Signed-off-by: Mattias Andrée <maandree@operamail.com>
| -rwxr-xr-x | src/featherweight.py | 38 | ||||
| -rw-r--r-- | src/flocker.py | 6 |
2 files changed, 36 insertions, 8 deletions
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 <http://www.gnu.org/licenses/>. ''' +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) |
