Use temp file to write changes safely, remove dupes with awk

This commit is contained in:
Lucas Noki 2019-12-16 12:11:30 +01:00 committed by Marcel Schwarz
parent 0c6744e378
commit 7da9cd3472

View File

@ -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")