talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 39e6a93d9760ee655f8f99252522859b50ae307a
parent 7b3d97649043f857a1c53baec3dcb23eccbd91c6
Author: Ricardo Mones <ricardo@mones.org>
Date:   Fri, 19 Mar 2021 19:25:43 +0100

Update python examples to Python3 with Gtk3

Diffstat:
Msrc/plugins/python/examples/auto/compose_any | 17++++++++---------
Msrc/plugins/python/examples/auto/shutdown | 6------
Msrc/plugins/python/examples/auto/startup | 40+++++++++++++++++++++++-----------------
Msrc/plugins/python/examples/compose/Macro-Expansion | 2+-
Msrc/plugins/python/examples/main/Create-Tomboy-Note | 99++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/plugins/python/examples/main/Mass-mail | 12++++++------
Msrc/plugins/python/examples/main/Open-Tomboy-Notes | 2--
Msrc/plugins/python/examples/main/Print-action-names-to-stdout | 23++++++++++++++++++++++-
Msrc/plugins/python/examples/main/Recusively-mark-messages-as-read | 12++++++------
9 files changed, 119 insertions(+), 94 deletions(-)

diff --git a/src/plugins/python/examples/auto/compose_any b/src/plugins/python/examples/auto/compose_any @@ -16,7 +16,7 @@ import re # prefixes are defined below. # So, for example, replying to a mail with # Subject: R: Re: R: Re: Aw: R: Re: Old topic -# will result in +# will result in # Subject: Re: Old topic # # This is a slightly adapted version of a script provided @@ -25,19 +25,19 @@ def strip_subject_prefixes(): # A list of prefixes to strip during reply. Add the ones that are # interesting for you here. prefixes = ["Re", "R", "Odp", "Aw"] - + # Build up regex to match unwanted prefixes prefix_string = "|".join(prefixes) regex_str = r"^(Re|Fwd|Fw):( (%s):)+" % prefix_string - + # Get a string with those prefixes stripped - new_subject = re.sub(regex_str, r"\1:", clawsmail.compose_window.get_subject()) - + new_subject = re.sub(regex_str, r"\1:", clawsmail.compose_window.get_subject()) + # Set this string to be the new subject clawsmail.compose_window.set_subject(new_subject) - + # Normally, when the subject or body is modified, the mail gets marked - # as modified, wich results in a popup dialog when the compose window is + # as modified, wich results in a popup dialog when the compose window is # just closed. We don't want to treat the automatic modification of the # subject line from above to trigger such a popup, so we override the # modification marker. @@ -47,4 +47,4 @@ def strip_subject_prefixes(): clawsmail.compose_window.set_modified(False) -strip_subject_prefixes() -\ No newline at end of file +strip_subject_prefixes() diff --git a/src/plugins/python/examples/auto/shutdown b/src/plugins/python/examples/auto/shutdown @@ -1,12 +1,7 @@ # -*- coding: utf-8 -*- -import pygtk -pygtk.require('2.0') -import gtk - # stuff that only needs to be cleaned up if claws mail is not exiting anyways if not clawsmail.is_exiting(): - # cleanup menus and actions that have been added in the startup script try: ui_manager = clawsmail.get_mainwindow_ui_manager() @@ -20,4 +15,3 @@ if not clawsmail.is_exiting(): group.remove_action(action) except NameError: pass - diff --git a/src/plugins/python/examples/auto/startup b/src/plugins/python/examples/auto/startup @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- -import pygtk -pygtk.require('2.0') -import gtk +import gi +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk # lists to store information for cleanup in the shutdown script mainwindow_merge_ids = [] mainwindow_actions = [] - # function definitions def add_python_documentation_menu_item(): @@ -24,15 +23,17 @@ def add_python_documentation_menu_item(): global mainwindow_merge_ids global mainwindow_actions - + # create "Python API documentation" menu item group = clawsmail.get_mainwindow_action_group() ui_manager = clawsmail.get_mainwindow_ui_manager() - action = gtk.Action("pydoc", "Python API documentation", None, None) + action = Gtk.Action(name="pydoc", label="Python API documentation") action.connect("activate", pydoc_cb) group.add_action(action) merge_id = ui_manager.new_merge_id() - ui_manager.add_ui(merge_id, "/Menu/Help", "pydoc", "pydoc", gtk.UI_MANAGER_MENUITEM, True) + ui_manager.add_ui( + merge_id, "/Menu/Help", "pydoc", "pydoc", + Gtk.UIManagerItemType.MENUITEM, True) mainwindow_merge_ids.append(merge_id) mainwindow_actions.append(action) @@ -44,19 +45,21 @@ def add_mark_thread_read_menu_item(): group = clawsmail.get_mainwindow_action_group() group.get_action("Edit/SelectThread").activate() group.get_action("Message/Mark/MarkRead").activate() - clawsmail.summaryview_select_messages(selected_messages) + clawsmail.summaryview_select_messages(selected_messages) global mainwindow_merge_ids global mainwindow_actions - + # create "Mark thread read" menu item group = clawsmail.get_mainwindow_action_group() ui_manager = clawsmail.get_mainwindow_ui_manager() - action = gtk.Action("ThreadRead", "Mark thread as read", None, None) + action = Gtk.Action(name="ThreadRead", label="Mark thread as read") action.connect("activate", thread_read_cb) group.add_action_with_accel(action, None) merge_id = ui_manager.new_merge_id() - ui_manager.add_ui(merge_id, "/Menu/Edit", "ThreadRead", "ThreadRead", gtk.UI_MANAGER_MENUITEM, False) + ui_manager.add_ui( + merge_id, "/Menu/Edit", "ThreadRead", "ThreadRead", + Gtk.UIManagerItemType.MENUITEM, False) mainwindow_merge_ids.append(merge_id) mainwindow_actions.append(action) @@ -64,15 +67,18 @@ def add_dbus_interface(): # exports an interface to Claws Mail on the session D-Bus # # Example invokation to trigger an update of the summary view from the command line: - # dbus-send --session --type=method_call --dest=org.ClawsMail.PythonPlugin /org/ClawsMail/PythonPlugin org.ClawsMail.PythonPlugin.MainWindow.TriggerGtkAction string:'View/UpdateSummary' + # dbus-send --session --type=method_call --dest=org.ClawsMail.PythonPlugin \ + # /org/ClawsMail/PythonPlugin \ + # org.ClawsMail.PythonPlugin.MainWindow.TriggerGtkAction \ + # string:'View/UpdateSummary' try: import dbus import dbus.service - from dbus.mainloop.glib import DBusGMainLoop + from dbus.mainloop.glib import DBusGMainLoop except ImportError: - print 'Cannot setup D-Bus interface: D-Bus Python bindings not available.' + print("Can't setup D-Bus interface: D-Bus Python bindings not available.") return None - + class ClawsMailService(dbus.service.Object): @dbus.service.method("org.ClawsMail.PythonPlugin.MainWindow", in_signature='s', out_signature='') def TriggerGtkAction(self, action_path): @@ -80,8 +86,8 @@ def add_dbus_interface(): if action: action.activate() else: - print 'No such action:', action_path - + print('No such action: {}'.format(action_path)) + loop = DBusGMainLoop(set_as_default=True) session_bus = dbus.SessionBus() name = dbus.service.BusName("org.ClawsMail.PythonPlugin", session_bus) diff --git a/src/plugins/python/examples/compose/Macro-Expansion b/src/plugins/python/examples/compose/Macro-Expansion @@ -19,7 +19,7 @@ def get_current_word(buffer): if not end.ends_word(): end.forward_word_end() return (start.get_text(end), start, end) - + buffer = clawsmail.compose_window.text.get_buffer() # get text buffer of body editor (current_word, start, end) = get_current_word(buffer) # get current word under the cursor diff --git a/src/plugins/python/examples/main/Create-Tomboy-Note b/src/plugins/python/examples/main/Create-Tomboy-Note @@ -1,10 +1,13 @@ # -*- Mode: python -*- import dbus -import gtk +import gi +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk + def add_note(msg): - + def get_reminder(desc): get_reminder.reminder = None @@ -20,70 +23,78 @@ def add_note(msg): if cb.get_active(): get_reminder.reminder += "".join([" at ", "%02d" % hours.get_value_as_int(), ":", "%02d" % minutes.get_value_as_int()]) ww.destroy() - + def day_selected(cal, exp): ymd = list(cal.get_date()) ymd[1] += 1 exp.set_label("/".join(str(vv) for vv in ymd)) - + def custom(button, ww, entry): get_reminder.reminder = entry.get_text() ww.destroy() - + # Check if the user wants a reminder in a dialog box - win = gtk.Window() + win = Gtk.Window() win.set_title("Reminder") - win.connect("destroy", gtk.main_quit) - win.set_position(gtk.WIN_POS_CENTER) - table = gtk.Table(2,7) + win.connect("destroy", Gtk.main_quit) + win.set_position(Gtk.WindowPosition.CENTER) + table = Gtk.Table(n_rows=2, n_columns=7) win.add(table) - table.attach(gtk.Label(desc), 0, 2, 0, 1) + table.attach(Gtk.Label(label=desc), 0, 2, 0, 1) # no reminder - button = gtk.Button("No reminder") + button = Gtk.Button(label="No reminder") button.connect("clicked", no_reminder, win) - table.attach(button, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0, ypadding=4) - table.attach(gtk.HSeparator(), 0, 2, 2, 3) + table.attach(button, 0, 1, 1, 2, + xoptions=Gtk.AttachOptions.FILL, yoptions=0, ypadding=4) + table.attach(Gtk.HSeparator(), 0, 2, 2, 3) # date / time reminder - button = gtk.Button("Date/Time") - table.attach(button, 0, 1, 3, 4, xoptions=gtk.FILL, yoptions=gtk.EXPAND | gtk.FILL, ypadding=4) - hbox = gtk.HBox() + button = Gtk.Button(label="Date/Time") + table.attach(button, 0, 1, 3, 4, + xoptions=Gtk.AttachOptions.FILL, + yoptions=Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, ypadding=4) + hbox = Gtk.HBox() table.attach(hbox, 1, 2, 3, 4) - cal = gtk.Calendar() - exp = gtk.Expander() + cal = Gtk.Calendar() + exp = Gtk.Expander() day_selected(cal, exp) cal.connect("day-selected", day_selected, exp) exp.add(cal) - hbox.pack_start(exp) - cb = gtk.CheckButton("at") - hbox.pack_start(cb, False, False) - hours = gtk.SpinButton(gtk.Adjustment(12.0, 0.0, 24.0, 1.0, 5.0, 0.0)) + hbox.pack_start(exp, False, False, 5) + cb = Gtk.CheckButton(label="at") + hbox.pack_start(cb, False, False, 5) + hours = Gtk.SpinButton.new(Gtk.Adjustment( + value=12.0, lower=0.0, upper=24.0, step_increment=1.0, + page_increment=5.0, page_size=0.0), 1.0, 0) hours.set_numeric(True) hours.set_wrap(True) - hbox.pack_start(hours, False, False) - hbox.pack_start(gtk.Label(":"), False, False) - minutes = gtk.SpinButton(gtk.Adjustment(0.0, 0.0, 59.0, 1.0, 5.0, 0.0)) + hbox.pack_start(hours, False, False, 5) + hbox.pack_start(Gtk.Label(label=":"), False, False, 5) + minutes = Gtk.SpinButton.new(Gtk.Adjustment( + value=0.0, lower=0.0, upper=59.0, step_increment=1.0, + page_increment=5.0, page_size=0.0), 1.0, 0) minutes.set_numeric(True) minutes.set_wrap(True) - hbox.pack_start(minutes, False, False) + hbox.pack_start(minutes, False, False, 5) button.connect("clicked", date_time_cb, win, cal, cb, hours, minutes) # custom - button = gtk.Button("custom") - table.attach(button, 0, 1, 4, 5, xoptions=gtk.FILL, yoptions=0, ypadding=4) - entry = gtk.Entry() + button = Gtk.Button(label="custom") + table.attach(button, 0, 1, 4, 5, + xoptions=Gtk.AttachOptions.FILL, yoptions=0, ypadding=4) + entry = Gtk.Entry() button.connect("clicked", custom, win, entry) table.attach(entry, 1, 2, 4, 5) - + # "Show note" toggle option - table.attach(gtk.HSeparator(), 0, 2, 5, 6) - cb = gtk.CheckButton("Show note") + table.attach(Gtk.HSeparator(), 0, 2, 5, 6) + cb = Gtk.CheckButton(label="Show note") table.attach(cb, 0, 2, 6, 7) win.show_all() win.present() - gtk.main() + Gtk.main() return (get_reminder.reminder, cb.get_active()) - + title = msg.Subject # set up contents: opening tag content = ["<note-content>"] @@ -98,13 +109,14 @@ def add_note(msg): content.extend(["!", reminder, "\n"]) # link back to email msgid = msg.MessageID - if msgid[0] != "<": - msgid = "<" + msgid - if msgid[-1] != ">": - msgid += ">" - msgid = msgid.replace("<", "&lt;").replace(">", "&gt;") - link = '<link:cm-mail uri="%s/%s">%s</link:cm-mail>' % (clawsmail.get_folderview_selected_folder().get_identifier(), msgid, msg.Subject) - content.append(link) + if msgid: + if msgid[0] != "<": + msgid = "<" + msgid + if msgid[-1] != ">": + msgid += ">" + msgid = msgid.replace("<", "&lt;").replace(">", "&gt;") + link = '<link:cm-mail uri="%s/%s">%s</link:cm-mail>' % (clawsmail.get_folderview_selected_folder().get_identifier(), msgid, msg.Subject) + content.append(link) #closing tag content.append("</note-content>") @@ -122,9 +134,4 @@ def add_note(msg): # iterate over all notes for msg in clawsmail.get_summaryview_selected_message_list(): add_note(msg) - - - - - diff --git a/src/plugins/python/examples/main/Mass-mail b/src/plugins/python/examples/main/Mass-mail @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # Send the same mail to a list of people, one at a time. -# As this is normal Python code, the message contents and -# the addresses could also come from an external source +# As this is normal Python code, the message contents and +# the addresses could also come from an external source # (such as a file, or a database). addresses = ["mail1@example.tld", "mail2@example.tld", "mail3@example.tld"] @@ -13,17 +13,17 @@ for address in addresses: # in which case the same rules as on a menu click one "New message" # are applied. cw = clawsmail.ComposeWindow("berndth@gmx.de") - + # Add a recipient. There are also add_Cc and add_Bcc functions. cw.add_To(address) - + # Set the subject of the message cw.set_subject("Mass mail") - + # For the message body, access to the GtkTextView is granted in ComposeWindow.text. buffer = cw.text.get_buffer() buffer.set_text("This is an automatic message") - + # Access to the GtkUIManager is also provided, look for "send later" action action = None for action_group in cw.ui_manager.get_action_groups(): diff --git a/src/plugins/python/examples/main/Open-Tomboy-Notes b/src/plugins/python/examples/main/Open-Tomboy-Notes @@ -1,9 +1,7 @@ # -*- Mode: python -*- import re - import dbus -import gtk # collect message ids of all selected messages msgids = set() diff --git a/src/plugins/python/examples/main/Print-action-names-to-stdout b/src/plugins/python/examples/main/Print-action-names-to-stdout @@ -1,5 +1,26 @@ # -*- coding: utf-8 -*- +import gi +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk + action_group = clawsmail.get_mainwindow_action_group() +actions = [] for action in action_group.list_actions(): - print action.get_name() + actions.append(action.get_name()) +actions.sort() + +win = Gtk.Window(title="List of actions") +win.connect("destroy", Gtk.main_quit) +win.set_position(Gtk.WindowPosition.CENTER) +swin = Gtk.ScrolledWindow(None, None) +swin.set_min_content_height(400) +swin.set_min_content_width(600) +buf = Gtk.TextBuffer() +buf.set_text('\n'.join(actions)) +view = Gtk.TextView.new_with_buffer(buf) +swin.add(view) +win.add(swin) +win.show_all() +win.present() +Gtk.main() diff --git a/src/plugins/python/examples/main/Recusively-mark-messages-as-read b/src/plugins/python/examples/main/Recusively-mark-messages-as-read @@ -7,13 +7,13 @@ def deal_with_folder(folder): action_group = clawsmail.get_mainwindow_action_group(); select_all_action = action_group.get_action("Edit/SelectAll") mark_read_action = action_group.get_action("Message/Mark/MarkRead") - + # Select given folder clawsmail.folderview_select_folder(folder) - + # Search for messages with age greater than 28 days clawsmail.quicksearch_search("ag 28", clawsmail.QUICK_SEARCH_EXTENDED) - + # Mark all messages in the search result as read select_all_action.activate() mark_read_action.activate() @@ -28,12 +28,12 @@ if root is not None: # Get a tree of subfolders. The argument could also be a string of a mailbox name, # or left out for a list of mailbox trees. tree = clawsmail.get_folder_tree(root) - + # Call above function for all folders. tree.traverse(deal_with_folder) - + # Clear the quicksearch widget again clawsmail.quicksearch_clear() - + # Change back to original folder clawsmail.folderview_select(root)