http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10296
Revision: 10296 Author: xqt Date: 2012-06-04 17:02:54 +0000 (Mon, 04 Jun 2012) Log Message: ----------- split editarticle to library and script part
Modified Paths: -------------- branches/rewrite/scripts/blockpageschecker.py branches/rewrite/scripts/editarticle.py branches/rewrite/scripts/replace.py branches/rewrite/scripts/solve_disambiguation.py branches/rewrite/scripts/upload.py
Added Paths: ----------- branches/rewrite/pywikibot/editor.py
Copied: branches/rewrite/pywikibot/editor.py (from rev 10289, branches/rewrite/scripts/editarticle.py) =================================================================== --- branches/rewrite/pywikibot/editor.py (rev 0) +++ branches/rewrite/pywikibot/editor.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -0,0 +1,111 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +Text editor class for your favourite editor. +""" + +# +# (C) Gerrit Holl 2004 +# (C) Pywikipedia team, 2004-2012 +# +__version__ = "$Id$" +# +# Distributed under the terms of the MIT license. +# + +__metaclass__ = type +import sys +import os +import tempfile +import pywikibot +from pywikibot import config2 as config + + +class TextEditor(object): + def __init__(self): + pass + + def command(self, tempFilename, text, jumpIndex = None): + command = config.editor + if jumpIndex: + # Some editors make it possible to mark occurences of substrings, + # or to jump to the line of the first occurence. + # TODO: Find a better solution than hardcoding these, e.g. a config + # option. + line = text[:jumpIndex].count('\n') + column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) + else: + line = column = 0 + # Linux editors. We use startswith() because some users might use + # parameters. + if config.editor.startswith('kate'): + command += " -l %i -c %i" % (line + 1, column + 1) + elif config.editor.startswith('gedit'): + command += " +%i" % (line + 1) # seems not to support columns + elif config.editor.startswith('emacs'): + command += " +%i" % (line + 1) # seems not to support columns + elif config.editor.startswith('jedit'): + command += " +line:%i" % (line + 1) # seems not to support columns + elif config.editor.startswith('vim'): + command += " +%i" % (line + 1) # seems not to support columns + elif config.editor.startswith('nano'): + command += " +%i,%i" % (line + 1, column + 1) + # Windows editors + elif config.editor.lower().endswith('notepad++.exe'): + command += " -n%i" % (line + 1) # seems not to support columns + + command += ' %s' % tempFilename + #print command + return command + + def convertLinebreaks(self, text): + if sys.platform=='win32': + return text.replace('\r\n', '\n') + # TODO: Mac OS handling + return text + + def restoreLinebreaks(self, text): + if text is None: + return None + if sys.platform=='win32': + return text.replace('\n', '\r\n') + # TODO: Mac OS handling + return text + + def edit(self, text, jumpIndex = None, highlight = None): + """ + Calls the editor and thus allows the user to change the text. + Returns the modified text. Halts the thread's operation until the editor + is closed. + + Returns None if the user didn't save the text file in his text editor. + + Parameters: + * text - a Unicode string + * jumpIndex - an integer: position at which to put the caret + * highlight - a substring; each occurence will be highlighted + """ + text = self.convertLinebreaks(text) + if config.editor: + tempFilename = '%s.%s' % (tempfile.mktemp(), + config.editor_filename_extension) + tempFile = open(tempFilename, 'w') + tempFile.write(text.encode(config.editor_encoding)) + tempFile.close() + creationDate = os.stat(tempFilename).st_mtime + command = self.command(tempFilename, text, jumpIndex) + os.system(command) + lastChangeDate = os.stat(tempFilename).st_mtime + if lastChangeDate == creationDate: + # Nothing changed + return None + else: + newcontent = open(tempFilename).read().decode( + config.editor_encoding) + os.unlink(tempFilename) + return self.restoreLinebreaks(newcontent) + else: + return self.restoreLinebreaks( + pywikibot.editText(text, jumpIndex=jumpIndex, + highlight=highlight)) +
Modified: branches/rewrite/scripts/blockpageschecker.py =================================================================== --- branches/rewrite/scripts/blockpageschecker.py 2012-06-04 16:43:06 UTC (rev 10295) +++ branches/rewrite/scripts/blockpageschecker.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -198,7 +198,7 @@ if quest == 'b': webbrowser.open(url) elif quest == 'g': - import editarticle + from pywikibot import editor as editarticle editor = editarticle.TextEditor() text = editor.edit(page.get())
Modified: branches/rewrite/scripts/editarticle.py =================================================================== --- branches/rewrite/scripts/editarticle.py 2012-06-04 16:43:06 UTC (rev 10295) +++ branches/rewrite/scripts/editarticle.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -6,7 +6,7 @@
# # (C) Gerrit Holl 2004 -# (C) Pywikipedia team, 2004-2011 +# (C) Pywikipedia team, 2004-2012 # __version__ = "$Id$" # @@ -22,104 +22,16 @@ # - ...
__metaclass__ = type -import sys import os import string import optparse import tempfile
import pywikibot -from pywikibot import config, i18n +from pywikibot import i18n +from pywikibot.editor import TextEditor
-class TextEditor: - def __init__(self): - pass - - def command(self, tempFilename, text, jumpIndex = None): - command = config.editor - if jumpIndex: - # Some editors make it possible to mark occurences of substrings, - # or to jump to the line of the first occurence. - # TODO: Find a better solution than hardcoding these, e.g. a config - # option. - line = text[:jumpIndex].count('\n') - column = jumpIndex - (text[:jumpIndex].rfind('\n') + 1) - else: - line = column = 0 - # Linux editors. We use startswith() because some users might use - # parameters. - if config.editor.startswith('kate'): - command += " -l %i -c %i" % (line + 1, column + 1) - elif config.editor.startswith('gedit'): - command += " +%i" % (line + 1) # seems not to support columns - elif config.editor.startswith('emacs'): - command += " +%i" % (line + 1) # seems not to support columns - elif config.editor.startswith('jedit'): - command += " +line:%i" % (line + 1) # seems not to support columns - elif config.editor.startswith('vim'): - command += " +%i" % (line + 1) # seems not to support columns - elif config.editor.startswith('nano'): - command += " +%i,%i" % (line + 1, column + 1) - # Windows editors - elif config.editor.lower().endswith('notepad++.exe'): - command += " -n%i" % (line + 1) # seems not to support columns - - command += ' %s' % tempFilename - #print command - return command - - def convertLinebreaks(self, text): - if sys.platform=='win32': - return text.replace('\r\n', '\n') - # TODO: Mac OS handling - return text - - def restoreLinebreaks(self, text): - if text is None: - return None - if sys.platform=='win32': - return text.replace('\n', '\r\n') - # TODO: Mac OS handling - return text - - def edit(self, text, jumpIndex = None, highlight = None): - """ - Calls the editor and thus allows the user to change the text. - Returns the modified text. Halts the thread's operation until the editor - is closed. - - Returns None if the user didn't save the text file in his text editor. - - Parameters: - * text - a Unicode string - * jumpIndex - an integer: position at which to put the caret - * highlight - a substring; each occurence will be highlighted - """ - text = self.convertLinebreaks(text) - if config.editor: - tempFilename = '%s.%s' % (tempfile.mktemp(), - config.editor_filename_extension) - tempFile = open(tempFilename, 'w') - tempFile.write(text.encode(config.editor_encoding)) - tempFile.close() - creationDate = os.stat(tempFilename).st_mtime - command = self.command(tempFilename, text, jumpIndex) - os.system(command) - lastChangeDate = os.stat(tempFilename).st_mtime - if lastChangeDate == creationDate: - # Nothing changed - return None - else: - newcontent = open(tempFilename).read().decode( - config.editor_encoding) - os.unlink(tempFilename) - return self.restoreLinebreaks(newcontent) - else: - return self.restoreLinebreaks( - pywikibot.editText(text, jumpIndex=jumpIndex, - highlight=highlight)) - class ArticleEditor: # join lines if line starts with this ones joinchars = string.letters + '[]' + string.digits
Modified: branches/rewrite/scripts/replace.py =================================================================== --- branches/rewrite/scripts/replace.py 2012-06-04 16:43:06 UTC (rev 10295) +++ branches/rewrite/scripts/replace.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -125,7 +125,7 @@ import sys, re, time import pywikibot from pywikibot import pagegenerators -from scripts import editarticle +from pywikibot import editor as editarticle from pywikibot import catlib, config from pywikibot import i18n import webbrowser
Modified: branches/rewrite/scripts/solve_disambiguation.py =================================================================== --- branches/rewrite/scripts/solve_disambiguation.py 2012-06-04 16:43:06 UTC (rev 10295) +++ branches/rewrite/scripts/solve_disambiguation.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -86,7 +86,7 @@
# Application specific imports import pywikibot -from scripts import editarticle +from pywikibot import editor as editarticle from pywikibot import pagegenerators from pywikibot import config from pywikibot import i18n
Modified: branches/rewrite/scripts/upload.py =================================================================== --- branches/rewrite/scripts/upload.py 2012-06-04 16:43:06 UTC (rev 10295) +++ branches/rewrite/scripts/upload.py 2012-06-04 17:02:54 UTC (rev 10296) @@ -174,7 +174,7 @@ u'Do you want to change this description?', ['Yes', 'No'], ['y', 'N'], 'n') if choice == 'y': - import editarticle + from pywikibot import editor as editarticle editor = editarticle.TextEditor() newDescription = editor.edit(self.description) # if user saved / didn't press Cancel