mirror of
https://github.com/imarkoff/Marble-shell-theme.git
synced 2025-11-21 21:16:00 -08:00
Fixed GDM installation, reworked label_files.py, fixed theme removing
This commit is contained in:
@@ -3,7 +3,6 @@ from .copy_files import copy_files
|
||||
from .destinaiton_return import destination_return
|
||||
from .generate_file import generate_file
|
||||
from .hex_to_rgba import hex_to_rgba
|
||||
from .label_files import label_files
|
||||
from .remove_files import remove_files
|
||||
from .remove_keywords import remove_keywords
|
||||
from .remove_properties import remove_properties
|
||||
|
||||
51
scripts/utils/files_labeler.py
Normal file
51
scripts/utils/files_labeler.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
|
||||
type LabeledFileGroup = tuple[str, str]
|
||||
|
||||
class FilesLabeler:
|
||||
def __init__(self, directory: str, *args: str):
|
||||
"""
|
||||
Initialize the working directory and files to change
|
||||
"""
|
||||
self.directory = directory
|
||||
self.files = args
|
||||
|
||||
def append_label(self, label: str):
|
||||
"""
|
||||
Append a label to all files in the directory
|
||||
and update references in the files
|
||||
"""
|
||||
labeled_files = self._label_files(label)
|
||||
self._update_references(labeled_files)
|
||||
|
||||
def _label_files(self, label: str) -> list[LabeledFileGroup]:
|
||||
labeled_files = []
|
||||
for filename in os.listdir(self.directory):
|
||||
if label in filename: continue
|
||||
|
||||
name, extension = os.path.splitext(filename)
|
||||
new_filename = f"{name}-{label}{extension}"
|
||||
|
||||
old_filepath = os.path.join(self.directory, filename)
|
||||
new_filepath = os.path.join(self.directory, new_filename)
|
||||
os.rename(old_filepath, new_filepath)
|
||||
|
||||
labeled_files.append((filename, new_filename))
|
||||
return labeled_files
|
||||
|
||||
def _update_references(self, labeled_files: list[LabeledFileGroup]):
|
||||
for file_path in self.files:
|
||||
with open(file_path, 'r') as file:
|
||||
file_content = file.read()
|
||||
|
||||
file_content = self._update_references_in_file(file_content, labeled_files)
|
||||
|
||||
with open(file_path, 'w') as file:
|
||||
file.write(file_content)
|
||||
|
||||
@staticmethod
|
||||
def _update_references_in_file(file_content: str, labeled_files: list[LabeledFileGroup]) -> str:
|
||||
replaced_content = file_content
|
||||
for old_name, new_name in labeled_files:
|
||||
replaced_content = replaced_content.replace(old_name, new_name)
|
||||
return replaced_content
|
||||
@@ -1,44 +0,0 @@
|
||||
import os
|
||||
|
||||
def label_files(directory, label, *args):
|
||||
"""
|
||||
Add a label to all files in a directory
|
||||
:param directory: folder where files are located
|
||||
:param label: label to add
|
||||
:param args: files to change links to labeled files
|
||||
:return:
|
||||
"""
|
||||
|
||||
# Open all files
|
||||
files = [open(file, 'r') for file in args]
|
||||
read_files = []
|
||||
|
||||
filenames = []
|
||||
|
||||
for filename in os.listdir(directory):
|
||||
# Skip if the file is already labeled
|
||||
if label in filename:
|
||||
continue
|
||||
|
||||
# Split the filename into name and extension
|
||||
name, extension = os.path.splitext(filename)
|
||||
|
||||
# Form the new filename and rename the file
|
||||
new_filename = f"{name}-{label}{extension}"
|
||||
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
|
||||
|
||||
filenames.append((filename, new_filename))
|
||||
|
||||
# Replace the filename in all files
|
||||
for i, file in enumerate(files):
|
||||
read_file = file.read()
|
||||
read_file.replace(filenames[i][0], filenames[i][1])
|
||||
read_files.append(read_file)
|
||||
file.close()
|
||||
|
||||
write_files = [open(file, 'w') for file in args]
|
||||
|
||||
# Write the changes to the files and close them
|
||||
for i, file in enumerate(write_files):
|
||||
file.write(read_files[i])
|
||||
file.close()
|
||||
Reference in New Issue
Block a user