From 1f2c52bb47370f66dcb2e56f2200e6040ce2d34b Mon Sep 17 00:00:00 2001 From: oysteini Date: Tue, 11 Oct 2011 15:44:17 +0000 Subject: [PATCH] Hacks for character encoding in fileformat.py. I try to consistently translate strings to unicode objects when reading, and encode all unicode objects when writing. But it is not clear when the translation should happen. This module needs to be cleaned up a little. --- cli/fileformat.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cli/fileformat.py b/cli/fileformat.py index c2d005f..7c09060 100644 --- a/cli/fileformat.py +++ b/cli/fileformat.py @@ -1,6 +1,8 @@ import re import types +file_encoding = 'utf8' + # The possible fields for each type of object. # # Each field is a tuple (fieldname, fieldtype). Fieldtype is either @@ -171,23 +173,27 @@ def write_field_value_str(val): lines = '' if not val: val = '' - val = unicode(val) value_lines = val.split('\n') for l in value_lines: lines += ' ' + l + '\n' + if len(value_lines) > 1: + lines = '\n' + lines return lines +def write_field_value_int(val): + return ' %s\n' % str(val) + def write_field_value_dict(val): lines = '\n' for (key,values) in val.items(): for single_value in values: - lines += ' ' + key + ' ' + unicode(single_value) + '\n' + lines += ' ' + key + ' ' + single_value + '\n' return lines def write_field_value_list(val): lines = '' for single_value in val: - lines += ' ' + unicode(single_value) + lines += ' ' + single_value return lines def make_comment(s): @@ -197,7 +203,7 @@ def make_comment(s): def write_action(d): if type(d) in types.StringTypes: return make_comment(d) - lines = '' + lines = u'' if 'comment' in d: lines += make_comment(d['comment']) action = d['action'] @@ -206,13 +212,14 @@ def write_action(d): for field, ftype in fields[data_type]: if field in d: value_writer = {'s': write_field_value_str, - 'i': write_field_value_str, + 'i': write_field_value_int, 'd': write_field_value_dict, 'l': write_field_value_list}[ftype] lines += field + ':' + value_writer(d[field]) - return lines + return lines.encode(file_encoding) def write_actionlist(actions): - return '\n'.join(map(write_action, actions)) + encoding_comment = '# -*- coding: %s -*-\n' % file_encoding + return encoding_comment + '\n'.join(map(write_action, actions)) # test: print write_actionlist([{'comment':'Foo!\nBar!','action':'new-book','isbn':'434545'},{'action':'edit-book','isbn':'654654745','persons':{'author':['ab','foo'],'illustrator':['moo']}},'This\nis\na\ncomment.',{'action':'edit-category','id':'matematikk','name':'Matematikk','placement':['T10','T11']}])