00001 """\
00002 @file servicebuilder.py
00003 @author Phoenix
00004 @brief Class which will generate service urls.
00005
00006 $LicenseInfo:firstyear=2007&license=mit$
00007
00008 Copyright (c) 2007-2008, Linden Research, Inc.
00009
00010 Permission is hereby granted, free of charge, to any person obtaining a copy
00011 of this software and associated documentation files (the "Software"), to deal
00012 in the Software without restriction, including without limitation the rights
00013 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00014 copies of the Software, and to permit persons to whom the Software is
00015 furnished to do so, subject to the following conditions:
00016
00017 The above copyright notice and this permission notice shall be included in
00018 all copies or substantial portions of the Software.
00019
00020 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00023 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00026 THE SOFTWARE.
00027 $/LicenseInfo$
00028 """
00029
00030 from indra.base import config
00031 from indra.ipc import llsdhttp
00032 from indra.ipc import russ
00033
00034
00035 services_config = {}
00036 try:
00037 services_config = llsdhttp.get(config.get('services-config'))
00038 except:
00039 pass
00040
00041 _g_builder = None
00042 def build(name, context={}, **kwargs):
00043 """ Convenience method for using a global, singleton, service builder. Pass arguments either via a dict or via python keyword arguments, or both!
00044
00045 Example use:
00046 > context = {'channel':'Second Life Release', 'version':'1.18.2.0'}
00047 > servicebuilder.build('version-manager-version', context)
00048 'http://int.util.vaak.lindenlab.com/channel/Second%20Life%20Release/1.18.2.0'
00049 > servicebuilder.build('version-manager-version', channel='Second Life Release', version='1.18.2.0')
00050 'http://int.util.vaak.lindenlab.com/channel/Second%20Life%20Release/1.18.2.0'
00051 > servicebuilder.build('version-manager-version', context, version='1.18.1.2')
00052 'http://int.util.vaak.lindenlab.com/channel/Second%20Life%20Release/1.18.1.2'
00053 """
00054 context = context.copy()
00055 context.update(kwargs)
00056 global _g_builder
00057 if _g_builder is None:
00058 _g_builder = ServiceBuilder()
00059 return _g_builder.buildServiceURL(name, context)
00060
00061 class ServiceBuilder(object):
00062 def __init__(self, services_definition = services_config):
00063 """\
00064 @brief
00065 @brief Create a ServiceBuilder.
00066 @param services_definition Complete services definition, services.xml.
00067 """
00068
00069
00070 self.services = services_definition['services']
00071 self.builders = {}
00072 for service in self.services:
00073 service_builder = service.get('service-builder')
00074 if not service_builder:
00075 continue
00076 if isinstance(service_builder, dict):
00077
00078 for name, builder in service_builder.items():
00079 full_builder_name = service['name'] + '-' + name
00080 self.builders[full_builder_name] = builder
00081 else:
00082 self.builders[service['name']] = service_builder
00083
00084 def buildServiceURL(self, name, context):
00085 """\
00086 @brief given the environment on construction, return a service URL.
00087 @param name The name of the service.
00088 @param context A dict of name value lookups for the service.
00089 @returns Returns the
00090 """
00091 base_url = config.get('services-base-url')
00092 svc_path = russ.format(self.builders[name], context)
00093 return base_url + svc_path