[kupfer: 14/51] grouping: Unify email convenience functions in obj.contacts.



commit 645fe750780ac466185537ab23e981068ab49a09
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Jan 7 03:12:19 2010 +0100

    grouping: Unify email convenience functions in obj.contacts.
    
    Sharing common code is good: Lines deleted are lines saved.

 kupfer/obj/contacts.py       |   35 +++++++++++++++++++++++++++++++++++
 kupfer/plugin/clawsmail.py   |   30 ++++--------------------------
 kupfer/plugin/thunderbird.py |   34 +++-------------------------------
 3 files changed, 42 insertions(+), 57 deletions(-)
---
diff --git a/kupfer/obj/contacts.py b/kupfer/obj/contacts.py
index 448fa5d..8c638bd 100644
--- a/kupfer/obj/contacts.py
+++ b/kupfer/obj/contacts.py
@@ -1,7 +1,15 @@
 # -*- encoding: utf-8 -*-
 """
 Kupfer's Contacts API
+
+Main definition and *constructor* classes.
+
+Constructor classes such as EmailContact are used to conveniently construct
+contacts with common traits. To *use* contacts, always use ContactLeaf, asking
+for specific slots to be filled.
 """
+import re
+
 from kupfer.obj.grouping import GroupingLeaf
 
 __author__ = ("Ulrik Sverdrup <ulrik sverdrup gmail com>, "
@@ -18,6 +26,33 @@ class ContactLeaf(GroupingLeaf):
 	def get_icon_name(self):
 		return "stock_person"
 
+
+## E-mail convenience and constructors
+
+def _get_email_from_url(url):
+	''' convert http://foo bar pl -> foo bar pl '''
+	sep = url.find('://')
+	return url[sep+3:] if sep > -1 else url
+
+_CHECK_EMAIL_RE = re.compile(r"^[a-z0-9\ _%-+]+\ [a-z0-9 _%-]+\ [a-z]{2,6}$")
+
+def _check_email(email):
+	''' simple email check '''
+	return len(email) > 7 and _CHECK_EMAIL_RE.match(email.lower()) is not None
+
+def email_from_leaf(leaf):
+	"""
+	Return an email address string if @leaf has a valid email address.
+
+	@leaf may also be a TextLeaf or UrlLeaf.
+	Return a false value if no valid email is found.
+	"""
+	if isinstance(leaf, ContactLeaf):
+		return EMAIL_KEY in leaf and leaf[EMAIL_KEY]
+	email = _get_email_from_url(leaf.object)
+	return _check_email(email) and email
+
+
 class EmailContact (ContactLeaf):
 	def __init__(self, email, name):
 		slots = {EMAIL_KEY: email, NAME_KEY: name}
diff --git a/kupfer/plugin/clawsmail.py b/kupfer/plugin/clawsmail.py
index 170cbc5..9831c01 100644
--- a/kupfer/plugin/clawsmail.py
+++ b/kupfer/plugin/clawsmail.py
@@ -1,6 +1,5 @@
 # -*- coding: UTF-8 -*-
 import os
-import re
 from xml.dom import minidom
 
 from kupfer.objects import Leaf, Action, Source
@@ -9,29 +8,16 @@ from kupfer.objects import (TextLeaf, UrlLeaf, RunnableLeaf, FileLeaf,
 from kupfer import utils
 from kupfer.helplib import FilesystemWatchMixin
 from kupfer.obj.grouping import ToplevelGroupingSource
-from kupfer.obj.contacts import EMAIL_KEY, ContactLeaf, EmailContact
+from kupfer.obj.contacts import EMAIL_KEY, ContactLeaf, EmailContact, email_from_leaf
 
 __kupfer_name__ = _("Claws Mail")
 __kupfer_sources__ = ("ClawsContactsSource", )
 __kupfer_actions__ = ("NewMailAction", "SendFileByMail")
 __description__ = _("Claws Mail Contacts and Actions")
-__version__ = "0.2"
+__version__ = "2010-01-07"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 
-def _get_email_from_url(url):
-	''' convert http://foo bar pl -> foo bar pl '''
-	sep = url.find('://')
-	return url[sep+3:] if sep > -1 else url
-
-_CHECK_EMAIL_RE = re.compile(r"^[a-z0-9\ _%-+]+\ [a-z0-9 _%-]+\ [a-z]{2,6}$")
-
-def _check_email(email):
-	''' simple email check '''
-	return len(email) > 7 and _CHECK_EMAIL_RE.match(email.lower()) is not None
-
-
-
 class ComposeMail(RunnableLeaf):
 	''' Create new mail without recipient '''
 	def __init__(self):
@@ -62,21 +48,13 @@ class ReceiveMail(RunnableLeaf):
 		return "mail-send-receive"
 
 
-def _email_from_leaf(leaf):
-	if isinstance(leaf, UrlLeaf):
-		return _check_email(leaf.object) and _get_email_from_url(leaf.object)
-	if isinstance(leaf, TextLeaf):
-		return _check_email(leaf.object) and leaf.object
-	if isinstance(leaf, ContactLeaf):
-		return EMAIL_KEY in leaf and leaf[EMAIL_KEY]
-
 class NewMailAction(Action):
 	''' Create new mail to selected leaf'''
 	def __init__(self):
 		Action.__init__(self, _('Compose New Mail To'))
 
 	def activate(self, leaf):
-		email = _email_from_leaf(leaf)
+		email = email_from_leaf(leaf)
 		utils.launch_commandline("claws-mail --compose '%s'" % email)
 
 	def get_icon_name(self):
@@ -89,7 +67,7 @@ class NewMailAction(Action):
 		yield UrlLeaf
 
 	def valid_for_item(self, item):
-		return bool(_email_from_leaf(item))
+		return bool(email_from_leaf(item))
 
 class SendFileByMail(Action):
 	''' Createn new mail and attach selected file'''
diff --git a/kupfer/plugin/thunderbird.py b/kupfer/plugin/thunderbird.py
index 75fb8bf..00b96fb 100644
--- a/kupfer/plugin/thunderbird.py
+++ b/kupfer/plugin/thunderbird.py
@@ -3,14 +3,13 @@
 from __future__ import with_statement
 
 import os
-import re
 
 from kupfer.objects import Leaf, Action, Source
 from kupfer.objects import TextLeaf, UrlLeaf, RunnableLeaf, AppLeafContentMixin
 from kupfer.helplib import FilesystemWatchMixin
 from kupfer import utils, icons
 from kupfer.obj.grouping import ToplevelGroupingSource
-from kupfer.obj.contacts import EMAIL_KEY, ContactLeaf, EmailContact
+from kupfer.obj.contacts import EMAIL_KEY, ContactLeaf, EmailContact, email_from_leaf
 
 from kupfer.plugin import thunderbird_support as support
 
@@ -22,17 +21,6 @@ __version__ = "2009-12-13"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 
-def _get_email_from_url(url):
-	''' convert http://foo bar pl -> foo bar pl '''
-	sep = url.find('://')
-	return url[sep+3:] if sep > -1 else url
-
-_CHECK_EMAIL_RE = re.compile(r"^[a-z0-9\ _%-+]+\ [a-z0-9 _%-]+\ [a-z]{2,6}$")
-
-def _check_email(email):
-	''' simple email check '''
-	return len(email) > 7 and _CHECK_EMAIL_RE.match(email.lower()) is not None
-
 
 class ComposeMail(RunnableLeaf):
 	''' Create new mail without recipient '''
@@ -56,12 +44,7 @@ class NewMailAction(Action):
 		Action.__init__(self, _('Compose New Mail To'))
 
 	def activate(self, leaf):
-		if isinstance(leaf, ContactLeaf):
-			email = leaf[EMAIL_KEY]
-		elif isinstance(leaf, UrlLeaf):
-			email = _get_email_from_url(email)
-		else:
-			email = leaf.object
+		email = email_from_leaf(leaf)
 
 		if not utils.launch_commandline("thunderbird mailto:%s"; % email):
 			utils.launch_commandline("icedove mailto:%s"; % email)
@@ -76,18 +59,7 @@ class NewMailAction(Action):
 		yield UrlLeaf
 
 	def valid_for_item(self, item):
-		if isinstance(item, ContactLeaf):
-			return EMAIL_KEY in item
-
-		elif isinstance(item, TextLeaf):
-			return _check_email(item.object)
-
-		elif isinstance(item, UrlLeaf):
-			url = _get_email_from_url(item.object)
-			return _check_email(url)
-
-		return False
-
+		return bool(email_from_leaf(item))
 
 class ContactsSource(AppLeafContentMixin, Source, FilesystemWatchMixin):
 	appleaf_content_id = ('thunderbird', 'icedove')



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]