[kupfer: 1/2] thunderbird: update plugin; fix problem with loading contancts (lp747438)



commit 2e8b4de60f171375d2b3d2e7dc8dfcf9d6c98af9
Author: Karol BÄ?dkowski <karol bedkowski gmail com>
Date:   Sun Apr 10 15:49:03 2011 +0200

    thunderbird: update plugin; fix problem with loading contancts (lp747438)
    
    + mork parser: skipping empty cells when prcessing dangling rows
    + loading also contacts without display/first/last name
    + fix problem with parsing many times one part of mork file - this
      causes showing duplicated and deleted contacts (fix 747438)
    + show only contacts defined in primary (1:80) mork table - this prevent
      showing deleted contacts
    + monitor for updates localstore.rdf - this should help with detecting
      updates of address book files
    + read address books from all possible locations and profiles

 kupfer/plugin/thunderbird.py         |   21 ++----
 kupfer/plugin/thunderbird_support.py |  120 ++++++++++++++++------------------
 2 files changed, 64 insertions(+), 77 deletions(-)
---
diff --git a/kupfer/plugin/thunderbird.py b/kupfer/plugin/thunderbird.py
index d50a7f1..ff0799e 100644
--- a/kupfer/plugin/thunderbird.py
+++ b/kupfer/plugin/thunderbird.py
@@ -5,11 +5,9 @@ __kupfer_name__ = _("Thunderbird")
 __kupfer_sources__ = ("ContactsSource", )
 __kupfer_actions__ = ("NewMailAction", )
 __description__ = _("Thunderbird/Icedove Contacts and Actions")
-__version__ = "2009-12-13"
+__version__ = "2011-04-10"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
-import os
-
 from kupfer.objects import Action
 from kupfer.objects import TextLeaf, UrlLeaf, RunnableLeaf
 from kupfer.obj.apps import AppLeafContentMixin
@@ -21,8 +19,6 @@ from kupfer.obj.contacts import ContactLeaf, EmailContact, email_from_leaf
 from kupfer.plugin import thunderbird_support as support
 
 
-
-
 class ComposeMail(RunnableLeaf):
 	''' Create new mail without recipient '''
 	def __init__(self):
@@ -62,6 +58,7 @@ class NewMailAction(Action):
 	def valid_for_item(self, item):
 		return bool(email_from_leaf(item))
 
+
 class ContactsSource(AppLeafContentMixin, ToplevelGroupingSource,
 		FilesystemWatchMixin):
 	appleaf_content_id = ('thunderbird', 'icedove')
@@ -72,13 +69,14 @@ class ContactsSource(AppLeafContentMixin, ToplevelGroupingSource,
 
 	def initialize(self):
 		ToplevelGroupingSource.initialize(self)
-		abook_dir = support.get_addressbook_dir()
-		if not abook_dir or not os.path.isdir(abook_dir):
-			return
-		self.monitor_token = self.monitor_directories(abook_dir)
+		abook_dirs = list(support.get_addressbook_dirs())
+		if abook_dirs:
+			self.monitor_token = self.monitor_directories(*abook_dirs)
 
 	def monitor_include_file(self, gfile):
-		return gfile and gfile.get_basename().endswith('.mab')
+		print gfile.get_basename()
+		return gfile and (gfile.get_basename().endswith('.mab') \
+				or gfile.get_basename() == 'localstore.rdf')
 
 	def get_items(self):
 		for name, email in support.get_contacts():
@@ -98,6 +96,3 @@ class ContactsSource(AppLeafContentMixin, ToplevelGroupingSource,
 	def provides(self):
 		yield ContactLeaf
 		yield RunnableLeaf
-
-
-
diff --git a/kupfer/plugin/thunderbird_support.py b/kupfer/plugin/thunderbird_support.py
index 75a89b2..cedf247 100644
--- a/kupfer/plugin/thunderbird_support.py
+++ b/kupfer/plugin/thunderbird_support.py
@@ -8,7 +8,7 @@ from ConfigParser import RawConfigParser
 
 from kupfer import pretty
 
-__version__ = "2011-01-20"
+__version__ = "2011-04-10"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 '''
@@ -22,8 +22,7 @@ Concept for mork parser from:
 THUNDERBIRD_HOME = map(os.path.expanduser,
 		('~/.mozilla-thunderbird/', '~/.thunderbird', '~/.icedove/'))
 
-THUNDERBIRD_PROFILES = [
-		(thome, os.path.join(thome, 'profiles.ini'))
+THUNDERBIRD_PROFILES = [(thome, os.path.join(thome, 'profiles.ini'))
 		for thome in THUNDERBIRD_HOME]
 
 
@@ -168,17 +167,19 @@ def _read_mork(filename):
 				if not active_trans or tran != '-':
 					rowdata = row[2:]
 					for rowcell in rowdata:
+						if not rowcell:
+							continue
 						for cell in RE_CELL.findall(rowcell):
 							atom, col = None, None
-							match = RE_CELL_TEXT.match(cell)
-							if match:
-								col = cells.get(match.group(1))
-								atom = match.group(2)
+							cmatch = RE_CELL_TEXT.match(cell)
+							if cmatch:
+								col = cells.get(cmatch.group(1))
+								atom = cmatch.group(2)
 							else:
-								match = RE_CELL_OID.match(cell)
-								if match:
-									col = cells.get(match.group(1))
-									atom = atoms.get(match.group(2))
+								cmatch = RE_CELL_OID.match(cell)
+								if cmatch:
+									col = cells.get(cmatch.group(1))
+									atom = atoms.get(cmatch.group(2))
 							if col and atom:
 								table.add_cell(rowid, col, atom)
 			pos = match.span()[1]
@@ -207,22 +208,25 @@ def _read_mork(filename):
 					table.del_row(rowid)
 			if tran != '-':
 				rowdata = row[2:]
-				if not table:
-					table = tables['1:80'] = _Table('1:80')
-				for rowcell in rowdata:
-					for cell in RE_CELL.findall(rowcell):
-						atom, col = None, None
-						match = RE_CELL_TEXT.match(cell)
-						if match:
-							col = cells.get(match.group(1))
-							atom = match.group(2)
-						else:
-							match = RE_CELL_OID.match(cell)
-							if match:
-								col = cells.get(match.group(1))
-								atom = atoms.get(match.group(2))
-						if col and atom:
-							table.add_cell(rowid, col, atom)
+				if rowdata:
+					if not table:
+						table = tables['1:80'] = _Table('1:80')
+					for rowcell in rowdata:
+						if not rowcell:
+							continue
+						for cell in RE_CELL.findall(str(rowcell)):
+							atom, col = None, None
+							cmatch = RE_CELL_TEXT.match(cell)
+							if cmatch:
+								col = cells.get(cmatch.group(1))
+								atom = cmatch.group(2)
+							else:
+								cmatch = RE_CELL_OID.match(cell)
+								if cmatch:
+									col = cells.get(cmatch.group(1))
+									atom = atoms.get(cmatch.group(2))
+							if col and atom:
+								table.add_cell(rowid, col, atom)
 			pos = match.span()[1]
 			continue
 
@@ -234,67 +238,55 @@ def _mork2contacts(tables):
 	''' Get contacts from mork table prepared by _read_mork '''
 	if not tables:
 		return
-
-	for table in tables.itervalues():
+	# get only default table
+	table = tables.get('1:80')
+	if table:
 		for row in table.rows.itervalues():
 			display_name = row.get('DisplayName')
 			if not display_name:
 				first_name = row.get('FirstName', '')
 				last_name = row.get('LastName', '')
 				display_name = ' '.join((first_name, last_name))
-
-			display_name = display_name.strip()
-			if not display_name:
-				continue
+			if display_name:
+				display_name = display_name.strip()
 			for key in ('PrimaryEmail', 'SecondEmail'):
 				email = row.get(key)
 				if email:
-					yield (display_name, email)
+					yield (display_name or email[:email.find('@')], email)
 
 
-def get_addressbook_dir():
+def get_addressbook_dirs():
 	''' Get path to addressbook file from default profile. '''
-	thunderbird_home = None
 	for thome, tprofile in THUNDERBIRD_PROFILES:
 		if os.path.isfile(tprofile):
-			thunderbird_home = thome
-			break
-	if not thunderbird_home:
-		return None
-	config = RawConfigParser()
-	config.read(tprofile)
-	path = None
-	for section in config.sections():
-		if config.has_option(section, "Default") and \
-				config.get(section, "Default") == "1" and \
-				config.has_option(section, "Path"):
-			path = config.get(section, "Path")
-			break
-		elif config.has_option(section, "Path"):
-			path = config.get(section, "Path")
-	if path:
-		path = os.path.join(thunderbird_home, path)
-	# I thought it was strange to return something that is constant here
-	return path
+			config = RawConfigParser()
+			config.read(tprofile)
+			for section in config.sections():
+				if config.has_option(section, "Path"):
+					path = config.get(section, "Path")
+					if not os.path.isabs(path):
+						path = os.path.join(thome, path)
+					if os.path.isdir(path):
+						yield path
 
 
 def get_addressbook_files():
 	''' Get full path to all Thunderbird address book files. '''
-	path = get_addressbook_dir()
-	if not path:
-		return
-	files = os.listdir(path)
-	for filename in files:
-		if filename.endswith('.mab'):
-			fullpath = os.path.join(path, filename)
-			if os.path.isfile(fullpath):
-				yield fullpath
+	for path in get_addressbook_dirs():
+		pretty.print_debug(__name__, 'get_addressbook_files dir:', path)
+		files = os.listdir(path)
+		for filename in files:
+			if filename.endswith('.mab'):
+				fullpath = os.path.join(path, filename)
+				if os.path.isfile(fullpath):
+					yield fullpath
 
 
 def get_contacts():
 	''' Get all contacts from all Thunderbird address books as
 		((contact name, contact email)) '''
 	for abook in get_addressbook_files():
+		pretty.print_debug(__name__, 'get_contacts:', abook)
 		try:
 			tables = _read_mork(abook)
 		except IOError, err:



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