From 0ff43214077cdb71f3f0a7db931122f05dc765b8 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Fri, 12 Nov 2021 08:30:41 +0900 Subject: [PATCH 1/2] Enhanced Downloadables. Changed Downloadables to accept iterable on initialization. Fixed Downloadables.update to correctly add downloadables to stringtable. Added add_from_file/remove_from_file functions to Downloadables. --- .../source-python/stringtables/downloads.py | 79 ++++++++++++++++++- 1 file changed, 76 insertions(+), 3 deletions(-) mode change 100644 => 100755 addons/source-python/packages/source-python/stringtables/downloads.py diff --git a/addons/source-python/packages/source-python/stringtables/downloads.py b/addons/source-python/packages/source-python/stringtables/downloads.py old mode 100644 new mode 100755 index dc2a2bf46..b66f5f569 --- a/addons/source-python/packages/source-python/stringtables/downloads.py +++ b/addons/source-python/packages/source-python/stringtables/downloads.py @@ -15,6 +15,10 @@ # Stringtables from stringtables import string_tables +# Site-Package Imports +# Path +from path import Path + # ============================================================================= # >> ALL DECLARATION @@ -29,11 +33,17 @@ class Downloadables(AutoUnload, set): """Class used to store downloadables for a script.""" - def __init__(self): - """Add the instance to the downloadables list.""" - super().__init__() + def __init__(self, *items): + """Add the instance to the downloadables list. + + :param iterable items: + The paths to add to the downloadables. + """ + super().__init__(*items) _downloadables_list.append(self) + self._set_all_downloads() + def add(self, item): """Add an item to the downloadables for a script. @@ -52,6 +62,25 @@ def add(self, item): # Add the item to the script's downloadables super().add(item) + def update(self, items=None): + """Add an items to the downloadables. + + :param iterable items: + The paths to add to the downloadables. + """ + if items is None: + return + + for item in items: + # Is the item not in the list? + if item not in self: + + # Add the item to the downloadables stringtable + _downloadables_list._add_to_download_table(item) + + # Add the items to the downloadables + super().update(items) + def add_directory(self, directory): """Add all files in the given directory to the downloadables. @@ -85,6 +114,50 @@ def remove_directory(self, directory): # Remove the item from the set self.remove(item) + def add_from_file(self, file_path): + """Add all the paths listed in the file to the downloadables. + + :param str file_path: + The file that contains the paths to add to the downloadables. + Lines that starts with '#' or '//' will be ignored. + :return: + Return the number of files that have been added. + :rtype: int + """ + file_path = Path(file_path) + if not file_path.exists(): + with_game_path = GAME_PATH.joinpath(file_path) + if with_game_path.exists(): + file_path = with_game_path + + with open(file_path, 'r') as file: + lines = file.read().splitlines() + + # Remove comments and empty lines + items = list( + filter(lambda x:x and not x.startswith(('#', '//')), lines)) + + self.update(items) + + return len(items) + + def remove_from_file(self, file_path): + """Remove all the paths listed in the file from the downloadables. + + :param str file_path: + The file that contains the paths to remove from the downloadables. + """ + file_path = Path(file_path) + if not file_path.exists(): + with_game_path = GAME_PATH.joinpath(file_path) + if with_game_path.exists(): + file_path = with_game_path + + with open(file_path, 'r') as file: + lines = file.read().splitlines() + + self.difference_update(lines) + def _set_all_downloads(self): """Add all downloadables for the script on level init.""" # Loop through all items in the list From 9b53244fb830885c1b9091450688a81e422724a8 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Mon, 15 Nov 2021 14:19:58 +0900 Subject: [PATCH 2/2] Minor fixes. --- .../source-python/stringtables/downloads.py | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/addons/source-python/packages/source-python/stringtables/downloads.py b/addons/source-python/packages/source-python/stringtables/downloads.py index b66f5f569..e4aef4410 100755 --- a/addons/source-python/packages/source-python/stringtables/downloads.py +++ b/addons/source-python/packages/source-python/stringtables/downloads.py @@ -114,7 +114,7 @@ def remove_directory(self, directory): # Remove the item from the set self.remove(item) - def add_from_file(self, file_path): + def add_from_file(self, file_path, encoding='utf-8'): """Add all the paths listed in the file to the downloadables. :param str file_path: @@ -124,14 +124,7 @@ def add_from_file(self, file_path): Return the number of files that have been added. :rtype: int """ - file_path = Path(file_path) - if not file_path.exists(): - with_game_path = GAME_PATH.joinpath(file_path) - if with_game_path.exists(): - file_path = with_game_path - - with open(file_path, 'r') as file: - lines = file.read().splitlines() + lines = self._get_lines_from_file(file_path, encoding) # Remove comments and empty lines items = list( @@ -141,20 +134,13 @@ def add_from_file(self, file_path): return len(items) - def remove_from_file(self, file_path): + def remove_from_file(self, file_path, encoding='utf-8'): """Remove all the paths listed in the file from the downloadables. :param str file_path: The file that contains the paths to remove from the downloadables. """ - file_path = Path(file_path) - if not file_path.exists(): - with_game_path = GAME_PATH.joinpath(file_path) - if with_game_path.exists(): - file_path = with_game_path - - with open(file_path, 'r') as file: - lines = file.read().splitlines() + lines = self._get_lines_from_file(file_path, encoding) self.difference_update(lines) @@ -166,6 +152,27 @@ def _set_all_downloads(self): # Add the item to the downloadables stringtable _downloadables_list._add_to_download_table(item) + @classmethod + def _get_lines_from_file(cls, file_path, encoding): + """Checks the file path and then return the lines.""" + file_path = Path(file_path) + + # If the file or directory exists, ignore it + if not file_path.exists(): + + # File does not exist, search for it with GAME_PATH + with_game_path = GAME_PATH / file_path + if with_game_path.exists(): + file_path = with_game_path + + if not file_path.isfile(): + raise FileNotFoundError(f'No file found at "{file_path}"') + + with file_path.open('r', encoding=encoding) as file: + lines = [line.strip() for line in file.readlines()] + + return lines + def _unload_instance(self): """Remove the instance from the downloadables list.""" _downloadables_list.remove(self)