Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 '''\
00002 Supplemental support for json plugins
00003 '''
00004 
00005 import os.path, logging
00006 import zope.component as capi
00007 from zope.interface import implements, Interface
00008 from zope.publisher.browser import TestRequest
00009 from interfaces import IJSONStreamWriteable
00010 
00011 logger = logging.getLogger('kss.core')
00012 
00013 def getJsonAddonFiles():
00014     'Gets the addon javascript files for json'
00015     files = []
00016     # Try adding the jsonserver files
00017     request = TestRequest()
00018     try:
00019         # use the files already registered to that concat resource
00020         jsonrpc = capi.getAdapter(request, Interface, 'jsonrpc.js')
00021     except capi.ComponentLookupError:
00022         pass    # JSON not present.
00023     else:
00024         # JSON present.
00025         files.extend(jsonrpc.context.context.getPathList())
00026         # add the json kukit support file
00027         plugins_dir = os.path.split(globals()['__file__'])[0]
00028         files.append(os.path.join(plugins_dir, 'browser', 'jsonkukit.js'))
00029         logger.info('Lazy plugin construction: Installed support for JSON-RPC transport.')
00030     return files
00031 
00032 class JsonCommandView(object):
00033     '''View of a command for JSON requests.
00034     
00035     We siply return the commands since they
00036     will be transparently rendered for JSON,
00037     via the writer adapter hooks.
00038     '''
00039     
00040     def __init__(self, context, request):
00041         self.context = context
00042         self.request = request
00043 
00044     def render(self):
00045         return self.context
00046 
00047 class KSSCommandWriter(object):
00048     'Writes a command to JSON'
00049     implements(IJSONStreamWriteable)
00050 
00051     def __init__(self, context, writer):
00052         self.context = context
00053         self.writer = writer
00054         
00055     def __jsonwrite__(self):
00056         writer = self.writer
00057         # All is written as a dict
00058         d = dict(self.context.__dict__)
00059         # params are converted to a dict from a list.
00060         # Also get rid of "none" params that were only a hack for xml
00061         d['params'] = dict([(param.name, param.content) for param in d['params'] if param.name != 'none'])
00062         writer.write_repr(d)