44
44
ADDON_CATALOG_URL = (
45
45
"https://raw.githubusercontent.com/FreeCAD/FreeCAD-addons/master/AddonCatalog.json"
46
46
)
47
- BASE_DIRECTORY = "./"
47
+ BASE_DIRECTORY = "./CatalogCache "
48
48
MAX_COUNT = 10 # Do at most this many repos (for testing purposes)
49
49
50
50
# Repos that are too large, or that should for some reason not be cloned here
@@ -62,13 +62,17 @@ class CacheEntry:
62
62
icon_data : str = ""
63
63
64
64
65
- class AddonCatalogCacheCreator :
65
+ class CatalogFetcher :
66
+ """Fetches the addon catalog from the given URL and returns an AddonCatalog object. Separated
67
+ from the main class for easy mocking during tests. Note that every instantiation of this class
68
+ will run a new fetch of the catalog."""
66
69
67
- def __init__ (self , addon_catalog_url = ADDON_CATALOG_URL ):
70
+ def __init__ (self , addon_catalog_url : str = ADDON_CATALOG_URL ):
68
71
self .addon_catalog_url = addon_catalog_url
69
72
self .catalog = self .fetch_catalog ()
70
73
71
74
def fetch_catalog (self ) -> AddonCatalog .AddonCatalog :
75
+ """Fetch the addon catalog from the given URL and return an AddonCatalog object."""
72
76
response = requests .get (self .addon_catalog_url )
73
77
if response .status_code != 200 :
74
78
raise RuntimeError (
@@ -78,22 +82,30 @@ def fetch_catalog(self) -> AddonCatalog.AddonCatalog:
78
82
79
83
80
84
class CacheWriter :
85
+ """Writes a JSON file containing a cache of all addon catalog entries. The cache is a copy of
86
+ the package.xml, requirements.txt, and metadata.txt files from the addon repositories, as well
87
+ as a base64-encoded icon image. The cache is written to the current working directory."""
81
88
82
89
def __init__ (self ):
83
90
self .catalog : AddonCatalog = None
84
91
if os .path .isabs (BASE_DIRECTORY ):
85
92
self .cwd = BASE_DIRECTORY
86
93
else :
87
- self .cwd = os .path .normpath (os .path .join (os .path . curdir , BASE_DIRECTORY ))
94
+ self .cwd = os .path .normpath (os .path .join (os .getcwd () , BASE_DIRECTORY ))
88
95
self ._cache = {}
89
96
90
97
def write (self ):
98
+ original_working_directory = os .getcwd ()
99
+ os .makedirs (self .cwd , exist_ok = True )
100
+ os .chdir (self .cwd )
91
101
self .create_local_copy_of_addons ()
92
102
with open ("addon_catalog_cache.json" , "w" , encoding = "utf-8" ) as f :
93
103
f .write (json .dumps (self ._cache , indent = " " ))
104
+ os .chdir (original_working_directory )
105
+ print (f"Wrote cache to { os .path .join (self .cwd , 'addon_catalog_cache.json' )} " )
94
106
95
107
def create_local_copy_of_addons (self ):
96
- self .catalog = AddonCatalogCacheCreator ().catalog
108
+ self .catalog = CatalogFetcher ().catalog
97
109
counter = 0
98
110
for addon_id , catalog_entries in self .catalog .get_catalog ().items ():
99
111
if addon_id in EXCLUDED_REPOS :
@@ -128,7 +140,7 @@ def create_local_copy_of_single_addon(
128
140
def generate_cache_entry (
129
141
self , addon_id : str , index : int , catalog_entry : AddonCatalog .AddonCatalogEntry
130
142
) -> Optional [CacheEntry ]:
131
- """Create the cache entry for this catalog entry, if there is data to cache. If there is
143
+ """Create the cache entry for this catalog entry if there is data to cache. If there is
132
144
nothing to cache, returns None."""
133
145
path_to_package_xml = self .find_file ("package.xml" , addon_id , index , catalog_entry )
134
146
cache_entry = None
0 commit comments