Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 91 lines (72 sloc) 2.65 KB
#!/usr/bin/env python3
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from xml.dom import minidom
import glob
import os
import sys
class Match(object):
def __init__(self, name, email):
self.name = name
self.email = email
def __eq__(self, other):
# Intentionally ignore name here.
return self.email == other.email
def __lt__(self, other):
# Intentionally ignore name here.
return self.email < other.email
def __hash__(self):
# Intentionally ignore name here.
return hash(self.email)
def select(xmlPath, arg):
def getName(entryNode):
nameNodes = [i for i in entryNode.childNodes if i.localName == "name"]
for nameNode in nameNodes:
fullNameNodes = [i for i in nameNode.childNodes if i.localName == "fullName"]
for fullNameNode in fullNameNodes:
return fullNameNode.childNodes[0].wholeText
def getEmails(entryNode):
emails = []
emailNodes = [i for i in entryNode.childNodes if i.localName == "email"]
for emailNode in emailNodes:
if emailNode.hasAttribute("address"):
emails.append(emailNode.getAttribute("address"))
return emails
def match(pattern, name, email):
if email:
pattern = pattern.lower()
name = name.lower()
email = email.lower()
if pattern in name:
return True
elif pattern in email:
return True
return False
patternString = arg
if not os.path.exists(xmlPath):
return
contacts = minidom.parse(xmlPath)
feedNode = [i for i in contacts.childNodes if i.localName == "feed"][0]
contactNodes = [i for i in feedNode.childNodes if i.localName == "entry"]
matches = []
for contactNode in contactNodes:
name = getName(contactNode)
emails = getEmails(contactNode)
for email in emails:
if match(patternString, name, email):
matches.append(Match(name, email))
matches = sorted(set(matches))
for i in matches:
lineString = '"%s" <%s>' % (i.name, i.email)
print(lineString)
def main(arg):
# The directory is expected to have multiple XML files as data sources.
configPath = os.path.expanduser("~/.mutt/contacts")
for xmlPath in glob.glob(os.path.join(configPath, "*.xml")):
select(xmlPath, arg)
if __name__ == "__main__":
main(sys.argv[1])
# vim:set shiftwidth=4 softtabstop=4 expandtab: