Updated for type hinting

This commit is contained in:
Dimitris Zlatanidis 2023-04-26 18:24:45 +03:00
parent c6daead1c5
commit b938706990

View file

@ -5,6 +5,7 @@ import sys
import shutil import shutil
import difflib import difflib
import subprocess import subprocess
from typing import Any
from pathlib import Path from pathlib import Path
@ -38,7 +39,7 @@ class NewConfigs:
self.choice = None self.choice = None
def set_no_colors(self): def set_no_colors(self) -> None:
if '--no-colors' in self.options: if '--no-colors' in self.options:
self.bold: str = '' self.bold: str = ''
self.red: str = '' self.red: str = ''
@ -53,7 +54,7 @@ class NewConfigs:
self.violet: str = '' self.violet: str = ''
self.endc: str = '' self.endc: str = ''
def check(self): def check(self) -> None:
""" Checks for .new files. """ """ Checks for .new files. """
print('\nChecking for NEW configuration files...') print('\nChecking for NEW configuration files...')
if (self.slpkg_config_new.is_file() or self.blacklist_config_new.is_file() if (self.slpkg_config_new.is_file() or self.blacklist_config_new.is_file()
@ -84,11 +85,11 @@ class NewConfigs:
and not self.repositories_config_new.is_file()): and not self.repositories_config_new.is_file()):
print(f"\n{'No .new files found.':>23}\n") print(f"\n{'No .new files found.':>23}\n")
def menu(self): def menu(self) -> None:
""" Menu of choices. """ """ Menu of choices. """
choice = input('Choice: ') choice: str = input('Choice: ')
choice = choice.lower() choice: str = choice.lower()
arguments: dict = { arguments: dict = {
'k': self.keep, 'k': self.keep,
@ -103,10 +104,10 @@ class NewConfigs:
self.keep() self.keep()
@staticmethod @staticmethod
def keep(): def keep() -> None:
print("\nNo changes were made.\n") print("\nNo changes were made.\n")
def overwrite(self): def overwrite(self) -> None:
""" Copy tne .new files and rename the olds to .orig. """ """ Copy tne .new files and rename the olds to .orig. """
if self.slpkg_config_new.is_file(): if self.slpkg_config_new.is_file():
self.overwrite_config_file() self.overwrite_config_file()
@ -118,7 +119,7 @@ class NewConfigs:
self.overwrite_blacklist_file() self.overwrite_blacklist_file()
print() # new line print() # new line
def overwrite_config_file(self): def overwrite_config_file(self) -> None:
""" Copy tne slpkg.toml.new file and rename the old to .orig. """ """ Copy tne slpkg.toml.new file and rename the old to .orig. """
if self.slpkg_config.is_file(): if self.slpkg_config.is_file():
shutil.copy(self.slpkg_config, f"{self.slpkg_config}.orig") shutil.copy(self.slpkg_config, f"{self.slpkg_config}.orig")
@ -127,7 +128,7 @@ class NewConfigs:
shutil.move(self.slpkg_config_new, self.slpkg_config) shutil.move(self.slpkg_config_new, self.slpkg_config)
print(f"mv {self.slpkg_config_new} -> {self.green}{self.slpkg_config}{self.endc}") print(f"mv {self.slpkg_config_new} -> {self.green}{self.slpkg_config}{self.endc}")
def overwrite_repositories_file(self): def overwrite_repositories_file(self) -> None:
""" Copy tne repositories.toml.new file and rename the old to .orig. """ """ Copy tne repositories.toml.new file and rename the old to .orig. """
if self.slpkg_config.is_file(): if self.slpkg_config.is_file():
shutil.copy(self.repositories_config, f"{self.repositories_config}.orig") shutil.copy(self.repositories_config, f"{self.repositories_config}.orig")
@ -136,7 +137,7 @@ class NewConfigs:
shutil.move(self.repositories_config_new, self.repositories_config) shutil.move(self.repositories_config_new, self.repositories_config)
print(f"mv {self.repositories_config_new} -> {self.green}{self.repositories_config}{self.endc}") print(f"mv {self.repositories_config_new} -> {self.green}{self.repositories_config}{self.endc}")
def overwrite_blacklist_file(self): def overwrite_blacklist_file(self) -> None:
""" Copy tne blacklist.toml.new file and rename the old to .orig. """ """ Copy tne blacklist.toml.new file and rename the old to .orig. """
if self.blacklist_config.is_file(): if self.blacklist_config.is_file():
shutil.copy(self.blacklist_config, f"{self.blacklist_config}.orig") shutil.copy(self.blacklist_config, f"{self.blacklist_config}.orig")
@ -145,7 +146,7 @@ class NewConfigs:
shutil.move(self.blacklist_config_new, self.blacklist_config) shutil.move(self.blacklist_config_new, self.blacklist_config)
print(f"mv {self.blacklist_config_new} -> {self.green}{self.blacklist_config}{self.endc}") print(f"mv {self.blacklist_config_new} -> {self.green}{self.blacklist_config}{self.endc}")
def remove(self): def remove(self) -> None:
""" Removes the .new files. """ """ Removes the .new files. """
print() # new line print() # new line
self.remove_config_new_file() self.remove_config_new_file()
@ -153,25 +154,25 @@ class NewConfigs:
self.remove_blacklist_new_file() self.remove_blacklist_new_file()
print() # new line print() # new line
def remove_config_new_file(self): def remove_config_new_file(self) -> None:
""" Remove slpkg.toml.new file. """ """ Remove slpkg.toml.new file. """
if self.slpkg_config_new.is_file(): if self.slpkg_config_new.is_file():
self.slpkg_config_new.unlink() self.slpkg_config_new.unlink()
print(f"rm {self.red}{self.slpkg_config_new}{self.endc}") print(f"rm {self.red}{self.slpkg_config_new}{self.endc}")
def remove_repositories_new_file(self): def remove_repositories_new_file(self) -> None:
""" Remove repositories.toml.new file. """ """ Remove repositories.toml.new file. """
if self.repositories_config_new.is_file(): if self.repositories_config_new.is_file():
self.repositories_config_new.unlink() self.repositories_config_new.unlink()
print(f"rm {self.red}{self.repositories_config_new}{self.endc}") print(f"rm {self.red}{self.repositories_config_new}{self.endc}")
def remove_blacklist_new_file(self): def remove_blacklist_new_file(self) -> None:
""" Remove blacklist.toml.new file. """ """ Remove blacklist.toml.new file. """
if self.blacklist_config_new.is_file(): if self.blacklist_config_new.is_file():
self.blacklist_config_new.unlink() self.blacklist_config_new.unlink()
print(f"rm {self.red}{self.blacklist_config_new}{self.endc}") print(f"rm {self.red}{self.blacklist_config_new}{self.endc}")
def prompt(self): def prompt(self) -> None:
""" Prompt K, O, R selection for every single file. """ """ Prompt K, O, R selection for every single file. """
print(f"\n{'':>2}({self.byellow}K{self.endc})eep, ({self.byellow}O{self.endc})verwrite, " print(f"\n{'':>2}({self.byellow}K{self.endc})eep, ({self.byellow}O{self.endc})verwrite, "
f"({self.byellow}R{self.endc})emove, ({self.byellow}D{self.endc})iff, " f"({self.byellow}R{self.endc})emove, ({self.byellow}D{self.endc})iff, "
@ -238,7 +239,7 @@ class NewConfigs:
self.vimdiff(self.blacklist_config_new, self.blacklist_config) self.vimdiff(self.blacklist_config_new, self.blacklist_config)
@staticmethod @staticmethod
def diff_files(file2, file1): def diff_files(file2: Any, file1: Any) -> None:
""" Diff the .new and the current file. """ """ Diff the .new and the current file. """
with open(file1, 'r') as f1: with open(file1, 'r') as f1:
with open(file2, 'r') as f2: with open(file2, 'r') as f2:
@ -252,14 +253,14 @@ class NewConfigs:
print(line, end='') print(line, end='')
@staticmethod @staticmethod
def vimdiff(file1, file2): def vimdiff(file1: Any, file2: Any) -> None:
output = subprocess.call(f'vimdiff {file1} {file2}', shell=True) output = subprocess.call(f'vimdiff {file1} {file2}', shell=True)
if output != 0: if output != 0:
raise SystemExit(output) raise SystemExit(output)
def main(): def main():
args = sys.argv args: list = sys.argv
args.pop(0) args.pop(0)
options: list = [ options: list = [