""" Sprockets Module This is sort of like the central nervous system of dbsprockets. Views and Sessions are collected in separate caches and served up as sprockets. The cache objects may be solidified at some point with a parent class. They work for right now. Classes: Name Description Sprockets A cache of Sprockets Sprocket A binding of Session and View configs Views A cache of Views Sessions A cache of Sessions Exceptions: SessionConfigError ViewConfigError Functions: None Copywrite (c) 2007 Christopher Perkins Original Version by Christopher Perkins 2007 Released under MIT license. """ import types from dbsprockets.viewfactory import ViewFactory from dbsprockets.viewconfig import * from dbsprockets.sessionconfig import * from dbsprockets.saprovider import SAProvider from dbsprockets.iprovider import IProvider from dbsprockets.metadata import * from dbsprockets.view import View class SessionConfigError(Exception):pass class ViewConfigError(Exception):pass class ConfigCache(object): """Container for View Objects""" default_configs = None separator = '__' def __init__(self, provider, controller=None): """Construct a Views Object. This object is capable of creating ViewConfigs and acting as a widget cache for the views once they have been created from the ViewConfigs. Future implementations will have the ability to register different ViewConfig defaults. provider a ``IProvider`` object to instantiate views with. Usually this is an SAProvider controller String. This is a link to the URL in the application where the view is intended to be mounted. I am hoping this is eliminated in the future. """ if not isinstance(provider, IProvider): raise TypeError('provider is not of type IProvider') if controller is not None and not isinstance(controller, types.StringTypes): raise TypeError('controller is not of type String') self.view_factory = ViewFactory() self.provider = provider self.controller = controller def _split_identifiers(self, key): separator = '__' if self.separator not in key: identifier = None view_type = key else: view_type, identifier = key.split(self.separator) return view_type, identifier def _get(self, key): if self.separator not in key: identifier = '' view_type = key else: view_type, identifier = key.split(self.separator) if view_type not in self.default_configs: raise ViewConfigError('view_type:%s not found in default Views'%view_type) view_config = self.default_configs[view_type](self.provider, identifier, self.controller) return self.view_factory.create(view_config, id=key) def __getitem__(self, key): print '*'*80 print 'getting sprocket with key', key if key in self.__dict__: return self.__dict__[key] view = self._get(key) #self.__dict__[key] = view return view class Views(ConfigCache): default_configs = {'database_view': DatabaseViewConfig, 'edit_record' : EditRecordViewConfig, 'add_record' : AddRecordViewConfig, 'table_view' : TableViewConfig, 'table_def' : TableDefViewConfig, } class Sessions(ConfigCache): default_configs = { 'database_view': DatabaseSessionConfig, 'table_def' : SessionConfig, 'view_record' : SessionConfig, 'table_view' : TableViewSessionConfig, 'edit_record' : EditRecordSessionConfig, 'add_record' : AddRecordSessionConfig, } class Sprocket: """Association between a view and a sessionConfig""" def __init__(self, view, session): """Construct a Sprocket Object view a ``view`` object which has been instantiated from a ``ViewConfig`` session a ``sessionConfig`` object which contains the method for obtaining data for the associated view. """ self.session = session self.view = view class Sprockets(object): """Set of Associations between widgets and the method to obtain their data""" sprocket_type = Sprocket sessions_type = Sessions views_type = Views def __init__(self, provider, controller=None): """Construct a Sprockets Object provider a ``IProvider`` object to instantiate views with. Usually this is an SAProvider controller String. This is a link to the URL in the application where the view is intended to be mounted. I am hoping this is eliminated in the future. Right now this is needed for some widgets to provide links to the interface. """ if not isinstance(provider, IProvider): raise TypeError('provider is not of type IProvider') if controller is not None and not isinstance(controller, types.StringTypes): raise TypeError('controller is not of type String') self.views = self.views_type(provider, controller) self.sessions = self.sessions_type(provider) self.controller = controller def __getitem__(self, key): print '$'*80 print key #if key in self.__dict__: # import pdb; pdb.set_trace() # return self.__dict__.__getitem__(key) sprocket = self._get_sprocket(key) #self.__dict__[key] = sprocket return sprocket def _get_sprocket(self, key): print 'in get_sprocket', key view = self.views[key] session = self.sessions[key] return self.sprocket_type(view, session) def __setitem__(self, key, item): return if not isinstance(item, Sprocket): raise TypeError('item must be of type Sprocket') return self.__dict__.__setitem__(key, item)