|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Edit a Wikipedia article with your favourite editor. |
| 5 | +""" |
| 6 | + |
| 7 | +# |
| 8 | +# (C) Gerrit Holl 2004 |
| 9 | +# (C) Pywikipedia team, 2004-2011 |
| 10 | +# |
| 11 | +__version__ = "$Id: editarticle.py 9370 2011-07-13 06:39:47Z xqt $" |
| 12 | +# |
| 13 | +# Distributed under the terms of the MIT license. |
| 14 | +# |
| 15 | + |
| 16 | +# Version 0.4. |
| 17 | +# |
| 18 | +# TODO: - non existing pages |
| 19 | +# - edit conflicts |
| 20 | +# - minor edits |
| 21 | +# - watch/unwatch |
| 22 | +# - ... |
| 23 | + |
| 24 | +__metaclass__ = type |
| 25 | +import sys |
| 26 | +import os |
| 27 | +import string |
| 28 | +import optparse |
| 29 | +import tempfile |
| 30 | + |
| 31 | +import wikipedia as pywikibot |
| 32 | +from pywikibot import i18n |
| 33 | +import config |
| 34 | + |
| 35 | + |
| 36 | +class TextEditor: |
| 37 | + def __init__(self): |
| 38 | + pass |
| 39 | + |
| 40 | + def command(self, tempFilename, text, jumpIndex = None): |
| 41 | + command = config.editor |
| 42 | + if jumpIndex: |
| 43 | + # Some editors make it possible to mark occurences of substrings, |
| 44 | + # or to jump to the line of the first occurence. |
| 45 | + # TODO: Find a better solution than hardcoding these, e.g. a config |
| 46 | + # option. |
| 47 | + line = text[:jumpIndex].count('\n') |
| 48 | + column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) |
| 49 | + else: |
| 50 | + line = column = 0 |
| 51 | + # Linux editors. We use startswith() because some users might use |
| 52 | + # parameters. |
| 53 | + if config.editor.startswith('kate'): |
| 54 | + command += " -l %i -c %i" % (line + 1, column + 1) |
| 55 | + elif config.editor.startswith('gedit'): |
| 56 | + command += " +%i" % (line + 1) # seems not to support columns |
| 57 | + elif config.editor.startswith('emacs'): |
| 58 | + command += " +%i" % (line + 1) # seems not to support columns |
| 59 | + elif config.editor.startswith('jedit'): |
| 60 | + command += " +line:%i" % (line + 1) # seems not to support columns |
| 61 | + elif config.editor.startswith('vim'): |
| 62 | + command += " +%i" % (line + 1) # seems not to support columns |
| 63 | + elif config.editor.startswith('nano'): |
| 64 | + command += " +%i,%i" % (line + 1, column + 1) |
| 65 | + # Windows editors |
| 66 | + elif config.editor.lower().endswith('notepad++.exe'): |
| 67 | + command += " -n%i" % (line + 1) # seems not to support columns |
| 68 | + |
| 69 | + command += ' %s' % tempFilename |
| 70 | + #print command |
| 71 | + return command |
| 72 | + |
| 73 | + def convertLinebreaks(self, text): |
| 74 | + if sys.platform=='win32': |
| 75 | + return text.replace('\r\n', '\n') |
| 76 | + # TODO: Mac OS handling |
| 77 | + return text |
| 78 | + |
| 79 | + def restoreLinebreaks(self, text): |
| 80 | + if text is None: |
| 81 | + return None |
| 82 | + if sys.platform=='win32': |
| 83 | + return text.replace('\n', '\r\n') |
| 84 | + # TODO: Mac OS handling |
| 85 | + return text |
| 86 | + |
| 87 | + def edit(self, text, jumpIndex = None, highlight = None): |
| 88 | + """ |
| 89 | + Calls the editor and thus allows the user to change the text. |
| 90 | + Returns the modified text. Halts the thread's operation until the editor |
| 91 | + is closed. |
| 92 | +
|
| 93 | + Returns None if the user didn't save the text file in his text editor. |
| 94 | +
|
| 95 | + Parameters: |
| 96 | + * text - a Unicode string |
| 97 | + * jumpIndex - an integer: position at which to put the caret |
| 98 | + * highlight - a substring; each occurence will be highlighted |
| 99 | + """ |
| 100 | + text = self.convertLinebreaks(text) |
| 101 | + if config.editor: |
| 102 | + tempFilename = '%s.%s' % (tempfile.mktemp(), |
| 103 | + config.editor_filename_extension) |
| 104 | + tempFile = open(tempFilename, 'w') |
| 105 | + tempFile.write(text.encode(config.editor_encoding)) |
| 106 | + tempFile.close() |
| 107 | + creationDate = os.stat(tempFilename).st_mtime |
| 108 | + command = self.command(tempFilename, text, jumpIndex) |
| 109 | + os.system(command) |
| 110 | + lastChangeDate = os.stat(tempFilename).st_mtime |
| 111 | + if lastChangeDate == creationDate: |
| 112 | + # Nothing changed |
| 113 | + return None |
| 114 | + else: |
| 115 | + newcontent = open(tempFilename).read().decode( |
| 116 | + config.editor_encoding) |
| 117 | + os.unlink(tempFilename) |
| 118 | + return self.restoreLinebreaks(newcontent) |
| 119 | + else: |
| 120 | + return self.restoreLinebreaks( |
| 121 | + pywikibot.ui.editText(text, jumpIndex=jumpIndex, |
| 122 | + highlight=highlight)) |
| 123 | + |
| 124 | +class ArticleEditor: |
| 125 | + # join lines if line starts with this ones |
| 126 | + joinchars = string.letters + '[]' + string.digits |
| 127 | + |
| 128 | + def __init__(self, *args): |
| 129 | + self.set_options(*args) |
| 130 | + self.setpage() |
| 131 | + self.site = pywikibot.getSite() |
| 132 | + |
| 133 | + def set_options(self, *args): |
| 134 | + """Parse commandline and set options attribute""" |
| 135 | + my_args = [] |
| 136 | + for arg in pywikibot.handleArgs(*args): |
| 137 | + my_args.append(arg) |
| 138 | + parser = optparse.OptionParser() |
| 139 | + parser.add_option("-r", "--edit_redirect", action="store_true", |
| 140 | + default=False, help="Ignore/edit redirects") |
| 141 | + parser.add_option("-p", "--page", help="Page to edit") |
| 142 | + parser.add_option("-w", "--watch", action="store_true", default=False, |
| 143 | + help="Watch article after edit") |
| 144 | + #parser.add_option("-n", "--new_data", default="", |
| 145 | + # help="Automatically generated content") |
| 146 | + (self.options, args) = parser.parse_args(args=my_args) |
| 147 | + |
| 148 | + # for convenience, if we have an arg, stuff it into the opt, so we |
| 149 | + # can act like a normal editor. |
| 150 | + if (len(args) == 1): |
| 151 | + self.options.page = args[0] |
| 152 | + |
| 153 | + def setpage(self): |
| 154 | + """Sets page and page title""" |
| 155 | + site = pywikibot.getSite() |
| 156 | + pageTitle = self.options.page or pywikibot.input(u"Page to edit:") |
| 157 | + self.page = pywikibot.Page(site, pageTitle) |
| 158 | + if not self.options.edit_redirect and self.page.isRedirectPage(): |
| 159 | + self.page = self.page.getRedirectTarget() |
| 160 | + |
| 161 | + def handle_edit_conflict(self): |
| 162 | + fn = os.path.join(tempfile.gettempdir(), self.page.title()) |
| 163 | + fp = open(fn, 'w') |
| 164 | + fp.write(new) |
| 165 | + fp.close() |
| 166 | + pywikibot.output( |
| 167 | + u"An edit conflict has arisen. Your edit has been saved to %s. Please try again." |
| 168 | + % fn) |
| 169 | + |
| 170 | + def run(self): |
| 171 | + try: |
| 172 | + old = self.page.get(get_redirect = self.options.edit_redirect) |
| 173 | + except pywikibot.NoPage: |
| 174 | + old = "" |
| 175 | + textEditor = TextEditor() |
| 176 | + new = textEditor.edit(old) |
| 177 | + if new and old != new: |
| 178 | + pywikibot.showDiff(old, new) |
| 179 | + changes = pywikibot.input(u"What did you change?") |
| 180 | + comment = i18n.twtranslate(pywikibot.getSite(), 'editarticle-edit', |
| 181 | + {'description': changes}) |
| 182 | + try: |
| 183 | + self.page.put(new, comment=comment, minorEdit=False, |
| 184 | + watchArticle=self.options.watch) |
| 185 | + except pywikibot.EditConflict: |
| 186 | + self.handle_edit_conflict(new) |
| 187 | + else: |
| 188 | + pywikibot.output(u"Nothing changed") |
| 189 | + |
| 190 | +def main(*args): |
| 191 | + app = ArticleEditor(*args) |
| 192 | + app.run() |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + try: |
| 196 | + main() |
| 197 | + finally: |
| 198 | + pywikibot.stopme() |
| 199 | + |
0 commit comments