Change vimrc content in code not on disk

Remove shutil
Remove awk bash command
Fix typo
This commit is contained in:
Lucas Noki 2019-12-22 07:49:51 +01:00 committed by Marcel Schwarz
parent 04e6f9b160
commit 381d01cb82

View File

@ -2,7 +2,6 @@ from AbstractModule import AbstractModule
from subprocess import call from subprocess import call
from PyInquirer import prompt from PyInquirer import prompt
import os import os
import shutil
import json import json
@ -13,7 +12,7 @@ class VimModule(AbstractModule):
with open("modules/vim/vim_plugins.json") as plugins: with open("modules/vim/vim_plugins.json") as plugins:
self.plugins = json.load(plugins)['plugins'] self.plugins = json.load(plugins)['plugins']
def do_module_selection(self): def do_plugin_selection(self):
mapped_list = [] mapped_list = []
for choice in self.plugins.keys(): for choice in self.plugins.keys():
mapped_list.append({ mapped_list.append({
@ -33,42 +32,40 @@ class VimModule(AbstractModule):
def run(self): def run(self):
selected_modules = self.do_module_selection() selected_plugins = self.do_plugin_selection()
# print out selected plugins # print out selected plugins
[print(x) for x in selected_modules] [print(x) for x in selected_plugins]
# Create vimrc file # Create vimrc file
vimrc_exists = os.path.isfile("testfile") try:
with open("vim_testfile", "r") as vim_file:
vimrc_content = vim_file.readlines()
except FileNotFoundError:
vimrc_content = []
if vimrc_exists: try:
temp_file = open('temp', 'w') index = vimrc_content.index("call plug#begin('~/.vim/plugged')\n") + 1
with open("testfile", "r") as vimrc_out_file: print("Index of plugman " + str(index))
for line in vimrc_out_file: for plugin in selected_plugins:
if line.startswith("call plug#begin('~/.vim/plugged')"): vimrc_content.insert(index, self.plugins[plugin] + "\n")
for element in selected_modules: except ValueError:
line = line.strip() + "\n" + self.plugins[element] + "\n" vimrc_content.append("call plug#begin('~/.vim/plugged')\n")
temp_file.write(line) for element in selected_plugins:
temp_file.close() vimrc_content.append(self.plugins[element] + "\n")
shutil.move('temp', 'testfile') vimrc_content.append("call plug#end()\n")
# Merge duplicates to first ocassion with awk seen = set()
sorted_file = "awk -i inplace ' !x[$0]++' ~/Gitlab/linux-tools/testfile" seen_add = seen.add
call(sorted_file, shell=True) vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))]
else: with open("vim_testfile", "w") as vimrc_file:
with open("testfile", "w+") as vimrc_file: for line in vimrc_content:
vimrc_file.write("\n" + "call plug#begin('~/.vim/plugged')\n") vimrc_file.write(line)
for element in selected_modules:
vimrc_file.write(self.plugins[element] + "\n")
vimrc_file.write("call plug#end()\n")
vimdir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/")
autoloaddir_exists = os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/autoload/")
# Create necessary folder structure for Plugin Manager # Create necessary folder structure for Plugin Manager
if not autoloaddir_exists: if not os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/autoload/"):
if vimdir_exists: if os.path.isdir("/home/clay/Gitlab/linux-tools/testdirectory/"):
install_vimplug = "cd ~/Gitlab/linux-tools/testdirectory/ " \ install_vimplug = "cd ~/Gitlab/linux-tools/testdirectory/ " \
"&& mkdir autoload " \ "&& mkdir autoload " \
"&& cd autoload " \ "&& cd autoload " \