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

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

Tiny modification to citation 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,name=''):
40        model={'name':None,'email':'','phone':'','fax':'','address':[],'role':''}
41        holder.__init__(self,model)
42        if name=='':
43            for k in model:
44                if model[k]=='': 
45                    self[k]=helper.getText(elem,keys[k])
46        else:
47            self['name']=name
48    def toHTML(self):
49        print self
50        if self.empty: return 'Unknown'
51        h=''
52        r=self['role']
53        if r<>'':r+=': '
54        if self['name'] is not None: h+='%s%s<br/>'%(r,self['name'])
55        if self['email']<>'': h+='email:%s<br/>'%self['email']
56        if self['phone']<>'' or self['fax']<>'':
57            h+='Phone:%s, Fax:%s<br/>'%(self['phone'],self['fax'])
58        for i in self['address']: h+=i+'<br/>'
59        return h
60    def url(self):
61        return 'url to a person?'
62
63class Organisation(holder):
64    ''' A simple information modle and html representation of an organisation '''
65    def __init__(self,helper,elem,keys):
66        model={'sname':'','lname':'','address':[],'person':None,'url':''}
67        holder.__init__(self,model)
68        for k in model:
69            if model[k]=='': 
70                self[k]=helper.getText(elem,keys[k])
71                print k,keys[k],self[k],elem.tag
72    def toHTML(self):
73        print self
74        if self.empty: return 'Unknown'
75        h=''
76        if self['lname']=='' and self['sname']=='':
77            return 'Unknown'
78        if self['sname']=='':
79            if self['url']<>'':
80                h+=hyperlink(self['lname'],self['url'])+'<br/>'
81            else: h+=self['lname']+'<br/>'
82        else:
83            u=self.url()
84            if self['lname']<>'':
85                h+='%s (%s)<br/>'%(self['lname'],u)
86            else: h+=u+'<br/>'
87        for i in self['address']: h+=i+'<br/'
88        if self['person'] is not None:
89            h+=self['person'].toHTML()
90        return h
91    def url(self):
92        ''' Return hyperlink to centre ''' 
93        if self['url']<>'':
94                u=hyperlink(self['sname'],self['url'])
95        else: u=self['sname']
96        return u
97
98class dgContact(Contact):
99    '''A simple summary of contact information for an organisation or a person '''
100    def __init__(self,elem,helper=None):
101        ''' Instantiate a MOLES contact element '''
102        self.elem=element
103        if helper is None: helper=nsdumb()
104        keys={'email':'contactDetails/eMail','phone':'contactDetails/telephone'}
105        contact.__init__(self,helper,elem,keys)
106               
107class DIFcontact:
108    def __init__(self,helper,elem,name=''):
109        okeys={'sname':'Data_Center_Name/Short_Name','lname':'Data_Center_Name/Long_Name',
110                'url':'Data_Center_URL'}
111        pkeys={'role':'Role','name':None,'phone':'Phone','fax':'Fax','address':None,'email':'Email'}
112        if name=='':
113            if elem is None:
114                self.core=None
115                return
116        else:
117            self.core=Contact(0,0,0,name=name)
118            return
119        if helper.strip(elem.tag)=='Data_Center':
120            self.core=Organisation(helper,elem,okeys)
121            p=helper.find(elem,'Personnel')
122            self.core['person']=DIFcontact(helper,p)
123        elif helper.strip(elem.tag)=='Personnel':
124            self.core=Contact(helper,elem,pkeys)
125            a=[]
126            # we don't need the helper because we're getting the tags from the element.
127            # should we worry about empty elements here? yes!
128            for t in helper.find(elem,'Contact_Address'):
129                if t.text is not None: a.append(t.text)
130            self.core['address']=a
131            self.core['name']=self.__handleName(
132                    helper.getText(elem,'First_Name'),helper.getText(elem,'Last_Name'))
133        else:
134            raise ValueError('DIF contact type not understood [%s]'%elem.tag)
135    def toHTML(self):
136        if self.core is None: 
137            return 'Not Found'
138        else: return self.core.toHTML()
139    def url(self):
140        if self.core is None:
141            return 'Not Found'
142        else: return self.core.url()
143    def __handleName(self,f,l):
144        ''' Sort out first name last name handling '''
145        if f=='': return l
146        return '%s, %s'%(l,f)
147       
148def getAuthors(dclist):
149    ''' Parse a list of DIFcontacts and get any "author" types out '''
150    authorTypes=['Investigator','Dataset_Creator','Dataset_Author']
151    authors=[]
152    for a in dclist:
153        if a.core['role'] in authorTypes:authors.append(a)
154    return authors
155       
156import unittest
157Example='''
158<Data_Center> 
159<Data_Center_Name> 
160<Short_Name>BODC</Short_Name>
161<Long_Name>British Oceanographic Data Centre</Long_Name>
162</Data_Center_Name>
163<Data_Center_URL>http://www.bodc.ac.uk</Data_Center_URL>
164<Personnel> 
165<Role>Data Center Contact</Role>
166<Last_Name>British Oceanographic Data Centre</Last_Name>
167<Phone>+44 (0)151 795 4884</Phone>
168<Fax>+44 (0)151 795 4912</Fax>
169<Contact_Address> 
170<Address>Joseph Proudman Building</Address>
171<Address>6 Brownlow Street</Address>
172<City>UKPT0811</City>
173<Postal_Code>L3 5DA</Postal_Code>
174<Country>74</Country>
175</Contact_Address>
176</Personnel>
177</Data_Center>
178'''
179Example2='''
180<Data_Center>
181<Data_Center_Name>
182<Short_Name>BODC</Short_Name>
183<Long_Name>British Oceanographic Data Centre</Long_Name>
184</Data_Center_Name>
185<Data_Center_URL>http://www.bodc.ac.uk/</Data_Center_URL>
186<Personnel>
187<Role>BODC Enquiries Officer</Role>
188<First_Name></First_Name>
189<Last_Name></Last_Name>
190<Email>enquiries@bodc.ac.uk</Email>
191<Phone>+44 (0) 151 795 4884</Phone>
192<Fax>+44 (0) 151 795 4912</Fax>
193<Contact_Address>
194<Address>6 Brownlow Street</Address>
195<City>Liverpool</City>
196<Province_or_State>Merseyside</Province_or_State>
197<Postal_Code>L3 5DA</Postal_Code>
198<Country>United Kingdom</Country>
199</Contact_Address>
200</Personnel>
201</Data_Center>'''
202
203
204class TestCase(unittest.TestCase):
205   
206    def testDIFContact(self):
207        '''Test instantiating a DIFContact'''
208        w='<DIF xmlns="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">%s</DIF>'%Example2
209        #w='<wrap>%s</wrap>'%Example2
210        y=xmlHandler(w,string=1)
211        h=nsdumb(y.tree)
212        e=h.find(y.tree,'Data_Center')
213        d=DIFcontact(h,e)
214        print d.toHTML()
215   
216    def testDIFname(self):
217        '''Make sure we can create a contact, with just a name '''
218        x=DIFcontact(0,0,name='Bryan Lawrence')
219        print x.toHTML()
220   
221if __name__=="__main__":
222    unittest.main()         
Note: See TracBrowser for help on using the repository browser.