Update new config

This commit is contained in:
Dimitris Zlatanidis 2015-08-03 17:00:16 +03:00
parent 1749ebbae8
commit 857a03c14f

View file

@ -82,7 +82,11 @@ class NewConfig(object):
print("| {0}P{1}{2}rompt O, R, option for each single file".format( print("| {0}P{1}{2}rompt O, R, option for each single file".format(
self.red, self.endc, self.br)) self.red, self.endc, self.br))
Msg().template(78) Msg().template(78)
try:
choose = raw_input("\nWhat would you like to do [O/R/P]? ") choose = raw_input("\nWhat would you like to do [O/R/P]? ")
except KeyboardInterrupt:
print("")
raise SystemExit()
if choose == "O": if choose == "O":
self.overwrite_all() self.overwrite_all()
elif choose == "R": elif choose == "R":
@ -97,10 +101,7 @@ class NewConfig(object):
old with suffix .old old with suffix .old
""" """
for n in self.news: for n in self.news:
if os.path.isfile(n[:-4]): self._overwrite(n)
shutil.copy2(n[:-4], n[:-4] + ".old")
if os.path.isfile(n):
shutil.move(n, n[:-4])
def remove_all(self): def remove_all(self):
"""Remove all .new files """Remove all .new files
@ -110,6 +111,8 @@ class NewConfig(object):
os.remove(n) os.remove(n)
def prompt(self): def prompt(self):
"""Select file
"""
print("") print("")
Msg().template(78) Msg().template(78)
print("| Choose what to do file by file:") print("| Choose what to do file by file:")
@ -119,20 +122,38 @@ class NewConfig(object):
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)) self.red, self.endc, self.br))
Msg().template(78) Msg().template(78)
for n in self.news: self.i = 0
self.question(n) try:
while self.i < len(self.news):
self.question(self.news[self.i])
self.i += 1
except KeyboardInterrupt:
print("")
raise SystemExit()
def question(self, n): def question(self, n):
"""Choose what do to file by file
"""
prompt_ask = raw_input("{0} [K/O/R/D/M]? ".format(n)) prompt_ask = raw_input("{0} [K/O/R/D/M]? ".format(n))
if prompt_ask == "D": if prompt_ask == "K":
self.diff(n) self.keep()
elif prompt_ask == "O": elif prompt_ask == "O":
self._overwrite(n) self._overwrite(n)
elif prompt_ask == "R":
self._remove(n)
elif prompt_ask == "D":
self.diff(n)
self.i -= 1
def _remove(self): def _remove(self, n):
pass """Remove one single file
"""
if os.path.isfile(n):
os.remove(n)
def _overwrite(self, n): def _overwrite(self, n):
"""Overwrite old file with new and keep file with suffix .old
"""
if os.path.isfile(n[:-4]): if os.path.isfile(n[:-4]):
shutil.copy2(n[:-4], n[:-4] + ".old") shutil.copy2(n[:-4], n[:-4] + ".old")
if os.path.isfile(n): if os.path.isfile(n):
@ -144,10 +165,11 @@ class NewConfig(object):
def diff(self, n): def diff(self, n):
"""Print the differences between the two files """Print the differences between the two files
""" """
if os.path.isfile(n[:-4]):
diff1 = Utils().read_file(n[:-4]).splitlines() diff1 = Utils().read_file(n[:-4]).splitlines()
if os.path.isfile(n):
diff2 = Utils().read_file(n).splitlines() diff2 = Utils().read_file(n).splitlines()
lines = [] lines, l, c = [], 0, 0
l, c = 0, 0
for a, b in itertools.izip_longest(diff1, diff2): for a, b in itertools.izip_longest(diff1, diff2):
l += 1 l += 1
if a != b: if a != b: