Module nameparser
[hide private]
[frames] | no frames]

Source Code for Module nameparser

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2003-2004 Roger Binns <rogerb@rogerbinns.com> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: nameparser.py 3632 2006-10-27 03:08:31Z djpham $ 
  9   
 10  """Various routines that deal with names""" 
 11   
12 -def formatfullname(name):
13 """Returns a string of the name, including all fields that are present""" 14 15 res="" 16 full=name.get("full", "") 17 fml=' '.join([x for x in getparts(name) if x]) 18 19 if len(fml) or len(full): 20 # are they the same 21 if fml==full: 22 res+=full 23 else: 24 # different 25 if len(full): 26 res+=full 27 if len(fml): 28 if len(res): 29 res+=" | " 30 res+=fml 31 32 if name.has_key("nickname"): 33 res+=" ("+name["nickname"]+")" 34 return res
35
36 -def formatsimplename(name):
37 "like L{formatname}, except we use the first matching component" 38 _fullname=getfullname(name) 39 if _fullname: 40 return _fullname 41 return name.get('nickname', "")
42
43 -def formatsimplefirstlast(name):
44 "Returns the name formatted as First Middle Last" 45 return ' '.join([x for x in getparts(name) if x])
46 -def formatsimplelastfirst(name):
47 "Returns the name formatted as Last, First Middle" 48 f,m,l=getparts(name) 49 if len(l): 50 if len(f+m): 51 return l+", "+" ".join([f,m]) 52 return l 53 return " ".join([f,m])
54
55 -def getfullname(name):
56 """Gets the full name, joining the first/middle/last if necessary""" 57 if name.has_key("full"): 58 return name["full"] 59 return ' '.join([x for x in getparts(name) if x])
60 61 # See the following references for name parsing and how little fun it 62 # is. 63 # 64 # The simple way: 65 # http://cvs.gnome.org/lxr/source/evolution-data-server/addressbook/libebook/ 66 # e-name-western* 67 # 68 # The "proper" way: 69 # http://cvs.xemacs.org/viewcvs.cgi/XEmacs/packages/xemacs-packages/mail-lib/mail-extr.el 70 # 71 # How we do it 72 # 73 # [1] The name is split into white-space seperated parts 74 # [2] If there is only one part, it becomes the firstname 75 # [3] If there are only two parts, they become first name and surname 76 # [4] For three or more parts, the first part is the first name and the last 77 # part is the surname. Then while the last part of the remainder starts with 78 # a lower case letter or is in the list below, it is prepended to the surname. 79 # Whatever is left becomes the middle name. 80 81 lastparts= [ "van", "von", "de", "di" ] 82 83 # I would also like to proudly point out that this code has no comment saying 84 # "Have I no shame". It will be considered incomplete until that happens 85
86 -def _getparts_FML(name):
87 n=name.get("full") 88 89 # [1] 90 parts=n.split() 91 92 # [2] 93 if len(parts)<=1: 94 return (n, "", "") 95 96 # [3] 97 if len(parts)==2: 98 return (parts[0], "", parts[1]) 99 100 # [4] 101 f=[parts[0]] 102 m=[] 103 l=[parts[-1]] 104 del parts[0] 105 del parts[-1] 106 while len(parts) and (parts[-1][0].lower()==parts[-1][0] or parts[-1].lower() in lastparts): 107 l=[parts[-1]]+l 108 del parts[-1] 109 m=parts 110 111 # return it all 112 return (" ".join(f), " ".join(m), " ".join(l))
113
114 -def _getparts_LFM(name):
115 n=name.get("full") 116 117 parts=n.split(',') 118 119 if len(parts)<=1: 120 return (n, '', '') 121 122 _last=parts[0] 123 _first='' 124 _middle='' 125 parts=parts[1].split() 126 if len(parts)>=1: 127 _first=parts[0] 128 if len(parts)>1: 129 _middle=' '.join(parts[1:]) 130 return (_first, _middle, _last)
131
132 -def getparts(name):
133 """Returns (first, middle, last) for name. If the part doesn't exist 134 then a blank string is returned""" 135 136 # do we have any of the parts? 137 for i in ("first", "middle", "last"): 138 if name.has_key(i): 139 return (name.get("first", ""), name.get("middle", ""), name.get("last", "")) 140 141 # check we have full. if not return nickname 142 if not name.has_key("full"): 143 return (name.get("nickname", ""), "", "") 144 145 n=name.get("full") 146 if ',' in n: 147 return _getparts_LFM(name) 148 return _getparts_FML(name)
149 150 # convenience functions 151
152 -def getfirst(name):
153 return getparts(name)[0]
154
155 -def getmiddle(name):
156 return getparts(name)[1]
157
158 -def getlast(name):
159 return getparts(name)[2]
160