From fc3e3096c80336c45246a2dd38045234febaf44d Mon Sep 17 00:00:00 2001 From: helmutm Date: Fri, 28 Mar 2008 14:42:54 +0000 Subject: [PATCH] export and import of DC annotations basically working git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@2479 fd906abe-77d9-0310-91a1-e0d9ade77398 --- external/README.txt | 42 ++++++++++++++++++++++++++++++++++++++---- external/annotation.py | 10 +++++++++- util.py | 8 ++++---- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/external/README.txt b/external/README.txt index c2fedda..e15245b 100644 --- a/external/README.txt +++ b/external/README.txt @@ -77,16 +77,29 @@ Working with nodes Sub-elements ------------ +Complex attributes or other informations related to an object may be +represented by sub-elements. The standard example for this kind of data +are the Dublin Core (DC) attributes. + +By importing the annotation module the corresponding element class will be +registered. + >>> from loops.external import annotation >>> input = """concept('myquery', u'My Query', 'query', viewName='mystuff.html')[ - ... annotations(creators='john')]""" + ... annotations(creators=(u'john',))]""" >>> elements = reader.read(input) >>> elements[0].subElements - [{'creators': 'john'}] + [{'creators': (u'john',)}] + +Loading the element with the sub-element stores the DC attributes. >>> loader.load(elements) - [('creators', 'john')] + >>> from zope.dublincore.interfaces import IZopeDublinCore + >>> dc = IZopeDublinCore(concepts['myquery']) + >>> dc.creators + (u'john',) + Exporting loops Objects ======================= @@ -120,20 +133,41 @@ Writing object information to the external storage node('home', u'Home', '', u'menu', body=u'Welcome') node('myquery', u'My Query', 'home', u'page', target=u'concepts/myquery')... -Writing subElements +Writing sub-elements ------------------- +Let's first set up a sequence with one element containing +two sub-elements. + >>> input = """concept('myquery', u'My Query', 'query', viewName='mystuff.html')[ ... annotations(creators='john'), ... annotations(modified='2007-08-12')]""" >>> elements = reader.read(input) >>> output = StringIO() >>> writer.write(elements, output) + +Writing this sequence reproduces the import format. + >>> print output.getvalue() concept('myquery', u'My Query', 'query', viewName='mystuff.html')[ annotations(creators='john'), annotations(modified='2007-08-12')]... +DC annotations will be exported automaticall after registering the +corresponding extractor adapter. + + >>> from loops.external.annotation import AnnotationsExtractor + >>> component.provideAdapter(AnnotationsExtractor) + + >>> output = StringIO() + >>> extractor = Extractor(loopsRoot, os.path.join(dataDirectory, 'export')) + >>> PyWriter().write(extractor.extract(), output) + + >>> print output.getvalue() + type(u'customer', u'Customer', options=u'', typeInterface=u'', viewName=u'')... + concept(u'myquery', u'My Query', u'query', options=u'', viewName='mystuff.html')[ + annotations(creators=(u'john',))]... + The Export/Import View ====================== diff --git a/external/annotation.py b/external/annotation.py index 2a33531..163321d 100644 --- a/external/annotation.py +++ b/external/annotation.py @@ -22,6 +22,8 @@ Export/import of annotations. $Id$ """ +from datetime import datetime +from time import time from zope.component import adapts from zope.dublincore.interfaces import IZopeDublinCore from zope.interface import implements @@ -40,7 +42,13 @@ class AnnotationsElement(Element): self[k] = v def __call__(self, loader): - print self.items() + obj = self.parent.object + dc = IZopeDublinCore(obj, None) + if dc is not None: + for k, v in self.items(): + if k in ('created', 'modified'): + v = datetime(*time.strptime(u'%Y-%m-%dT%H:%M')[0:6]) + setattr(dc, k, v) class AnnotationsExtractor(object): diff --git a/util.py b/util.py index fdb3c39..0aab98d 100644 --- a/util.py +++ b/util.py @@ -76,11 +76,11 @@ def nl2br(text): else: # gracefully handle Mac line endings return '
\n'.join(text.split('\r')) -def toUnicode(text, encoding='UTF-8'): - if type(text) is not unicode: - return text.decode(encoding) +def toUnicode(value, encoding='UTF-8'): + if type(value) is not unicode: + return value.decode(encoding) else: - return text + return value def getObjectForUid(uid, intIds=None):