本文整理汇总了Python中pywikibot.error函数的典型用法代码示例。如果您正苦于以下问题:Python error函数的具体用法?Python error怎么用?Python error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: revert
def revert(self, item):
history = pywikibot.Page(self.site, item['title']).fullVersionHistory(
total=2, rollback=self.rollback)
if len(history) > 1:
rev = history[1]
else:
return False
comment = i18n.twtranslate(pywikibot.Site(), 'revertbot-revert', {'revid': rev[0], 'author': rev[2], 'timestamp': rev[1]})
if self.comment:
comment += ': ' + self.comment
page = pywikibot.Page(self.site, item['title'])
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title(asLink=True, forceInterwiki=True,
textlink=True))
if not self.rollback:
old = page.text
page.text = rev[3]
pywikibot.showDiff(old, page.text)
page.save(comment)
return comment
try:
pywikibot.data.api.Request(action="rollback", title=page.title(), user=self.user,
token=rev[4], markbot=1).submit()
except pywikibot.data.api.APIError as e:
if e.code == 'badtoken':
pywikibot.error("There was an API token error rollbacking the edit")
else:
pywikibot.exception()
return False
return u"The edit(s) made in %s by %s was rollbacked" % (page.title(), self.user)
示例2: login
def login(self, retry=False):
if not self.password:
# As we don't want the password to appear on the screen, we set
# password = True
self.password = pywikibot.input(
u'Password for user %(name)s on %(site)s (no characters will '
u'be shown):' % {'name': self.username, 'site': self.site},
password=True)
# self.password = self.password.encode(self.site.encoding())
pywikibot.output(u"Logging in to %(site)s as %(name)s"
% {'name': self.username, 'site': self.site})
try:
cookiedata = self.getCookie()
except pywikibot.data.api.APIError as e:
pywikibot.error(u"Login failed (%s)." % e.code)
if retry:
self.password = None
return self.login(retry=True)
else:
return False
self.storecookiedata(cookiedata)
pywikibot.log(u"Should be logged in now")
## # Show a warning according to the local bot policy
## FIXME: disabled due to recursion; need to move this to the Site object after
## login
## if not self.botAllowed():
## logger.error(
## u"Username '%(name)s' is not listed on [[%(page)s]]."
## % {'name': self.username,
## 'page': botList[self.site.family.name][self.site.code]})
## logger.error(
##"Please make sure you are allowed to use the robot before actually using it!")
## return False
return True
示例3: listchoice
def listchoice(clist, message=None, default=None):
"""Ask the user to select one entry from a list of entries."""
if not message:
message = u"Select"
if default:
message += u" (default: %s)" % default
message += u": "
line_template = u"{{0: >{0}}}: {{1}}".format(int(math.log10(len(clist)) + 1))
for n, i in enumerate(clist):
pywikibot.output(line_template.format(n + 1, i))
while True:
choice = pywikibot.input(message)
if choice == '' and default:
return default
try:
choice = int(choice) - 1
except ValueError:
try:
choice = clist.index(choice)
except IndexError:
choice = -1
# User typed choice number
if 0 <= choice < len(clist):
return clist[choice]
else:
pywikibot.error("Invalid response")
示例4: translate
def translate(self, string):
"""Translate expiry time string into german."""
table = {
'gmt': 'UTC',
'mon': 'Montag',
'sat': 'Samstag',
'sun': 'Sonntag',
'second': 'Sekunde',
'seconds': 'Sekunden',
'min': 'Min.',
'minute': 'Minute',
'minutes': 'Minuten',
'hour': 'Stunde',
'hours': 'Stunden',
'day': 'Tag',
'days': 'Tage',
'week': 'Woche',
'weeks': 'Wochen',
'month': 'Monat',
'months': 'Monate',
'year': 'Jahr',
'years': 'Jahre',
'infinite': 'unbeschränkt',
'indefinite': 'unbestimmt',
}
for pattern in re.findall('([DHIMSWYa-z]+)', string):
try:
string = string.replace(pattern, table[pattern.lower()])
except KeyError:
pywikibot.error(pattern + ' not found.')
return string
示例5: upload_image
def upload_image(self, html, data, imgfile):
site = self.targetSite
# Construct the name
commons_filename = "AMH-%s-%s_%s.jpg" % (
data["amh_id"],
data["institution_shortcode"].upper(),
data["title_en"][:150]
)
if self.page_exists(commons_filename):
pywikibot.output("%s already exists, skipping" % commons_filename)
return
imagepage = pywikibot.ImagePage(site, commons_filename) # normalizes filename
imagepage.text = html
pywikibot.output(u'Uploading file %s to %s via API....' % (commons_filename, site))
try:
site.upload(imagepage, source_filename = imgfile)
except pywikibot.UploadWarning as warn:
pywikibot.output(u"We got a warning message: ", newline=False)
pywikibot.output(str(warn))
except Exception as e:
pywikibot.error("Upload error: ", exc_info=True)
else:
# No warning, upload complete.
pywikibot.output(u"Upload successful.")
示例6: _call_cmd
def _call_cmd(args, lib='djvulibre'):
"""
Tiny wrapper around subprocess.Popen().
@param args: same as Popen()
@type args: sequence or string
@param library: library to be logged in logging messages
@type library: string
@param log: log process output; errors are always logged.
@type library: bool
@return: returns a tuple (res, stdoutdata), where
res is True if dp.returncode != 0 else False
"""
if not isinstance(args, StringTypes):
# upcast if any param in sequence args is not in StringTypes
args = [str(a) if not isinstance(a, StringTypes) else a for a in args]
cmd = ' '.join(args)
else:
cmd = args
dp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdoutdata, stderrdata = dp.communicate()
if dp.returncode != 0:
pywikibot.error('{0} error; {1}'.format(lib, cmd))
pywikibot.error('{0}'.format(stderrdata))
return (False, stdoutdata)
pywikibot.log('SUCCESS: {0} (PID: {1})'.format(cmd, dp.pid))
return (True, stdoutdata)
示例7: new_from_site
def new_from_site(cls, site):
try:
page = site.page_from_repository('Q10784379')
except (NotImplementedError, UnknownExtension) as e:
pywikibot.error(e)
return None
return cls.new_from_text(page.text, site.dbName())
示例8: main
def main(*args):
options = {}
local_args = pywikibot.handle_args(args)
genFactory = pagegenerators.GeneratorFactory()
for arg in local_args:
if genFactory.handleArg(arg):
continue
if arg.startswith('-'):
arg, sep, value = arg.partition(':')
if value != '':
options[arg[1:]] = value if not value.isdigit() else int(value)
else:
options[arg[1:]] = True
generator = genFactory.getCombinedGenerator(preload=True)
site = pywikibot.Site()
if not generator:
try:
category = site.page_from_repository('Q11925744')
except (NotImplementedError, UnknownExtension) as e:
pywikibot.error(e)
return
if not category:
pywikibot.output("%s doesn't have an appropriate category" % site)
return
gen_combined = pagegenerators.CombinedPageGenerator(
[category.articles(namespaces=0), category.subcategories()])
generator = pagegenerators.WikibaseItemFilterPageGenerator(gen_combined)
bot = CommonscatCleaningBot(generator, site=site, **options)
bot.run()
示例9: login
def login(self, retry=False, force=False):
"""
Attempt to log into the server.
@param retry: infinitely retry if exception occurs during authentication.
@type retry: bool
@param force: force to re-authenticate
@type force: bool
"""
if self.access_token is None or force:
pywikibot.output('Logging in to {site!s} via OAuth consumer {key!s}'.format(**{'key': self.consumer_token[0],
'site': self.site}))
consumer_token = mwoauth.ConsumerToken(self.consumer_token[0],
self.consumer_token[1])
handshaker = mwoauth.Handshaker(
self.site.base_url(self.site.path()), consumer_token)
try:
redirect, request_token = handshaker.initiate()
pywikibot.stdout('Authenticate via web browser..')
webbrowser.open(redirect)
pywikibot.stdout('If your web browser does not open '
'automatically, please point it to: %s'
% redirect)
request_qs = pywikibot.input('Response query string: ')
access_token = handshaker.complete(request_token,
request_qs)
self._access_token = (access_token.key, access_token.secret)
except Exception as e:
pywikibot.error(e)
if retry:
self.login(retry=True, force=force)
else:
pywikibot.output('Logged in to {site!s} via consumer {key!s}'.format(**{'key': self.consumer_token[0],
'site': self.site}))
示例10: treat_page
def treat_page(self):
commons = pywikibot.Site(code = u'commons', fam = u'commons')
today = datetime.date.today()
# fileTemplate = pywikibot.Page(commons, u'Template:Potd filename')
# captionTemplate = pywikibot.Page(commons, u'Template:Potd description') # (Potd page, POTD description)
filePage = pywikibot.Page(commons, u'Template:Potd/%s' % today.isoformat())
file = get_template_parameter_value(filePage, u'Potd filename', u'1')
# TODO: use languages instead of lang
captionPage = pywikibot.Page(commons, u'Template:Potd/%s (%s)'
% (today.isoformat(), self.current_page.site.lang))
if self.current_page.site.lang != u'en' and not captionPage.exists():
pywikibot.warning(u'%s does not exist' % captionPage.title(asLink=True))
# try en instead
captionPage = pywikibot.Page(commons, u'Template:Potd/%s (en)' % today.isoformat())
caption = get_template_parameter_value(captionPage, u'Potd description', u'1')
# TODO: Complete caption parsing to fix links (if not an interwiki then make it an interwiki to Commons)
caption = re.sub(r"\[\[([^:])", r"[[:\1", caption, flags=re.UNICODE) # Force links to start with ':'
caption = re.sub(r"\[\[(:Category:)", r"[[:c\1", caption, flags=re.UNICODE | re.IGNORECASE) # Make category links interwiki links
# TODO: Use [[d:Q4608595]] to get the local {{Documentation}}
doc = u'Documentation'
if file != u'':
summary = u'Updating Commons picture of the day'
if caption != u'':
summary = summary + u', [[:c:%s|caption attribution]]' % captionPage.title()
else:
summary = summary + u', failed to parse caption'
pywikibot.error(u'Failed to parse parameter 1 from {{Potd description}} on %s'
% captionPage.title(asLink=True))
self.put_current(u'<includeonly>{{#switch:{{{1|}}}|caption=%s|#default=%s}}</includeonly><noinclude>\n{{%s}}</noinclude>'
% (caption, file, doc), summary=summary, minor=False)
else:
pywikibot.error(u'Failed to parse parameter 1 from {{Potd filename}} on %s'
% filePage.title(asLink=True))
示例11: delete_redirect
def delete_redirect(self, page, summary_key):
"""Delete the redirect page."""
assert page.site == self.site, (
'target page is on different site {0}'.format(page.site))
reason = i18n.twtranslate(self.site, summary_key)
if page.site.logged_in(sysop=True):
page.delete(reason, prompt=False)
elif i18n.twhas_key(page.site,
'redirect-broken-redirect-template'):
pywikibot.output(u"No sysop in user-config.py, "
u"put page to speedy deletion.")
try:
content = page.get(get_redirect=True)
except pywikibot.SectionError:
content_page = pywikibot.Page(page.site,
page.title(withSection=False))
content = content_page.get(get_redirect=True)
# TODO: Add bot's signature if needed (Bug: T131517)
content = i18n.twtranslate(
page.site,
'redirect-broken-redirect-template') + '\n' + content
try:
page.put(content, reason)
except pywikibot.PageSaveRelatedError as e:
pywikibot.error(e)
else:
pywikibot.output(
u'No speedy deletion template available')
示例12: _template_link_target
def _template_link_target(self, item, link_text):
link = pywikibot.Link(link_text)
try:
linked_page = pywikibot.Page(link)
except pywikibot.exceptions.InvalidTitle:
pywikibot.error('%s is not a valid title so it cannot be linked. '
'Skipping.' % link_text)
return
if not linked_page.exists():
pywikibot.output('%s does not exist so it cannot be linked. '
'Skipping.' % (linked_page))
return
if linked_page.isRedirectPage():
linked_page = linked_page.getRedirectTarget()
try:
linked_item = pywikibot.ItemPage.fromPage(linked_page)
except pywikibot.NoPage:
linked_item = None
if not linked_item or not linked_item.exists():
pywikibot.output('%s does not have a wikidata item to link with. '
'Skipping.' % (linked_page))
return
if linked_item.title() == item.title():
pywikibot.output('%s links to itself. Skipping.' % (linked_page))
return
return linked_item
示例13: main
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
@rtype: bool
"""
exists_arg = ''
commandline_claims = list()
# Process global args and prepare generator args parser
local_args = pywikibot.handle_args(args)
gen = pagegenerators.GeneratorFactory()
for arg in local_args:
# Handle args specifying how to handle duplicate claims
if arg.startswith('-exists:'):
exists_arg = arg.split(':')[1]
continue
# Handle page generator args
if gen.handleArg(arg):
continue
commandline_claims.append(arg)
if len(commandline_claims) % 2:
pywikibot.error('Incomplete command line property-value pair.')
return False
claims = list()
repo = pywikibot.Site().data_repository()
for i in range(0, len(commandline_claims), 2):
claim = pywikibot.Claim(repo, commandline_claims[i])
if claim.type == 'wikibase-item':
target = pywikibot.ItemPage(repo, commandline_claims[i + 1])
elif claim.type == 'string':
target = commandline_claims[i + 1]
elif claim.type == 'globe-coordinate':
coord_args = [float(c) for c in commandline_claims[i + 1].split(',')]
if len(coord_args) >= 3:
precision = coord_args[2]
else:
precision = 0.0001 # Default value (~10 m at equator)
target = pywikibot.Coordinate(coord_args[0], coord_args[1], precision=precision)
else:
raise NotImplementedError(
"%s datatype is not yet supported by claimit.py"
% claim.type)
claim.setTarget(target)
claims.append(claim)
generator = gen.getCombinedGenerator()
if not generator:
pywikibot.bot.suggest_help(missing_generator=True)
return False
bot = ClaimRobot(generator, claims, exists_arg)
bot.run()
return True
示例14: _oauth_login
def _oauth_login(site):
consumer_key, consumer_secret = _get_consumer_token(site)
login_manager = OauthLoginManager(consumer_secret, False, site,
consumer_key)
login_manager.login()
identity = login_manager.identity
if identity is None:
pywikibot.error('Invalid OAuth info for %(site)s.' %
{'site': site})
elif site.username() != identity['username']:
pywikibot.error('Logged in on %(site)s via OAuth as %(wrong)s, '
'but expect as %(right)s'
% {'site': site,
'wrong': identity['username'],
'right': site.username()})
else:
oauth_token = login_manager.consumer_token + login_manager.access_token
pywikibot.output('Logged in on %(site)s as %(username)s'
'via OAuth consumer %(consumer)s'
% {'site': site,
'username': site.username(sysop=False),
'consumer': consumer_key})
pywikibot.output('NOTE: To use OAuth, you need to copy the '
'following line to your user-config.py:')
pywikibot.output('authenticate[\'%(hostname)s\'] = %(oauth_token)s' %
{'hostname': site.hostname(),
'oauth_token': oauth_token})
示例15: add_mbid_claim_to_item
def add_mbid_claim_to_item(self, item, mbid):
"""
Adds a claim with pid `pid` with value `mbid` to `item` and call `donefunc`
with `mbid` to signal the completion.
:type pid: str
:type mbid: str
:type item: pywikibot.ItemPage
"""
claim = wp.Claim(const.WIKIDATA_DATASITE, self.property_id)
claim.setTarget(mbid)
wp.debug(u"Adding property {pid}, value {mbid} to {title}".format
(pid=self.property_id, mbid=mbid, title=item.title()),
layer="")
if wp.config.simulate:
wp.output("Simulation, no property has been added")
return
try:
item.addClaim(claim, True)
except wp.UserBlocked as e:
wp.error("I have been blocked")
exit(1)
except wp.Error as e:
wp.warning(e)
return
else:
wp.debug("Adding the source Claim", layer="")
claim.addSources([const.MUSICBRAINZ_CLAIM, const.RETRIEVED_CLAIM], bot=True)
self.donefunc(mbid)