Skip to content

Commit

Permalink
optionally, cache page text until next edit operation
Browse files Browse the repository at this point in the history
If 'cache' is set to True on init, store the results of
page.text() operations in a simple cache dict. This avoids
unnecessary remote roundtrips. Cache is cleared on each
successful page.save() operation.

page.text() has its own 'cache' argument which, if set to
True or False, overrides whatever the instance setting is.
We might consider also clearing the cache if the instance
setting is True but text() is called with cache set False.
  • Loading branch information
AdamWill committed Mar 25, 2015
1 parent a93c58a commit f148b59
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions mwclient/page.py
Expand Up @@ -10,12 +10,14 @@

class Page(object):

def __init__(self, site, name, info=None, extra_properties={}):
def __init__(self, site, name, info=None, extra_properties={}, cache=False):
if type(name) is type(self):
return self.__dict__.update(name.__dict__)
self.site = site
self.name = name
self.section = None
self.cache = cache
self._textcache = {}

if not info:
if extra_properties:
Expand Down Expand Up @@ -110,20 +112,24 @@ def get_expanded(self):

return self.text(expandtemplates=True)

def edit(self, *args, **kwargs):
def edit(self, section=None, *args, **kwargs):
"""Deprecated. Use page.text() instead"""
warnings.warn("page.edit() was deprecated in mwclient 0.7.0, please use page.text() instead.",
category=DeprecationWarning, stacklevel=2)
return self.text(*args, **kwargs)
if section:
self.section = section
return self.text(section, *args, **kwargs)

def text(self, section=None, expandtemplates=False):
def text(self, section=None, expandtemplates=False, cache=None):
"""
Returns the current wikitext of the page, or of a specific section.
If the page does not exist, an empty string is returned.
:Arguments:
- `section` : numbered section or `None` to get the whole page (default: `None`)
- `expandtemplates` : set to `True` to expand templates (default: `False`)
- `cache` : set to `True` or `False` to override the instance cache setting
(default: `None`, respect instance setting)
"""

if not self.can('read'):
Expand All @@ -132,19 +138,30 @@ def text(self, section=None, expandtemplates=False):
return u''
if section is not None:
section = text_type(section)
if cache is None:
cache = self.cache

revs = self.revisions(prop='content|timestamp', limit=1, section=section, expandtemplates=expandtemplates)
try:
rev = revs.next()
text = rev['*']
key = (section, expandtemplates)
if cache and key in self._textcache:
text = self._textcache[key]
else:
revs = self.revisions(prop='content|timestamp', limit=1, section=section, expandtemplates=expandtemplates)
try:
rev = revs.next()
text = rev['*']
self.last_rev_time = rev['timestamp']
except StopIteration:
text = u''
self.last_rev_time = None
if not expandtemplates:
self.edit_time = time.gmtime()

if cache:
self._textcache[key] = text
if text:
self.section = section
self.last_rev_time = rev['timestamp']
except StopIteration:
text = u''
else:
self.section = None
self.last_rev_time = None
if not expandtemplates:
self.edit_time = time.gmtime()
return text

def save(self, text, summary=u'', minor=False, bot=True, section=None, **kwargs):
Expand Down Expand Up @@ -206,6 +223,8 @@ def do_edit():
# 'newtimestamp' is not included if no change was made
if 'newtimestamp' in result['edit'].keys():
self.last_rev_time = client.parse_timestamp(result['edit'].get('newtimestamp'))
# clear the page text cache
self._textcache = {}
return result['edit']

def handle_edit_error(self, e, summary):
Expand Down

0 comments on commit f148b59

Please sign in to comment.