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:
parent
dc970aa2f7
commit
73311e2cf8
5 changed files with 118 additions and 66 deletions
|
@ -29,6 +29,7 @@ from zope.traversing.browser import absoluteURL
|
|||
|
||||
from cybertools.integrator.bscw import ContainerFactory
|
||||
from cybertools.integrator.interfaces import IContainerFactory
|
||||
from cybertools.integrator.interfaces import IItemFactory, IFileFactory
|
||||
|
||||
|
||||
view_macros = ViewPageTemplateFile('view.pt')
|
||||
|
@ -74,9 +75,6 @@ class BSCWView(BaseView):
|
|||
viewTemplate = view_macros
|
||||
itemView = ItemView
|
||||
|
||||
baseUrl = ''
|
||||
baseId = ''
|
||||
|
||||
@Lazy
|
||||
def listing(self):
|
||||
return self.viewTemplate.macros['listing']
|
||||
|
@ -87,16 +85,9 @@ class BSCWView(BaseView):
|
|||
|
||||
@Lazy
|
||||
def remoteProxy(self):
|
||||
url = self.context.getRepositoryURL()
|
||||
if isinstance(url, basestring):
|
||||
server, id = url.rsplit('/', 1)
|
||||
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)
|
||||
id = self.request.form.get('id')
|
||||
proxy = self.context.getProxy(address=id)
|
||||
return proxy
|
||||
|
||||
@Lazy
|
||||
def item(self):
|
||||
|
@ -107,6 +98,3 @@ class BSCWView(BaseView):
|
|||
for obj in proxy.values():
|
||||
yield self.itemView(obj, self.request, self)
|
||||
|
||||
#def getUrlForObject(self, obj):
|
||||
# url = absoluteURL(self.context, self.request)
|
||||
# return '%s?id=%s' % (url, obj.internalPath)
|
||||
|
|
|
@ -9,14 +9,24 @@
|
|||
</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">
|
||||
<dl>
|
||||
<dl tal:condition="python: view.remoteProxy.itemType == 'Folder'">
|
||||
<tal:item repeat="item view/content">
|
||||
<dt>
|
||||
<a tal:attributes="href item/url">
|
||||
<img width="16" height="16"
|
||||
src="http://localhost:8080/plone/folder_icon.gif"
|
||||
alt="BSCW Repository"
|
||||
tal:attributes="src item/icon" />
|
||||
</a>
|
||||
<a tal:attributes="href item/url">
|
||||
|
|
|
@ -33,6 +33,8 @@ from zope.interface import implements, Attribute
|
|||
from cybertools.integrator.base import ContainerFactory, ItemFactory, FileFactory
|
||||
from cybertools.integrator.base import ReadContainer, Item, File, Image
|
||||
from cybertools.integrator.base import ExternalUrlInfo
|
||||
from cybertools.integrator.interfaces import IContainerFactory
|
||||
from cybertools.integrator.interfaces import IItemFactory, IFileFactory
|
||||
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', ]
|
||||
|
||||
|
||||
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
|
||||
|
||||
class BSCWProxyBase(object):
|
||||
|
@ -62,7 +121,15 @@ class BSCWProxyBase(object):
|
|||
id = self.address
|
||||
if id.startswith('bs_'):
|
||||
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
|
||||
def title(self):
|
||||
|
@ -77,15 +144,6 @@ class ReadContainer(BSCWProxyBase, ReadContainer):
|
|||
|
||||
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
|
||||
def data(self):
|
||||
data = self.attributes
|
||||
|
@ -109,18 +167,7 @@ class ReadContainer(BSCWProxyBase, ReadContainer):
|
|||
if key not in self.data:
|
||||
return default
|
||||
item = self.data[key]
|
||||
itemType = item['__class__'].split('.')[-1]
|
||||
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)
|
||||
return self.connection.getProxy(item)
|
||||
|
||||
def values(self):
|
||||
return [self.get(k) for k in self]
|
||||
|
@ -164,13 +211,13 @@ class ContainerFactory(ContainerFactory):
|
|||
|
||||
proxyClass = ReadContainer
|
||||
|
||||
def __call__(self, address, **kw):
|
||||
def xxx__call__(self, address, **kw):
|
||||
server = kw.pop('server')
|
||||
if isinstance(server, basestring): # just a URL, resolve for XML-RPC
|
||||
server = ServerProxy(server)
|
||||
baseUrl = server
|
||||
baseUrl = kw.pop('baseUrl', '')
|
||||
return self.proxyClass(address, server=server, baseUrl=baseUrl, **kw)
|
||||
baseURL = server
|
||||
baseURL = kw.pop('baseURL', '')
|
||||
return self.proxyClass(address, server=server, baseURL=baseURL, **kw)
|
||||
|
||||
|
||||
class ItemFactory(ItemFactory):
|
||||
|
|
|
@ -12,7 +12,7 @@ Integration of external sources.
|
|||
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.
|
||||
|
||||
>>> 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(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
|
||||
>>> root = component.getUtility(IContainerFactory, name='bscw')('4', server=server,
|
||||
... baseUrl='http://localhost/bscw.cgi/')
|
||||
>>> from cybertools.integrator.bscw import BSCWConnection
|
||||
>>> connection = BSCWConnection('http://localhost/bscw.cgi/4',
|
||||
... server=server)
|
||||
|
||||
>>> root = connection.getProxy()
|
||||
>>> root
|
||||
<...bscw.ReadContainer...>
|
||||
|
||||
>>> sorted(root.items())
|
||||
[('bs_5', <...ReadContainer object...>)]
|
||||
[('bs_5', <...bscw.ReadContainer object...>)]
|
||||
|
||||
>>> root.address
|
||||
'4'
|
||||
'bs_4'
|
||||
>>> root.internalPath
|
||||
''
|
||||
'bs_4'
|
||||
>>> root.icon
|
||||
'folder'
|
||||
>>> 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)
|
||||
|
||||
>>> bs_5.items()
|
||||
[]
|
||||
[('bs_6', <...bscw.File ...>), ('bs_7', <...bscw.Item ...>)]
|
||||
>>> bs_5.address
|
||||
'bs_5'
|
||||
>>> 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
|
||||
========================
|
||||
|
||||
>>> class BSCWAccess(object):
|
||||
... def __init__(self, server):
|
||||
... self.server = server
|
||||
... def getRepositoryURL(self):
|
||||
... return self.server
|
||||
|
||||
>>> site['bscw'] = BSCWAccess(server)
|
||||
>>> site['bscw'] = connection
|
||||
>>> bscwAccess = site['bscw']
|
||||
|
||||
>>> from cybertools.integrator.browser.bscw import BSCWView
|
||||
|
|
|
@ -31,6 +31,7 @@ class Artifact(dict):
|
|||
|
||||
attributes = bscw.baseAttributes
|
||||
repository = None
|
||||
content = ''
|
||||
|
||||
def __init__(self, id, **kw):
|
||||
if not id.startswith('bs_'):
|
||||
|
@ -69,14 +70,19 @@ class BSCWRepository(dict):
|
|||
sampleObjects = BSCWRepository(
|
||||
Artifact('4', name='public', descr='Public Repository', children=['5'],
|
||||
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):
|
||||
|
||||
def __init__(self, objects):
|
||||
self. objects = objects
|
||||
self.objects = objects
|
||||
|
||||
def get_attributes(self, id=None, attribute_names=['__id__', 'name'], depth=0,
|
||||
nested=False, offset=0, number=0, sorted_by=None):
|
||||
|
@ -84,11 +90,14 @@ class BSCWServer(object):
|
|||
if obj is None:
|
||||
raise Fault(10101, 'Bad object id: %s' % id)
|
||||
result = [obj.getData(attribute_names)]
|
||||
children = []
|
||||
if nested:
|
||||
for level in range(depth):
|
||||
for id in obj.children:
|
||||
result.append(self.get_attributes(id, attribute_names,
|
||||
depth-1, nested, offset, number, sorted_by))
|
||||
if not children:
|
||||
result.append(children)
|
||||
children.append(self.get_attributes(id, attribute_names,
|
||||
depth-1, nested, offset, number, sorted_by)[0])
|
||||
return result
|
||||
|
||||
def get_attributenames(self, __class__):
|
||||
|
@ -98,7 +107,7 @@ class BSCWServer(object):
|
|||
obj = self.objects.get(id)
|
||||
if obj is None:
|
||||
raise Fault(10101, 'Bad object id: %s' % id)
|
||||
return ''
|
||||
return obj.get('content', '')
|
||||
|
||||
def get_path(id):
|
||||
return self.get_attributes(id, ['containers'])[0].get('containers', [])
|
||||
|
|
Loading…
Add table
Reference in a new issue