def fields_as_dict(self, date_format='%Y %b %d %H:%M', none_string='', escape_style=None, bool_strings=None): if bool_strings is None: bools = {True: 'True', False: 'False'} else: bools = {True: bool_strings[0], False: bool_strings[1]} data = {} for field in self._idx.keys(): # FIXME: harden against BYTEA fields #if type(self._payload[self._idx[field]]) == ... # data[field] = _('<%s bytes of binary data>') % len(self._payload[self._idx[field]]) # continue val = self._payload[self._idx[field]] if val is None: data[field] = none_string continue if isinstance(val, bool): data[field] = bools[val] continue if isinstance(val, datetime.datetime): if date_format is None: data[field] = val continue data[field] = pydt_strftime(val, format=date_format) if escape_style in ['latex', 'tex']: data[field] = tex_escape_string(data[field]) elif escape_style in ['xetex', 'xelatex']: data[field] = xetex_escape_string(data[field]) continue try: data[field] = str(val, encoding='utf8', errors='replace') except TypeError: try: data[field] = str(val) except (UnicodeDecodeError, TypeError): val = '%s' % str(val) data[field] = val.decode('utf8', 'replace') if escape_style in ['latex', 'tex']: data[field] = tex_escape_string(data[field]) elif escape_style in ['xetex', 'xelatex']: data[field] = xetex_escape_string(data[field]) return data
def fields_as_dict(self, date_format='%Y %b %d %H:%M', none_string='', escape_style=None, bool_strings=None): if bool_strings is None: bools = {True: 'True', False: 'False'} else: bools = {True: bool_strings[0], False: bool_strings[1]} data = {} for field in self._idx.keys(): # FIXME: harden against BYTEA fields #if type(self._payload[self._idx[field]]) == ... # data[field] = _('<%s bytes of binary data>') % len(self._payload[self._idx[field]]) # continue val = self._payload[self._idx[field]] if val is None: data[field] = none_string continue if isinstance(val, bool): data[field] = bools[val] continue if isinstance(val, datetime.datetime): if date_format is None: data[field] = val continue data[field] = pydt_strftime(val, format = date_format) if escape_style in ['latex', 'tex']: data[field] = tex_escape_string(data[field]) elif escape_style in ['xetex', 'xelatex']: data[field] = xetex_escape_string(data[field]) continue try: data[field] = str(val, encoding = 'utf8', errors = 'replace') except TypeError: try: data[field] = str(val) except (UnicodeDecodeError, TypeError): val = '%s' % str(val) data[field] = val.decode('utf8', 'replace') if escape_style in ['latex', 'tex']: data[field] = tex_escape_string(data[field]) elif escape_style in ['xetex', 'xelatex']: data[field] = xetex_escape_string(data[field]) return data