From 36edc4c1833d361767bd4bdaa07cb90bd513c653 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sun, 2 Aug 2020 03:50:50 +0200 Subject: [PATCH] Remove class structure from vim module --- main.py | 4 +- modules/vim/VimModule.py | 104 --------------------------------------- modules/vim/__init__.py | 92 ++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 106 deletions(-) delete mode 100644 modules/vim/VimModule.py diff --git a/main.py b/main.py index 41c80e8..00c1d86 100644 --- a/main.py +++ b/main.py @@ -7,8 +7,8 @@ import modules.helloworld as helloworld import modules.install as install import modules.systemupdate as update import modules.teamspeak as teamspeak +import modules.vim as vim from modules.swap.SwapModule import SwapModule -from modules.vim.VimModule import VimModule modules = { "bashrc": bashrc.functions, @@ -19,7 +19,7 @@ modules = { "swap": SwapModule().run, "update": update.functions, "teamspeak": teamspeak.functions, - "vim": VimModule().run, + "vim": vim.functions, } fire.Fire(modules) diff --git a/modules/vim/VimModule.py b/modules/vim/VimModule.py deleted file mode 100644 index 8cf26ce..0000000 --- a/modules/vim/VimModule.py +++ /dev/null @@ -1,104 +0,0 @@ -import json -import os -import subprocess -from pathlib import Path - -from PyInquirer import prompt - - -class VimModule: - def __init__(self): - super().__init__() - self._debugFlag = False - self._json_config = {} - with open("modules/vim/vimrc_conf.json") as plugins: - self._json_config = json.load(plugins) - - def _get_vim_root(self): - if self._debugFlag: - return Path(os.getcwd()).joinpath("devenv", ".vim") - else: - return Path().home().joinpath(".vim") - - def _create_folder_structure(self): - os.makedirs(str(self._get_vim_root().joinpath("autoload")), exist_ok=True) - os.makedirs(str(self._get_vim_root().joinpath("plugged")), exist_ok=True) - - def _get_vimplug_file(self): - vimplug_filepath = Path(str(self._get_vim_root().joinpath("autoload"))) - print(vimplug_filepath) - if os.path.isfile(str(self._get_vim_root().joinpath("autoload", "plug.vim"))): - print("Vimplug already installed!") - else: - curl_request = "curl {} -o {}".format( - self._json_config['pluginmanager_url'], - str(self._get_vim_root().joinpath("autoload", "plug.vim")) - ) - subprocess.call(curl_request, shell=True) - - def _create_plugin_section(self): - vimrc_content = [] - plugins = self._json_config['plugins'] - print("Available Plugins:") - plugin_selection = [ - { - 'type': 'checkbox', - 'message': 'Select plugins', - 'name': 'modules', - 'choices': list(map(lambda x: {"name": x}, plugins.keys())) - } - ] - selected_plugins = prompt(plugin_selection)['modules'] - if len(selected_plugins) > 0: - self._get_vimplug_file() - print("\033[4mYour selection:\033[0m") - [print(x) for x in selected_plugins] - - vimrc_content.append("call plug#begin('~/.vim/plugged')\n") - for element in selected_plugins: - vimrc_content.append(plugins[element] + "\n") - vimrc_content.append("call plug#end()\n") - - return vimrc_content - - def _create_setting_section(self): - default_settings = list(self._json_config['settings'].values()) - - print("\n\033[4mDefault settings:\033[0m") - [print(i) for i in default_settings] - - vimrc_content = list(map(lambda x: x + "\n", default_settings)) - return vimrc_content - - def _get_vimfile_working_copy(self): - vimrc_path = str(self._get_vim_root().joinpath(".vimrc")) - try: - with open(vimrc_path, "r") as vimrc_file: - vimrc_content = vimrc_file.readlines() - except FileNotFoundError: - vimrc_content = [] - - return vimrc_content - - def _write_vimfile(self, vimrc_content): - seen = set() - seen_add = seen.add - vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))] - - vimrc_path = str(self._get_vim_root().joinpath(".vimrc")) - with open(vimrc_path, "w") as vimrc_file: - for line in vimrc_content: - vimrc_file.write(line) - - def _exec_plugin_manager(self): - install_plugins = "vim +PlugInstall +qall +silent" - subprocess.call(install_plugins, shell=True) - - def run(self, debug=False): - if debug: - self._debugFlag = True - self._create_folder_structure() - plugin_section = self._create_plugin_section() - settings_section = self._create_setting_section() - self._write_vimfile(settings_section + plugin_section) - self._exec_plugin_manager() diff --git a/modules/vim/__init__.py b/modules/vim/__init__.py index e69de29..48b4b1b 100644 --- a/modules/vim/__init__.py +++ b/modules/vim/__init__.py @@ -0,0 +1,92 @@ +import json +import os +import subprocess +from pathlib import Path + +from PyInquirer import prompt + +vim_root = Path().home().joinpath(".vim") +vimrc_path = str(Path().home().joinpath(".vimrc")) +json_config = {} + + +def create_plugin_section(): + plugins = json_config['plugins'] + print("Available Plugins:") + plugin_selection = [ + { + 'type': 'checkbox', + 'message': 'Select plugins', + 'name': 'modules', + 'choices': list(map(lambda x: {"name": x}, plugins.keys())) + } + ] + selected_plugins = prompt(plugin_selection)['modules'] + print("\033[4mYour selection:\033[0m") + [print(x) for x in selected_plugins] + + # Install vimplug if plugins selected + vimplug_path = str(vim_root.joinpath("autoload", "plug.vim")) + if selected_plugins and not os.path.isfile(vimplug_path): + curl_request = f"curl {json_config['pluginmanager_url']} -o {vimplug_path}" + subprocess.call(curl_request, shell=True) + print("Pluginmanager was set up") + + vimrc_content = ["call plug#begin('~/.vim/plugged')\n"] + for element in selected_plugins: + vimrc_content.append(plugins[element] + "\n") + vimrc_content.append("call plug#end()\n") + return vimrc_content + + +def create_setting_section(): + default_settings = list(json_config['settings'].values()) + + print("\n\033[4mDefault settings:\033[0m") + [print(i) for i in default_settings] + + vimrc_content = list(map(lambda x: x + "\n", default_settings)) + return vimrc_content + + +def write_vimfile(vimrc_content): + seen = set() + seen_add = seen.add + vimrc_content = [x1 for x1 in vimrc_content if not (x1 in seen or seen_add(x1))] + + with open(vimrc_path, "w") as vimrc_file: + for line in vimrc_content: + vimrc_file.write(line) + + +# Unused +def _get_vimfile_working_copy(): + try: + with open(vimrc_path, "r") as vimrc_file: + vimrc_content = vimrc_file.readlines() + except FileNotFoundError: + vimrc_content = [] + return vimrc_content + + +def run(): + global json_config + with open("modules/vim/vimrc_conf.json", "r") as config: + json_config = json.load(config) + + # Create folders + os.makedirs(str(vim_root.joinpath("autoload")), exist_ok=True) + os.makedirs(str(vim_root.joinpath("plugged")), exist_ok=True) + + plugin_section = create_plugin_section() + settings_section = create_setting_section() + + write_vimfile(settings_section + plugin_section) + + # Exec plugin manager + install_plugins = "vim +PlugInstall +qall" + subprocess.call(install_plugins, shell=True) + print("Module finished successfully!") + + +functions = run