Exemplo n.º 1
0
def test_encoded():
    from clldutils.misc import encoded

    s = '\xe4'
    latin1 = binary_type(s.encode('latin1'))
    utf8 = binary_type(s.encode('utf8'))
    assert encoded(s) == utf8
    assert encoded(s, 'latin1') == latin1
    assert encoded(utf8) == utf8
    assert encoded(latin1) == utf8
    assert encoded(latin1, 'latin1') == latin1
Exemplo n.º 2
0
def test_encoded():
    from clldutils.misc import encoded

    s = '\xe4'
    latin1 = binary_type(s.encode('latin1'))
    utf8 = binary_type(s.encode('utf8'))
    assert encoded(s) == utf8
    assert encoded(s, 'latin1') == latin1
    assert encoded(utf8) == utf8
    assert encoded(latin1) == utf8
    assert encoded(latin1, 'latin1') == latin1
Exemplo n.º 3
0
def convert(string, from_, to_):
    if from_ == to_:
        return encoded(string)
    assert from_ in FORMATS and (to_ is None or to_ in FORMATS)
    g = Graph()
    g.parse(BytesIO(encoded(string)), format=from_)
    if to_ is None:
        return g
    out = BytesIO()
    g.serialize(out, format=to_)
    out.seek(0)
    return out.read()
Exemplo n.º 4
0
def convert(string, from_, to_):
    if from_ == to_:
        return encoded(string)
    assert from_ in FORMATS and (to_ is None or to_ in FORMATS)
    g = Graph()
    g.parse(io.BytesIO(encoded(string)), format=from_)
    if to_ is None:
        return g
    out = io.BytesIO()
    g.serialize(out, format=to_)
    out.seek(0)
    return out.read()
Exemplo n.º 5
0
def convert(string, from_, to_=None):
    assert from_ in FORMATS and (to_ is None or to_ in FORMATS)

    res = pipe('%s2xml -i utf8 -o utf8' % from_, encoded(string))
    if to_:
        res = pipe('xml2%s -i utf8 -o utf8' % to_, res)
    return res.decode('utf8')
Exemplo n.º 6
0
def pipe(cmd, input_):
    proc = subprocess.Popen(cmd,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True)
    return proc.communicate(encoded(input_))[0]
Exemplo n.º 7
0
def convert(string, from_, to_=None):
    assert from_ in FORMATS and (to_ is None or to_ in FORMATS)

    res = pipe('%s2xml -i utf8 -o utf8' % from_, encoded(string))
    if to_:
        res = pipe('xml2%s -i utf8 -o utf8' % to_, res)
    return res.decode('utf8')
Exemplo n.º 8
0
def _encoded(value):
    if not isinstance(value, binary_type) and not isinstance(
            value, string_types):
        value = '%s' % value
    return encoded(value)
Exemplo n.º 9
0
 def writerow(self, row):
     if not PY3:
         row = ['' if s is None else encoded('%s' % s, self.encoding) for s in row]
     self.writer.writerow(row)
Exemplo n.º 10
0
def _encoded(value):
    if not isinstance(value, bytes) and not isinstance(value, str):
        value = '%s' % value
    return encoded(value)
Exemplo n.º 11
0
def pipe(cmd, input_):
    proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
    return proc.communicate(encoded(input_))[0]