Package ndg :: Package xacml :: Package parsers :: Package etree :: Module factory
[hide private]

Source Code for Module ndg.xacml.parsers.etree.factory

  1  """NDG XACML ElementTree reader module containing reader base class  
  2   
  3  NERC DataGrid 
  4  """ 
  5  __author__ = "P J Kershaw" 
  6  __date__ = "19/03/10" 
  7  __copyright__ = "(C) 2010 Science and Technology Facilities Council" 
  8  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
  9  __license__ = "BSD - see LICENSE file in top-level directory" 
 10  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
 11  __revision__ = "$Id: factory.py 7661 2010-10-27 15:05:18Z pjkersha $" 
 12  import logging 
 13  log = logging.getLogger(__name__) 
 14   
 15  from ndg.xacml.parsers import AbstractReaderFactory 
 16  from ndg.xacml.utils.factory import importModuleObject 
 17  from ndg.xacml.utils import VettedDict 
 18  from ndg.xacml.core import XacmlCoreBase 
 19  from ndg.xacml.parsers.etree.reader import ETreeAbstractReader 
20 21 22 -class ETreeReaderClassMap(VettedDict):
23 """Specialised dictionary to hold mappings of XACML classes to their 24 equivalent ElementTree reader classes 25 """ 26
27 - def __init__(self):
28 """Force entries to derive from AttributeValue and IDs to 29 be string type 30 """ 31 # Filters are defined as staticmethods but reference via self here to 32 # enable derived class to override them as standard methods without 33 # needing to redefine this __init__ method 34 VettedDict.__init__(self, self.keyFilter, self.valueFilter)
35 36 @staticmethod
37 - def keyFilter(key):
38 """Enforce XACML base class type keys 39 40 @param key: URN for attribute 41 @type key: basestring 42 @return: boolean True indicating key is OK 43 @rtype: bool 44 @raise TypeError: incorrect input type 45 """ 46 if not issubclass(key, XacmlCoreBase): 47 raise TypeError('Expecting %r derived type for key; got %r' % 48 (XacmlCoreBase, type(key))) 49 return True
50 51 @staticmethod
52 - def valueFilter(value):
53 """Enforce ElementTree abstract reader derived types for values 54 @param value: attribute value 55 @type value: ndg.xacml.core.attributevalue.AttributeValue derived type 56 @return: boolean True indicating attribute value is correct type 57 @rtype: bool 58 @raise TypeError: incorrect input type 59 """ 60 if not issubclass(value, ETreeAbstractReader): 61 raise TypeError('Expecting %r derived type for value; got %r' % 62 (ETreeAbstractReader, type(value))) 63 return True
64
65 66 -class ReaderFactory(AbstractReaderFactory):
67 """Parser factory for ElementTree based parsers for XACML types""" 68 READER_CLASS_MAP = ETreeReaderClassMap() 69 70 @classmethod
71 - def addReader(cls, xacmlType, readerClass):
72 """Add custom classes and readers 73 74 @param xacmlType: XACML type to return a parser class for 75 @type xacmlType: type 76 @param readerClass: ElementTree based reader for the input XACML type. 77 @type readerClass: ndg.xacml.parsers.etree.reader.ETreeAbstractReader 78 derived type 79 """ 80 cls.READER_CLASS_MAP[xacmlType] = readerClass
81 82 @classmethod
83 - def getReader(cls, xacmlType):
84 """Return ElementTree based Reader class for the given input 85 86 @param xacmlType: XACML type to return a parser class for 87 @type xacmlType: type 88 @return: ElementTree based reader for the input XACML type. The class 89 and module containing the class are inferred from the XACML class name 90 input e.g. 91 92 ndg.xacml.core.Subject => ndg.xacml.parsers.etree.subjectreader.SubjectReader 93 94 @rtype: ndg.xacml.parsers.etree.reader.ETreeAbstractReader derived 95 type 96 @raise ImportError: if no reader class found for input type 97 """ 98 if xacmlType in cls.READER_CLASS_MAP: 99 # Retrieve from mapping 100 return cls.READER_CLASS_MAP[xacmlType] 101 else: 102 # Infer from the package structure 103 xacmlTypeName = xacmlType.__name__ 104 readerClassName = 'ndg.xacml.parsers.etree.%sreader.%sReader' % ( 105 xacmlTypeName.lower(), 106 xacmlTypeName) 107 readerClass = importModuleObject(readerClassName) 108 return readerClass
109