PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lib/python/indra/ipc/compatibility.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 123 lines | 114 code | 0 blank | 9 comment | 0 complexity | 8cae8a8f892f46c79783409c8b3500b2 MD5 | raw file
  1"""\
  2@file compatibility.py
  3@brief Classes that manage compatibility states.
  4
  5$LicenseInfo:firstyear=2007&license=mit$
  6
  7Copyright (c) 2007-2009, Linden Research, Inc.
  8
  9Permission is hereby granted, free of charge, to any person obtaining a copy
 10of this software and associated documentation files (the "Software"), to deal
 11in the Software without restriction, including without limitation the rights
 12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 13copies of the Software, and to permit persons to whom the Software is
 14furnished to do so, subject to the following conditions:
 15
 16The above copyright notice and this permission notice shall be included in
 17all copies or substantial portions of the Software.
 18
 19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 25THE SOFTWARE.
 26$/LicenseInfo$
 27"""
 28
 29
 30"""Compatibility combination table:
 31
 32    I   M   O   N   S
 33    --  --  --  --  --
 34I:  I   I   I   I   I
 35M:  I   M   M   M   M   
 36O:  I   M   O   M   O
 37N:  I   M   M   N   N
 38S:  I   M   O   N   S
 39
 40"""
 41
 42class _Compatibility(object):
 43    def __init__(self, reason):
 44        self.reasons = [ ]
 45        if reason:
 46            self.reasons.append(reason)
 47        
 48    def combine(self, other):
 49        if self._level() <= other._level():
 50            return self._buildclone(other)
 51        else:
 52            return other._buildclone(self)
 53    
 54    def prefix(self, leadin):
 55        self.reasons = [ leadin + r for r in self.reasons ] 
 56    
 57    def same(self):         return self._level() >=  1
 58    def deployable(self):   return self._level() >   0
 59    def resolved(self):     return self._level() >  -1
 60    def compatible(self):   return self._level() >  -2
 61    
 62    def explain(self):
 63        return self.__class__.__name__ + "\n" + "\n".join(self.reasons) + "\n"
 64        
 65    def _buildclone(self, other=None):
 66        c = self._buildinstance()
 67        c.reasons = self.reasons
 68        if other:
 69            c.reasons = c.reasons + other.reasons
 70        return c
 71        
 72    def _buildinstance(self):
 73        return self.__class__(None)
 74
 75#    def _level(self):
 76#        raise RuntimeError('implement in subclass')
 77
 78
 79class Incompatible(_Compatibility):
 80    def _level(self):
 81        return -2
 82
 83class Mixed(_Compatibility):
 84    def __init__(self, *inputs):
 85        _Compatibility.__init__(self, None)
 86        for i in inputs:
 87            self.reasons += i.reasons
 88                    
 89    def _buildinstance(self):
 90        return self.__class__()
 91        
 92    def _level(self):
 93        return -1
 94
 95class _Aged(_Compatibility):
 96    def combine(self, other):
 97        if self._level() == other._level():
 98            return self._buildclone(other)
 99        if int(self._level()) == int(other._level()):
100            return Mixed(self, other)
101        return _Compatibility.combine(self, other)
102
103class Older(_Aged):
104    def _level(self):
105        return -0.25
106    
107class Newer(_Aged):
108    def _level(self):
109        return 0.25
110
111class Same(_Compatibility):
112    def __init__(self):
113        _Compatibility.__init__(self, None)
114    
115    def _buildinstance(self):
116        return self.__class__()
117        
118    def _level(self):
119        return 1
120
121
122
123