~osomon/pyexiv2/pyexiv2-0.3

« back to all changes in this revision

Viewing changes to src/pyexiv2/xmp.py

  • Committer: Olivier Tilloy
  • Date: 2011-08-18 06:51:18 UTC
  • mfrom: (360.1.6 check-tag-family)
  • Revision ID: olivier@tilloy.net-20110818065118-bqkpt1305v64nqg7
Check the type before setting an XMP tag’s value and raise a TypeError if it doesn’t match.
Allow setting the value of a LangAlt tag to a single string, in which case it is automatically converted to {'x-default': value}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
218
218
                stype = stype[17:]
219
219
            self.raw_value = self._convert_to_string(value, stype)
220
220
        elif type in ('XmpAlt', 'XmpBag', 'XmpSeq'):
 
221
            if not isinstance(value, (list, tuple)):
 
222
                raise TypeError('Expecting a list of values')
221
223
            stype = self.type[4:]
222
224
            if stype.lower().startswith('closed choice of'):
223
225
                stype = stype[17:]
224
226
            self.raw_value = map(lambda x: self._convert_to_string(x, stype), value)
225
227
        elif type == 'LangAlt':
 
228
            if isinstance(value, basestring):
 
229
                value = {'x-default': value}
 
230
            if not isinstance(value, dict):
 
231
                raise TypeError('Expecting a dictionary mapping language codes to values')
226
232
            raw_value = {}
227
233
            for k, v in value.iteritems():
228
234
                try: