Use temp file to write changes safely, remove dupes with awk
This commit is contained in:
parent
0c6744e378
commit
7da9cd3472
@ -3,7 +3,6 @@ from subprocess import call
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
|
||||||
class VimModule(IModule):
|
class VimModule(IModule):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -42,14 +41,18 @@ class VimModule(IModule):
|
|||||||
vimrc_exists = os.path.isfile("testfile")
|
vimrc_exists = os.path.isfile("testfile")
|
||||||
|
|
||||||
if vimrc_exists:
|
if vimrc_exists:
|
||||||
with open("testfile", "r") as vimrc_in_file:
|
temp_file = open('temp', 'w')
|
||||||
buf = vimrc_in_file.readlines()
|
with open("testfile", "r") as vimrc_out_file:
|
||||||
|
for line in vimrc_out_file:
|
||||||
with open("testfile", "w") as vimrc_out_file:
|
if line.startswith("call plug#begin('~/.vim/plugged')"):
|
||||||
for line in buf:
|
for element in clean_plugin_list:
|
||||||
if line == "call plug#begin('~/.vim/plugged')\n":
|
selection = element - 1
|
||||||
line = line + plugins[selection][1] + "\n"
|
line = line.strip() + "\n" + plugins[selection][1] + "\n"
|
||||||
vimrc_out_file.write(line)
|
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:
|
else:
|
||||||
with open("testfile", "w+") as vimrc_file:
|
with open("testfile", "w+") as vimrc_file:
|
||||||
@ -57,4 +60,4 @@ class VimModule(IModule):
|
|||||||
for element in clean_plugin_list:
|
for element in clean_plugin_list:
|
||||||
selection = element -1
|
selection = element -1
|
||||||
vimrc_file.write(plugins[selection][1] + "\n")
|
vimrc_file.write(plugins[selection][1] + "\n")
|
||||||
vimrc_file.write("call plug#end()\n")
|
vimrc_file.write("call plug#end()\n")
|
Loading…
Reference in New Issue
Block a user