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

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

Improvements in date and person handling.

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?
121            for t in helper.find(elem,'Contact_Address'):a.append(t.text)
122            self.core['address']=a
123            self.core['name']=helper.getText(elem,'First_Name')+' '+helper.getText(elem,'Last_Name')
124        else:
125            raise ValueError('DIF contact type not understood [%s]'%elem.tag)
126    def toHTML(self):
127        if self.core is None: 
128            return 'Not Found'
129        else: return self.core.toHTML()
130    def url(self):
131        if self.core is None:
132            return 'Not Found'
133        else: return self.core.url()
134   
135       
136
137class DIFAuthors:
138    ''' Attempt to get dataset authorship information from a DIF '''
139    def __init__(self,elem,helper=None):
140        if helper is None: helper=nsdumb()
141        # first try for an investigator in the personnel section
142        authors=['Investigator','Dataset_Creator','Dataset_Author']
143        people=helper.findall(elem,'Personnel')
144        a,e='',''
145        people=helper.findall(elem,'Personnel')
146        for p in people:
147            role=helper.getText(p,'Role')
148            if role=='Investigator':
149                a+='%s, %s;'%(helper.getText(p,'Last_Name'),helper.getText(p,'First_Name'))
150                e=helper.getText(p,'Email')
151        if a!='':a=a[0:-1]
152        if a=='':
153            # carry on looking in the citation element instead
154            a=helper.getText(elem,'Data_Set_Citation/Dataset_Creator')
155        self.authors,self.email=a,e
156    def __str__(self):
157        return self.authors
158   
159import unittest
160Example='''
161<Data_Center> 
162<Data_Center_Name> 
163<Short_Name>BODC</Short_Name>
164<Long_Name>British Oceanographic Data Centre</Long_Name>
165</Data_Center_Name>
166<Data_Center_URL>http://www.bodc.ac.uk</Data_Center_URL>
167<Personnel> 
168<Role>Data Center Contact</Role>
169<Last_Name>British Oceanographic Data Centre</Last_Name>
170<Phone>+44 (0)151 795 4884</Phone>
171<Fax>+44 (0)151 795 4912</Fax>
172<Contact_Address> 
173<Address>Joseph Proudman Building</Address>
174<Address>6 Brownlow Street</Address>
175<City>UKPT0811</City>
176<Postal_Code>L3 5DA</Postal_Code>
177<Country>74</Country>
178</Contact_Address>
179</Personnel>
180</Data_Center>
181'''
182Example2='''
183<Data_Center>
184<Data_Center_Name>
185<Short_Name>BODC</Short_Name>
186<Long_Name>British Oceanographic Data Centre</Long_Name>
187</Data_Center_Name>
188<Data_Center_URL>http://www.bodc.ac.uk/</Data_Center_URL>
189<Personnel>
190<Role>BODC Enquiries Officer</Role>
191<First_Name></First_Name>
192<Last_Name></Last_Name>
193<Email>enquiries@bodc.ac.uk</Email>
194<Phone>+44 (0) 151 795 4884</Phone>
195<Fax>+44 (0) 151 795 4912</Fax>
196<Contact_Address>
197<Address>6 Brownlow Street</Address>
198<City>Liverpool</City>
199<Province_or_State>Merseyside</Province_or_State>
200<Postal_Code>L3 5DA</Postal_Code>
201<Country>United Kingdom</Country>
202</Contact_Address>
203</Personnel>
204</Data_Center>'''
205
206
207class TestCase(unittest.TestCase):
208   
209    def testDIFContact(self):
210        '''Test instantiating a DIFContact'''
211        w='<DIF xmlns="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">%s</DIF>'%Example2
212        #w='<wrap>%s</wrap>'%Example2
213        y=xmlHandler(w,string=1)
214        h=nsdumb(y.tree)
215        e=h.find(y.tree,'Data_Center')
216        d=DIFcontact(h,e)
217        print d.toHTML()
218   
219if __name__=="__main__":
220    unittest.main()         
Note: See TracBrowser for help on using the repository browser.