source: cows/trunk/cows/builder.py @ 4008

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/builder.py@4078
Revision 4008, 5.5 KB checked in by spascoe, 12 years ago (diff)

New COWS distribution. See [4005] for description.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Construct a ServiceMetadata instance with a simplified interface.
7
8"""
9
10import ConfigParser
11from cStringIO import StringIO
12
13from cows.model import *
14
15class ServiceMetadataBuilder(object):
16    def __init__(self, serviceMetadata=None):
17        if serviceMetadata is None:
18            self.clear()
19        else:
20            self.serviceMetadata = serviceMetadata
21
22    def clear(self):
23        """
24        Clear the serviceMetadata instance.
25
26        """
27        self.serviceMetadata = ServiceMetadata()
28
29    def fromConfig(self, config):
30        """
31        Load ServiceIdentification and ServiceProvider information from a
32        config file.
33
34        """
35
36        self.serviceMetadata.serviceIdentification =  self._siFromConfig(config)
37        self.serviceMetadata.serviceProvider = self._spFromConfig(config)
38
39
40    #-------------------------------------------------------------------------
41
42    def _siFromConfig(self, config):
43
44        siConfig = dict(config.items('serviceIdentification'))
45
46        # serviceType and versions are typically populated from the framework
47        # They are included here for completeness and because serviceType is
48        # required in the data model
49        type = siConfig.get('type', 'OWS')
50        versions = siConfig.get('versions', '').split()
51       
52        profiles = siConfig.get('profiles', '').split()
53        fees = siConfig.get('fees')
54        accessConstraints = siConfig.get('accessConstraints')
55
56        # Only one title & abstract supported
57        title = siConfig.get('title')
58        abstract = siConfig.get('abstract')
59
60        keywords = siConfig.get('keywords', '').split()
61
62        return ServiceIdentification(serviceType=type,
63                                     serviceTypeVersions=versions,
64                                     profiles=profiles,
65                                     fees=fees,
66                                     accessConstraints=accessConstraints,
67                                     titles=[title],
68                                     abstracts=[abstract],
69                                     keywords=keywords)
70
71    def _spFromConfig(self, config):
72        spConfig = dict(config.items('serviceProvider'))
73
74        provider = spConfig['provider']
75        site = spConfig.get('site')
76
77        # Contact info
78        name = spConfig.get('contact.name')
79        position = spConfig.get('contact.position')
80        role = spConfig.get('contact.role')
81        hos = spConfig.get('contact.hoursOfService')
82        instr = spConfig.get('contact.instructions')
83
84        # Look for all address fields of name contact.address<n>
85        address = []
86        for i in range(1, 8):
87            k = 'contact.address%d' % i
88            try:
89                address.append(spConfig[k])
90            except KeyError:
91                break
92        city = spConfig.get('contact.city')
93        adminArea = spConfig.get('contact.administrativeArea')
94        postalCode = spConfig.get('contact.postalCode')
95        country = spConfig.get('contact.country')
96        email = spConfig.get('contact.email')
97        phone = spConfig.get('contact.phone')
98        fax = spConfig.get('contact.faxcimile')
99        onlineResource = spConfig.get('contact.onlineResource')
100
101        a = Address(deliveryPoints=address, city=city,
102                    administrativeArea=adminArea, postalCode=postalCode,
103                    country=country, electronicMailAddress=email)
104        t = Telephone(voice=phone, facsimile=fax)
105        c = Contact(hoursOfService=hos,
106                    contactInstructions=instr,
107                    address=a,
108                    phone=t,
109                    onlineResource=onlineResource)
110        rp = ResponsibleParty(individualName=name,
111                              positionName=position,
112                              role=role,
113                              contactInfo=c)
114
115        return ServiceProvider(providerName=provider,
116                               serviceContact=rp,
117                               providerSite=site)
118   
119   
120#
121# Simple functional interface
122#
123
124def loadConfigFile(filename):
125    b = ServiceMetadataBuilder()
126
127    config = ConfigParser.SafeConfigParser()
128    config.read([filename])
129
130    b.fromConfig(config)
131
132    return b.serviceMetadata
133
134def loadConfig(configString):
135    b = ServiceMetadataBuilder()
136
137    config = ConfigParser.SafeConfigParser()
138    fp = StringIO(configString)
139    config.readfp(fp)
140
141    b.fromConfig(config)
142
143    return b.serviceMetadata
144
145
146#-------------------------------------------------------------------------------
147
148def test_loadConfig():
149    sm = loadConfig("""
150[serviceIdentification]
151type: WMS
152versions: 1.1.1 1.3.0
153fees: Loadsamoney
154accessConstraints: byebye
155title: The quick brown fox jumps
156  over the lazy dog
157abstract: None
158keywords: foo bar baz
159  spud
160
161[serviceProvider]
162provider: Superman
163site: http://example.com
164contact.name: Joe Blogs
165contact.address1: foo
166contact.address2: bar
167contact.email: foo@bar.baz
168
169""")
170
171    from genshi.template import TemplateLoader
172    from pkg_resources import resource_filename
173    loader = TemplateLoader(
174               [resource_filename('cows.pylons.templates', '')])
175    t = loader.load('wms_capabilities_1_1_1.xml')
176
177    # Mock objects
178    class C:
179        service_metadata = sm
180    c = C()
181
182    class H:
183        def url_for(self): return 'http://example.com/'
184    h=H()
185
186    print t.generate(c=c, h=h).render()
Note: See TracBrowser for help on using the repository browser.