Esempio n. 1
0
def fix_kw(kw):
    """We make sure format parameters have the correct type."""
    for name in 'delimiter quotechar escapechar'.split():
        c = kw.get(name)
        if c and PY2 and isinstance(c, text_type):
            kw[name] = to_binary(c)
    return kw
Esempio n. 2
0
 def _delatex(s):
     try:
         t = to_binary(s, encoding='latin1')
         result = t.decode('latex+latin1')
     except UnicodeEncodeError:
         result = string
     u_result = text_type(result)
     return u_result
Esempio n. 3
0
 def _delatex(s):
     try:
         t = to_binary(s, encoding="latin1")
         result = t.decode("latex+latin1")
     except UnicodeEncodeError:
         result = string
     u_result = text_type(result)
     return u_result
Esempio n. 4
0
 def render_to_response(self, ctx, req):
     res = Response(self.render(ctx, req))
     res.vary = to_binary('Accept')
     res.content_type = str(self.send_mimetype or self.mimetype)
     if self.charset:
         res.content_type += str('; charset=') + str(self.charset)
     if self.content_type_params:
         d = res.content_type_params
         for k, v in self.content_type_params.items():
             d[str(k)] = str(v)
         res.content_type_params = d
     return res
Esempio n. 5
0
 def render_to_response(self, ctx, req):
     res = Response(self.render(ctx, req))
     res.vary = to_binary('Accept')
     res.content_type = str(self.send_mimetype or self.mimetype)
     if self.charset:
         res.content_type += str('; charset=') + str(self.charset)
     if self.content_type_params:
         d = res.content_type_params
         for k, v in self.content_type_params.items():
             d[str(k)] = str(v)
         res.content_type_params = d
     return res
Esempio n. 6
0
 def __unicode__(self):
     pairs = [(to_binary('ctx_ver'), to_binary('Z39.88-2004')),
              (to_binary('rft_val_fmt'),
               to_binary('info:ofi/fmt:kev:mtx:') + _encoded(self.mtx)),
              (to_binary('rfr_id'),
               to_binary('info:sid/') + _encoded(self.sid))]
     for pair in self:
         pairs.append((_encoded(pair[0]), _encoded(pair[1])))
     try:
         return urlencode(pairs)
     except UnicodeDecodeError:  # pragma: no cover
         return to_binary('')
Esempio n. 7
0
    def test_reader(self):
        from clldutils.dsv import reader

        lines = ['first\tline', 'sücond\tläneß']
        encoded_lines = [l.encode('utf8') for l in lines]
        csv_lines = [l.replace('\t', ',') for l in lines]

        def check(r):
            res = list(r)
            assert len(res) == 2
            assert res[1][1] == 'läneß'

        check(reader(lines, delimiter='\t'))
        for lt in ['\n', '\r\n', '\r']:
            if PY3:  # pragma: no cover
                # Simulate file opened in text mode:
                fp = StringIO(lt.join(lines), newline='')
            else:
                # Simulate file opened in binary mode:
                fp = BytesIO(to_binary(lt).join(encoded_lines))
            check(reader(fp, delimiter='\t'))
        check(reader(FIXTURES.joinpath('csv.txt')))

        res = list(reader(FIXTURES.joinpath('tsv.txt'), namedtuples=True, delimiter='\t'))
        assert res[0].a_name == 'b'
        # Missing column values should be set to None:
        assert res[2].a_name is None

        r = list(reader(lines, dicts=True, delimiter='\t'))
        assert len(r) == 1 and r[0]['first'] == 'sücond'
        r = list(reader(lines, namedtuples=True, delimiter='\t'))
        assert len(r) == 1 and r[0].first == 'sücond'
        r = list(reader(csv_lines, namedtuples=True))
        assert len(r) == 1 and r[0].first == 'sücond'
        self.assertEqual(list(reader([], dicts=True, delimiter='\t')), [])
        self.assertEqual(
            list(reader([''], dicts=True, fieldnames=['a', 'b'], delimiter='\t')), [])
        self.assertEqual(list(reader(['a,b', ''], dicts=True, delimiter='\t')), [])

        r = reader(
            ['a,b', '1,2,3,4', '1'], dicts=True, restkey='x', restval='y', delimiter=',')
        self.assertEqual(list(r), [dict(a='1', b='2', x=['3', '4']), dict(a='1', b='y')])
Esempio n. 8
0
 def dump_rendered(self, req, fp, item, index, rendered):
     body = rendered.split(to_binary('rdf:Description'))[1]
     fp.write(
         to_binary('<rdf:Description') + body +
         to_binary('rdf:Description>\n'))
Esempio n. 9
0
 def after(self, req, fp):
     fp.write(to_binary('</rdf:RDF>'))
Esempio n. 10
0
 def before(self, req, fp):
     fp.write(
         to_binary('<rdf:RDF ') + to_binary(rdf_namespace_attrs()) +
         to_binary('>\n'))
Esempio n. 11
0
 def dump_rendered(self, req, fp, item, index, rendered):
     header, body = rendered.split(to_binary('\n\n'), 1)
     if index == 0:
         fp.write(header)
         fp.write(to_binary('\n\n'))
     fp.write(body)
Esempio n. 12
0
 def dump_rendered(self, req, fp, item, index, rendered):
     body = rendered.split(to_binary('rdf:Description'))[1]
     fp.write(to_binary('<rdf:Description') + body + to_binary('rdf:Description>\n'))
Esempio n. 13
0
 def after(self, req, fp):
     fp.write(to_binary('</rdf:RDF>'))
Esempio n. 14
0
 def before(self, req, fp):
     fp.write(
         to_binary('<rdf:RDF ') + to_binary(rdf_namespace_attrs()) + to_binary('>\n'))
Esempio n. 15
0
 def dump_rendered(self, req, fp, item, index, rendered):
     header, body = rendered.split(to_binary('\n\n'), 1)
     if index == 0:
         fp.write(header)
         fp.write(to_binary('\n\n'))
     fp.write(body)