From 2cf6e2e0a22d0b8898b7931c971b1c1e62c9b123 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Fri, 2 Feb 2024 18:30:01 +0100 Subject: [PATCH] Add ignore factories option for craft bench and fluid packer --- factorygame/data/fetch.py | 5 +++-- factorygame/data/sfp.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/factorygame/data/fetch.py b/factorygame/data/fetch.py index b60fa06..ca0f759 100755 --- a/factorygame/data/fetch.py +++ b/factorygame/data/fetch.py @@ -13,8 +13,9 @@ from ..helper import click_prompt @click.command() @click.option("--debug", is_flag=True) @click.option("--refetch", is_flag=True) +@click.option("--ignore-factories", type=list[str], default=["A.I. Fluid Packer", "Craft Bench"]) @click.argument("search") -def main(debug: bool, refetch: bool, search: str): +def main(debug: bool, refetch: bool, ignore_factories: list[str], search: str): engine = create_engine("sqlite:///file.db", echo=debug) Base.metadata.create_all(bind=engine) if not search: @@ -25,7 +26,7 @@ def main(debug: bool, refetch: bool, search: str): resource = chose_resource(session=session, resource_label=search, prompt=click_prompt).result() exists_in_db = resource is not None - with SatisfactoryPlus(debug=debug) as data_provider: + with SatisfactoryPlus(ignore_factories=ignore_factories, debug=debug) as data_provider: if resource is None: ret = data_provider.search_for_resource(session=session, search=search) if ret is None: diff --git a/factorygame/data/sfp.py b/factorygame/data/sfp.py index fd87db9..35472ab 100644 --- a/factorygame/data/sfp.py +++ b/factorygame/data/sfp.py @@ -15,9 +15,13 @@ from ..helper import click_prompt class SatisfactoryPlus(RecipeProvider, AbstractContextManager): _browser: Optional[Firefox] = None + ignore_factories: list[str] = [] - def __init__(self, debug: bool = False): + def __init__(self, ignore_factories: list[str] = None, debug: bool = False): super().__init__() + if ignore_factories: + for factory in ignore_factories: + self.ignore_factories.append(factory.casefold()) self.debug = debug def _init_browser(self) -> Firefox: @@ -109,6 +113,9 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager): factory_label = factory_html_elem.text.strip() assert factory_label, "factory label is missing (a[text])" + if factory_label.casefold() in self.ignore_factories: + return + # re-use existing Factory or create new factory = session.scalars(Factory.by_label(factory_label)).one_or_none() if factory is None: @@ -117,6 +124,9 @@ class SatisfactoryPlus(RecipeProvider, AbstractContextManager): factory = Factory(label=factory_label, uri=self._normalize_url(href=factory_href)) session.add(factory) recipe_factories.append(factory) + if not recipe_factories: + # ignore recipe when no applicable factories exist + return def find_or_create_resource(resource_label: str, resource_uri_getter: Callable) -> Resource: db_resource = session.scalars(Resource.by_label(resource_label)).one_or_none()