-
Notifications
You must be signed in to change notification settings - Fork 1
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
Improve history #2
Comments
I'd be happy to work on this feature. It would help me to clarify a few things first.
Let me know if you have any feedback and then I'll start making the changes. |
Hi maowen ! Happy to see you again ! I've already make a work around for the historic, not released yet only on github, you can check it here: https://github.com/karec/oct-browser/blob/master/octbrowser/history.py But it's only a very basic work around I will try to answer your questions :
I hope that was helpful, but I think that we can make a mix of those ideas, and make the browser more extensible. I realy think we should implement the history property as a class, and let's the user create a whole new historic class if needed. Plus we can implement multiples historic in the core, we just need to define an abstract class and inherit from it. I will take a look at it and update my work for the base |
Karec, Thanks for answering my questions! I see now that you already started
from collections import deque
MAXLEN = 10 # or MAXLEN = None for unlimited deque length
class History(object):
def __init__(self, maxlen=MAXLEN):
self.current = 0
self.history = deque(maxlen=maxlen)
...
def back(self):
"""Implementation that explicitly handles each supported history item type"""
try:
item = self._history[-1]
# String url history item
if issubclass(item, str):
resp = self.open_url(item, back=True)
# requests.Response history item
elif issubclass(item, requests.Response):
self._url = item.url
resp = self._parse_html(item)
return resp
else:
raise Exception('Invalid history item')
return resp
except IndexError:
raise Exception("No history, cannot go back")
def back(self):
"""
Implementation that requires history item to have a url attribute
If the history item contains the cache content as well then it won't require
a reload of the page.
"""
try:
item = self._history[-1]
try:
url = item.url
except AttributeError:
raise Exception('History item must have url attribute')
self._url = item
try:
resp = self._parse_html(item)
except AttributeError:
resp = self.open_url(item, back=True)
resp = self._parse_html(resp)
return resp
except IndexError:
raise Exception("No history, cannot go back")
Thanks for letting me contribute and being so welcoming! Also, your english is very good! Mark |
maowen, Thanks again for your work ! I think github issue is the right tool for discussing about a feature, so we can keep a trace of all our work around in the same place, but if you want to show more complex code example, you can use gists ! (https://gist.github.com/) It's pretty simple to use and more comfortable than markdown ;) I will try to answer your questions :
The
So that's it, three lines, and if the historic raise an exception, the user can catch it in his own code ! And if the historic does not return a correct What do you think of this solution ?
Thanks for your contribution ! I really appreciate ! Karec |
Karec, Yes! I do like the concept of requiring the history to return a def open_url(self, url, data=None, back=False, **kwargs):
"""Open the given url"""
if data:
# No history is stored for POST requests
response = self.session.post(url, data, **kwargs)
self._url = url
else:
response = self.session.get(url, **kwargs)
if not back:
assert issubclass(response, request.Response) # Unnecessary
self._history.append_item(response)
self._url = response.url
response = self._parse_html(response)
response.connection.close()
return response
def back(self):
"""Basic implementation for back method of the browser"""
response = self._history.back()
assert issubclass(response, request.Response) # Or let the user catch any exceptions
parsed_response = self._parse_html(response)
self._url = parsed_response.url Using The other thing I think we should define is the default behavior of the octbrowser history. It will be great to use a These are the two potential solutions that I can think of that provide browser-like default behavior. The third option is simple but is not guaranteed to provide a good representation of the historic request response:
The problem I see with only saving the URL in the default history is that there Which solution (1, 2, or 3) do you think is best for the default history behavior? I suppose the beauty of the design is it is very easy for the user to change between any of those history implementations list above or any others that they desired. Implementing the history as a One last question, how do you want me to help implement these changes? I'm happy to make the changes in both Thanks! maowen |
… a BaseHistory class + allow browser._history to be set to None
I've committed a little work around for the history, you can check the commit for all details but here is the summary :
I think that's it ! It would be really great if you can take care of the Also for its implementation I think your 1st proposition is the best : cache the full I think the I will continue to work on the browser side / BaseHistory and try to make regular commits for it Thank again for your contribution ! Karec |
Karec, Do you think that making the CachedHistory class the default mode for the browser is a good idea? Or, do you prefer that history 'None' is the default? If we did make CachedHistory the default mode, one approach I can think of would be to remove from octbrowser.history.cached import CachedHistory
class Browser(object):
def __init__(self, session=None, base_url='', **kwargs):
try:
self._history = kwargs['history']
except KeyError:
self._history = CachedHistory() What do you think? maowen |
I think that making the CachedHistory as the default mode is the best solution for the browser ! I like your example, but instead of use a def __init__(self, session=None, base_url='', **kwargs):
self._history = kwargs.get('history', CachedHistory()) What do you think of this solution ? Karec |
Karec, I like it! That's a much cleaner way of doing it. I noticed that the documentation needs to be updated some to reflect the changes to we've made. If it's okay with you I'll work on improving the docs some this week. maowen |
It would be a pleasure ! But if you don't feel to write it I can do it too :) I've already made some updates to some docstrings in the code, mostly adding parameter type. I will work on updating the README and the changelog too, and maybe (or maybe for the next release) improving the form management in the browser. Thanks again for your contribution ! Karec |
For the moment, the history feature is very basic and not very powerful, I think we can improve it by adding some features :
The text was updated successfully, but these errors were encountered: