fix entries, add exception if file not exists

This commit is contained in:
Jakobus Schürz 2020-02-12 20:48:59 +01:00
parent 292817433e
commit 2c250a8428

View file

@ -199,23 +199,26 @@ def main(args):
# to use it with alias gowork and goarchive # to use it with alias gowork and goarchive
foundarchive=False foundarchive=False
foundwork=False foundwork=False
with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs', try:
inplace=True) as fh: with fileinput.input(os.environ['HOME']+'/.config/user-dirs.dirs',
for line in fh: inplace=True) as fh:
if line.startswith('XDG_ARCHIVE_DIR'): for line in fh:
print("XDG_ARCHIVE_DIR=\"${HOME}/"+args.archive+'"', end='\n') if line.startswith('XDG_ARCHIVE_DIR'):
foundarchive=True print("XDG_ARCHIVE_DIR=\"$HOME/"+args.archive+'"', end='\n')
elif line.startswith('XDG_WORK_DIR'): foundarchive=True
print("XDG_WORK_DIR=\"${HOME}/"+args.mountpoint+'"', end='\n') elif line.startswith('XDG_WORK_DIR'):
foundwork=True print("XDG_WORK_DIR=\"$HOME/"+args.mountpoint+'"', end='\n')
else: foundwork=True
print(line, end='') else:
print(line, end='')
except:
print("File not existing, create it: {}".format(os.environ['HOME']+'/.config/user-dirs.dirs'))
if not foundarchive: if not foundarchive:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: 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: if not foundwork:
with open(os.environ['HOME']+'/.config/user-dirs.dirs', 'a') as fh: 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 # start FUSE filesystem
FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True) FUSE(WorkdirFS(args), os.path.join(os.environ['HOME'], args.mountpoint), nothreads=True, foreground=True)