Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
CookStar committed Nov 11, 2021
commit 0ff43214077cdb71f3f0a7db931122f05dc765b8
79 changes: 76 additions & 3 deletions addons/source-python/packages/source-python/stringtables/downloads.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
# Stringtables
from stringtables import string_tables

# Site-Package Imports
# Path
from path import Path


# =============================================================================
# >> ALL DECLARATION
Expand All @@ -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.

Expand All @@ -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
Copy link
Member

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:

x = set()
x.update(None)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    x.update(None)
TypeError: 'NoneType' object is not iterable


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.

Expand Down Expand Up @@ -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
Expand Down