Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

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.
This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-10-11 15:44:17 +00:00
parent bf188c99ae
commit 1f2c52bb47
1 changed files with 14 additions and 7 deletions

View File

@ -1,6 +1,8 @@
import re import re
import types import types
file_encoding = 'utf8'
# The possible fields for each type of object. # The possible fields for each type of object.
# #
# Each field is a tuple (fieldname, fieldtype). Fieldtype is either # Each field is a tuple (fieldname, fieldtype). Fieldtype is either
@ -171,23 +173,27 @@ def write_field_value_str(val):
lines = '' lines = ''
if not val: if not val:
val = '' val = ''
val = unicode(val)
value_lines = val.split('\n') value_lines = val.split('\n')
for l in value_lines: for l in value_lines:
lines += ' ' + l + '\n' lines += ' ' + l + '\n'
if len(value_lines) > 1:
lines = '\n' + lines
return lines return lines
def write_field_value_int(val):
return ' %s\n' % str(val)
def write_field_value_dict(val): def write_field_value_dict(val):
lines = '\n' lines = '\n'
for (key,values) in val.items(): for (key,values) in val.items():
for single_value in values: for single_value in values:
lines += ' ' + key + ' ' + unicode(single_value) + '\n' lines += ' ' + key + ' ' + single_value + '\n'
return lines return lines
def write_field_value_list(val): def write_field_value_list(val):
lines = '' lines = ''
for single_value in val: for single_value in val:
lines += ' ' + unicode(single_value) lines += ' ' + single_value
return lines return lines
def make_comment(s): def make_comment(s):
@ -197,7 +203,7 @@ def make_comment(s):
def write_action(d): def write_action(d):
if type(d) in types.StringTypes: if type(d) in types.StringTypes:
return make_comment(d) return make_comment(d)
lines = '' lines = u''
if 'comment' in d: if 'comment' in d:
lines += make_comment(d['comment']) lines += make_comment(d['comment'])
action = d['action'] action = d['action']
@ -206,13 +212,14 @@ def write_action(d):
for field, ftype in fields[data_type]: for field, ftype in fields[data_type]:
if field in d: if field in d:
value_writer = {'s': write_field_value_str, value_writer = {'s': write_field_value_str,
'i': write_field_value_str, 'i': write_field_value_int,
'd': write_field_value_dict, 'd': write_field_value_dict,
'l': write_field_value_list}[ftype] 'l': write_field_value_list}[ftype]
lines += field + ':' + value_writer(d[field]) lines += field + ':' + value_writer(d[field])
return lines return lines.encode(file_encoding)
def write_actionlist(actions): 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']}]) # 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']}])