From 2c250a842823ac82c9b61f52fe5c5d5c26599d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakobus=20Sch=C3=BCrz?= Date: Wed, 12 Feb 2020 20:48:59 +0100 Subject: [PATCH] fix entries, add exception if file not exists --- workdirfs.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/workdirfs.py b/workdirfs.py index a2d273c..b69a144 100755 --- a/workdirfs.py +++ b/workdirfs.py @@ -199,23 +199,26 @@ def main(args): # to use it with alias gowork and goarchive foundarchive=False foundwork=False - with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs', - inplace=True) as fh: - for line in fh: - if line.startswith('XDG_ARCHIVE_DIR'): - print("XDG_ARCHIVE_DIR=\"${HOME}/"+args.archive+'"', end='\n') - foundarchive=True - elif line.startswith('XDG_WORK_DIR'): - print("XDG_WORK_DIR=\"${HOME}/"+args.mountpoint+'"', end='\n') - foundwork=True - else: - print(line, end='') + try: + with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs', + inplace=True) as fh: + for line in fh: + if line.startswith('XDG_ARCHIVE_DIR'): + print("XDG_ARCHIVE_DIR=\"$HOME/"+args.archive+'"', end='\n') + foundarchive=True + elif line.startswith('XDG_WORK_DIR'): + print("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"', end='\n') + foundwork=True + else: + print(line, end='') + except: + print("File not existing, create it: {}".format(os.environ['HOME']+'/.config/user-dirs.dirs')) if not foundarchive: with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: - fh.write("XDG_ARCHIVE_DIR=\"${HOME}/"+args.archive+'"\n') + fh.write("XDG_ARCHIVE_DIR=\"$HOME/"+args.archive+'"\n') if not foundwork: with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: - fh.write("XDG_WORK_DIR=\"${HOME}/"+args.mountpoint+'"\n') + fh.write("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"\n') # start FUSE filesystem FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True)