From: Armin Ronacher Date: Thu, 25 Jul 2013 07:31:34 +0000 (+0200) Subject: Some codestyle updates in babel.messages.pofile X-Git-Tag: 1.0~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a86b1259a02da4b0ab4911c59381a5e345e9e190;p=thirdparty%2Fbabel.git Some codestyle updates in babel.messages.pofile --- diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 6c9c6f9f..4aaf406f 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -17,8 +17,6 @@ from babel.messages.catalog import Catalog, Message from babel.util import wraptext from babel._compat import text_type -__all__ = ['read_po', 'write_po'] - def unescape(string): r"""Reverse `escape` the given string. @@ -29,7 +27,6 @@ def unescape(string): :param string: the string to unescape - :return: the unescaped string """ def replace_escapes(match): m = match.group(1) @@ -43,6 +40,7 @@ def unescape(string): return m return re.compile(r'\\([\\trn"])').sub(replace_escapes, string[1:-1]) + def denormalize(string): r"""Reverse the normalization done by the `normalize` function. @@ -63,8 +61,6 @@ def denormalize(string): :param string: the string to denormalize - :return: the denormalized string - :rtype: `unicode` or `str` """ if '\n' in string: escaped_lines = string.splitlines() @@ -75,6 +71,7 @@ def denormalize(string): else: return unescape(string) + def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=None): """Read messages from a ``gettext`` PO (portable object) file from the given file-like object and return a `Catalog`. @@ -120,8 +117,6 @@ def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=No :param domain: the message domain :param ignore_obsolete: whether to ignore obsolete messages in the input :param charset: the character set of the catalog. - :return: a catalog object representing the parsed PO file - :rtype: `Catalog` """ catalog = Catalog(locale=locale, domain=domain, charset=charset) @@ -252,12 +247,14 @@ def read_po(fileobj, locale=None, domain=None, ignore_obsolete=False, charset=No return catalog + WORD_SEP = re.compile('(' r'\s+|' # any whitespace r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w)' # em-dash ')') + def escape(string): r"""Escape the given string so that it can be included in double-quoted strings in ``PO`` files. @@ -268,7 +265,6 @@ def escape(string): '"Say:\\n \\"hello, world!\\"\\n"' :param string: the string to escape - :return: the escaped string """ return '"%s"' % string.replace('\\', '\\\\') \ .replace('\t', '\\t') \ @@ -276,6 +272,7 @@ def escape(string): .replace('\n', '\\n') \ .replace('\"', '\\"') + def normalize(string, prefix='', width=76): r"""Convert a string into a format that is appropriate for .po files. @@ -299,7 +296,6 @@ def normalize(string, prefix='', width=76): :param prefix: a string that should be prepended to every line :param width: the maximum line width; use `None`, 0, or a negative number to completely disable line wrapping - :return: the normalized string """ if width and width > 0: prefixlen = len(prefix) @@ -337,6 +333,7 @@ def normalize(string, prefix='', width=76): lines[-1] += '\n' return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines]) + def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, sort_output=False, sort_by_file=False, ignore_obsolete=False, include_previous=False):