source: TI07-MOLES/trunk/PythonCode/wsgi/People2.py @ 2390

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/People2.py@2390
Revision 2390, 7.5 KB checked in by lawrence, 12 years ago (diff)

Tiny fix for address handling with empty elements

Line 
1from Utilities import wrapGetText
2from ETxmlView import nsdumb
3from htmlUtilities import *
4from xmlHandler import xmlHandler
5
6
7def tr(x):return '<tr>%s</tr>'%x
8def td(x,c=1):return '<td colspan="%s">%s</td>'%(c,x)
9tde='<td></td>'
10
11class Name:
12    ''' Handles standar formatting of a name, for MOLES! '''
13    def __init__(self,helper,elem):
14        self.name=''
15        if elem is None: return 
16        if helper.getText(elem,'nameOrder')=='L2R':
17            o=('title','initials','familyName')
18        else:
19            o=('title','familyName','personalName')
20        for i in o: self.name+=' '+helper.getText(elem,i)
21    def __str__(self):
22        return self.name
23       
24class holder:
25    def __init__(self,model):
26        self.data=model
27        self.empty=1
28    def __getitem__(self,item):
29        return self.data[item]
30    def __setitem__(self,item,val):
31        self.data[item]=val
32        self.empty=0
33    def __str__(self):
34        return str(self.data)
35       
36       
37class Contact(holder):
38    ''' A simple information model and html representation of a contact '''
39    def __init__(self,helper,elem,keys):
40        model={'name':None,'email':'','phone':'','fax':'','address':None,'role':''}
41        holder.__init__(self,model)
42        for k in model:
43            if model[k]=='': 
44                self[k]=helper.getText(elem,keys[k])
45    def toHTML(self):
46        print self
47        if self.empty: return 'Unknown'
48        h=''
49        r=self['role']
50        if r<>'':r+=': '
51        if self['name'] is not None: h+='%s%s<br/>'%(r,self['name'])
52        if self['email']<>'': h+='email:%s<br/>'%self['email']
53        if self['phone']<>'' or self['fax']<>'':
54            h+='Phone:%s, Fax:%s<br/>'%(self['phone'],self['fax'])
55        for i in self['address']: h+=i+'<br/>'
56        return h
57    def url(self):
58        return 'url to a person?'
59
60class Organisation(holder):
61    ''' A simple information modle and html representation of an organisation '''
62    def __init__(self,helper,elem,keys):
63        model={'sname':'','lname':'','address':[],'person':None,'url':''}
64        holder.__init__(self,model)
65        for k in model:
66            if model[k]=='': 
67                self[k]=helper.getText(elem,keys[k])
68                print k,keys[k],self[k],elem.tag
69    def toHTML(self):
70        print self
71        if self.empty: return 'Unknown'
72        h=''
73        if self['lname']=='' and self['sname']=='':
74            return 'Unknown'
75        if self['sname']=='':
76            if self['url']<>'':
77                h+=hyperlink(self['lname'],self['url'])+'<br/>'
78            else: h+=self['lname']+'<br/>'
79        else:
80            u=self.url()
81            if self['lname']<>'':
82                h+='%s (%s)<br/>'%(self['lname'],u)
83            else: h+=u+'<br/>'
84        for i in self['address']: h+=i+'<br/'
85        if self['person'] is not None:
86            h+=self['person'].toHTML()
87        return h
88    def url(self):
89        ''' Return hyperlink to centre ''' 
90        if self['url']<>'':
91                u=hyperlink(self['sname'],self['url'])
92        else: u=self['sname']
93        return u
94
95class dgContact(Contact):
96    '''A simple summary of contact information for an organisation or a person '''
97    def __init__(self,elem,helper=None):
98        ''' Instantiate a MOLES contact element '''
99        self.elem=element
100        if helper is None: helper=nsdumb()
101        keys={'email':'contactDetails/eMail','phone':'contactDetails/telephone'}
102        contact.__init__(self,helper,elem,keys)
103               
104class DIFcontact:
105    def __init__(self,helper,elem):
106        okeys={'sname':'Data_Center_Name/Short_Name','lname':'Data_Center_Name/Long_Name',
107                'url':'Data_Center_URL'}
108        pkeys={'role':'Role','name':None,'phone':'Phone','fax':'Fax','address':None,'email':'Email'}
109        if elem is None:
110            self.core=None
111            return
112        if helper.strip(elem.tag)=='Data_Center':
113            self.core=Organisation(helper,elem,okeys)
114            p=helper.find(elem,'Personnel')
115            self.core['person']=DIFcontact(helper,p)
116        elif helper.strip(elem.tag)=='Personnel':
117            self.core=Contact(helper,elem,pkeys)
118            a=[]
119            # we don't need the helper because we're getting the tags from the element.
120            # should we worry about empty elements here? yes!
121            for t in helper.find(elem,'Contact_Address'):
122                if t.text is not None: a.append(t.text)
123            self.core['address']=a
124            self.core['name']=helper.getText(elem,'First_Name')+' '+helper.getText(elem,'Last_Name')
125        else:
126            raise ValueError('DIF contact type not understood [%s]'%elem.tag)
127    def toHTML(self):
128        if self.core is None: 
129            return 'Not Found'
130        else: return self.core.toHTML()
131    def url(self):
132        if self.core is None:
133            return 'Not Found'
134        else: return self.core.url()
135   
136       
137
138class DIFAuthors:
139    ''' Attempt to get dataset authorship information from a DIF '''
140    def __init__(self,elem,helper=None):
141        if helper is None: helper=nsdumb()
142        # first try for an investigator in the personnel section
143        authors=['Investigator','Dataset_Creator','Dataset_Author']
144        people=helper.findall(elem,'Personnel')
145        a,e='',''
146        people=helper.findall(elem,'Personnel')
147        for p in people:
148            role=helper.getText(p,'Role')
149            if role=='Investigator':
150                a+='%s, %s;'%(helper.getText(p,'Last_Name'),helper.getText(p,'First_Name'))
151                e=helper.getText(p,'Email')
152        if a!='':a=a[0:-1]
153        if a=='':
154            # carry on looking in the citation element instead
155            a=helper.getText(elem,'Data_Set_Citation/Dataset_Creator')
156        self.authors,self.email=a,e
157    def __str__(self):
158        return self.authors
159   
160import unittest
161Example='''
162<Data_Center> 
163<Data_Center_Name> 
164<Short_Name>BODC</Short_Name>
165<Long_Name>British Oceanographic Data Centre</Long_Name>
166</Data_Center_Name>
167<Data_Center_URL>http://www.bodc.ac.uk</Data_Center_URL>
168<Personnel> 
169<Role>Data Center Contact</Role>
170<Last_Name>British Oceanographic Data Centre</Last_Name>
171<Phone>+44 (0)151 795 4884</Phone>
172<Fax>+44 (0)151 795 4912</Fax>
173<Contact_Address> 
174<Address>Joseph Proudman Building</Address>
175<Address>6 Brownlow Street</Address>
176<City>UKPT0811</City>
177<Postal_Code>L3 5DA</Postal_Code>
178<Country>74</Country>
179</Contact_Address>
180</Personnel>
181</Data_Center>
182'''
183Example2='''
184<Data_Center>
185<Data_Center_Name>
186<Short_Name>BODC</Short_Name>
187<Long_Name>British Oceanographic Data Centre</Long_Name>
188</Data_Center_Name>
189<Data_Center_URL>http://www.bodc.ac.uk/</Data_Center_URL>
190<Personnel>
191<Role>BODC Enquiries Officer</Role>
192<First_Name></First_Name>
193<Last_Name></Last_Name>
194<Email>enquiries@bodc.ac.uk</Email>
195<Phone>+44 (0) 151 795 4884</Phone>
196<Fax>+44 (0) 151 795 4912</Fax>
197<Contact_Address>
198<Address>6 Brownlow Street</Address>
199<City>Liverpool</City>
200<Province_or_State>Merseyside</Province_or_State>
201<Postal_Code>L3 5DA</Postal_Code>
202<Country>United Kingdom</Country>
203</Contact_Address>
204</Personnel>
205</Data_Center>'''
206
207
208class TestCase(unittest.TestCase):
209   
210    def testDIFContact(self):
211        '''Test instantiating a DIFContact'''
212        w='<DIF xmlns="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">%s</DIF>'%Example2
213        #w='<wrap>%s</wrap>'%Example2
214        y=xmlHandler(w,string=1)
215        h=nsdumb(y.tree)
216        e=h.find(y.tree,'Data_Center')
217        d=DIFcontact(h,e)
218        print d.toHTML()
219   
220if __name__=="__main__":
221    unittest.main()         
Note: See TracBrowser for help on using the repository browser.