Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
minor fixes, moved static methods to util module
Browse files Browse the repository at this point in the history
  • Loading branch information
lrusso96 committed Apr 20, 2019
1 parent f89cdb6 commit 62d5345
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
12 changes: 6 additions & 6 deletions mwklient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def email(self, user, text, subject, cc=False):
>>> try:
... site.email('SomeUser', 'Some message', 'Some subject')
... except mwklient.errors.NoSpecifiedEmailError as e:
... except mwklient.errors.NoSpecifiedEmailError as err:
... print 'The user does not accept email, or has not specified
an email address.'
Expand All @@ -506,10 +506,10 @@ def email(self, user, text, subject, cc=False):
try:
info = self.post('emailuser', target=user, subject=subject,
text=text, ccme=cc, token=token)
except errors.APIError as e:
if e.args[0] == u'noemail':
raise errors.NoSpecifiedEmail(user, e.args[1])
raise errors.EmailError(*e)
except errors.APIError as err:
if err.args[0] == u'noemail':
raise errors.NoSpecifiedEmail(user, err.args[1])
raise errors.EmailError(*err.args)

return info

Expand Down Expand Up @@ -689,7 +689,7 @@ def upload(self, file=None, filename=None, description='', ignore=False,
'filename': filename,
'comment': comment,
'text': text,
'token': image.get_token('edit'),
'token': image.__get_token__('edit'),
}

if ignore:
Expand Down
32 changes: 8 additions & 24 deletions mwklient/page.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import six
from six import text_type
from mwklient.util import parse_timestamp
from mwklient.util import strip_namespace, parse_timestamp
import mwklient.listing
import mwklient.errors

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self, site, name, info=None, extra_properties=None):
self.namespace = info.get('ns', 0)
self.name = info.get('title', u'')
if self.namespace:
self.page_title = self.strip_namespace(self.name)
self.page_title = strip_namespace(self.name)
else:
self.page_title = self.name

Expand Down Expand Up @@ -95,22 +95,6 @@ def __repr__(self):
def __unicode__(self):
return self.name

@staticmethod
def strip_namespace(title):
if title[0] == ':':
title = title[1:]
return title[title.find(':') + 1:]

@staticmethod
def normalize_title(title):
# TODO: Make site dependent
title = title.strip()
if title[0] == ':':
title = title[1:]
title = title[0].upper() + title[1:]
title = title.replace(' ', '_')
return title

def can(self, action):
"""Check if the current user has the right to carry out some action
with the current page.
Expand All @@ -126,7 +110,7 @@ def can(self, action):

return level in self.site.rights

def get_token(self, type_t, force=False):
def __get_token__(self, type_t, force=False):
return self.site.get_token(type_t, force, title=self.name)

def text(self,
Expand Down Expand Up @@ -199,7 +183,7 @@ def __check_edit__(self):
def __do_edit__(self, **kwargs):
result = self.site.post(
'edit',
token=self.get_token('edit'),
token=self.__get_token__('edit'),
**kwargs)
if result['edit'].get('result').lower() == 'failure':
raise mwklient.errors.EditError(self, result['edit'])
Expand Down Expand Up @@ -247,7 +231,7 @@ def save(self,
except mwklient.errors.APIError as err:
if err.code == 'badtoken':
# Retry, but only once to avoid an infinite loop
self.get_token('edit', force=True)
self.__get_token__('edit', force=True)
try:
result = self.__do_edit__()
except mwklient.errors.APIError as err:
Expand Down Expand Up @@ -291,7 +275,7 @@ def undo(self, rev_id, summary=u'', bot=False, **kwargs):
except mwklient.errors.APIError as err:
if err.code == 'badtoken':
# Retry, but only once to avoid an infinite loop
self.get_token('edit', force=True)
self.__get_token__('edit', force=True)
try:
result = self.__do_edit__(**data)
except mwklient.errors.APIError as err:
Expand Down Expand Up @@ -347,7 +331,7 @@ def move(self, new_title, reason='', move_talk=True, no_redirect=False):
result = self.site.post(
'move', ('from', self.name),
to=new_title,
token=self.get_token('move'),
token=self.__get_token__('move'),
reason=reason,
**data)
return result['move']
Expand Down Expand Up @@ -375,7 +359,7 @@ def delete(self, reason='', watch=False, unwatch=False, oldimage=False):
result = self.site.post(
'delete',
title=self.name,
token=self.get_token('delete'),
token=self.__get_token__('delete'),
reason=reason,
**data)
return result['delete']
Expand Down
16 changes: 16 additions & 0 deletions mwklient/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
from time import strptime


def strip_namespace(title):
if title[0] == ':':
title = title[1:]
return title[title.find(':') + 1:]


def normalize_title(title):
# TODO: Make site dependent
title = title.strip()
if title[0] == ':':
title = title[1:]
title = title[0].upper() + title[1:]
title = title.replace(' ', '_')
return title


def parse_timestamp(timestamp):
"""Parses a string to a time tuple.
Expand Down

0 comments on commit 62d5345

Please sign in to comment.