Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web_poet.page_object decorator #44

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web_poet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
HttpResponseBody,
)
from .overrides import PageObjectRegistry, consume_modules, OverrideRule

from .functional import page_object

default_registry = PageObjectRegistry()
handle_urls = default_registry.handle_urls
52 changes: 52 additions & 0 deletions web_poet/functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Callable, Type

from web_poet.pages import ItemPage


def page_object(func: Callable) -> Type[ItemPage]:
"""
Decorator to create a Page Object from a function.
Note that the original function is destroyed, and
a class is created instead.

.. code-block:: python

@web_poet.page_object
def MyPage(resp: HttpResponse):
return {"title": resp.css("title::text").get()}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative syntax, which doesn't destroy the function:

def parse_foo(resp: HttpResponse):
    return {"title": resp.css("title::text").get()}

FooPage = web_poet.page_object(parse_foo)


is a shortcut for

.. code-block:: python

class MyPage(web_poet.ItemPage):
def __init__(self, resp: HttpResponse):
self.resp = resp

def to_item(self):
return {"title": self.resp.css("title::text").get()}

"""
class PageObject(ItemPage):
def __init__(self, *args, **kwargs):
# FIXME: save arguments as properly named attributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is optional, to keep the generated class as close as possible to the hand-written class. See example above - MyPage has self.resp attribute.

# FIXME: __init__ method doesn't fail if parameters are wrong
self.args = args
self.kwargs = kwargs
super().__init__()

# TODO: async def support
def to_item(self):
return func(*self.args, **self.kwargs)

# FIXME: robustness, edge cases
PageObject.__module__ = func.__module__
PageObject.__name__ = func.__name__
PageObject.__qualname__ = func.__qualname__
PageObject.__doc__ = func.__doc__
PageObject.__init__.__annotations__ = func.__annotations__

# TODO: check that type annotations work properly
# TODO: preserve the original function as an attribute?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to keep the implementation closer to functools.update_wrapper? Maybe we could leave this for a future change.

Copy link
Member Author

@kmike kmike May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more about this: if you decorate function, you can't use it anymore, it becomes a class with a completely different API; this is weird. https://github.com/scrapinghub/web-poet/pull/44/files#r882977954 could be another way to address it.

return PageObject