本文整理汇总了Python中mwtemplates.TemplateEditor类的典型用法代码示例。如果您正苦于以下问题:Python TemplateEditor类的具体用法?Python TemplateEditor怎么用?Python TemplateEditor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TemplateEditor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_edit_anonymous_parameter2
def test_edit_anonymous_parameter2(self):
# Check that an anomyous parameter can be edited, preserving declaration
text = 'Lorem ipsum {{lang|1=fr}} amet'
dp = TemplateEditor(text)
dp.templates['lang'][0].parameters[1] = 'no'
tmp = text.replace('fr', 'no')
self.assertEqual(dp.wikitext(), tmp)
示例2: test_alter_anonymous_param1
def test_alter_anonymous_param1(self):
# Check that an anomyous parameter can be edited correctly
text = 'Lorem ipsum {{lang|fr}} amet'
dp = TemplateEditor(text)
dp.templates['lang'][0].parameters[1] = 'no'
tmp = text.replace('fr', 'no')
self.assertEqual(dp.wikitext(), tmp)
示例3: test_add_parameter3
def test_add_parameter3(self):
# Check that a parameter can be added, and that an existing whitespace pattern is followed
text = 'Lorem ipsum {{ Infoboks A | a = 2 }} dolor sit amet'
text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 }} dolor sit amet'
dp = TemplateEditor(text)
dp.templates['infoboks A'][0].parameters['b'] = '3'
self.assertEqual(dp.wikitext(), text2)
示例4: test_edit_parameter_using_value_that_needs_xml_escape
def test_edit_parameter_using_value_that_needs_xml_escape(self):
# Check that "&" in values are properly handled
text = 'Lorem ipsum {{kilde www}} amet'
result = 'Lorem ipsum {{kilde www| url=http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201¤tPage=83 }} amet'
dp = TemplateEditor(text)
dp.templates['kilde www'][0].parameters['url'] = 'http://www.skulabladid.fo/fo-FO/Tíðindasavn/Eldri-tíðindi.aspx?PID=30&M=NewsV2&Action=1&NewsId=3201¤tPage=83'
self.assertEqual(dp.wikitext(), result)
示例5: test_add_parameter2
def test_add_parameter2(self):
# Check that a parameter can be added, and that an existing whitespace pattern is followed
text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
text2 = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n| dato = TEST \n}} dolor sit amet'
dp = TemplateEditor(text)
dp.templates['infoboks A'][0].parameters['dato'] = 'TEST'
self.assertEqual(dp.wikitext(), text2)
示例6: test_delete_template
def test_delete_template(self):
# Check that a parameter can be deleted
text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit{{lang|fr}} amet'
text2 = 'Lorem ipsum dolor sit{{lang|fr}} amet'
dp = TemplateEditor(text)
dp.templates['Infoboks A'][0].remove() # Note that 'del dp.templates['Infoboks A'][0]' currently does not work
self.assertEqual(dp.wikitext(), text2)
示例7: check_randompage
def check_randompage(pagename):
logger.debug('Page: %s', pagename)
page = site.pages[pagename]
inputtxt = page.edit(readonly=True)
dp = TemplateEditor(inputtxt)
outputtxt = dp.wikitext()
assert inputtxt == outputtxt
示例8: test_edit_nonempty_parameter_alternative
def test_edit_nonempty_parameter_alternative(self):
# Check that a parameter can be renamed using 'parameter.value='
text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
templ.parameters['maks'].value = '3'
tmp = text.replace('2', '3')
self.assertEqual(dp.wikitext(), tmp)
示例9: test_edit_empty_parameter_with_linebreak
def test_edit_empty_parameter_with_linebreak(self):
# Check that an empty parameter can be edited, with linebreak preserved
text = 'Lorem ipsum {{ Infoboks A \n| date= \n| title= \n}} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
templ.parameters['date'] = 'test'
tmp = text.replace('date= ', 'date= test')
self.assertEqual(dp.wikitext(), tmp)
示例10: test_change_template_name
def test_change_template_name(self):
# Check that a template can be renamed, with whitespace preserved
text = 'Lorem ipsum {{ Infoboks A\n| maks=2\n}} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
templ.name = 'Mal B'
tmp = text.replace('Infoboks A', 'Mal B')
self.assertEqual(dp.wikitext(), tmp)
示例11: test_delete_parameter_alternative
def test_delete_parameter_alternative(self):
# Check that a parameter can be deleted
text = 'Lorem ipsum {{ Infoboks A | a = 2 | b = 3 | c = 4 }} dolor sit amet'
text2 = 'Lorem ipsum {{ Infoboks A | a = 2 | c = 4 }} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
templ.parameters.remove('b')
self.assertEqual(dp.wikitext(), text2)
示例12: test_delete_parameter
def test_delete_parameter(self):
# Check that a parameter can be renamed, with whitespace preserved
text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
text2 = 'Lorem ipsum {{ Infoboks A\n}} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
del templ.parameters['maks']
self.assertEqual(dp.wikitext(), text2)
示例13: test_edit_parameter_using_integer_alternative
def test_edit_parameter_using_integer_alternative(self):
# Check that a parameter can be renamed, with whitespace preserved
text = 'Lorem ipsum {{ Infoboks A\n| maks = 2 \n}} dolor sit amet'
dp = TemplateEditor(text)
templ = dp.templates['infoboks A'][0]
templ.parameters['maks'].edit(3)
tmp = text.replace('2', '3')
self.assertEqual(dp.wikitext(), tmp)
示例14: __init__
def __init__(self, page, interactive_mode):
logger.info('Checking page: %s', page.name)
self.checked = 0
self.modified = []
self.unresolved = []
# te = page.text()
txt = page.text()
# Due to <https://github.com/danmichaelo/mwtemplates/issues/3>
if re.search('<nowiki ?/>', txt, re.I) is not None:
return
te = TemplateEditor(txt)
modified = False
for k, v in te.templates.iteritems():
if k in ['Kilde www', 'Kilde bok', 'Kilde artikkel', 'Kilde avhandling', 'Kilde avis', 'Cite web', 'Citeweb', 'Cite news', 'Cite journal', 'Cite book', 'Tidningsref', 'Webbref', 'Bokref']:
for tpl in v:
t = Template(tpl, interactive_mode)
self.checked += t.checked
self.modified.extend(t.modified)
self.unresolved.extend(t.unresolved)
for u in self.unresolved:
u['page'] = page.name
if len(self.modified) != 0:
if len(self.modified) == 1:
summary = 'CS1-kompatible datoer: %s' % (self.format_entry(self.modified[0]))
elif len(self.modified) == 2:
summary = 'CS1-kompatible datoer: %s, %s' % (self.format_entry(self.modified[0]), self.format_entry(self.modified[1]))
else:
summary = 'CS1-kompatible datoer: Fikset %d datoer' % (len(self.modified))
logger.info('Saving %d fixed date(s)', len(self.modified))
time.sleep(1)
try:
res = page.save(te.wikitext(), summary=summary)
except mwclient.errors.ProtectedPageError:
logger.error('ERROR: Page protected, could not save')
if res.get('newrevid') is not None:
with codecs.open('modified.txt', 'a', 'utf8') as f:
for x in self.modified:
ti = quote(page.name.replace(' ', '_').encode('utf8'))
difflink = '//no.wikipedia.org/w/index.php?title=%s&diff=%s&oldid=%s' % (ti, res['newrevid'], res['oldrevid'])
f.write('| [[%s]] ([%s diff]) || Endret %s fra %s til %s || %s\n|-\n' % (page.name, difflink, x['key'], x['old'], x['new'], 'kompleks' if x['complex'] else ''))
with codecs.open('modified-simple.txt', 'a', 'utf8') as f:
for x in self.modified:
ti = quote(page.name.replace(' ', '_').encode('utf8'))
f.write('%s\t%s\t%s\n' % (page.name, x['old'], x['new']))
示例15: remove_template
def remove_template(self, name, page_nom, page_subject):
backlinks = page_nom.backlinks(generator = False, redirect = True)
if page_subject.name in backlinks:
logger.info(' Fjerner {{Sletting}} fra %s' % page_subject.name)
text = page_subject.text()
try:
dp = TemplateEditor(text)
if 'Sletting' in dp.templates:
dp.templates['Sletting'][0].remove()
elif 'Slett' in dp.templates:
dp.templates['Slett'][0].remove()
elif 'Slettingfordi' in dp.templates:
dp.templates['slettingfordi'][0].remove()
else:
logger.info(' Fant ingen slettemal')
return
text = dp.get_wikitext()
text = text.strip()
logger.info(' vha. TemplateEditor')
except:
text = re.sub(r'\{\{[Ss]lett.+?\}\}', '', text) # this may fail if template contains subtemplates
logger.info(' vha. regexp')
sleeper = sleepers.make()
while True:
try:
summary = 'Beholdt etter [[%s|slettediskusjon]]' % name
if self.simulate:
print "--------" + page_subject.name + ": " + summary + "------------"
print text
else:
page_subject.save(text, summary = summary)
except mwclient.EditError, e:
try:
sleeper.sleep()
except mwclient.MaximumRetriesExceeded:
return logger.error('Unable to save page')
else:
return True