-
Notifications
You must be signed in to change notification settings - Fork 37
Enhanced Downloadables. #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,36 @@ def remove_directory(self, directory): | |
# Remove the item from the set | ||
self.remove(item) | ||
|
||
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: | ||
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 | ||
""" | ||
lines = self._get_lines_from_file(file_path, encoding) | ||
|
||
# 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, 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. | ||
""" | ||
lines = self._get_lines_from_file(file_path, encoding) | ||
|
||
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 | ||
|
@@ -93,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()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use lines = {
Path(file.strip()).normpath()
for file in set(ConfigFile(file_path, as_strings=True))
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fully agree on the normalizing part. At the moment normalizing isn't that important as no users are involved. But I think it's better to normalize. Especially with this PR.
|
||
|
||
return lines | ||
|
||
def _unload_instance(self): | ||
"""Remove the instance from the downloadables list.""" | ||
_downloadables_list.remove(self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the point of the None check here? A Python set would throw an error: