From 7da9cd3472479c50519f607a4c5d7c078f616211 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Mon, 16 Dec 2019 12:11:30 +0100 Subject: [PATCH] Use temp file to write changes safely, remove dupes with awk --- modules/vim/module.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/modules/vim/module.py b/modules/vim/module.py index e8b93dd..f689b1f 100644 --- a/modules/vim/module.py +++ b/modules/vim/module.py @@ -3,7 +3,6 @@ from subprocess import call import os import shutil - class VimModule(IModule): def __init__(self): super().__init__() @@ -42,14 +41,18 @@ class VimModule(IModule): vimrc_exists = os.path.isfile("testfile") if vimrc_exists: - with open("testfile", "r") as vimrc_in_file: - buf = vimrc_in_file.readlines() - - with open("testfile", "w") as vimrc_out_file: - for line in buf: - if line == "call plug#begin('~/.vim/plugged')\n": - line = line + plugins[selection][1] + "\n" - vimrc_out_file.write(line) + temp_file = open('temp', 'w') + with open("testfile", "r") as vimrc_out_file: + for line in vimrc_out_file: + if line.startswith("call plug#begin('~/.vim/plugged')"): + for element in clean_plugin_list: + selection = element - 1 + line = line.strip() + "\n" + plugins[selection][1] + "\n" + temp_file.write(line) + temp_file.close() + shutil.move('temp', 'testfile') + sorted_file = "awk -i inplace ' !x[$0]++' ~/Gitlab/linux-tools/testfile" + call(sorted_file, shell=True) else: with open("testfile", "w+") as vimrc_file: @@ -57,4 +60,4 @@ class VimModule(IModule): for element in clean_plugin_list: selection = element -1 vimrc_file.write(plugins[selection][1] + "\n") - vimrc_file.write("call plug#end()\n") + vimrc_file.write("call plug#end()\n") \ No newline at end of file