Package omeroweb :: Module connector
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.connector

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # 
  5  # Copyright (C) 2011 University of Dundee & Open Microscopy Environment. 
  6  # All rights reserved. 
  7  # 
  8  # This program is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU Affero General Public License as 
 10  # published by the Free Software Foundation, either version 3 of the 
 11  # License, or (at your option) any later version. 
 12  # 
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU Affero General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU Affero General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20  # 
 21   
 22  import re 
 23  import logging 
 24   
 25  from django.utils.encoding import smart_unicode, force_unicode 
 26   
 27  from omero import client_wrapper 
 28  from omero_version import omero_version 
 29   
 30  logger = logging.getLogger(__name__) 
31 32 -class IterRegistry(type):
33 - def __new__(cls, name, bases, attr):
34 attr['_registry'] = {} 35 attr['_frozen'] = False 36 return type.__new__(cls, name, bases, attr)
37
38 - def __iter__(cls):
39 return iter(cls._registry.values())
40
41 -class ServerBase(object):
42 __metaclass__ = IterRegistry 43 _next_id = 1 44
45 - def __init__(self, host, port, server=None):
46 if hasattr(self, 'host') or hasattr(self, 'port'): 47 return 48 self.id = type(self)._next_id 49 self.host = host 50 self.port = port 51 self.server = (server is not None and server != '') and server or None 52 type(self)._registry[self.id] = self 53 type(self)._next_id += 1
54
55 - def __new__(cls, host, port, server=None):
56 for key in cls._registry: 57 val = cls._registry[key] 58 if val.host == host and val.port == port: 59 return cls._registry[key] 60 61 if cls._frozen: 62 raise TypeError('No more instances allowed') 63 else: 64 return object.__new__(cls)
65 66 @classmethod
67 - def instance(cls, pk):
68 if cls._registry.has_key(pk): 69 return cls._registry[pk] 70 return None
71 72 @classmethod
73 - def freeze(cls):
74 cls._frozen = True
75 76 @classmethod
77 - def reset(cls):
78 cls._registry = {} 79 cls._frozen = False 80 cls._next_id = 1
81
82 -class Server(ServerBase):
83
84 - def __repr__(self):
85 """ 86 Json for printin settings.py: [["localhost", 4064, "omero"]]' 87 """ 88 return """["%s", %s, "%s"]""" % (self.host, self.port, self.server)
89
90 - def __str__(self):
91 return force_unicode(self).encode('utf-8')
92
93 - def __unicode__(self):
94 return str(self.id)
95 96 @classmethod
97 - def get(cls, pk):
98 r = None 99 try: 100 pk = int(pk) 101 except: 102 pass 103 else: 104 if cls._registry.has_key(pk): 105 r = cls._registry[pk] 106 return r
107 108 @classmethod
109 - def find(cls, host=None, port=None, server=None):
110 rv = [] 111 for s in cls._registry.values(): 112 if (host is not None and host != s.host) or \ 113 (port is not None and port != s.port) or \ 114 (server is not None and server != s.server): 115 continue 116 rv.append(s) 117 return rv
118
119 -class Connector(object):
120 """ 121 Object which encompasses all of the logic related to a Blitz connection 122 and its status with respect to OMERO.web. 123 """ 124 125 SERVER_VERSION_RE = re.compile("^.*?[-]?(\\d+[.]\\d+([.]\\d+)?)[-]?.*?$") 126
127 - def __init__(self, server_id, is_secure):
128 self.server_id = server_id 129 self.is_secure = is_secure 130 self.is_public = False 131 self.omero_session_key = None 132 self.user_id = None
133
134 - def lookup_host_and_port(self):
135 server = Server.get(self.server_id) 136 if server is None: 137 server = Server.find(server=self.server_id)[0] 138 return (server.host, server.port)
139
140 - def create_gateway(self, useragent, username=None, password=None):
141 host, port = self.lookup_host_and_port() 142 return client_wrapper( 143 username, password, host=host, port=port, secure=self.is_secure, 144 useragent=useragent, anonymous=self.is_public)
145
146 - def prepare_gateway(self, connection):
147 connection.server_id = self.server_id 148 # Lazy import due to the potential usage of the decorator in 149 # the omeroweb.webgateway.views package. 150 # TODO: UserProxy needs to be moved to this package or similar 151 from omeroweb.webgateway.views import UserProxy 152 connection.user = UserProxy(connection) 153 connection.user.logIn() 154 self.omero_session_key = connection._sessionUuid 155 self.user_id = connection.getUserId() 156 logger.debug('Successfully prepared gateway: %s' % \ 157 self.omero_session_key)
158 # TODO: Properly handle activating the weblitz_cache 159
160 - def create_connection(self, useragent, username, password, is_public=False):
161 self.is_public = is_public 162 try: 163 connection = self.create_gateway(useragent, username, password) 164 if connection.connect(): 165 logger.debug('Successfully created connection for: %s' % \ 166 username) 167 self.prepare_gateway(connection) 168 return connection 169 except: 170 logger.debug('Cannot create a new connection.', exc_info=True) 171 return None
172
173 - def create_guest_connection(self, useragent, is_public=False):
174 connection = None 175 guest = 'guest' 176 try: 177 connection = self.create_gateway(useragent, guest, guest) 178 if connection.connect(): 179 logger.debug('Successfully created a guest connection.') 180 else: 181 logger.warn('Cannot create a guest connection.') 182 except: 183 logger.error('Cannot create a guest connection.', exc_info=True) 184 return connection
185
186 - def join_connection(self, useragent):
187 try: 188 connection = self.create_gateway(useragent) 189 if connection.connect(sUuid=self.omero_session_key): 190 logger.debug('Successfully joined connection: %s' % \ 191 self.omero_session_key) 192 connection.setUserId(self.user_id) 193 self.prepare_gateway(connection) 194 return connection 195 except: 196 logger.debug('Cannot create a new connection.', exc_info=True) 197 return None
198
199 - def is_server_up(self, useragent):
200 connection = self.create_guest_connection(useragent) 201 if connection is None: 202 return False 203 try: 204 connection.getServerVersion() 205 return True 206 except: 207 logger.error('Cannot request server version.', exc_info=True) 208 return False
209
210 - def check_version(self, useragent):
211 connection = self.create_guest_connection(useragent) 212 if connection is None: 213 return False 214 try: 215 server_version = connection.getServerVersion() 216 server_version = self.SERVER_VERSION_RE.match(server_version) 217 server_version = server_version.group(1).split('.') 218 219 client_version = self.SERVER_VERSION_RE.match(omero_version) 220 client_version = client_version.group(1).split('.') 221 logger.info("Client version: '%s'; Server version: '%s'" % \ 222 (client_version, server_version)) 223 return server_version == client_version 224 except: 225 logger.error('Cannot compare server to client version.', 226 exc_info=True) 227 return False
228