/usr/share/pyshared/zope/dublincore/dcsv.py is in python-zope.dublincore 3.8.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | ##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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 Values (DCSV) scheme.
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/
"""
__docformat__ = 'restructuredtext'
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, "values") + ";")
else:
k, v = item
if not isinstance(v, basestring):
raise TypeError("values must be strings; found %r" % 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, "labels")
s = "%s=%s;" % (k, v)
else:
s = v + ";"
L.append(s)
return " ".join(L)
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"\=")
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():].lstrip()
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.rstrip()
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.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
|