Skip to content

Commit

Permalink
Replaced cStringIO.StringIO by io.BytesIO
Browse files Browse the repository at this point in the history
The cStringIO module is no more in Python 3, and the io module
has been introduced in Python 2.6.
  • Loading branch information
claudep committed Aug 18, 2015
1 parent c5e7ada commit a5eebce
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 48 deletions.
4 changes: 2 additions & 2 deletions translate/convert/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
:mod:`translate.convert` tools)."""

import os.path
from cStringIO import StringIO
from io import BytesIO

from translate.misc import optrecurse

Expand Down Expand Up @@ -391,7 +391,7 @@ def openoutputfile(self, options, fulloutputpath):
if outputstream is None:
self.warning("Could not find where to put %s in output "
"archive; writing to tmp" % fulloutputpath)
return StringIO()
return BytesIO()
return outputstream
else:
return super(ArchiveConvertOptionParser, self).openoutputfile(options, fulloutputpath)
Expand Down
4 changes: 2 additions & 2 deletions translate/convert/idml2po.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

"""Convert IDML files to PO localization files."""

from cStringIO import StringIO
from io import BytesIO

from translate.convert import convert
from translate.storage import factory
Expand Down Expand Up @@ -48,7 +48,7 @@ def convert_idml(inputfile, outputfile, template):
for filename, translatable_file in contents.iteritems():
parse_state = ParseState(NO_TRANSLATE_ELEMENTS, INLINE_ELEMENTS)
po_store_adder = make_postore_adder(store, id_maker, filename)
build_idml_store(StringIO(translatable_file), store, parse_state,
build_idml_store(BytesIO(translatable_file), store, parse_state,
store_adder=po_store_adder)

store.save()
Expand Down
4 changes: 2 additions & 2 deletions translate/convert/odf2xliff.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
for examples and usage instructions.
"""

from cStringIO import StringIO
from io import BytesIO

from translate.convert import convert
from translate.storage import factory
Expand Down Expand Up @@ -52,7 +52,7 @@ def convertodf(inputfile, outputfile, templates):
contents = open_odf(inputfile)
for data in contents.values():
parse_state = ParseState(no_translate_content_elements, inline_elements)
build_store(StringIO(data), store, parse_state)
build_store(BytesIO(data), store, parse_state)

store.save()
return True
Expand Down
4 changes: 2 additions & 2 deletions translate/convert/po2idml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
of the PO file.
"""

from cStringIO import StringIO
from io import BytesIO
from zipfile import ZIP_DEFLATED, ZipFile

import lxml.etree as etree
Expand Down Expand Up @@ -167,7 +167,7 @@ def convertpo(input_file, output_file, template):
if filename.startswith('Stories/')]

po_data = input_file.read()
dom_trees = translate_idml(template, StringIO(po_data), translatable_files)
dom_trees = translate_idml(template, BytesIO(po_data), translatable_files)

write_idml(template_zip, output_file, dom_trees)
output_file.close()
Expand Down
5 changes: 3 additions & 2 deletions translate/convert/po2web2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
for examples and usage instructions.
"""

from io import BytesIO

from translate.convert import convert
from translate.storage import factory

Expand All @@ -35,8 +37,7 @@ def __init__(self):
return

def convertstore(self, inputstore, includefuzzy):
from cStringIO import StringIO
str_obj = StringIO()
str_obj = BytesIO()

mydict = dict()
for unit in inputstore.units:
Expand Down
6 changes: 3 additions & 3 deletions translate/convert/xliff2odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"""

import zipfile
from cStringIO import StringIO
from io import BytesIO

import lxml.etree as etree

Expand All @@ -49,7 +49,7 @@ def load_dom_trees(template):
the etrees for each of those translatable files.
"""
odf_data = open_odf(template)
return dict((filename, etree.parse(StringIO(data)))
return dict((filename, etree.parse(BytesIO(data)))
for filename, data in odf_data.iteritems())

def load_unit_tree(input_file):
Expand Down Expand Up @@ -127,7 +127,7 @@ def convertxliff(input_file, output_file, template):
output_file = file(output_file.name, mode='wb')

xlf_data = input_file.read()
dom_trees = translate_odf(template, StringIO(xlf_data))
dom_trees = translate_odf(template, BytesIO(xlf_data))
write_odf(template, output_file, dom_trees)
output_file.close()
return True
Expand Down
4 changes: 2 additions & 2 deletions translate/misc/optrecurse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import re
import sys
import traceback
from cStringIO import StringIO
from io import BytesIO

from translate import __version__
from translate.misc import progressbar
Expand Down Expand Up @@ -536,7 +536,7 @@ def openoutputfile(self, options, fulloutputpath):

def opentempoutputfile(self, options, fulloutputpath):
"""Opens a temporary output file."""
return StringIO()
return BytesIO()

def finalizetempoutputfile(self, options, outputfile, fulloutputpath):
"""Write the temp outputfile to its final destination."""
Expand Down
4 changes: 2 additions & 2 deletions translate/services/tmserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import json
import logging
from argparse import ArgumentParser
from io import BytesIO
from urlparse import parse_qs

from translate.misc import selector, wsgi
Expand Down Expand Up @@ -122,10 +123,9 @@ def get_store_stats(self, environ, start_response, sid):
@selector.opliant
def upload_store(self, environ, start_response, sid, slang, tlang):
"""add units from uploaded file to tmdb"""
from cStringIO import StringIO
from translate.storage import factory
start_response("200 OK", [('Content-type', 'text/plain')])
data = StringIO(environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])))
data = BytesIO(environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])))
data.name = sid
store = factory.getobject(data)
count = self.tmdb.add_store(store, slang, tlang)
Expand Down
6 changes: 3 additions & 3 deletions translate/storage/csvl10n.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import codecs
import csv
from cStringIO import StringIO
from io import BytesIO

from translate.misc import sparse
from translate.storage import base
Expand Down Expand Up @@ -323,7 +323,7 @@ def valid_fieldnames(fieldnames):

def detect_header(sample, dialect, fieldnames):
"""Test if file has a header or not, also returns number of columns in first row"""
inputfile = StringIO(sample)
inputfile = BytesIO(sample)
try:
reader = csv.reader(inputfile, dialect)
except csv.Error:
Expand Down Expand Up @@ -420,7 +420,7 @@ def __str__(self):
return source.encode(encoding)

def getoutput(self):
outputfile = StringIO()
outputfile = BytesIO()
writer = csv.DictWriter(outputfile, self.fieldnames, extrasaction='ignore', dialect=self.dialect)
# write header
hdict = dict(map(None, self.fieldnames, self.fieldnames))
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/dtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

import re
import warnings
from cStringIO import StringIO
from io import BytesIO
try:
from lxml import etree
except ImportError:
Expand Down Expand Up @@ -598,7 +598,7 @@ def _valid_store(self):
if etree is not None and not self.android:
try:
# #expand is a Mozilla hack and are removed as they are not valid in DTDs
dtd = etree.DTD(StringIO(re.sub("#expand", "", self.getoutput())))
dtd = etree.DTD(BytesIO(re.sub("#expand", "", self.getoutput())))
except etree.DTDParseError as e:
warnings.warn("DTD parse error: %s" % e.error_log)
return False
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/fpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import copy
import re
from cStringIO import StringIO
from io import BytesIO

from translate.lang import data
from translate.misc.multistring import multistring
Expand Down Expand Up @@ -330,7 +330,7 @@ def hasplural(self):

def parse(self, src):
raise DeprecationWarning("Should not be parsing with a unit")
return poparser.parse_unit(poparser.ParseState(StringIO(src), pounit), self)
return poparser.parse_unit(poparser.ParseState(BytesIO(src), pounit), self)

def __str__(self):
"""convert to a string. double check that unicode is handled somehow here"""
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/ical.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"""

import re
from cStringIO import StringIO
from io import BytesIO

import vobject

Expand Down Expand Up @@ -120,7 +120,7 @@ def parse(self, input):
input.close()
input = inisrc
if isinstance(input, str):
input = StringIO(input)
input = BytesIO(input)
self._icalfile = vobject.readComponents(input).next()
else:
self._icalfile = vobject.readComponents(open(input)).next()
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"""

import re
from cStringIO import StringIO
from io import BytesIO

from iniparse import INIConfig

Expand Down Expand Up @@ -128,7 +128,7 @@ def parse(self, input):
input = inisrc

if isinstance(input, str):
input = StringIO(input)
input = BytesIO(input)
self._inifile = INIConfig(input, optionxformvalue=None)
else:
self._inifile = INIConfig(file(input), optionxformvalue=None)
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/jsonl10n.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

import json
import os
from cStringIO import StringIO
from io import BytesIO

from translate.storage import base

Expand Down Expand Up @@ -212,7 +212,7 @@ def parse(self, input):
input.close()
input = src
if isinstance(input, str):
input = StringIO(input)
input = BytesIO(input)
try:
self._file = json.load(input)
except ValueError as e:
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/placeables/test_terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from cStringIO import StringIO
from io import BytesIO

from translate.search.match import terminologymatcher
from translate.storage.placeables import StringElem, base, general, parse
Expand All @@ -43,7 +43,7 @@ class TestTerminologyPlaceable:
"""

def setup_method(self, method):
self.term_po = pofile(StringIO(self.TERMINOLOGY))
self.term_po = pofile(BytesIO(self.TERMINOLOGY))
self.matcher = terminologymatcher(self.term_po)
self.test_string = u'<b>Inpüt</b> file name thingy.'

Expand Down
6 changes: 3 additions & 3 deletions translate/storage/pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import copy
import re
import textwrap
from cStringIO import StringIO
from io import BytesIO

from translate.lang import data
from translate.misc import quote
Expand Down Expand Up @@ -576,7 +576,7 @@ def hasplural(self):
return len(self.msgid_plural) > 0

def parse(self, src):
return poparser.parse_unit(poparser.ParseState(StringIO(src), pounit), self)
return poparser.parse_unit(poparser.ParseState(BytesIO(src), pounit), self)

def _getmsgpartstr(self, partname, partlines, partcomments=""):
if isinstance(partlines, dict):
Expand Down Expand Up @@ -765,7 +765,7 @@ def parse(self, input):
elif not getattr(self, 'filename', ''):
self.filename = ''
if isinstance(input, str):
input = StringIO(input)
input = BytesIO(input)
# clear units to get rid of automatically generated headers before parsing
self.units = []
poparser.parse_units(poparser.ParseState(input, pounit), self)
Expand Down
4 changes: 2 additions & 2 deletions translate/storage/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import os
import tempfile
from cStringIO import StringIO
from io import BytesIO

try:
from aeidon import Subtitle, documents, newlines
Expand Down Expand Up @@ -97,7 +97,7 @@ def __str__(self):
subtitle.start = unit._start
subtitle.end = unit._end
subtitles.append(subtitle)
output = StringIO()
output = BytesIO()
self._subtitlefile.write_to_file(subtitles, documents.MAIN, output)
return output.getvalue().encode(self._subtitlefile.encoding)

Expand Down
4 changes: 2 additions & 2 deletions translate/storage/test_mo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import subprocess
import sys
from cStringIO import StringIO
from io import BytesIO

from translate.storage import factory, mo, test_base

Expand Down Expand Up @@ -150,7 +150,7 @@ def test_output(self):
subprocess.call(['msgfmt', PO_FILE, '-o', MO_MSGFMT])
subprocess.call(['pocompile', '--errorlevel=traceback', PO_FILE, MO_POCOMPILE])

store = factory.getobject(StringIO(posource))
store = factory.getobject(BytesIO(posource))
if store.isempty() and not os.path.exists(MO_POCOMPILE):
# pocompile doesn't create MO files for empty PO files, so we
# can skip the checks here.
Expand Down

0 comments on commit a5eebce

Please sign in to comment.