Package omero :: Package gateway :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module omero.gateway.utils

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #  
  4  # webclient_gateway 
  5  #  
  6  # Copyright (c) 2008-2011 University of Dundee. 
  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  # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2012 
 22  #  
 23  # Version: 1.0 
 24  # 
 25   
 26  import logging 
 27   
 28  logger = logging.getLogger(__name__) 
 29   
30 -class GatewayConfig(object):
31 """ 32 Global Gateway configuration 33 - IMG_RDEFNS: a namespace for annotations linked on images holding the default rendering 34 settings object id. 35 - IMG_ROPTSNS: a namespace for annotations linked on images holding default rendering options 36 that don't get saved in the rendering settings. 37 """
38 - def __init__ (self):
39 self.IMG_RDEFNS = None 40 self.IMG_ROPTSNS = None
41
42 -class ServiceOptsDict(dict):
43
44 - def __new__(cls, *args, **kwargs):
45 return super(ServiceOptsDict, cls).__new__(cls, *args, **kwargs)
46
47 - def __init__(self, data=None, *args, **kwargs):
48 if data is None: 49 data = dict() 50 if len(kwargs) > 0: 51 for key, val in dict(*args, **kwargs).iteritems(): 52 self[key] = val 53 if isinstance(data, dict): 54 for key in data: 55 item = data[key] 56 if self._testItem(item): 57 self[key] = str(item) 58 else: 59 logger.debug("None or non- string, unicode or numeric type values are ignored, (%r, %r)" % (key,item)) 60 else: 61 raise AttributeError("%s argument (%r:%s) must be a dictionary" % (self.__class__.__name__, data, type(data)))
62
63 - def __repr__(self):
64 return "<%s: %s>" % (self.__class__.__name__, 65 super(ServiceOptsDict, self).__repr__())
66
67 - def __setitem__(self, key, item):
68 """Set key to value as string.""" 69 if self._testItem(item): 70 super(ServiceOptsDict, self).__setitem__(key, str(item)) 71 logger.debug("Setting %r to %r" % (key, item)) 72 else: 73 raise AttributeError("%s argument (%r:%s) must be a string, unicode or numeric type" % (self.__class__.__name__, item, type(item)))
74
75 - def __getitem__(self, key):
76 """Return the value for key if key is in the dictionary. Raises a KeyError if key is not in the map.""" 77 try: 78 return super(ServiceOptsDict, self).__getitem__(key) 79 except KeyError: 80 raise KeyError("Key %r not found in %r" % (key, self))
81
82 - def __delitem__(self, key):
83 """Remove dict[key] from dict. Raises a KeyError if key is not in the map.""" 84 super(ServiceOptsDict, self).__delitem__(key) 85 logger.debug("Deleting %r" % (key))
86
87 - def copy(self):
88 """Returns a copy of this object.""" 89 return self.__class__(self)
90
91 - def clear(self):
92 """Remove all items from the dictionary.""" 93 super(ServiceOptsDict, self).clear()
94
95 - def get(self, key, default=None):
96 """Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.""" 97 try: 98 return self.__getitem__(key) 99 except KeyError: 100 return default
101
102 - def set(self, key, value):
103 """Set key to value as string.""" 104 return self.__setitem__(key,value)
105
106 - def getOmeroGroup(self):
107 return self.get('omero.group')
108
109 - def setOmeroGroup(self, value=None):
110 if value is not None: 111 self.set('omero.group',value) 112 else: 113 try: 114 del self['omero.group'] 115 except KeyError: 116 logger.debug("Key 'omero.group' not found in %r" % self)
117
118 - def getOmeroUser(self):
119 return self.get('omero.user')
120
121 - def setOmeroUser(self, value=None):
122 if value is not None: 123 self.set('omero.user',value) 124 else: 125 try: 126 del self['omero.user'] 127 except KeyError: 128 logger.debug("Key 'omero.user' not found in %r" % self)
129
130 - def getOmeroShare(self):
131 return self.get('omero.share')
132
133 - def setOmeroShare(self, value=None):
134 if value is not None: 135 self.set('omero.share',value) 136 else: 137 try: 138 del self['omero.share'] 139 except KeyError: #pragma: no cover 140 logger.debug("Key 'omero.share' not found in %r" % self)
141
142 - def _testItem(self, item):
143 if item is not None and not isinstance(item, bool) and \ 144 (isinstance(item, basestring) or \ 145 isinstance(item, int) or \ 146 isinstance(item, long) or \ 147 isinstance(item, float)): 148 return True 149 return False
150