Improving code for Python 3

Signed-off-by: Dimitris Zlatanidis <d.zlatanidis@gmail.com>
This commit is contained in:
Dimitris Zlatanidis 2020-01-13 15:39:26 +01:00
parent c7f550d343
commit 7f3c597488
6 changed files with 51 additions and 75 deletions

View file

@ -779,8 +779,8 @@ def check_exists_repositories(repo):
if check_for_local_repos(repo) is True:
pkg_list = "PACKAGES.TXT"
return ""
if not os.path.isfile(f"{_meta_.lib_path}{repo}_repo/{pkg_list}"
# .format(_meta_.lib_path, repo, "_repo/{0}".format(pkg_list))):
if not os.path.isfile(f"{_meta_.lib_path}{repo}_repo/{pkg_list}"):
# .format(_meta_.lib_path, repo, "_repo/{0}".format(pkg_list))):
return repo
return ""

View file

@ -37,14 +37,12 @@ def library(repo):
pkg_list, packages = [], ""
if repo == "sbo":
if (os.path.isfile(
_meta_.lib_path + "{0}_repo/SLACKBUILDS.TXT".format(repo))):
packages = Utils().read_file(_meta_.lib_path + "{0}_repo/"
"SLACKBUILDS.TXT".format(repo))
_meta_.lib_path + f"{repo}_repo/SLACKBUILDS.TXT")):
packages = Utils().read_file(_meta_.lib_path + f"{repo}_repo/SLACKBUILDS.TXT")
else:
if (os.path.isfile(
_meta_.lib_path + "{0}_repo/PACKAGES.TXT".format(repo))):
packages = Utils().read_file(_meta_.lib_path + "{0}_repo/"
"PACKAGES.TXT".format(repo))
_meta_.lib_path + f"{repo}_repo/PACKAGES.TXT")):
packages = Utils().read_file(_meta_.lib_path + f"{repo}_repo/PACKAGES.TXT")
for line in packages.splitlines():
if repo == "sbo":
if line.startswith("SLACKBUILD NAME: "):

View file

@ -677,8 +677,7 @@ class ArgParse:
self.args.remove(arg)
break
if tag and tag not in colors:
print("\nslpkg: Error: Available colors {0}\n".format(
colors))
print(f"\nslpkg: Error: Available colors {colors}\n")
raise SystemExit()
if (len(self.args) == 3 and self.args[0] in options and
self.args[1] in self.meta.repositories and tag in colors):

View file

@ -32,32 +32,34 @@ class Msg:
"""
def __init__(self):
self.meta = _meta_
self.grey = _meta_.color["GREY"]
self.red = _meta_.color["RED"]
self.cyan = _meta_.color["CYAN"]
self.endc = _meta_.color["ENDC"]
def pkg_not_found(self, bol, pkg, message, eol):
"""Print message when package not found
"""
print("{0}No such package {1}: {2}{3}".format(bol, pkg, message, eol))
print(f"{bol}No such package {pkg}: {message}{eol}")
def pkg_found(self, prgnam):
"""Print message when package found
"""
print("| Package {0} is already installed".format(prgnam))
print(f"| Package {prgnam} is already installed")
def pkg_installed(self, pkg):
"""Print message when package installed
"""
print("| Package {0} installed".format(pkg))
print(f"| Package {pkg} installed")
def build_FAILED(self, prgnam):
"""Print error message if build failed
"""
self.template(78)
print("| Some error on the package {0} [ {1}FAILED{2} ]".format(
prgnam, self.meta.color["RED"], self.meta.color["ENDC"]))
print(f"| Some error on the package {prgnam} [ {self.red}FAILED{self.endc} ]")
self.template(78)
print("| See the log file in '{0}/var/log/slpkg/sbo/build_logs{1}' "
"directory or read the README file".format(
self.meta.color["CYAN"], self.meta.color["ENDC"]))
print(f"| See the log file in '{self.cyan}/var/log/slpkg/sbo/build_logs{self.endc}' "
f"directory or read the README file")
self.template(78)
print() # new line at end
@ -69,29 +71,22 @@ class Msg:
def checking(self):
"""Message checking
"""
print("{0}Checking...{1} ".format(self.meta.color["GREY"],
self.meta.color["ENDC"]), end="",
flush=True)
print(f"{self.grey}Checking...{self.endc} ", end="", flush=True)
def reading(self):
"""Message reading
"""
print("{0}Reading package lists...{1} ".format(
self.meta.color["GREY"], self.meta.color["ENDC"]), end="",
flush=True)
print(f"{self.grey}Reading package lists...{self.endc} ", end="", flush=True)
def resolving(self):
"""Message resolving
"""
print("{0}Resolving dependencies...{1} ".format(
self.meta.color["GREY"], self.meta.color["ENDC"]), end="",
flush=True)
print(f"{self.grey}Resolving dependencies...{self.endc} ", end="", flush=True)
def done(self):
"""Message done
"""
print("\b{0}Done{1}\n".format(self.meta.color["GREY"],
self.meta.color["ENDC"]), end="")
print(f"\b{self.grey}Done{self.endc}\n", end="")
def pkg(self, count):
"""Print singular plural
@ -135,12 +130,11 @@ class Msg:
"""
print()
self.template(78)
print("| {0}{1}*** WARNING ***{2}").format(
" " * 27, self.meta.color["RED"], self.meta.color["ENDC"])
print(f"| {' ' * 27}{self.red}*** WARNING ***{self.endc}")
self.template(78)
print("| Before proceed with the package '{0}' will you must read\n"
"| the README file. You can use the command "
"'slpkg -n {1}'").format(pkg, pkg)
print(f"| Before proceed with the package '{pkg}' will you must read\n"
f"| the README file. You can use the command "
f"'slpkg -n {pkg}'")
self.template(78)
print()
@ -149,23 +143,19 @@ class Msg:
and upgraded
"""
self.template(78)
print("| Total {0} {1} installed and {2} {3} upgraded".format(
len(install), self.pkg(len(install)),
len(upgrade), self.pkg(len(upgrade))))
print(f"| Total {len(install)} {self.pkg(len(install))} installed and "
f"{len(upgrade)} {self.pkg(len(upgrade))} upgraded")
self.template(78)
for installed, upgraded in itertools.zip_longest(install, upgrade):
if upgraded:
print("| Package {0} upgraded successfully".format(upgraded))
print(f"| Package {upgraded} upgraded successfully")
if installed:
print("| Package {0} installed successfully".format(installed))
print(f"| Package {installed} installed successfully")
self.template(78)
print()
def matching(self, packages):
"""Message for matching packages
"""
print("\nNot found package with the name [ {0}{1}{2} ]. "
"Matching packages:\nNOTE: Not dependenc"
"ies are resolved\n".format(self.meta.color["CYAN"],
"".join(packages),
self.meta.color["ENDC"]))
print(f"\nNot found package with the name [ {self.cyan}{''.join(packages)}{self.endc} ]. "
f"Matching packages:\nNOTE: Not dependencies are resolved\n")

View file

@ -51,11 +51,10 @@ class NewConfig:
"""
self.find_new()
for n in self.news:
print("{0}".format(n))
print(f"{n}")
print()
self.msg.template(78)
print("| Installed {0} new configuration files:".format(
len(self.news)))
print(f"| Installed {len(self.news)} new configuration files:")
self.msg.template(78)
self.choices()
@ -76,16 +75,12 @@ class NewConfig:
def choices(self):
"""Menu options for new configuration files
"""
print("| {0}K{1}{2}eep the old and .new files, no changes".format(
self.red, self.endc, self.br))
print("| {0}O{1}{2}verwrite all old configuration files with new "
"ones".format(self.red, self.endc, self.br))
print(f"| {self.red}K{self.endc}{self.br}eep the old and .new files, no changes")
print(f"| {self.red}O{self.endc}{self.br}verwrite all old configuration files with new ones")
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}P{1}{2}rompt K, O, R, D, M option for each single "
"file".format(self.red, self.endc, self.br))
print("| {0}Q{1}{2}uit from menu".format(self.red, self.endc, self.br))
print(f"| {self.red}R{self.endc}{self.br}emove all .new files")
print(f"| {self.red}P{self.endc}{self.br}rompt K, O, R, D, M option for each single file")
print(f"| {self.red}Q{self.endc}{self.br}uit from menu")
self.msg.template(78)
try:
choose = input("\nWhat would you like to do [K/O/R/P/Q]? ")
@ -141,7 +136,7 @@ class NewConfig:
"""Choose what do to file by file
"""
print()
prompt_ask = input("{0} [K/O/R/D/M/Q]? ".format(n))
prompt_ask = input(f"{n} [K/O/R/D/M/Q]? ")
print()
if prompt_ask in ("K", "k"):
self.keep()
@ -163,7 +158,7 @@ class NewConfig:
if os.path.isfile(n):
os.remove(n)
if not os.path.isfile(n):
print("File '{0}' removed".format(n))
print(f"File '{n}' removed")
def _overwrite(self, n):
"""Overwrite old file with new and keep file with suffix .old
@ -195,15 +190,15 @@ class NewConfig:
c += 1
if s1 != s2:
break
print("@@ -{0},{1} +{2},{3} @@\n".format(ln, c, ln, c))
print(f"@@ -{ln},{c} +{ln},{c} @@\n")
for line in lines[-3:]:
print("{0}".format(line))
print(f"{line}")
if a is None:
a = ""
print("{0}{1}{2}{3}".format(self.red, "-", self.endc, a))
print(f"{self.red}-{self.endc}{a}")
if b is None:
b = ""
print("{0}{1}{2}{3}".format(self.green, "+", self.endc, b))
print(f"{self.green}+{self.endc}{b}")
lines = []
c = 0
else:
@ -230,4 +225,4 @@ class NewConfig:
n.split("/")[-1], n[:-4].split("/")[-1]))
def quit(self):
raise SystemExit()
raise SystemExit()

View file

@ -45,11 +45,9 @@ class FindFromRepos:
def find(self, pkg, flag):
"""Start to find packages and print
"""
print("\nPackages with name matching [ {0}{1}{2} ]\n".format(
self.cyan, ", ".join(pkg), self.endc))
print(f"\nPackages with name matching [ {self.cyan}{', '.join(pkg)}{self.endc} ]\n")
Msg().template(78)
print("| {0} {1}{2}{3}".format("Repository", "Package", " " * 54,
"Size"))
print(f"| Repository Package {' ' * 54}Size")
Msg().template(78)
for repo in _meta_.repositories:
PACKAGES_TXT = PackageManager(pkg).list_lib(repo)
@ -69,16 +67,12 @@ class FindFromRepos:
self.cache = repo
self.count_pkg += 1
ver = self.sbo_version(repo, find)
print(" {0}{1}{2}{3}{4} {5}{6:>11}".format(
self.cyan, repo, self.endc,
" " * (12 - len(repo)),
find + ver, " " * (53 - len(find + ver)),
size))
print(f" {self.cyan}{repo}{self.endc}{' ' * (12 - len(repo))}{find + ver} "
f"{' ' * (53 -len(find + ver))}{size:>11}")
print("\nFound summary")
print("=" * 79)
print("{0}Total found {1} packages in {2} repositories."
"{3}\n".format(self.grey, self.count_pkg,
self.count_repo, self.endc))
print(f"{self.grey}Total found {self.count_pkg} packages in "
f"{self.count_repo} repositories.{self.endc}\n")
def sbo_version(self, repo, find):
"""