slpkg/slpkg/configs.py

135 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-06-19 21:18:54 +02:00
import os
2022-06-22 21:05:56 +02:00
import yaml
2022-06-19 21:18:54 +02:00
from dataclasses import dataclass
@dataclass
2022-06-18 20:35:23 +02:00
class Configs:
2022-06-18 20:25:14 +02:00
# Programme name
prog_name: str = 'slpkg'
''' Default configurations. '''
# OS architecture by default
os_arch: str = 'x86_64'
2022-06-18 20:25:14 +02:00
# All necessary paths
tmp_path: str = '/tmp'
tmp_slpkg: str = f'{tmp_path}/{prog_name}'
build_path: str = f'/tmp/{prog_name}/build'
download_only: str = f'{tmp_slpkg}/'
lib_path: str = f'/var/lib/{prog_name}'
2022-06-18 16:04:23 +02:00
etc_path: str = f'/etc/{prog_name}'
db_path: str = f'/var/lib/{prog_name}/database'
sbo_repo_path: str = f'/var/lib/{prog_name}/repository'
log_packages: str = '/var/log/packages'
2022-06-18 20:25:14 +02:00
# Database name
database: str = f'database.{prog_name}'
2022-06-18 20:25:14 +02:00
# SBo repository configs
sbo_repo_url: str = 'http://slackbuilds.org/slackbuilds/15.0'
sbo_txt: str = 'SLACKBUILDS.TXT'
chglog_txt: str = 'ChangeLog.txt'
sbo_tar_suffix: str = '.tar.gz'
sbo_repo_tag: str = '_SBo'
2022-06-18 20:25:14 +02:00
# Slackware commands
installpkg: str = 'upgradepkg --install-new'
2022-06-17 17:02:35 +02:00
reinstall: str = 'upgradepkg --reinstall'
removepkg: str = 'removepkg'
2022-06-18 20:25:14 +02:00
# Cli menu colors configs
colors: str = 'off'
# Wget options
wget_options = '-c -N'
2022-06-18 09:31:59 +02:00
''' Overwrite with user configuration. '''
config_file: str = f'{etc_path}/{prog_name}.yml'
2022-06-19 21:18:54 +02:00
if os.path.isfile(config_file):
with open(config_file, 'r') as conf:
2022-06-22 21:05:56 +02:00
configs = yaml.safe_load(conf)
try:
config = configs['configs']
# OS architecture by default
os_arch: str = config['os_arch']
# All necessary paths
tmp_slpkg: str = config['tmp_slpkg']
build_path: str = config['build_path']
download_only: str = config['download_only']
sbo_repo_path: str = config['sbo_repo_path']
# Database name
database: str = config['database']
# SBo repository details
sbo_repo_url: str = config['sbo_repo_url']
sbo_txt: str = config['sbo_txt']
chglog_txt: str = config['chglog_txt']
sbo_tar_suffix: str = config['sbo_tar_suffix']
sbo_repo_tag: str = config['sbo_repo_tag']
# Slackware commands
installpkg: str = config['installpkg']
reinstall: str = config['reinstall']
removepkg: str = config['removepkg']
# Cli menu colors configs
colors: str = config['colors']
# Wget options
wget_options: str = config['wget_options']
except KeyError:
pass
2022-06-18 09:31:59 +02:00
# Creating the paths if they doesn't 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)
@classmethod
def colour(cls):
color = {
2022-06-22 21:05:56 +02:00
'BOLD': '',
'RED': '',
'GREEN': '',
'YELLOW': '',
'CYAN': '',
2022-06-19 21:18:54 +02:00
'BLUE': '',
'GREY': '',
'ENDC': ''
}
2022-06-22 21:05:56 +02:00
if cls.colors:
color = {
2022-06-21 08:35:35 +02:00
'BOLD': '\033[1m',
2022-06-18 21:07:06 +02:00
'RED': '\x1b[91m',
'GREEN': '\x1b[32m',
'YELLOW': '\x1b[93m',
2022-06-18 21:07:06 +02:00
'CYAN': '\x1b[96m',
'BLUE': '\x1b[94m',
'GREY': '\x1b[38;5;247m',
'ENDC': '\x1b[0m'
}
2022-06-18 16:04:23 +02:00
return color