Package application :: Module schema
[hide private]
[frames] | no frames]

Source Code for Module application.schema

   1  #   Copyright (c) 2003-2007 Open Source Applications Foundation 
   2  # 
   3  #   Licensed under the Apache License, Version 2.0 (the "License"); 
   4  #   you may not use this file except in compliance with the License. 
   5  #   You may obtain a copy of the License at 
   6  # 
   7  #       http://www.apache.org/licenses/LICENSE-2.0 
   8  # 
   9  #   Unless required by applicable law or agreed to in writing, software 
  10  #   distributed under the License is distributed on an "AS IS" BASIS, 
  11  #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  12  #   See the License for the specific language governing permissions and 
  13  #   limitations under the License. 
  14   
  15   
  16  from chandlerdb.schema.c import Redirector 
  17  from chandlerdb.util.c import Nil 
  18  from repository.item.Item import Item as Base, ItemClass as BaseClass 
  19  from repository.item.Collection import CollectionClass as BaseCollectionClass 
  20  from repository.schema.Kind import CDescriptor, Kind 
  21  from repository.schema.Attribute import Attribute 
  22  from repository.schema import Types 
  23  from repository.schema.Cloud import Cloud as _Cloud 
  24  from repository.schema.Cloud import Endpoint as _Endpoint 
  25  from zope.interface.advice import getFrameInfo, addClassAdvisor 
  26  import __main__, repository, os, sys 
  27   
  28  __all__ = [ 
  29      'ActiveDescriptor', 'Activator', 'Descriptor', 'itemFor', 'kindInfo', 
  30      'One', 'Many', 'Sequence', 'Mapping', 'Item', 'ItemClass', 'initialValues', 
  31      'importString', 'parcel_for_module', 'TypeReference', 
  32      'Enumeration', 'Cloud', 'Endpoint', 'addClouds', 'Struct', 
  33      'assertResolved', 'Annotation', 'AnnotationItem', 
  34      'observer', 
  35  ] 
  36   
  37  all_aspects = Attribute.valueAspects + Attribute.refAspects + ('description',) 
  38   
  39  policy_values = ( 
  40      'byRef', 'byValue', 'byCloud', 'byRef', 'none', 'byMethod', 'literal' 
  41  ) 
  42   
  43  default_schema = { 
  44      Kind: ('description', 'notify'), 
  45      Base: ('description',) 
  46  } 
  47   
  48   
  49  # The next two functions are abominable hacks to work around the 
  50  # absence of generic functions.  Maybe this can be cleaned up in 0.7 
  51  # by adding RuleDispatch to the mix?  Or alternately, by defining 
  52  # Chandler's core schema using the schema API, rather than packs. 
  53  # 
54 -def _is_schema(ob):
55 fsi = getattr(ob,'_find_schema_item',None) 56 if fsi: 57 return fsi.im_self is not None # must be a *bound* method 58 return ob is Base or ob is Kind
59
60 -def _target_type(ob):
61 if ob is Base or ob is Kind: 62 return ob 63 else: 64 return ob.targetType()
65 66
67 -class TypeReference:
68 """Reference a core schema type (e.g. Integer) by its repository path""" 69
70 - def __init__(self,path):
71 #called_from_here = sys._getframe(1).f_globals is globals() 72 self.path = path 73 self.__name__ = path.split('/')[-1]
74
75 - def __repr__(self):
76 return "TypeReference(%r)" % self.path
77
78 - def _find_schema_item(self,view):
79 item = view.findPath(self.path) 80 if item is None: 81 raise TypeError("Unrecognized type", self.path) 82 return item
83
84 - def targetType(self):
85 return self
86 87 core_types = """ 88 Boolean Symbol Importable Bytes Text Integer Long Float Decimal 89 Tuple List Set Class Dictionary Anything 90 Date Time DateTime DateTimeTZ TimeDelta TimeZone 91 Lob URL Complex UUID Path ItemRef NilValue 92 """.split() 93 94 for name in core_types: 95 globals()[name] = TypeReference("//Schema/Core/"+name) 96 97 __all__.extend(core_types) 98
99 -class ActiveDescriptor(object):
100 """Abstract base for descriptors needing activation by their classes""" 101
102 - def activateInClass(self,cls,name):
103 """Redefine in subclasses to do useful things with `cls` & `name`""" 104 raise NotImplementedError
105 106
107 -class Activator(type):
108 """Metaclass that activates contained ``ActiveDescriptor`` instances""" 109
110 - def __init__(cls,clsName,bases,cdict):
111 for name,ob in cdict.items(): 112 if isinstance(ob,ActiveDescriptor): 113 ob.activateInClass(cls,name) 114 super(Activator,cls).__init__(clsName,bases,cdict)
115
116 - def targetType(cls):
117 """By default, backreferences to arbitrary classes go nowhere""" 118 return None
119 120
121 -class Descriptor(ActiveDescriptor,CDescriptor):
122 """Descriptor for a schema-defined attribute""" 123 124 owner = type = _inverse = _frozen = annotates = None 125 126 __slots__ = ['__dict__'] 127 __slots__.extend(all_aspects) 128
129 - def __new__(cls, type=None, **kw):
130 return super(Descriptor,cls).__new__(cls,kw.get('name'))
131
132 - def __init__(self,type=None,**kw):
133 super(Descriptor,self).