added slimParser
This commit is contained in:
@@ -29,7 +29,7 @@ xdg-desktop-portal-wlr
|
||||
qt5-qtwayland
|
||||
qt6-qtwayland
|
||||
|
||||
[other-programs]
|
||||
[Programs]
|
||||
gnome-font-viewer
|
||||
gnome-software
|
||||
kitty
|
||||
@@ -38,3 +38,7 @@ gnome-software
|
||||
nemo
|
||||
|
||||
[Flatpak]
|
||||
|
||||
[Repolist]
|
||||
|
||||
[Copr]
|
||||
+19
-33
@@ -14,6 +14,8 @@ import gdown
|
||||
from git import Repo
|
||||
from rich.console import Console
|
||||
from rich.theme import Theme
|
||||
from slimParser import SlimParser
|
||||
import slimParser
|
||||
|
||||
# pylint: disable=subprocess-run-check
|
||||
# pylint: disable=broad-exception-caught
|
||||
@@ -151,45 +153,29 @@ Type=Application ''')
|
||||
def install_programs_dnf():
|
||||
console.rule("Installing All Programs DNF ", style='checkt')
|
||||
|
||||
programs =[]
|
||||
others = []
|
||||
flatpaks= []
|
||||
parser = SlimParser('data.conf')
|
||||
parser.cleanAll()
|
||||
|
||||
with open("data.config", 'r') as file:
|
||||
lines = file.readlines()
|
||||
repos = parser.get('Repolist')
|
||||
main = parser.get('Main')
|
||||
programs = parser.get('Programs')
|
||||
flatpaks= parser.get('Flatpak')
|
||||
copr = parser.get('Copr')
|
||||
|
||||
# Variables to store line numbers of headers
|
||||
main_index = 0
|
||||
others_index = 0
|
||||
flatpak_index = 0
|
||||
|
||||
# Find the line numbers of headers
|
||||
for index, line in enumerate(lines):
|
||||
if '[Main]' in line:
|
||||
main_index = index
|
||||
elif '[other-programs]' in line:
|
||||
others_index = index
|
||||
elif '[Flatpak]' in line:
|
||||
flatpak_index = index
|
||||
|
||||
file.seek(0)
|
||||
for index,line in enumerate(file):
|
||||
#empty strings
|
||||
if not line.strip():
|
||||
continue
|
||||
elif index < others_index and ('[' not in line):
|
||||
programs.append(line.strip())
|
||||
elif index > others_index and index < flatpak_index:
|
||||
others.append(line.strip())
|
||||
elif index > flatpak_index:
|
||||
flatpaks.append(line.strip())
|
||||
elif '[' in line.strip():
|
||||
continue
|
||||
#try to add repos
|
||||
if repos:
|
||||
for url in repos:
|
||||
try:
|
||||
subprocess.run(f'dnf config-manager addrepo --from-repofile={url}')
|
||||
except Exception as e:
|
||||
console.print(Exception(),":x:" , style='error')
|
||||
logging.critical(f"Error at Installing programs: {str(e)}")
|
||||
|
||||
#for some reason they have to be passed to dnf individually
|
||||
# instead of unpacked list *programs
|
||||
programs.extend(others)
|
||||
for program in programs:
|
||||
main.extend(programs)
|
||||
for program in main:
|
||||
try:
|
||||
subprocess.run(f'dnf install -y {program} ', shell=True)
|
||||
except Exception as e:
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
'''
|
||||
this script parses conf file [section] -> content as key value pairs of Slimparser()
|
||||
'''
|
||||
|
||||
class SlimParser:
|
||||
def __init__(self,file):
|
||||
self.file = file
|
||||
self.headers = {}
|
||||
self.current_key = None
|
||||
|
||||
#run on init
|
||||
with open(file, 'r') as f:
|
||||
text = f.read()
|
||||
|
||||
lines = text.splitlines()
|
||||
|
||||
# Looks for [headers]
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
|
||||
if (
|
||||
line.startswith('[')
|
||||
and line.endswith(']')
|
||||
and len(line) > 2
|
||||
and line.count('[') == 1
|
||||
and line.count(']') == 1
|
||||
):
|
||||
section_name = line[1:-1].strip()
|
||||
|
||||
if section_name:
|
||||
self.current_key = section_name
|
||||
self.headers[self.current_key] = []
|
||||
elif self.current_key:
|
||||
self.headers[self.current_key].append(line)
|
||||
|
||||
|
||||
def getHeaders(self):
|
||||
return (self.headers.keys())
|
||||
|
||||
def get(self,section):
|
||||
"""Returns the header"""
|
||||
return self.headers.get(section)
|
||||
|
||||
def cleanSection(self, section):
|
||||
"""Strips and removes empty lines, duplicates from a section."""
|
||||
if section in self.headers:
|
||||
self.headers[section] = list(set(
|
||||
line.strip() for line in self.headers[section] if line.strip()
|
||||
))
|
||||
|
||||
elif section not in self.headers:
|
||||
raise ValueError(f"Section '{section}' not found in headers.")
|
||||
|
||||
def cleanAll(self):
|
||||
"""Strips and removes empty lines, duplicates from a All the file."""
|
||||
for section in self.headers.keys():
|
||||
self.headers[section] = list(set(
|
||||
line.strip() for line in self.headers[section] if line.strip()
|
||||
))
|
||||
Reference in New Issue
Block a user