Updated for pathlib

This commit is contained in:
Dimitris Zlatanidis 2022-12-30 20:49:39 +02:00
parent 55b5c4a557
commit 7472db4471
8 changed files with 28 additions and 29 deletions

View file

@ -1,8 +1,8 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import tomli
from pathlib import Path
from slpkg.configs import Configs
@ -15,7 +15,7 @@ class Blacklist:
def get(self) -> list:
""" Reads the blacklist file. """
file = os.path.join(self.configs.etc_path, 'blacklist.toml')
if os.path.isfile(file):
file = Path(self.configs.etc_path, 'blacklist.toml')
if file.is_file():
with open(file, 'rb') as black:
return tomli.load(black)['blacklist']['packages']

View file

@ -26,7 +26,7 @@ class CheckUpdates:
repo = http.request(
'GET', f'{self.configs.sbo_repo_url}/{self.configs.sbo_chglog_txt}')
if os.path.isfile(local_chg_txt):
if local_chg_txt.is_file():
local_date = int(os.stat(local_chg_txt).st_size)
repo_date = int(repo.headers['Content-Length'])

View file

@ -75,6 +75,6 @@ class Check:
def database(self):
""" Checking for empty table """
db = Path(self.configs.db_path, self.configs.database)
if not SBoQueries('').sbos() or not os.path.isfile(db):
if not SBoQueries('').sbos() or not db.is_file():
raise SystemExit('\nYou need to update the package lists first.\n'
'Please run slpkg update.\n')

View file

@ -14,9 +14,9 @@ class LoadConfigs:
def file(path: str, file: str) -> dict:
try:
""" Load the configs from the file. """
config_file = Path(path, f'{file}.toml')
if os.path.isfile(config_file):
with open(config_file, 'rb') as conf:
config_path_file = Path(path, f'{file}.toml')
if config_path_file.exists():
with open(config_path_file, 'rb') as conf:
return tomli.load(conf)
except (KeyError, tomli.TOMLDecodeError) as error:
raise SystemExit(f"\nError: {error}: in the configuration file "
@ -106,18 +106,18 @@ class Configs:
# Dialog utility
dialog: str = config['dialog']
# Creating the paths if not exists
paths = [tmp_slpkg,
build_path,
download_only,
sbo_repo_path,
lib_path,
etc_path,
db_path]
# Creating the paths if not exists
paths = [tmp_slpkg,
build_path,
download_only,
sbo_repo_path,
lib_path,
etc_path,
db_path]
for path in paths:
if not os.path.isdir(path):
os.makedirs(path)
for path in paths:
if not os.path.isdir(path):
os.makedirs(path)
@classmethod
def colour(cls):

View file

@ -23,7 +23,7 @@ class FormConfigs:
elements = []
config_file = Path(self.configs.etc_path, f'{self.configs.prog_name}.toml')
if os.path.isfile(config_file):
if config_file.is_file():
# Load the toml config file.
configs = self.load_configs.file(self.configs.etc_path,
self.configs.prog_name)

View file

@ -171,7 +171,7 @@ class Slackbuilds:
""" Patching SBo TAG from the configuration file. """
sbo_script = Path(self.configs.build_path, sbo, f'{sbo}.SlackBuild')
if os.path.isfile(sbo_script):
if sbo_script.is_file():
with open(sbo_script, 'r', encoding='utf-8') as f:
lines = f.readlines()

View file

@ -1,7 +1,6 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
from pathlib import Path
from slpkg.downloader import Wget
@ -52,8 +51,8 @@ class UpdateRepository:
def delete_file(folder: str, txt_file: str):
""" Delete the file. """
file = Path(folder, txt_file)
if os.path.exists(file):
os.remove(file)
if file.exists():
file.unlink()
def delete_sbo_data(self):
""" Delete the table from the database. """

View file

@ -36,22 +36,22 @@ class Utilities:
def remove_file_if_exists(path: str, file: str):
""" Clean the old files. """
archive = Path(path, file)
if os.path.isfile(archive):
os.remove(archive)
if archive.is_file():
archive.unlink()
@staticmethod
def remove_folder_if_exists(path: str, folder: str):
""" Clean the old folders. """
directory = Path(path, folder)
if os.path.isdir(directory):
if directory.exists():
shutil.rmtree(directory)
@staticmethod
def create_folder(path: str, folder: str):
""" Creates folder. """
directory = Path(path, folder)
if not os.path.isdir(directory):
os.makedirs(directory)
if not directory.exists():
directory.mkdir(parents=True, exist_ok=True)
def split_installed_pkg(self, package: str) -> list:
""" Split the package by the name, version, arch, build and tag. """