def contacts(schema, lines): while 1: try: delim, name, items = icslex.propertyitems(lines) except StopIteration: break if delim == 'begin': continue obj = {} for n, v in items: ty, fields, cmin, cmax = schema[n] if v.has_key('type'): v['type'] = v['type'].split(',') if fields: v.update(dict(lex_fields(v['_'], fields))) elif ty == 'text': v['text'] = icslex.unesc(v['_']) elif ty == 'mime': fix_bin(v) else: raise RuntimeError, 'type not implemented: %s' % ty if cmax is None: vlist = obj.get(n, None) if vlist is None: vlist = obj[n] = [] vlist.append(v) else: obj[n] = v yield obj
def lex_fields(txt, names, ty='text'): """parse the N parts and elaborate using hCard names >>> lex_fields('Doe;John;;;;', Properties['n'][1]) [('family-name', 'Doe'), ('given-name', 'John')] """ parts = txt.split(';') # @@ quoting details? if ty == 'text': parts = [icslex.unesc(v) for v in parts] return [(k, v) for k, v in zip(names, parts) if v]
def doText(sx, elt, params, val): attrs = {} val = unesc(val) # @@ or only if not QUOTED-PRINTABLE ? for pn, pv in params: if pn == 'VALUE': pass elif (pn, pv) == ('ENCODING', 'QUOTED-PRINTABLE'): val = quopri.decodestring(val) elif pn == "LANGUAGE": attrs['xml:lang'] = pv else: warn("unexpected param %s=%s on elt '%s'" % (pn, pv, elt)) sx.startElement(elt, attrs) sx.characters(val, 0, len(val)) sx.endElement(elt)
def doText(sx, elt, params, val): attrs = {} val = unesc(val) # @@ or only if not QUOTED-PRINTABLE ? for pn, pv in params: if pn=='VALUE': pass elif (pn, pv) == ('ENCODING', 'QUOTED-PRINTABLE'): val = quopri.decodestring(val) elif pn == "LANGUAGE": attrs[ 'xml:lang'] = pv else: warn("unexpected param %s=%s on elt '%s'" % (pn, pv, elt)) sx.startElement(elt, attrs) sx.characters(val, 0, len(val)) sx.endElement(elt)