Improving code for Python 3

Signed-off-by: Dimitris Zlatanidis <d.zlatanidis@gmail.com>
This commit is contained in:
Dimitris Zlatanidis 2020-01-10 23:56:56 +01:00
parent a3605544f6
commit be3025797e
4 changed files with 24 additions and 29 deletions

View file

@ -40,7 +40,7 @@ def update_repositories(repositories, conf_path):
"""
Upadate with user custom repositories
"""
repo_file = "{0}custom-repositories".format(conf_path)
repo_file = f"{conf_path}custom-repositories"
if os.path.isfile(repo_file):
f = open(repo_file, "r")
repositories_list = f.read()
@ -90,11 +90,11 @@ class MetaData:
slack_rel = "stable"
# Configuration path
conf_path = "/etc/{0}/".format(__all__)
conf_path = f"/etc/{__all__}/"
# tmp paths
tmp = "/tmp/"
tmp_path = "{0}{1}/".format(tmp, __all__)
tmp_path = f"{tmp}{__all__}/"
# Default configuration values
_conf_slpkg = {
@ -138,8 +138,8 @@ class MetaData:
# read value from configuration file
repositories = []
for files in ["slpkg.conf", "repositories.conf"]:
if os.path.isfile("%s%s" % (conf_path, files)):
f = open("%s%s" % (conf_path, files), "r")
if os.path.isfile(f"{conf_path}{files}"):
f = open(f"{conf_path}{files}", "r")
conf = f.read()
f.close()
for line in conf.splitlines():
@ -217,8 +217,8 @@ class MetaData:
"ENDC": ""
}
CHECKSUMS_link = ("https://gitlab.com/{0}/{1}/raw/"
"master/CHECKSUMS.md5".format(__author__, __all__))
CHECKSUMS_link = (f"https://gitlab.com/{__author__}/{__all__}/raw/"
"master/CHECKSUMS.md5")
# file spacer
sp = "-"

View file

@ -30,8 +30,7 @@ from slpkg.slack.slack_version import slack_ver
def header():
"""help header message"""
print("\nslpkg - version {0} | Slackware release: {1} - {2}\n".format(
_meta_.__version__, _meta_.slack_rel, slack_ver()))
print(f"\nslpkg - version {_meta_.__version__} | Slackware release: {_meta_.slack_rel} - {slack_ver()}\n")
def options():
@ -220,11 +219,11 @@ def usage(repo):
all_repos = RepoList().all_repos.keys()
del RepoList().all_repos
if repo in all_repos:
error_repo = ("slpkg: Error: Repository '{0}' is not activated"
"\n".format(repo))
error_repo = (f"slpkg: Error: Repository '{repo}' is not activated"
"\n")
else:
error_repo = ("slpkg: Error: Repository '{0}' does not exist"
"\n".format(repo))
error_repo = (f"slpkg: Error: Repository '{repo}' does not exist"
"\n")
print("\n" + error_repo)
raise SystemExit(1)
print(usage.__doc__)

View file

@ -22,8 +22,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from slpkg.messages import Msg
from slpkg.__metadata__ import MetaData as _meta_
@ -36,6 +34,10 @@ class Auto:
def __init__(self, packages):
self.packages = packages
self.meta = _meta_
self.green = _meta_.color["GREEN"]
self.red = _meta_.color["RED"]
self.cyan = _meta_.color["CYAN"]
self.endc = _meta_.color["ENDC"]
self.msg = Msg()
self.commands = {
"i": "installpkg",
@ -54,10 +56,7 @@ class Auto:
print("| Choose a Slackware command:")
self.msg.template(78)
for com in sorted(self.commands):
print("| {0}{1}{2}) {3}{4}{5}".format(
self.meta.color["RED"], com, self.meta.color["ENDC"],
self.meta.color["GREEN"], self.commands[com],
self.meta.color["ENDC"]))
print(f"| {self.red}{com}{self.endc}) {self.green}{self.commands[com]}{self.endc}")
self.msg.template(78)
try:
self.choice = input(" > ")
@ -65,9 +64,7 @@ class Auto:
print()
raise SystemExit()
if self.choice in self.commands.keys():
print(" \x1b[1A{0}{1}{2}\n\n".format(
self.meta.color["CYAN"], self.commands[self.choice],
self.meta.color["ENDC"]), end="")
print(f" \x1b[1A{self.cyan}{self.commands[self.choice]}{self.endc}", end="\n\n")
print(end="", flush=True)
self.execute()

View file

@ -33,8 +33,10 @@ class BlackList:
"""Blacklist class to add, remove or listed packages
in blacklist file."""
def __init__(self):
self.meta = _meta_
self.quit = False
self.green = _meta_.color["GREEN"]
self.red = _meta_.color["RED"]
self.endc = _meta_.color["ENDC"]
self.blackfile = "/etc/slpkg/blacklist"
self.black_conf = ""
if os.path.isfile(self.blackfile):
@ -56,8 +58,7 @@ class BlackList:
print("\nPackages in the blacklist:\n")
for black in self.get_black():
if black:
print("{0}{1}{2}".format(self.meta.color["GREEN"], black,
self.meta.color["ENDC"]))
print(f"{self.green}{black}{self.endc}")
self.quit = True
if self.quit:
print() # new line at exit
@ -71,8 +72,7 @@ class BlackList:
with open(self.blackfile, "a") as black_conf:
for pkg in pkgs:
if pkg not in blacklist:
print("{0}{1}{2}".format(self.meta.color["GREEN"], pkg,
self.meta.color["ENDC"]))
print(f"{self.green}{pkg}{self.endc}")
black_conf.write(pkg + "\n")
self.quit = True
black_conf.close()
@ -88,8 +88,7 @@ class BlackList:
if line not in pkgs:
remove.write(line + "\n")
else:
print("{0}{1}{2}".format(self.meta.color["RED"], line,
self.meta.color["ENDC"]))
print(f"{self.red}{line}{self.endc}")
self.quit = True
remove.close()
if self.quit: