Skip to content

Commit

Permalink
update get_overrides_from to accept an arbitrary number of str inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnzZ committed Jan 6, 2022
1 parent daa3ff9 commit 5d73e58
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
28 changes: 17 additions & 11 deletions docs/intro/overrides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,16 @@ Then we could easily retrieve all Page Objects per subpackage or module like thi
from web_poet import default_registry, consume_modules
# We can do it per website.
rules = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site")
rules = default_registry.get_overrides_from("my_page_obj_project.furniture_site")
rules_gadget = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site")
rules_furniture = default_registry.get_overrides_from("my_page_obj_project.furniture_site")
# It can also drill down to the country domains on a given site.
rules = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us")
rules = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.fr")
rules_gadget_us = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us")
rules_gadget_fr = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.fr")
# Or even drill down further to the specific module.
rules = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us.products")
rules = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us.product_listings")
rules_gadget_us_products = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us.products")
rules_gadget_us_listings = default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us.product_listings")
# Or simply all of the Override rules ever declared.
rules = default_registry.get_overrides()
Expand All @@ -273,6 +273,12 @@ Then we could easily retrieve all Page Objects per subpackage or module like thi
consume_modules("external_package_A.po", "another_ext_package.lib")
rules = default_registry.get_overrides()
.. warning::

Remember to consider calling :func:`~.web_poet.overrides.consume_modules`
when using :meth:`~.PageObjectRegistry.get_overrides` in case you have some
external package containing Page Objects of interest.

Multiple Registry Approach
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -376,11 +382,11 @@ retrieve such rules would be:
from web_poet import default_registry
product_listing_rules = [
default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.us.product_listings")
+ default_registry.get_overrides_from("my_page_obj_project.cool_gadget_site.fr.product_listings")
+ default_registry.get_overrides_from("my_page_obj_project.furniture_shop.product_listings")
]
product_listing_rules = default_registry.get_overrrides_from(
"my_page_obj_project.cool_gadget_site.us.product_listings",
"my_page_obj_project.cool_gadget_site.fr.product_listings",
"my_page_obj_project.furniture_shop.product_listings",
)
On the other hand, we can also create another :class:`~.PageObjectRegistry` instance
that we'll be using aside from the ``default_registry`` to help us better organize
Expand Down
17 changes: 16 additions & 1 deletion tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,29 @@ def test_list_page_objects_from_pkg():
assert rule.meta == rule.use.expected_meta, rule.use


def test_list_page_objects_from_module():
def test_list_page_objects_from_single():
rules = default_registry.get_overrides_from("tests.po_lib.a_module")
assert len(rules) == 1
rule = rules[0]
assert rule.use == POModule
assert rule.for_patterns == POModule.expected_patterns
assert rule.instead_of == POModule.expected_overrides

def test_list_page_objects_from_multiple():
rules = default_registry.get_overrides_from(
"tests.po_lib.a_module",
"tests.po_lib.nested_package.a_nested_module"
)
assert len(rules) == 2

assert rules[0].use == POModule
assert rules[0].for_patterns == POModule.expected_patterns
assert rules[0].instead_of == POModule.expected_overrides

assert rules[1].use == PONestedModule
assert rules[1].for_patterns == PONestedModule.expected_patterns
assert rules[1].instead_of == PONestedModule.expected_overrides


def test_list_page_objects_from_empty_module():
rules = default_registry.get_overrides_from("tests.po_lib.an_empty_module")
Expand Down
11 changes: 6 additions & 5 deletions web_poet/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,19 @@ def get_overrides(self) -> List[OverrideRule]:
"""Returns all override rules that were declared using ``@handle_urls``."""
return list(self.data.values())

def get_overrides_from(self, module: str) -> List[OverrideRule]:
def get_overrides_from(self, *pkgs_or_modules: str) -> List[OverrideRule]:
"""Returns the override rules that were declared using ``@handle_urls``
in a specific module.
in a specific modules/packages.
This is useful if you've organized your Page Objects into multiple
submodules in your project as you can filter them easily.
"""
# Dict ensures that no duplicates are collected and returned.
rules: Dict[Callable, OverrideRule] = {}

for mod in walk_module(module):
# Dict ensures that no duplicates are collected and returned.
rules.update(self._filter_from_module(mod.__name__))
for item in pkgs_or_modules:
for mod in walk_module(item):
rules.update(self._filter_from_module(mod.__name__))

return list(rules.values())

Expand Down

0 comments on commit 5d73e58

Please sign in to comment.