jenkins-bot has submitted this change and it was merged.
Change subject: Tk: Migrated Tkdialog class to gui.py ......................................................................
Tk: Migrated Tkdialog class to gui.py
Migrated Tkdialog class from scripts/flickrripper.py to pywikibot/userinterfaces/gui.py Added packages dependencies in setup.py and tests/scripts_tests.py. Added test to tests/tk_tests.py
Bug: T78505 Change-Id: I0001ec541f2b6679354314b92331f0cfcd381383 --- M pywikibot/userinterfaces/gui.py M scripts/flickrripper.py M setup.py M tests/script_tests.py M tests/tk_tests.py 5 files changed, 129 insertions(+), 129 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/userinterfaces/gui.py b/pywikibot/userinterfaces/gui.py index 8bd2800..0d8123f 100644 --- a/pywikibot/userinterfaces/gui.py +++ b/pywikibot/userinterfaces/gui.py @@ -30,6 +30,8 @@ from idlelib.configHandler import idleConf from idlelib.MultiCall import MultiCallCreator
+import pywikibot +
class TextEditor(ScrolledText):
@@ -439,3 +441,106 @@ self.listbox.config(height=laenge, width=maxbreite + 2) # wait for user to push a button which will destroy (close) the window return self.list + + +class Tkdialog: + + """ The dialog window for image info.""" + + def __init__(self, photo_description, photo, filename): + """Constructor.""" + self.root = Tkinter.Tk() + # "%dx%d%+d%+d" % (width, height, xoffset, yoffset) + self.root.geometry("%ix%i+10-10" % (pywikibot.config.tkhorsize, + pywikibot.config.tkvertsize)) + + self.root.title(filename) + self.photo_description = photo_description + self.filename = filename + self.photo = photo + self.skip = False + self.exit = False + + # --Init of the widgets + # The image + self.image = self.get_image(self.photo, 800, 600) + self.image_panel = Tkinter.Label(self.root, image=self.image) + + self.image_panel.image = self.image + + # The filename + self.filename_label = Tkinter.Label(self.root, text=u"Suggested filename") + self.filename_field = Tkinter.Entry(self.root, width=100) + self.filename_field.insert(Tkinter.END, filename) + + # The description + self.description_label = Tkinter.Label(self.root, + text=u"Suggested description") + self.description_scrollbar = Tkinter.Scrollbar(self.root, + orient=Tkinter.VERTICAL) + self.description_field = Tkinter.Text(self.root) + self.description_field.insert(Tkinter.END, photo_description) + self.description_field.config(state=Tkinter.NORMAL, height=12, width=100, + padx=0, pady=0, wrap=Tkinter.WORD, + yscrollcommand=self.description_scrollbar.set) + self.description_scrollbar.config(command=self.description_field.yview) + + # The buttons + self.ok_button = Tkinter.Button(self.root, text="OK", + command=self.ok_file) + self.skip_button = Tkinter.Button(self.root, text="Skip", + command=self.skip_file) + + # --Start grid + + # The image + self.image_panel.grid(row=0, column=0, rowspan=11, columnspan=4) + + # The buttons + self.ok_button.grid(row=11, column=1, rowspan=2) + self.skip_button.grid(row=11, column=2, rowspan=2) + + # The filename + self.filename_label.grid(row=13, column=0) + self.filename_field.grid(row=13, column=1, columnspan=3) + + # The description + self.description_label.grid(row=14, column=0) + self.description_field.grid(row=14, column=1, columnspan=3) + self.description_scrollbar.grid(row=14, column=5) + + def get_image(self, photo, width, height): + """Take the BytesIO object and build an imageTK thumbnail.""" + try: + from PIL import Image, ImageTk + except ImportError: + pywikibot.warning('This script requires ImageTk from the' + 'Python Imaging Library (PIL).\n' + 'See: https://www.mediawiki.org/wiki/' + 'Manual:Pywikibot/flickrripper.py') + raise + + image = Image.open(photo) + image.thumbnail((width, height)) + imageTk = ImageTk.PhotoImage(image) + return imageTk + + def ok_file(self): + """ The user pressed the OK button. """ + self.filename = self.filename_field.get() + self.photo_description = self.description_field.get(0.0, Tkinter.END) + self.root.destroy() + + def skip_file(self): + """ The user pressed the Skip button. """ + self.skip = True + self.root.destroy() + + def show_dialog(self): + """ Activate the dialog. + + @return: new description, name, and if the image is skipped + @rtype: tuple of (unicode, unicode, bool) + """ + self.root.mainloop() + return self.photo_description, self.filename, self.skip diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py index cad7b1d..fa9d9f0 100644 --- a/scripts/flickrripper.py +++ b/scripts/flickrripper.py @@ -46,35 +46,7 @@ from urllib import urlencode, urlopen
try: - if sys.version_info[0] > 2: - from tkinter import ( - Tk, Label, Entry, Scrollbar, Text, Button, - END, VERTICAL, NORMAL, WORD - ) - else: - from Tkinter import ( - Tk, Label, Entry, Scrollbar, Text, Button, - END, VERTICAL, NORMAL, WORD - ) -except ImportError as e: - print( - 'This script requires Tkinter, which is typically part of Python,\n' - 'but may be packaged separately on your platform.\n' - 'See: https://www.mediawiki.org/wiki/Manual:Pywikibot/flickrripper.py') - print(e) - sys.exit() - -try: - from PIL import Image, ImageTk -except ImportError as e: - print( - 'This script requires ImageTk from the Python Imaging Library (PIL).\n' - 'See: https://www.mediawiki.org/wiki/Manual:Pywikibot/flickrripper.py') - print(e) - sys.exit(1) - -try: - import flickrapi # see: http://stuvel.eu/projects/flickrapi + import flickrapi # see: http://stuvel.eu/projects/flickrapi except ImportError as e: print('This script requires the python flickrapi module. \n' 'See: http://stuvel.eu/projects/flickrapi') @@ -85,6 +57,9 @@
from pywikibot import config, textlib from scripts import upload + +from pywikibot.userinterfaces.gui import Tkdialog +
flickr_allowed_license = { 0: False, # All Rights Reserved @@ -317,9 +292,14 @@ removeCategories) # pywikibot.output(photoDescription) if not autonomous: - (newPhotoDescription, newFilename, skip) = Tkdialog( + try: + (newPhotoDescription, newFilename, skip) = Tkdialog( photoDescription, photo, filename).run() - else: + except ImportError as e: + pywikibot.warning(e) + pywikibot.warning('Switching to autonomous mode.') + autonomous = True + if autonomous: newPhotoDescription = photoDescription newFilename = filename skip = False @@ -342,93 +322,6 @@ else: pywikibot.output(u'Invalid license') return 0 - - -class Tkdialog: - - """ The user dialog. """ - - def __init__(self, photoDescription, photo, filename): - """Constructor.""" - self.root = Tk() - # "%dx%d%+d%+d" % (width, height, xoffset, yoffset) - self.root.geometry("%ix%i+10-10" % (config.tkhorsize, config.tkvertsize)) - - self.root.title(filename) - self.photoDescription = photoDescription - self.filename = filename - self.photo = photo - self.skip = False - self.exit = False - - # --Init of the widgets - # The image - self.image = self.getImage(self.photo, 800, 600) - self.imagePanel = Label(self.root, image=self.image) - - self.imagePanel.image = self.image - - # The filename - self.filenameLabel = Label(self.root, text=u"Suggested filename") - self.filenameField = Entry(self.root, width=100) - self.filenameField.insert(END, filename) - - # The description - self.descriptionLabel = Label(self.root, text=u"Suggested description") - self.descriptionScrollbar = Scrollbar(self.root, orient=VERTICAL) - self.descriptionField = Text(self.root) - self.descriptionField.insert(END, photoDescription) - self.descriptionField.config(state=NORMAL, height=12, width=100, padx=0, pady=0, wrap=WORD, yscrollcommand=self.descriptionScrollbar.set) - self.descriptionScrollbar.config(command=self.descriptionField.yview) - - # The buttons - self.okButton = Button(self.root, text="OK", command=self.okFile) - self.skipButton = Button(self.root, text="Skip", command=self.skipFile) - - # --Start grid - - # The image - self.imagePanel.grid(row=0, column=0, rowspan=11, columnspan=4) - - # The buttons - self.okButton.grid(row=11, column=1, rowspan=2) - self.skipButton.grid(row=11, column=2, rowspan=2) - - # The filename - self.filenameLabel.grid(row=13, column=0) - self.filenameField.grid(row=13, column=1, columnspan=3) - - # The description - self.descriptionLabel.grid(row=14, column=0) - self.descriptionField.grid(row=14, column=1, columnspan=3) - self.descriptionScrollbar.grid(row=14, column=5) - - def getImage(self, photo, width, height): - """Take the BytesIO object and build an imageTK thumbnail.""" - image = Image.open(photo) - image.thumbnail((width, height)) - imageTk = ImageTk.PhotoImage(image) - return imageTk - - def okFile(self): - """ The user pressed the OK button. """ - self.filename = self.filenameField.get() - self.photoDescription = self.descriptionField.get(0.0, END) - self.root.destroy() - - def skipFile(self): - """ The user pressed the Skip button. """ - self.skip = True - self.root.destroy() - - def run(self): - """ Activate the dialog. - - @return: new description, name, and if the image is skipped - @rtype: tuple of (unicode, unicode, bool) - """ - self.root.mainloop() - return self.photoDescription, self.filename, self.skip
def getPhotos(flickr=None, user_id=u'', group_id=u'', photoset_id=u'', diff --git a/setup.py b/setup.py index a0be084..48ec6c5 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,8 @@ 'Yahoo': ['pYsearch'], 'Google': ['google'], 'IRC': ['irc'], - 'mwparserfromhell': ['mwparserfromhell>=0.3.3'] + 'mwparserfromhell': ['mwparserfromhell>=0.3.3'], + 'Tkinter': ['Pillow'] }
if sys.version_info[0] == 2: diff --git a/tests/script_tests.py b/tests/script_tests.py index 8ee4be5..bdb5a9d 100644 --- a/tests/script_tests.py +++ b/tests/script_tests.py @@ -23,9 +23,7 @@ 'script_wui': ['crontab', 'lua'], # Note: package 'lunatic-python' provides module 'lua'
- 'flickrripper': ['PIL.ImageTk', 'flickrapi'], - # Note: 'PIL' is not available via pip2.7 on MS Windows, - # however it is available with setuptools. + 'flickrripper': ['flickrapi'],
'states_redirect': ['pycountry'], } diff --git a/tests/tk_tests.py b/tests/tk_tests.py index 44ec660..efa21a9 100644 --- a/tests/tk_tests.py +++ b/tests/tk_tests.py @@ -7,6 +7,7 @@ # __version__ = '$Id$'
+ import os import sys
@@ -15,15 +16,14 @@ import tkinter as Tkinter else: import Tkinter - from scripts import flickrripper - from pywikibot.userinterfaces.gui import EditBoxWindow + from pywikibot.userinterfaces.gui import EditBoxWindow, Tkdialog
import pywikibot
from tests.aspects import unittest, TestCase, DefaultSiteTestCase
-class TestFlickrRipper(TestCase): +class TestTkdialog(TestCase):
"""Test Tkdialog."""
@@ -32,12 +32,15 @@ @classmethod def setUpClass(cls): if os.environ.get('PYWIKIBOT2_TEST_GUI', '0') != '1': - raise unittest.SkipTest('FlickrRipper tests are disabled on Travis-CI') - super(TestFlickrRipper, cls).setUpClass() + raise unittest.SkipTest('Tkdialog tests are disabled on Travis-CI') + super(TestTkdialog, cls).setUpClass()
def testTkdialog(self): - box = flickrripper.Tkdialog('foo', 'tests/data/MP_sounds.png', 'MP_sounds.png') - box.run() + try: + box = Tkdialog('foo', 'tests/data/MP_sounds.png', 'MP_sounds.png') + box.show_dialog() + except ImportError as e: + pywikibot.warning(e)
class TestTkinter(DefaultSiteTestCase):
pywikibot-commits@lists.wikimedia.org