~osomon/pyexiv2/pyexiv2-0.3

281 by Olivier Tilloy
Encode file names in the file system encoding.
1
# -*- coding: utf-8 -*-
2
3
# ******************************************************************************
4
#
5
# Copyright (C) 2010 Olivier Tilloy <olivier@tilloy.net>
6
#
7
# This file is part of the pyexiv2 distribution.
8
#
9
# pyexiv2 is free software; you can redistribute it and/or
10
# modify it under the terms of the GNU General Public License
11
# as published by the Free Software Foundation; either version 2
12
# of the License, or (at your option) any later version.
13
#
14
# pyexiv2 is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with pyexiv2; if not, write to the Free Software
21
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
22
#
23
# Author: Olivier Tilloy <olivier@tilloy.net>
24
#
25
# ******************************************************************************
26
27
import unittest
28
import os
29
import sys
30
import binascii
322.1.1 by Olivier Tilloy
Skip unicode unit tests for unsupported encodings.
31
import locale
281 by Olivier Tilloy
Encode file names in the file system encoding.
32
from tempfile import gettempdir
33
34
from pyexiv2.metadata import ImageMetadata
35
36
37
_HEXDATA = """
38
ff d8 ff e0 00 10 4a 46  49 46 00 01 01 01 00 48
39
00 48 00 00 ff e1 00 36  45 78 69 66 00 00 49 49
40
2a 00 08 00 00 00 01 00  32 01 02 00 14 00 00 00
41
1a 00 00 00 00 00 00 00  32 30 31 30 3a 30 33 3a
42
31 38 20 31 33 3a 33 39  3a 35 38 00 ff db 00 43
43
00 05 03 04 04 04 03 05  04 04 04 05 05 05 06 07
44
0c 08 07 07 07 07 0f 0b  0b 09 0c 11 0f 12 12 11
45
0f 11 11 13 16 1c 17 13  14 1a 15 11 11 18 21 18
46
1a 1d 1d 1f 1f 1f 13 17  22 24 22 1e 24 1c 1e 1f
47
1e ff db 00 43 01 05 05  05 07 06 07 0e 08 08 0e
48
1e 14 11 14 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e
49
1e 1e 1e 1e 1e 1e 1e 1e  1e 1e 1e 1e 1e 1e 1e 1e
50
1e 1e 1e 1e 1e 1e ff c0  00 11 08 00 01 00 01 03
51
01 22 00 02 11 01 03 11  01 ff c4 00 15 00 01 01
52
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 08
53
ff c4 00 14 10 01 00 00  00 00 00 00 00 00 00 00
54
00 00 00 00 00 00 ff c4  00 14 01 01 00 00 00 00
55
00 00 00 00 00 00 00 00  00 00 00 00 ff c4 00 14
56
11 01 00 00 00 00 00 00  00 00 00 00 00 00 00 00
57
00 00 ff da 00 0c 03 01  00 02 11 03 11 00 3f 00
58
b2 c0 07 ff d9
59
"""
60
61
_BINDATA = binascii.unhexlify(''.join(_HEXDATA.split()))
62
63
64
class TestEncodings(unittest.TestCase):
65
66
    def setUp(self):
67
        self._cwd = os.getcwd()
68
        os.chdir(gettempdir())
322.1.1 by Olivier Tilloy
Skip unicode unit tests for unsupported encodings.
69
        try:
70
            locale.setlocale(locale.LC_ALL, '')
71
        except locale.Error:
72
            self.encoding = None
73
        else:
74
            lc, self.encoding = locale.getlocale()
281 by Olivier Tilloy
Encode file names in the file system encoding.
75
76
    def tearDown(self):
77
        os.chdir(self._cwd)
78
79
    def _create_file(self, filename):
80
        try:
81
            os.remove(filename)
82
        except OSError:
83
            pass
84
        fd = open(filename, 'wb')
85
        fd.write(_BINDATA)
86
        fd.close()
87
88
    def _test_filename(self, filename):
89
        self._create_file(filename)
90
        m = ImageMetadata(filename)
91
        m.read()
92
        os.remove(filename)
93
357.1.1 by Olivier Tilloy
Do not try (and fail!) to encode non-unicode filenames.
94
    def test_ascii(self):
281 by Olivier Tilloy
Encode file names in the file system encoding.
95
        self._test_filename('test.jpg')
96
357.1.1 by Olivier Tilloy
Do not try (and fail!) to encode non-unicode filenames.
97
    def test_latin1(self):
98
        self._test_filename('tést.jpg')
99
100
    def test_latin1_escaped(self):
101
        self._test_filename('t\xc3\xa9st.jpg')
102
103
    def test_unicode_ascii(self):
322.1.1 by Olivier Tilloy
Skip unicode unit tests for unsupported encodings.
104
        if self.encoding is not None:
105
            self._test_filename(u'test.jpg')
281 by Olivier Tilloy
Encode file names in the file system encoding.
106
357.1.1 by Olivier Tilloy
Do not try (and fail!) to encode non-unicode filenames.
107
    def test_unicode_latin1(self):
322.1.1 by Olivier Tilloy
Skip unicode unit tests for unsupported encodings.
108
        if self.encoding is not None:
109
            self._test_filename(u'tést.jpg')
281 by Olivier Tilloy
Encode file names in the file system encoding.
110
357.1.1 by Olivier Tilloy
Do not try (and fail!) to encode non-unicode filenames.
111
    def test_unicode_latin1_escaped(self):
322.1.1 by Olivier Tilloy
Skip unicode unit tests for unsupported encodings.
112
        if self.encoding is not None:
113
            self._test_filename(u't\xe9st.jpg')
281 by Olivier Tilloy
Encode file names in the file system encoding.
114