rearrange classes, added separate BSCWConnection class

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2675 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2008-06-07 11:54:06 +00:00
parent dc970aa2f7
commit 73311e2cf8
5 changed files with 118 additions and 66 deletions

View file

@ -29,6 +29,7 @@ from zope.traversing.browser import absoluteURL
from cybertools.integrator.bscw import ContainerFactory from cybertools.integrator.bscw import ContainerFactory
from cybertools.integrator.interfaces import IContainerFactory from cybertools.integrator.interfaces import IContainerFactory
from cybertools.integrator.interfaces import IItemFactory, IFileFactory
view_macros = ViewPageTemplateFile('view.pt') view_macros = ViewPageTemplateFile('view.pt')
@ -74,9 +75,6 @@ class BSCWView(BaseView):
viewTemplate = view_macros viewTemplate = view_macros
itemView = ItemView itemView = ItemView
baseUrl = ''
baseId = ''
@Lazy @Lazy
def listing(self): def listing(self):
return self.viewTemplate.macros['listing'] return self.viewTemplate.macros['listing']
@ -87,16 +85,9 @@ class BSCWView(BaseView):
@Lazy @Lazy
def remoteProxy(self): def remoteProxy(self):
url = self.context.getRepositoryURL() id = self.request.form.get('id')
if isinstance(url, basestring): proxy = self.context.getProxy(address=id)
server, id = url.rsplit('/', 1) return proxy
self.baseUrl = server
else: # already a real server object
server = url
id = self.baseId
id = self.request.form.get('id', id)
factory = component.getUtility(IContainerFactory, name='bscw')
return factory(id, server=server, baseUrl=self.baseUrl)
@Lazy @Lazy
def item(self): def item(self):
@ -107,6 +98,3 @@ class BSCWView(BaseView):
for obj in proxy.values(): for obj in proxy.values():
yield self.itemView(obj, self.request, self) yield self.itemView(obj, self.request, self)
#def getUrlForObject(self, obj):
# url = absoluteURL(self.context, self.request)
# return '%s?id=%s' % (url, obj.internalPath)

View file

@ -9,14 +9,24 @@
</metal:view> </metal:view>
<metal:data define-macro="data">
</metal:data>
<metal:document define-macro="document">
</metal:document>
<metal:url define-macro="url">
</metal:url>
<metal:listing define-macro="listing"> <metal:listing define-macro="listing">
<dl> <dl tal:condition="python: view.remoteProxy.itemType == 'Folder'">
<tal:item repeat="item view/content"> <tal:item repeat="item view/content">
<dt> <dt>
<a tal:attributes="href item/url"> <a tal:attributes="href item/url">
<img width="16" height="16" <img width="16" height="16"
src="http://localhost:8080/plone/folder_icon.gif"
alt="BSCW Repository"
tal:attributes="src item/icon" /> tal:attributes="src item/icon" />
</a> </a>
<a tal:attributes="href item/url"> <a tal:attributes="href item/url">

View file

@ -33,6 +33,8 @@ from zope.interface import implements, Attribute
from cybertools.integrator.base import ContainerFactory, ItemFactory, FileFactory from cybertools.integrator.base import ContainerFactory, ItemFactory, FileFactory
from cybertools.integrator.base import ReadContainer, Item, File, Image from cybertools.integrator.base import ReadContainer, Item, File, Image
from cybertools.integrator.base import ExternalUrlInfo from cybertools.integrator.base import ExternalUrlInfo
from cybertools.integrator.interfaces import IContainerFactory
from cybertools.integrator.interfaces import IItemFactory, IFileFactory
from cybertools.text import mimetypes from cybertools.text import mimetypes
@ -53,6 +55,63 @@ urlAttributes = ['url_link', 'last_verified', 'last_error', 'content_length',
classes = ['cl_core.Folder', 'cl_core.Document', 'cl_core.URL', ] classes = ['cl_core.Folder', 'cl_core.Document', 'cl_core.URL', ]
class BSCWConnection(object):
factoryName = 'bscw'
baseURL = rootId = ''
def __init__(self, url, server=None):
self.repositoryURL = url
self.setURLs()
if server is None:
server = ServerProxy(self.baseURL())
self.server = server
def getRepositoryURL(self):
return self.repositoryURL
def setURLs(self):
url = self.getRepositoryURL()
if url:
self.baseURL, self.rootId = url.rsplit('/', 1)
def getItem(self, address):
return self.server.get_attributes(address,
['__class__', 'type', 'id', 'name', 'descr', 'url_link'], 1, True)
def getProxy(self, item=None, address=None, parentPath=''):
if item is None:
if address is None:
address = self.rootId
item = self.getItem(address)[0]
address = item['id']
itemType = item['__class__'].split('.')[-1]
internalPath = '/'.join((parentPath, address)).strip('/')
params = dict(connection=self, internalPath=internalPath,
properties=item, baseURL=self.baseURL,
itemType=itemType)
if itemType == 'Folder':
return self.containerFactory(address, **params)
elif itemType == 'Document':
return self.fileFactory(address, contentType=item['type'],
**params)
else:
return self.itemFactory(address, **params)
@Lazy
def itemFactory(self):
return component.getUtility(IItemFactory, name=self.factoryName)
@Lazy
def fileFactory(self):
return component.getUtility(IFileFactory, name=self.factoryName)
@Lazy
def containerFactory(self):
return component.getUtility(IContainerFactory, name=self.factoryName)
# proxy classes # proxy classes
class BSCWProxyBase(object): class BSCWProxyBase(object):
@ -62,7 +121,15 @@ class BSCWProxyBase(object):
id = self.address id = self.address
if id.startswith('bs_'): if id.startswith('bs_'):
id = id[3:] id = id[3:]
return ExternalUrlInfo(self.baseUrl, id) return ExternalUrlInfo(self.baseURL, id)
@Lazy
def attributes(self):
return self.connection.getItem(self.address)
@Lazy
def properties(self):
return self.attributes[0]
@Lazy @Lazy
def title(self): def title(self):
@ -77,15 +144,6 @@ class ReadContainer(BSCWProxyBase, ReadContainer):
factoryName = 'bscw' factoryName = 'bscw'
@Lazy
def properties(self):
return self.attributes[0]
@Lazy
def attributes(self):
return self.server.get_attributes(self.address,
['__class__', 'type', 'id', 'name', 'descr', 'url_link'], 1, True)
@Lazy @Lazy
def data(self): def data(self):
data = self.attributes data = self.attributes
@ -109,18 +167,7 @@ class ReadContainer(BSCWProxyBase, ReadContainer):
if key not in self.data: if key not in self.data:
return default return default
item = self.data[key] item = self.data[key]
itemType = item['__class__'].split('.')[-1] return self.connection.getProxy(item)
internalPath = '/'.join((self.internalPath, key)).strip('/')
params = dict(server=self.server, internalPath=internalPath,
properties=item, baseUrl=self.baseUrl,
itemType=itemType)
if itemType == 'Folder':
return self.containerFactory(item['id'], **params)
elif itemType == 'Document':
return self.fileFactory(item['id'], contentType=item['type'],
**params)
else:
return self.itemFactory(item['id'], **params)
def values(self): def values(self):
return [self.get(k) for k in self] return [self.get(k) for k in self]
@ -164,13 +211,13 @@ class ContainerFactory(ContainerFactory):
proxyClass = ReadContainer proxyClass = ReadContainer
def __call__(self, address, **kw): def xxx__call__(self, address, **kw):
server = kw.pop('server') server = kw.pop('server')
if isinstance(server, basestring): # just a URL, resolve for XML-RPC if isinstance(server, basestring): # just a URL, resolve for XML-RPC
server = ServerProxy(server) server = ServerProxy(server)
baseUrl = server baseURL = server
baseUrl = kw.pop('baseUrl', '') baseURL = kw.pop('baseURL', '')
return self.proxyClass(address, server=server, baseUrl=baseUrl, **kw) return self.proxyClass(address, server=server, baseURL=baseURL, **kw)
class ItemFactory(ItemFactory): class ItemFactory(ItemFactory):

View file

@ -12,7 +12,7 @@ Integration of external sources.
Accessing Objects on a Remote BSCW Repository Accessing Objects on a Remote BSCW Repository
============================================= =============================================
In fact we do not access a remote repository but just a dummy (fake) During testing we do not access a remote repository but just a dummy (fake)
repository for testing purposes. repository for testing purposes.
>>> from cybertools.integrator.tests.bscw import BSCWServer, sampleObjects >>> from cybertools.integrator.tests.bscw import BSCWServer, sampleObjects
@ -33,19 +33,23 @@ Let's first register the proxy factory utilities.
>>> component.provideUtility(ItemFactory(), name='bscw') >>> component.provideUtility(ItemFactory(), name='bscw')
>>> component.provideUtility(FileFactory(), name='bscw') >>> component.provideUtility(FileFactory(), name='bscw')
We can now access the root object of the BSCW repository We can now access the root object of the BSCW repository.
>>> from cybertools.integrator.interfaces import IContainerFactory >>> from cybertools.integrator.bscw import BSCWConnection
>>> root = component.getUtility(IContainerFactory, name='bscw')('4', server=server, >>> connection = BSCWConnection('http://localhost/bscw.cgi/4',
... baseUrl='http://localhost/bscw.cgi/') ... server=server)
>>> root = connection.getProxy()
>>> root
<...bscw.ReadContainer...>
>>> sorted(root.items()) >>> sorted(root.items())
[('bs_5', <...ReadContainer object...>)] [('bs_5', <...bscw.ReadContainer object...>)]
>>> root.address >>> root.address
'4' 'bs_4'
>>> root.internalPath >>> root.internalPath
'' 'bs_4'
>>> root.icon >>> root.icon
'folder' 'folder'
>>> root.properties >>> root.properties
@ -65,7 +69,7 @@ Let's also have a look at the item contained in the root object.
... ['__class__', 'type', 'id', 'name', 'descr', 'url_link'], 1, True) ... ['__class__', 'type', 'id', 'name', 'descr', 'url_link'], 1, True)
>>> bs_5.items() >>> bs_5.items()
[] [('bs_6', <...bscw.File ...>), ('bs_7', <...bscw.Item ...>)]
>>> bs_5.address >>> bs_5.address
'bs_5' 'bs_5'
>>> bs_5.internalPath >>> bs_5.internalPath
@ -82,13 +86,7 @@ Let's also have a look at the item contained in the root object.
The BSCW Repository View The BSCW Repository View
======================== ========================
>>> class BSCWAccess(object): >>> site['bscw'] = connection
... def __init__(self, server):
... self.server = server
... def getRepositoryURL(self):
... return self.server
>>> site['bscw'] = BSCWAccess(server)
>>> bscwAccess = site['bscw'] >>> bscwAccess = site['bscw']
>>> from cybertools.integrator.browser.bscw import BSCWView >>> from cybertools.integrator.browser.bscw import BSCWView

View file

@ -31,6 +31,7 @@ class Artifact(dict):
attributes = bscw.baseAttributes attributes = bscw.baseAttributes
repository = None repository = None
content = ''
def __init__(self, id, **kw): def __init__(self, id, **kw):
if not id.startswith('bs_'): if not id.startswith('bs_'):
@ -69,14 +70,19 @@ class BSCWRepository(dict):
sampleObjects = BSCWRepository( sampleObjects = BSCWRepository(
Artifact('4', name='public', descr='Public Repository', children=['5'], Artifact('4', name='public', descr='Public Repository', children=['5'],
containers=[dict(__id__='4711', name='Community of Anonymous')]), containers=[dict(__id__='4711', name='Community of Anonymous')]),
Artifact('5', name='Introduction', descr='Introduction to BSCW'), Artifact('5', name='Introduction', descr='Introduction to BSCW',
children=['6', '7']),
Artifact('6', name='Overview', descr='BSCW Overview',
__class__='cl_core.Document', type='application/pdf'),
Artifact('7', name='BSCW Home', descr='BSCW Homepage',
__class__='cl_core.URL', url_link='http://www.bscw.de/'),
) )
class BSCWServer(object): class BSCWServer(object):
def __init__(self, objects): def __init__(self, objects):
self. objects = objects self.objects = objects
def get_attributes(self, id=None, attribute_names=['__id__', 'name'], depth=0, def get_attributes(self, id=None, attribute_names=['__id__', 'name'], depth=0,
nested=False, offset=0, number=0, sorted_by=None): nested=False, offset=0, number=0, sorted_by=None):
@ -84,11 +90,14 @@ class BSCWServer(object):
if obj is None: if obj is None:
raise Fault(10101, 'Bad object id: %s' % id) raise Fault(10101, 'Bad object id: %s' % id)
result = [obj.getData(attribute_names)] result = [obj.getData(attribute_names)]
children = []
if nested: if nested:
for level in range(depth): for level in range(depth):
for id in obj.children: for id in obj.children:
result.append(self.get_attributes(id, attribute_names, if not children:
depth-1, nested, offset, number, sorted_by)) result.append(children)
children.append(self.get_attributes(id, attribute_names,
depth-1, nested, offset, number, sorted_by)[0])
return result return result
def get_attributenames(self, __class__): def get_attributenames(self, __class__):
@ -98,7 +107,7 @@ class BSCWServer(object):
obj = self.objects.get(id) obj = self.objects.get(id)
if obj is None: if obj is None:
raise Fault(10101, 'Bad object id: %s' % id) raise Fault(10101, 'Bad object id: %s' % id)
return '' return obj.get('content', '')
def get_path(id): def get_path(id):
return self.get_attributes(id, ['containers'])[0].get('containers', []) return self.get_attributes(id, ['containers'])[0].get('containers', [])