doc_template: add RTF encoding of unicode values
This commit is contained in:
parent
b723a1ff8f
commit
061b85b6e6
|
@ -3,13 +3,14 @@ import os.path
|
|||
import re
|
||||
import contextlib
|
||||
|
||||
__ALL__ = [ 'DocTemplateError', 'replace_variables', 'make_doc_from_template' ]
|
||||
__ALL__ = [ 'DocTemplateError', 'make_doc_from_template' ]
|
||||
|
||||
VARIABLE_RE = re.compile(r'@[A-Z]{2,3}[0-9]{0,2}')
|
||||
|
||||
class DocTemplateError(RuntimeError):
|
||||
def __str__(self):
|
||||
return ' '.join(self.args)
|
||||
return ' '.join(map(str, self.args))
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def delete_on_error(f):
|
||||
|
@ -20,6 +21,7 @@ def delete_on_error(f):
|
|||
os.remove(f.name)
|
||||
raise
|
||||
|
||||
|
||||
def replace_variables(template, variables):
|
||||
'''Lookup all substring looking like @XXX9 in the string template, and
|
||||
replace them by the value of the key XXX9 in the dictionary variables.
|
||||
|
@ -42,9 +44,35 @@ def replace_variables(template, variables):
|
|||
return variables[match_obj.group(0)[1:]]
|
||||
return re.sub(VARIABLE_RE, variable_replacement, template)
|
||||
|
||||
|
||||
def char_to_rtf(c):
|
||||
if ord(c) < 128:
|
||||
return c
|
||||
else:
|
||||
return '\u%d?' % ord(c)
|
||||
|
||||
|
||||
def utf8_to_rtf(s):
|
||||
s = ''.join([ char_to_rtf(c) for c in s])
|
||||
return '{\uc1{%s}}' % s
|
||||
|
||||
|
||||
def unicode_to_rtf(value):
|
||||
try:
|
||||
value = unicode(value)
|
||||
except Exception, e:
|
||||
raise DocTemplateError('Unable to get a unicode value', e)
|
||||
return utf8_to_rtf(value)
|
||||
|
||||
|
||||
def variables_to_rtf(variables):
|
||||
return dict(((k, unicode_to_rtf(v)) for k,v in variables.iteritems()))
|
||||
|
||||
|
||||
def make_doc_from_template(from_path, to_path, variables):
|
||||
'''Use file from_path as a template to combine with the variables
|
||||
dictionary and place the result in the file to_path.
|
||||
Encode value of variable into encoding of UTF-8 for the RTF file format.
|
||||
|
||||
:param from_path: the template file path
|
||||
:param to_path: the newly created file containing the result of the templating
|
||||
|
@ -55,6 +83,7 @@ def make_doc_from_template(from_path, to_path, variables):
|
|||
raise DocTemplateError('Template file does not exist', repr(from_path))
|
||||
if os.path.exists(to_path):
|
||||
raise DocTemplateError('Destination file already exists', repr(to_path))
|
||||
variables = variables_to_rtf(variables)
|
||||
with open(to_path, 'w') as to_file:
|
||||
with open(from_path) as from_file:
|
||||
with delete_on_error(to_file):
|
||||
|
@ -63,8 +92,8 @@ def make_doc_from_template(from_path, to_path, variables):
|
|||
if __name__ == '__main__':
|
||||
import sys
|
||||
try:
|
||||
variables = dict((arg.split('=') for arg in sys.argv[3:]))
|
||||
variables = dict((map(lambda x: unicode(x, 'utf-8'), arg.split('=')) for arg in sys.argv[3:]))
|
||||
make_doc_from_template(sys.argv[1], sys.argv[2], variables)
|
||||
except Exception, e:
|
||||
print 'Error:', e
|
||||
raise
|
||||
print 'Usage: python doc_template.py <template.rtf> <output.rtf> [KEY1=value1 KEY2=value2 ...]'
|
||||
|
|
Reference in New Issue