Mercurial > babel > old > mirror
changeset 161:975c68c0f833 stable
Ported [161] to 0.8.x branch.
author | cmlenz |
---|---|
date | Thu, 21 Jun 2007 12:15:13 +0000 |
parents | fbc3f7b43577 |
children | bdf9b34ed1e4 |
files | 0.8.x/babel/messages/pofile.py |
diffstat | 1 files changed, 51 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/0.8.x/babel/messages/pofile.py +++ b/0.8.x/babel/messages/pofile.py @@ -31,7 +31,57 @@ from babel.messages.catalog import Catalog from babel.util import LOCALTZ -__all__ = ['escape', 'normalize', 'read_po', 'write_po'] +__all__ = ['unescape', 'denormalize', 'read_po', 'escape', 'normalize', + 'write_po'] + +def unescape(string): + r"""Reverse `escape` the given string. + + >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"') + Say: + "hello, world!" + <BLANKLINE> + + :param string: the string to unescape + :return: the unescaped string + :rtype: `str` or `unicode` + """ + return string[1:-1].replace('\\\\', '\\') \ + .replace('\\t', '\t') \ + .replace('\\r', '\r') \ + .replace('\\n', '\n') \ + .replace('\\"', '\"') + +def denormalize(string): + r"""Reverse the normalization done by the `normalize` function. + + >>> print denormalize(r'''"" + ... "Say:\n" + ... " \"hello, world!\"\n"''') + Say: + "hello, world!" + <BLANKLINE> + + >>> print denormalize(r'''"" + ... "Say:\n" + ... " \"Lorem ipsum dolor sit " + ... "amet, consectetur adipisicing" + ... " elit, \"\n"''') + Say: + "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " + <BLANKLINE> + + :param string: the string to denormalize + :return: the denormalized string + :rtype: `unicode` or `str` + """ + if string.startswith('""'): + lines = [] + for line in string.splitlines()[1:]: + lines.append(unescape(line)) + return ''.join(lines) + else: + return unescape(string) def read_po(fileobj): """Read messages from a ``gettext`` PO (portable object) file from the given @@ -172,24 +222,6 @@ .replace('\n', '\\n') \ .replace('\"', '\\"') -def unescape(string): - r"""Reverse escape the given string. - - >>> print unescape('"Say:\\n \\"hello, world!\\"\\n"') - Say: - "hello, world!" - <BLANKLINE> - - :param string: the string to unescape - :return: the unescaped string - :rtype: `str` or `unicode` - """ - return string[1:-1].replace('\\\\', '\\') \ - .replace('\\t', '\t') \ - .replace('\\r', '\r') \ - .replace('\\n', '\n') \ - .replace('\\"', '\"') - def normalize(string, width=76): r"""Convert a string into a format that is appropriate for .po files. @@ -250,37 +282,6 @@ lines[-1] += '\n' return u'""\n' + u'\n'.join([escape(l) for l in lines]) -def denormalize(string): - r"""Reverse the normalization done by the `normalize` function. - - >>> print denormalize(r'''"" - ... "Say:\n" - ... " \"hello, world!\"\n"''') - Say: - "hello, world!" - <BLANKLINE> - - >>> print denormalize(r'''"" - ... "Say:\n" - ... " \"Lorem ipsum dolor sit " - ... "amet, consectetur adipisicing" - ... " elit, \"\n"''') - Say: - "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " - <BLANKLINE> - - :param string: the string to denormalize - :return: the denormalized string - :rtype: `unicode` or `str` - """ - if string.startswith('""'): - lines = [] - for line in string.splitlines()[1:]: - lines.append(unescape(line)) - return ''.join(lines) - else: - return unescape(string) - def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, sort_output=False, sort_by_file=False): r"""Write a ``gettext`` PO (portable object) template file for a given