[Zope3-checkins] CVS: Zope3/src/zope/app/dublincore - dcsv.py:1.1
Fred L. Drake, Jr.
fred at zope.com
Thu Aug 21 01:40:10 EDT 2003
Update of /cvs-repository/Zope3/src/zope/app/dublincore
In directory cvs.zope.org:/tmp/cvs-serv9641
Added Files:
dcsv.py
Log Message:
preliminary support for Dublin Core Structured Values; need tests
=== Added File Zope3/src/zope/app/dublincore/dcsv.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Functions for working with Dublin Core Structured Value (DCSV) data.
DCSV is specified in 'DCMI DCSV: A syntax for writing a list of
labelled values in a text string', at:
http://dublincore.org/documents/dcmi-dcsv/
$Id: dcsv.py,v 1.1 2003/08/21 04:40:10 fdrake Exp $
"""
import re
__all__ = "encode", "decode"
try:
basestring
except NameError:
# define basestring in Python 2.2.x:
try:
unicode
except NameError:
basestring = str
else:
basestring = str, unicode
def encode(items):
L = []
for item in items:
if isinstance(item, basestring):
L.append(_encode_string(item) + ";")
else:
k, v = item
if not isinstance(v, basestring):
raise TypeError("values must be strings; found %r" % v)
v = _encode_string(v)
if k:
if not isinstance(k, basestring):
raise TypeError("labels must be strings; found %r" % k)
k = _encode_string(k)
s = "%s=%s;" % (k, v)
else:
s = v + ";"
L.append(s)
return " ".join(L)
def _encode_string(s):
return s.replace("\\", r"\\").replace(";", r"\;").replace("=", r"\=")
def decode(text):
items = []
text = text.strip()
while text:
m = _find_interesting(text)
if m:
prefix, char = m.group(1, 2)
prefix = _decode_string(prefix).rstrip()
if char == ";":
items.append(('', prefix))
text = text[m.end():].lstrip()
continue
else: # char == "="
text = text[m.end():].lstrip()
# else we have a label
m = _find_value(text)
if m:
value = m.group(1)
text = text[m.end():].rstrip()
else:
value = text
text = ''
items.append((prefix, _decode_string(value)))
else:
items.append(('', _decode_string(text)))
break
return items
_prefix = r"((?:[^;\\=]|\\[;\\=])*)"
_find_interesting = re.compile(_prefix + "([;=])").match
_find_value = re.compile(_prefix + ";").match
def _decode_string(s):
if "\\" not in s:
return s
r = ""
while s:
c1 = s[0]
if c1 == "\\":
c2 = s[1:2]
if not c2:
return r + c1
r += c2
s = s[2:]
else:
r += c1
s = s[1:]
return r
More information about the Zope3-Checkins
mailing list