File: test_file.py

package info (click to toggle)
python-gphoto2 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,440 kB
  • sloc: ansic: 71,119; python: 4,183; makefile: 4
file content (162 lines) | stat: -rw-r--r-- 7,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# python-gphoto2 - Python interface to libgphoto2
# http://github.com/jim-easterbrook/python-gphoto2
# Copyright (C) 2023  Jim Easterbrook  jim@jim-easterbrook.me.uk
#
# This file is part of python-gphoto2.
#
# python-gphoto2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# python-gphoto2 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-gphoto2.  If not, see
# <https://www.gnu.org/licenses/>.

import os
import tempfile
import unittest

import gphoto2 as gp


class TestFile(unittest.TestCase):
    def setUp(self):
        self.test_file = os.path.join(
            os.path.dirname(__file__), 'vcamera', 'copyright-free-image.jpg')
        with open(self.test_file, 'rb') as f:
            self.src_data = f.read()
        self.assertEqual(len(self.src_data), os.path.getsize(self.test_file))
        self.assertEqual(self.src_data[:10], b'\xff\xd8\xff\xe0\x00\x10JFIF')

    def test_oo_style(self):
        # create CameraFile from data
        cam_file = gp.CameraFile()
        cam_file.set_data_and_size(self.src_data)
        # get mime type from data
        cam_file.detect_mime_type()
        # check detected mime type
        self.assertEqual(cam_file.get_mime_type(), 'image/jpeg')
        # set mime type anyway
        cam_file.set_mime_type('image/jpeg')
        file_time = int(os.path.getmtime(self.test_file))
        cam_file.set_mtime(file_time)
        file_name = 'cam_file.jpg'
        cam_file.set_name(file_name)
        # read data from CameraFile
        self.assertEqual(cam_file.get_data_and_size(), self.src_data)
        self.assertEqual(cam_file.get_mime_type(), 'image/jpeg')
        self.assertEqual(cam_file.get_mtime(), file_time)
        self.assertEqual(cam_file.get_name(), file_name)
        self.assertEqual(
            cam_file.get_name_by_type(file_name, gp.GP_FILE_TYPE_RAW),
            'raw_' + file_name)
        self.assertEqual(cam_file.detect_mime_type(), None)
        # copy file
        file_copy = gp.CameraFile()
        file_copy.copy(cam_file)
        self.assertEqual(file_copy.get_data_and_size(), self.src_data)
        del file_copy
        # save CameraFile to computer
        with tempfile.TemporaryDirectory() as tmp_dir:
            temp_file = os.path.join(tmp_dir, file_name)
            cam_file.save(temp_file)
            self.assertEqual(os.path.getsize(temp_file), len(self.src_data))
            with open(temp_file, 'rb') as f:
                self.assertEqual(f.read(), self.src_data)
            self.assertEqual(int(os.path.getmtime(temp_file)), file_time)
        # wipe file data
        cam_file.clean()
        self.assertEqual(cam_file.get_data_and_size(), b'')
        self.assertEqual(cam_file.get_name(), '')
        del cam_file
        # open file directly
        direct_file = gp.CameraFile()
        direct_file.open(self.test_file)
        self.assertEqual(direct_file.get_data_and_size(), self.src_data)
        self.assertEqual(direct_file.get_mtime(), file_time)
        self.assertEqual(
            direct_file.get_name(), os.path.basename(self.test_file))
        del direct_file
        # create file from file descriptor
        file_copy = gp.CameraFile(os.open(self.test_file, os.O_RDONLY))
        self.assertEqual(file_copy.get_data_and_size(), self.src_data)
        del file_copy

    def test_c_style(self):
        # create CameraFile from data
        OK, cam_file = gp.gp_file_new()
        self.assertEqual(OK, gp.GP_OK)
        self.assertEqual(
            gp.gp_file_set_data_and_size(cam_file, self.src_data), gp.GP_OK)
        # get mime type from data
        self.assertEqual(gp.gp_file_detect_mime_type(cam_file), gp.GP_OK)
        # check detected mime type
        self.assertEqual(gp.gp_file_get_mime_type(cam_file),
                         [gp.GP_OK, 'image/jpeg'])
        # set mime type anyway
        self.assertEqual(
            gp.gp_file_set_mime_type(cam_file, 'image/jpeg'), gp.GP_OK)
        file_time = int(os.path.getmtime(self.test_file))
        self.assertEqual(gp.gp_file_set_mtime(cam_file, file_time), gp.GP_OK)
        file_name = 'cam_file.jpg'
        self.assertEqual(gp.gp_file_set_name(cam_file, file_name), gp.GP_OK)
        # read data from CameraFile
        self.assertEqual(gp.gp_file_get_data_and_size(cam_file),
                         [gp.GP_OK, self.src_data])
        self.assertEqual(gp.gp_file_get_mime_type(cam_file),
                         [gp.GP_OK, 'image/jpeg'])
        self.assertEqual(gp.gp_file_get_mtime(cam_file), [gp.GP_OK, file_time])
        self.assertEqual(gp.gp_file_get_name(cam_file), [gp.GP_OK, file_name])
        self.assertEqual(gp.gp_file_get_name_by_type(
            cam_file, file_name, gp.GP_FILE_TYPE_RAW),
                         [gp.GP_OK, 'raw_' + file_name])
        self.assertEqual(gp.gp_file_detect_mime_type(cam_file), gp.GP_OK)
        # copy file
        OK, file_copy = gp.gp_file_new()
        self.assertEqual(OK, gp.GP_OK)
        self.assertEqual(gp.gp_file_copy(file_copy, cam_file), gp.GP_OK)
        self.assertEqual(gp.gp_file_get_data_and_size(file_copy),
                         [gp.GP_OK, self.src_data])
        del file_copy
        # save CameraFile to computer
        with tempfile.TemporaryDirectory() as tmp_dir:
            temp_file = os.path.join(tmp_dir, file_name)
            self.assertEqual(gp.gp_file_save(cam_file, temp_file), gp.GP_OK)
            self.assertEqual(os.path.getsize(temp_file), len(self.src_data))
            with open(temp_file, 'rb') as f:
                self.assertEqual(f.read(), self.src_data)
            self.assertEqual(int(os.path.getmtime(temp_file)), file_time)
        # wipe file data
        self.assertEqual(gp.gp_file_clean(cam_file), gp.GP_OK)
        self.assertEqual(gp.gp_file_get_data_and_size(cam_file),
                         [gp.GP_OK, b''])
        self.assertEqual(gp.gp_file_get_name(cam_file), [gp.GP_OK, ''])
        del cam_file
        # open file directly
        OK, direct_file = gp.gp_file_new()
        self.assertEqual(OK, gp.GP_OK)
        self.assertEqual(gp.gp_file_open(direct_file, self.test_file), gp.GP_OK)
        self.assertEqual(gp.gp_file_get_data_and_size(direct_file),
                         [gp.GP_OK, self.src_data])
        self.assertEqual(gp.gp_file_get_mtime(direct_file),
                         [gp.GP_OK, file_time])
        self.assertEqual(gp.gp_file_get_name(direct_file),
                         [gp.GP_OK, os.path.basename(self.test_file)])
        del direct_file
        # create file from file descriptor
        OK, file_copy = gp.gp_file_new_from_fd(
            os.open(self.test_file, os.O_RDONLY))
        self.assertEqual(OK, gp.GP_OK)
        self.assertEqual(gp.gp_file_get_data_and_size(file_copy),
                         [gp.GP_OK, self.src_data])
        del file_copy


if __name__ == "__main__":
    unittest.main()