PyXR

c:\projects\bitpim\src \ nameparser.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2003-2004 Roger Binns <rogerb@rogerbinns.com>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: nameparser.py 3632 2006-10-27 03:08:31Z djpham $
0009 
0010 """Various routines that deal with names"""
0011 
0012 def formatfullname(name):
0013     """Returns a string of the name, including all fields that are present"""
0014 
0015     res=""
0016     full=name.get("full", "")
0017     fml=' '.join([x for x in getparts(name) if x])
0018 
0019     if len(fml) or len(full):
0020         # are they the same
0021         if fml==full:
0022             res+=full
0023         else:
0024             # different
0025             if len(full):
0026                 res+=full
0027             if len(fml):
0028                 if len(res):
0029                     res+=" | "
0030                 res+=fml
0031 
0032     if name.has_key("nickname"):
0033         res+=" ("+name["nickname"]+")"
0034     return res
0035 
0036 def formatsimplename(name):
0037     "like L{formatname}, except we use the first matching component"
0038     _fullname=getfullname(name)
0039     if _fullname:
0040         return _fullname
0041     return name.get('nickname', "")
0042 
0043 def formatsimplefirstlast(name):
0044     "Returns the name formatted as First Middle Last"
0045     return ' '.join([x for x in getparts(name) if x])
0046 def formatsimplelastfirst(name):
0047     "Returns the name formatted as Last, First Middle"
0048     f,m,l=getparts(name)
0049     if len(l):
0050         if len(f+m):
0051             return l+", "+" ".join([f,m])
0052         return l
0053     return " ".join([f,m])
0054 
0055 def getfullname(name):
0056     """Gets the full name, joining the first/middle/last if necessary"""
0057     if name.has_key("full"):
0058         return name["full"]
0059     return ' '.join([x for x in getparts(name) if x])
0060 
0061 # See the following references for name parsing and how little fun it
0062 # is.
0063 #
0064 # The simple way:
0065 # http://cvs.gnome.org/lxr/source/evolution-data-server/addressbook/libebook/
0066 # e-name-western*
0067 #
0068 # The "proper" way:
0069 # http://cvs.xemacs.org/viewcvs.cgi/XEmacs/packages/xemacs-packages/mail-lib/mail-extr.el
0070 #
0071 # How we do it
0072 #
0073 #  [1] The name is split into white-space seperated parts
0074 #  [2] If there is only one part, it becomes the firstname
0075 #  [3] If there are only two parts, they become first name and surname
0076 #  [4] For three or more parts, the first part is the first name and the last
0077 #      part is the surname.  Then while the last part of the remainder starts with
0078 #      a lower case letter or is in the list below, it is prepended to the surname.
0079 #      Whatever is left becomes the middle name.
0080 
0081 lastparts= [ "van", "von", "de", "di" ]
0082 
0083 # I would also like to proudly point out that this code has no comment saying
0084 # "Have I no shame".  It will be considered incomplete until that happens
0085 
0086 def _getparts_FML(name):
0087     n=name.get("full")
0088     
0089     # [1]
0090     parts=n.split()
0091 
0092     # [2]
0093     if len(parts)<=1:
0094         return (n, "", "")
0095     
0096     # [3]
0097     if len(parts)==2:
0098         return (parts[0], "", parts[1])
0099 
0100     # [4]
0101     f=[parts[0]]
0102     m=[]
0103     l=[parts[-1]]
0104     del parts[0]
0105     del parts[-1]
0106     while len(parts) and (parts[-1][0].lower()==parts[-1][0] or parts[-1].lower() in lastparts):
0107         l=[parts[-1]]+l
0108         del parts[-1]
0109     m=parts
0110 
0111     # return it all
0112     return (" ".join(f), " ".join(m), " ".join(l))
0113 
0114 def _getparts_LFM(name):
0115     n=name.get("full")
0116     
0117     parts=n.split(',')
0118 
0119     if len(parts)<=1:
0120         return (n, '', '')
0121     
0122     _last=parts[0]
0123     _first=''
0124     _middle=''
0125     parts=parts[1].split()
0126     if len(parts)>=1:
0127         _first=parts[0]
0128         if len(parts)>1:
0129             _middle=' '.join(parts[1:])
0130     return (_first, _middle, _last)
0131     
0132 def getparts(name):
0133     """Returns (first, middle, last) for name.  If the part doesn't exist
0134     then a blank string is returned"""
0135 
0136     # do we have any of the parts?
0137     for i in ("first", "middle", "last"):
0138         if name.has_key(i):
0139             return (name.get("first", ""), name.get("middle", ""), name.get("last", ""))
0140 
0141     # check we have full.  if not return nickname
0142     if not name.has_key("full"):
0143         return (name.get("nickname", ""), "", "")
0144 
0145     n=name.get("full")
0146     if ',' in n:
0147         return _getparts_LFM(name)
0148     return _getparts_FML(name)
0149 
0150 # convenience functions
0151 
0152 def getfirst(name):
0153     return getparts(name)[0]
0154 
0155 def getmiddle(name):
0156     return getparts(name)[1]
0157 
0158 def getlast(name):
0159     return getparts(name)[2]
0160 

Generated by PyXR 0.9.4