Refactor product filtering and loading for generic scripts

Generic scripts are scripts that handle multiple product based on some identifier (URL, coordinates...).
This creates a common function to list and load product configurations for those scripts.

It makes them simpler to read and maintain, while making the way they work much more consistent.
This commit is contained in:
Marc Wrobel
2023-05-13 14:46:04 +02:00
parent 7c2bddb860
commit 5176abd4d4
7 changed files with 99 additions and 149 deletions

27
src/common/endoflife.py Normal file
View File

@@ -0,0 +1,27 @@
import frontmatter
from glob import glob
from os import path
def list_products(method, products_filter=None, pathname = "website/products"):
"""Return a list of products that are using the same given update method.
"""
products_with_method = {}
for product_file in glob(f"{pathname}/*.md"):
product_name = path.splitext(path.basename(product_file))[0]
if products_filter and product_name != products_filter:
continue
with open(product_file, "r") as f:
data = frontmatter.load(f)
if "auto" in data:
configs = list(filter(
lambda config: method in config.keys(),
data["auto"]
))
if len(configs) > 0:
products_with_method[product_name] = configs
return products_with_method