Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
action="store_true",
)
(options, args) = optparser.parse_args()
if options.doctest:
import os
if "TERM" in os.environ:
del os.environ["TERM"]
import doctest
failures, tests = doctest.testmod()
sys.exit(failures and 1 or 0)
# replace polib._POFileParser to show linenum of problematic msgstr
class ExtPOFileParser(polib._POFileParser):
def process(self, symbol, linenum):
super(ExtPOFileParser, self).process(symbol, linenum)
if symbol == "MS": # msgstr
self.current_entry.linenum = linenum
polib._POFileParser = ExtPOFileParser
detected = []
warning = options.warning
for f in args:
detected.extend(
(f, pe, errors) for pe, errors in check(polib.pofile(f), warning=warning)
)
if detected:
for f, pe, errors in detected:
for level, checker, error in errors:
if "TERM" in os.environ:
del os.environ["TERM"]
import doctest
failures, tests = doctest.testmod()
sys.exit(failures and 1 or 0)
# replace polib._POFileParser to show linenum of problematic msgstr
class ExtPOFileParser(polib._POFileParser):
def process(self, symbol, linenum):
super(ExtPOFileParser, self).process(symbol, linenum)
if symbol == "MS": # msgstr
self.current_entry.linenum = linenum
polib._POFileParser = ExtPOFileParser
detected = []
warning = options.warning
for f in args:
detected.extend(
(f, pe, errors) for pe, errors in check(polib.pofile(f), warning=warning)
)
if detected:
for f, pe, errors in detected:
for level, checker, error in errors:
sys.stderr.write(
"%s:%d:%s(%s): %s\n" % (f, pe.linenum, level, checker, error)
)
sys.exit(1)
def po_to_unicode(po_obj):
"""
Turns a polib :class:`polib.PoFile` or a :class:`polib.PoEntry`
into a :class:`unicode` string.
:param po_obj: Either a :class:`polib.PoFile` or :class:`polib.PoEntry`.
:rtype: :class:`unicode` string.
"""
po_text = po_obj.__str__()
if type(po_text) != types.UnicodeType:
po_text = po_text.decode('utf-8')
return po_text
class PoStream(polib._POFileParser):
"""
Create a POFile object from a :class:`~StringIO.StringIO`
instead of a file name or handle.::
import codecs
from StringIO import StringIO
po_string = codecs.open('sompofile.po', 'r', 'utf-8').read()
po_file_from_string = PoStream(StringIO(po_string)).parse()
unicode(po_file_from_string)
"""
def __init__(self, stream, fpath=None, encoding='utf-8', wrapwidth=78):
"""a pofileparser that can read from a stream"""