From f9525c09642b9bf1d3ea6b40e4d4a266fde52ccf Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Mon, 3 Aug 2015 07:23:54 +0300 Subject: [PATCH] Update new config --- slpkg/new_config.py | 78 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/slpkg/new_config.py b/slpkg/new_config.py index df08da04..b385b97a 100644 --- a/slpkg/new_config.py +++ b/slpkg/new_config.py @@ -23,7 +23,9 @@ import os +import shutil +from messages import Msg from __metadata__ import MetaData as _meta_ @@ -33,7 +35,11 @@ class NewConfig(object): self.meta = _meta_ self.red = self.meta.color["RED"] self.endc = self.meta.color["ENDC"] - self.etc = "/etc/" + self.br = "" + if self.meta.use_colors in ["off", "OFF"]: + self.br = ")" + # self.etc = "/etc/" + self.etc = "/home/dslackw/Downloads/test/" self.news = [] def find_new(self): @@ -52,45 +58,81 @@ class NewConfig(object): self.find_new() for n in self.news: print("{0}".format(n)) - print("\nInstalled {0} new configuration files:\n".format( + print("") + Msg().template(78) + print("| Installed {0} new configuration files:".format( len(self.news))) + Msg().template(78) self.choices() def choices(self): """Menu options for new configuration files """ - br = "" - if self.meta.use_colors in ["off", "OFF"]: - br = ")" - print(" {0}O{1}{2}verwrite all old configuration files with new " - "ones".format(self.red, self.endc, br)) - print(" The old files will be saved with suffix .old") - print(" {0}R{1}{2}emove all .new files".format( - self.red, self.endc, br)) - print(" {0}P{1}{2}rompt O, R, option for each single file\n".format( - self.red, self.endc, br)) - choose = raw_input("What would you like to do [O/R/P]? ") + print("| {0}O{1}{2}verwrite all old configuration files with new " + "ones".format(self.red, self.endc, self.br)) + print("| The old files will be saved with suffix .old") + print("| {0}R{1}{2}emove all .new files".format( + self.red, self.endc, self.br)) + print("| {0}K{1}{2}eep the old and .new files, no changes".format( + self.red, self.endc, self.br)) + print("| {0}P{1}{2}rompt O, R, option for each single file".format( + self.red, self.endc, self.br)) + Msg().template(78) + choose = raw_input("\nWhat would you like to do [O/R/P]? ") if choose == "O": self.overwrite_all() elif choose == "R": self.remove_all() + elif choose == "K": + self.keep elif choose == "P": self.prompt() def overwrite_all(self): - pass + """Overwrite all .new files and keep + old with suffix .old + """ + for n in self.news: + if os.path.isfile(n[:-4]): + shutil.copy2(n[:-4], n[:-4] + ".old") + if os.path.isfile(n): + shutil.move(n, n[:-4]) def remove_all(self): - pass + """Remove all .new files + """ + for n in self.news: + if os.path.isfile(n): + os.remove(n) def prompt(self): - for p in self.news: - print("{0}".format(p)) + print("") + Msg().template(78) + print("| Choose what to do file by file:") + print("| {0}K{1}{2}epp, {3}O{4}{5}verwrite, {6}R{7}{8}emove, " + "{9}D{10}{11}iff, {12}M{13}{14}erge".format( + self.red, self.endc, self.br, self.red, self.endc, self.br, + self.red, self.endc, self.br, self.red, self.endc, self.br, + self.red, self.endc, self.br)) + Msg().template(78) + for n in self.news: + self.question(n) + + def question(self, n): + prompt_ask = raw_input("{0} [K/O/R/D/M]? ".format(n)) + if prompt_ask == "O": + self._overwrite(n) def _remove(self): pass - def _overwrite(self): + def _overwrite(self, n): + if os.path.isfile(n[:-4]): + shutil.copy2(n[:-4], n[:-4] + ".old") + if os.path.isfile(n): + shutil.move(n, n[:-4]) + + def keep(self): pass NewConfig().view_new()