constraint.py :  » Development » ASN.1-library-for-Python » pyasn1-0.0.11a » pyasn1 » type » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » ASN.1 library for Python 
ASN.1 library for Python » pyasn1 0.0.11a » pyasn1 » type » constraint.py
"""
   ASN.1 subtype constraints classes.

   Constraints are relatively rare, but every ASN1 object
   is doing checks all the time for whether they have any
   constraints and whether they are applicable to the object.

   What we're going to do is define objects/functions that
   can be called unconditionally if they are present, and that
   are simply not present if there are no constraints.

   Original concept and code by Mike C. Fletcher.
"""
import types
import string
from pyasn1.type import error

class AbstractConstraint:
    """Abstract base-class for constraint objects

       Constraints should be stored in a simple sequence in the
       namespace of their client Asn1Item sub-classes.
    """
    def __init__(self, *values):
        self._valueMap = {}
        self._setValues(values)
        self.__hashedValues = None
    def __call__(self, value, idx=None):
        try:
            self._testValue(value, idx)
        except error.ValueConstraintError, why:
            raise error.ValueConstraintError('%s failed at: %s' % (
                self, why
                ))
    def __repr__(self):
        return '%s(%s)' % (
            self.__class__.__name__,
            string.join(map(lambda x: str(x), self._values), ', ')
        )
    # __cmp__ must accompany __hash__
    def __cmp__(self, other):
        return self is other and 0 or cmp(
            (self.__class__, self._values), other
            )
    def __eq__(self, other):
        return self is other or not cmp(
            (self.__class__, self._values), other
            )
    def __hash__(self):
        if self.__hashedValues is None:
            self.__hashedValues = hash((self.__class__, self._values))
        return self.__hashedValues
    def _setValues(self, values): self._values = values
    def _testValue(self, value, idx):
        raise error.ValueConstraintError(value)

    # Constraints derivation logic
    def getValueMap(self): return self._valueMap
    def isSuperTypeOf(self, otherConstraint):
        return otherConstraint.getValueMap().has_key(self) or \
               otherConstraint is self or otherConstraint == self
    def isSubTypeOf(self, otherConstraint):
        return self._valueMap.has_key(otherConstraint) or \
               otherConstraint is self or otherConstraint == self

class SingleValueConstraint(AbstractConstraint):
    """Value must be part of defined values constraint"""
    def _testValue(self, value, idx):
        # XXX index vals for performance?
        if value not in self._values:
            raise error.ValueConstraintError(value)

class ContainedSubtypeConstraint(AbstractConstraint):
    """Value must satisfy all of defined set of constraints"""
    def _testValue(self, value, idx):
        for c in self._values:
            c(value, idx)

class ValueRangeConstraint(AbstractConstraint):
    """Value must be within start and stop values (inclusive)"""
    def _testValue(self, value, idx):
        if value < self.start or value > self.stop:
            raise error.ValueConstraintError(value)

    def _setValues(self, values):
        if len(values) != 2:
            raise error.PyAsn1Error(
                '%s: bad constraint values' % (self.__class__.__name__,)
                )
        self.start, self.stop = values
        if self.start > self.stop:
            raise error.PyAsn1Error(
                '%s: screwed constraint values (start > stop): %s > %s' % (
                    self.__class__.__name__,
                    self.start, self.stop
                )
            )
        AbstractConstraint._setValues(self, values)
        
class ValueSizeConstraint(ValueRangeConstraint):
    """len(value) must be within start and stop values (inclusive)"""
    def _testValue(self, value, idx):
        l = len(value)
        if l < self.start or l > self.stop:
            raise error.ValueConstraintError(value)

class PermittedAlphabetConstraint(SingleValueConstraint): pass

# This is a bit kludgy, meaning two op modes within a single constraing
class InnerTypeConstraint(AbstractConstraint):
    """Value must satisfy type and presense constraints"""
    def _testValue(self, value, idx):
        if self.__singleTypeConstraint:
            self.__singleTypeConstraint(value)
        elif self.__multipleTypeConstraint:
            if not self.__multipleTypeConstraint.has_key(idx):
                raise error.ValueConstraintError(value)
            constraint, status = self.__multipleTypeConstraint[idx]
            if status == 'ABSENT':   # XXX presense is not checked!
                raise error.ValueConstraintError(value)
            constraint(value)

    def _setValues(self, values):
        self.__multipleTypeConstraint = {}
        self.__singleTypeConstraint = None
        for v in values:
            if type(v) == types.TupleType:
                self.__multipleTypeConstraint[v[0]] = v[1], v[2]
            else:
                self.__singleTypeConstraint = v
        AbstractConstraint._setValues(self, values)

# Boolean ops on constraints 

class ConstraintsExclusion(AbstractConstraint):
    """Value must not fit the single constraint"""
    def _testValue(self, value, idx):
        try:
            self._values[0](value, idx)
        except error.ValueConstraintError:
            return
        else:
            raise error.ValueConstraintError(value)

    def _setValues(self, values):
        if len(values) != 1:
            raise error.PyAsn1Error('Single constraint expected')
        AbstractConstraint._setValues(self, values)

class AbstractConstraintSet(AbstractConstraint):
    """Value must not satisfy the single constraint"""
    def __getitem__(self, idx): return self._values[idx]

    def __add__(self, value): return self.__class__(self, value)
    def __radd__(self, value): return self.__class__(self, value)

    def __len__(self): return len(self._values)

    # Constraints inclusion in sets
    
    def _setValues(self, values):
        self._values = values
        for v in values:
            self._valueMap[v] = 1
            self._valueMap.update(v.getValueMap())

class ConstraintsIntersection(AbstractConstraintSet):
    """Value must satisfy all constraints"""
    def _testValue(self, value, idx):
        for v in self._values:
            v(value, idx)

class ConstraintsUnion(AbstractConstraintSet):
    """Value must satisfy at least one constraint"""
    def _testValue(self, value, idx):
        for v in self._values:
            try:
                v(value, idx)
            except error.ValueConstraintError:
                pass
            else:
                return
        raise error.ValueConstraintError(
            'all of %s failed for %s' % (self._values, value)
            )

# XXX
# add tests for type check
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.