Skip to content

Commit f699f57

Browse files
authored
Merge pull request #634 from hx2A/finish-processing-library-stuff
Finish processing library stuff
2 parents fe2b2e0 + 49198c0 commit f699f57

9 files changed

Lines changed: 104 additions & 11 deletions

generator/reference.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,13 @@
375375
("Py5Tools", "processing_download_library"): [
376376
(
377377
["library_name: str"],
378-
"bool",
378+
"dict",
379+
)
380+
],
381+
("Py5Tools", "processing_remove_library"): [
382+
(
383+
["library_name: str"],
384+
"None",
379385
)
380386
],
381387
}

py5_docs/Reference/api_en/Py5Tools_processing_check_library.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ library_name: str - name of Processing library
1414
Check if a Processing library has been downloaded and stored by py5. These are the same libraries available to you in the Library Manager when you use the Processing Development Environment (PDE). Downloaded libraries are available for you to import in Python after you import py5.
1515

1616
Downloaded libraries will be saved in the Processing library storage directory. Use [](py5tools_processing_library_storage_dir) to get the specific location of the storage directory on your machine.
17+
18+
@@ example
19+
import py5_tools
20+
21+
if py5_tools.processing.check_library("PeasyCam"):
22+
print("PeasyCam is in your stored library")

py5_docs/Reference/api_en/Py5Tools_processing_download_library.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ category = processing
55
subcategory = None
66

77
@@ signatures
8-
processing_download_library(library_name: str) -> bool
8+
processing_download_library(library_name: str) -> dict
99

1010
@@ variables
1111
library_name: str - name of Processing library
1212

1313
@@ description
1414
Download and store a Processing library. These are the same libraries available to you in the Library Manager when you use the Processing Development Environment (PDE). After this function downloads a Processing library, it will be available for you to import in Python after you import py5.
1515

16+
This function will also return a dictionary containing some basic information about the installed library. If the library was previously downloaded, it will check the version numbers and will update the library if it is not current. If you want to remove an installed library, use [](py5tools_processing_remove_library).
17+
1618
Downloaded libraries will be saved in the Processing library storage directory. Use [](py5tools_processing_library_storage_dir) to get the specific location of the storage directory on your machine.
19+
20+
@@ example
21+
import py5_tools
22+
23+
py5_tools.processing.download_library("PeasyCam")
24+
25+
import py5
26+
27+
from peasy import PeasyCam
28+
29+
def setup():
30+
py5.size(500, 500, py5.P3D)
31+
PeasyCam(py5.get_current_sketch(), 500)
32+
33+
34+
def draw():
35+
py5.background(128)
36+
py5.box(200)

py5_docs/Reference/api_en/Py5Tools_processing_installed_libraries.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ processing_installed_libraries() -> list[str]
1111
List the Processing libraries stored in your computer's Processing library storage directory. These are all of the Processing libraries that have been installed using the [](py5tools_processing_download_library) function. To get the location of the library storage directory, use the [](py5tools_processing_library_storage_dir) function.
1212

1313
Downloaded libraries will be saved in the Processing library storage directory. Use [](py5tools_processing_library_storage_dir) to get the specific location of the storage directory on your machine.
14+
15+
@@ example
16+
import py5_tools
17+
18+
print(py5_tools.processing.installed_libraries())
19+
# ['Camera 3D', 'PeasyCam', 'ColorBlindness']

py5_docs/Reference/api_en/Py5Tools_processing_library_storage_dir.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ subcategory = None
88
processing_library_storage_dir() -> Path
99

1010
@@ description
11-
Return the location of the Processing library storage directory. You should never need to manually edit files in this directory, but if for some reason you do, you'll first need to find it. By default, this will be `~/.cache/py5` on Linux and MacOS machines and `~/AppData/Local/py5` on Windows machines. These locations are similar to where other Python libraries store their data files. If you wish, set the `PY5_HOME` environment variable to move the storage directory to a location of your choosing.
11+
Return the location of the Processing library storage directory. You should never need to manually edit files in this directory, but if for some reason you do, you'll first need to find it. By default, this will be in `~/.cache/py5` on Linux and MacOS machines and `~/AppData/Local/py5` on Windows machines. These locations are similar to where other Python libraries store their data files. If you wish, set the `PY5_HOME` environment variable to move the storage directory to a location of your choosing.
1212

13-
If you wish to add Java Jar files to py5's classpath, don't use this directory. Instead, put jars in a `jars` subdirectory (relative to the current working directory of your Python code) or set the `PY5_JARS` environment variable to the path of the directory you wish to use.
13+
If you wish to manually add Java Jar files to py5's classpath, don't use this directory. Instead, put jars in a `jars` subdirectory (relative to the current working directory of your Python code) or set the `PY5_JARS` environment variable to the path of the directory you wish to use.
14+
15+
@@ example
16+
print(py5_tools.processing.library_storage_dir())
17+
# /home/jim/.cache/py5/processing-libraries
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@@ meta
2+
name = processing_remove_library()
3+
type = function
4+
category = processing
5+
subcategory = None
6+
7+
@@ signatures
8+
processing_remove_library(library_name: str) -> None
9+
10+
@@ variables
11+
library_name: str - name of Processing library
12+
13+
@@ description
14+
Remove a previously downloaded and stored Processing library. These are the same libraries available to you in the Library Manager when you use the Processing Development Environment (PDE). You download and install Processing libraries with [](py5tools_processing_download_library). After this function removes a Processing library, it will no longer be available for you to use with py5.
15+
16+
@@ example
17+
import py5_tools
18+
19+
py5_tools.processing.remove_library("PeasyCam")

py5_resources/data/py5tools.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ live_coding_activate,,,function,live_coding,,PYTHON,live_coding
2020
processing_library_storage_dir,,,function,processing,,PYTHON,processing
2121
processing_installed_libraries,,,function,processing,,PYTHON,processing
2222
processing_check_library,,,function,processing,,PYTHON,processing
23-
processing_download_library,,,function,processing,,PYTHON,processing
23+
processing_download_library,,,function,processing,,PYTHON,processing
24+
processing_remove_library,,,function,processing,,PYTHON,processing

py5_resources/py5_module/py5_tools/libraries.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def __init__(self):
3838
def _load_data(self):
3939
response = requests.get(PROCESSING_LIBRARY_URL)
4040
if response.status_code != 200:
41-
raise RuntimeError(
42-
f"could not download data file at {PROCESSING_LIBRARY_URL}"
43-
)
41+
# Could not download data file. Perhaps the user is offline.
42+
self._data = []
43+
return
4444

4545
blocks = [b for b in response.text.split("\n\n") if b.startswith("library")]
4646
data = [
@@ -102,7 +102,7 @@ def download_zip(self, dest, library_name=None, library_id=None):
102102

103103
response = requests.get(download_url)
104104
if response.status_code != 200:
105-
raise RuntimeError(f"could not download library at {download_url}")
105+
raise RuntimeError(f"Could not download library at {download_url}")
106106

107107
with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
108108
jars = []

py5_resources/py5_module/py5_tools/processing.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#
1919
# *****************************************************************************
2020
import datetime as dt
21+
import shutil
2122
from pathlib import Path
2223

2324
from .constants import PY5_HOME
@@ -114,7 +115,7 @@ def get_library(self, library_name):
114115
stored_info = self._load_library_info(library_name)
115116
if stored_info:
116117
if stored_info["version"] == info["version"]:
117-
return
118+
return stored_info
118119
print(f"Library {library_name} is outdated. Updating...")
119120

120121
try:
@@ -128,6 +129,26 @@ def get_library(self, library_name):
128129
print(f"Failed to download library {library_name}: {e}")
129130
return
130131

132+
def remove_library(self, library_name):
133+
"""Remove a library from the storage directory.
134+
135+
Args:
136+
library_name (str): The name of the library to remove.
137+
"""
138+
info = self._load_library_info(library_name)
139+
if info:
140+
# Remove the library directory
141+
lib_dir = STORAGE_DIR / info["dir"]
142+
if lib_dir.exists():
143+
shutil.rmtree(lib_dir)
144+
145+
# Remove the info file
146+
info_file = STORAGE_DIR / f"{library_name}.txt"
147+
if info_file.exists():
148+
info_file.unlink()
149+
else:
150+
print(f"Library {library_name} not found in storage.")
151+
131152

132153
def library_storage_dir() -> Path:
133154
"""$module_Py5Tools_processing_library_storage_dir"""
@@ -152,7 +173,7 @@ def check_library(library_name: str) -> bool:
152173
return _library_manager.check_library(library_name)
153174

154175

155-
def download_library(library_name: str) -> bool:
176+
def download_library(library_name: str) -> dict:
156177
"""$module_Py5Tools_processing_download_library"""
157178
global _library_manager
158179
if _library_manager is None:
@@ -161,11 +182,21 @@ def download_library(library_name: str) -> bool:
161182
return _library_manager.get_library(library_name)
162183

163184

185+
def remove_library(library_name: str) -> None:
186+
"""$module_Py5Tools_processing_remove_library"""
187+
global _library_manager
188+
if _library_manager is None:
189+
_library_manager = ProcessingLibraryManager()
190+
191+
_library_manager.remove_library(library_name)
192+
193+
164194
__all__ = [
165195
"check_library",
166196
"download_library",
167197
"installed_libraries",
168198
"library_storage_dir",
199+
"remove_library",
169200
]
170201

171202

0 commit comments

Comments
 (0)