[Zope3-checkins] CVS: Zope3/src/zope/app/dublincore - dcsv.py:1.3

Fred L. Drake, Jr. fred at zope.com
Fri Aug 22 10:08:41 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/dublincore
In directory cvs.zope.org:/tmp/cvs-serv21705

Modified Files:
	dcsv.py 
Log Message:
- more error checking when encoding strings; leading and trailing
  spaces in labels and values are not allowed since we won't read them
  back that way
- handle more edge cases when scanning labels and values: anything
  could be escaped even though it's not necessary to escape most
  things
- createMapping(): new convenience function


=== Zope3/src/zope/app/dublincore/dcsv.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/dublincore/dcsv.py:1.2	Thu Aug 21 00:52:51 2003
+++ Zope3/src/zope/app/dublincore/dcsv.py	Fri Aug 22 09:08:40 2003
@@ -41,23 +41,26 @@
     L = []
     for item in items:
         if isinstance(item, basestring):
-            L.append(_encode_string(item) + ";")
+            L.append(_encode_string(item, "values") + ";")
         else:
             k, v = item
             if not isinstance(v, basestring):
                 raise TypeError("values must be strings; found %r" % v)
-            v = _encode_string(v)
+            v = _encode_string(v, "values")
             if k:
                 if not isinstance(k, basestring):
                     raise TypeError("labels must be strings; found %r" % k)
-                k = _encode_string(k)
+                k = _encode_string(k, "labels")
                 s = "%s=%s;" % (k, v)
             else:
                 s = v + ";"
             L.append(s)
     return " ".join(L)
 
-def _encode_string(s):
+def _encode_string(s, what):
+    if s.strip() != s:
+        raise ValueError("%s may not include leading or trailing spaces: %r"
+                         % (what, s))
     return s.replace("\\", r"\\").replace(";", r"\;").replace("=", r"\=")
 
 
@@ -89,13 +92,13 @@
             break
     return items
 
-_prefix = r"((?:[^;\\=]|\\[;\\=])*)"
+_prefix = r"((?:[^;\\=]|\\.)*)"
 _find_interesting = re.compile(_prefix + "([;=])").match
 _find_value = re.compile(_prefix + ";").match
 
 def _decode_string(s):
     if "\\" not in s:
-        return s
+        return s.rstrip()
     r = ""
     while s:
         c1 = s[0]
@@ -108,4 +111,27 @@
         else:
             r += c1
             s = s[1:]
-    return r
+    return r.rstrip()
+
+
+def createMapping(items, allow_duplicates=False):
+    mapping = {}
+    for item in items:
+        if isinstance(item, basestring):
+            raise ValueError("can't create mapping with unlabelled data")
+        k, v = item
+        if not isinstance(k, basestring):
+            raise TypeError("labels must be strings; found %r" % k)
+        if not isinstance(v, basestring):
+            raise TypeError("values must be strings; found %r" % v)
+        if k in mapping:
+            if allow_duplicates:
+                mapping[k].append(v)
+            else:
+                raise ValueError("labels may not have more than one value")
+        else:
+            if allow_duplicates:
+                mapping[k] = [v]
+            else:
+                mapping[k] = v
+    return mapping




More information about the Zope3-Checkins mailing list