Python-File-Tools 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,517 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import shutil
5
+ import tempfile
6
+ import python_file_tools
7
+ import python_file_tools.test as pft_test
8
+ from os.path import abspath, basename, join
9
+
10
+ def test_read_text_file():
11
+ """
12
+ Tests the read_text_file function.
13
+ """
14
+ # Test reading a basic unicode text file
15
+ text_directory = abspath(join(pft_test.BASIC_DIRECTORY, "text"))
16
+ text_file = abspath(join(text_directory, "unicode.txt"))
17
+ assert python_file_tools.read_text_file(text_file) == "This is ünicode."
18
+ # Test reading non-unicode text files
19
+ text_file = abspath(join(text_directory, "latin1.txt"))
20
+ assert python_file_tools.read_text_file(text_file) == "This is lätin1."
21
+ text_file = abspath(join(text_directory, "cp437.TXT"))
22
+ assert python_file_tools.read_text_file(text_file) == "This is cp437."
23
+ # Test reading a non-text file
24
+ assert python_file_tools.read_text_file(pft_test.BASIC_DIRECTORY) is None
25
+
26
+ def test_write_text_file():
27
+ """
28
+ Tests the write_text_file function.
29
+ """
30
+ with tempfile.TemporaryDirectory() as temp_dir:
31
+ # Test writing a basic text file
32
+ text_file = abspath(join(temp_dir, "test.txt"))
33
+ python_file_tools.write_text_file(text_file, "This is text!")
34
+ assert python_file_tools.read_text_file(text_file) == "This is text!"
35
+ # Test overwriting a text file
36
+ python_file_tools.write_text_file(text_file, "New\nText.")
37
+ assert python_file_tools.read_text_file(text_file) == "New\nText."
38
+ # Test writing to an invalid location
39
+ fake_text_file = abspath(join("/non/existant/dir/", "fake.txt"))
40
+ python_file_tools.write_text_file(fake_text_file, "Thing")
41
+ assert os.listdir(abspath(temp_dir)) == ["test.txt"]
42
+
43
+ def test_read_json_file():
44
+ """
45
+ Tests the read_json_file function.
46
+ """
47
+ # Test reading a basic unicode JSON file
48
+ json_directory = abspath(join(pft_test.BASIC_DIRECTORY, "json"))
49
+ json_file = abspath(join(json_directory, "unicode.json"))
50
+ json = python_file_tools.read_json_file(json_file)
51
+ assert json["name"] == "vãlue"
52
+ assert json["number"] == 25
53
+ assert json["boolean"] == False
54
+ assert json["internal"] == {"key":"another"}
55
+ # Test reading a non-unicode JSON file
56
+ json_file = abspath(join(json_directory, "latin1.JSON"))
57
+ json = python_file_tools.read_json_file(json_file)
58
+ assert json["new"] == "Títle"
59
+ # Test reading a non-JSON file
60
+ json_file = abspath(join(pft_test.BASIC_DIRECTORY, "unicode.txt"))
61
+ assert python_file_tools.read_json_file(json_file) == {}
62
+ assert python_file_tools.read_json_file(pft_test.BASIC_DIRECTORY) == {}
63
+
64
+ def test_write_json_file():
65
+ """
66
+ Tests the write_json_file function.
67
+ """
68
+ with tempfile.TemporaryDirectory() as temp_dir:
69
+ # Test writing a JSON file
70
+ dictionary = {"name":"title", "num":42, "boolean":True}
71
+ json_file = abspath(join(temp_dir, "test.json"))
72
+ python_file_tools.write_json_file(json_file, dictionary)
73
+ assert python_file_tools.read_json_file(json_file) == dictionary
74
+ # Test overwriting a JSON file
75
+ python_file_tools.write_json_file(json_file, {"A":"B"})
76
+ assert python_file_tools.read_json_file(json_file) == {"A":"B"}
77
+ # Test writing a JSON file to an invalid location
78
+ fake_json_file = abspath(join("/non/existant/dir/", "fake.json"))
79
+ python_file_tools.write_json_file(fake_json_file, "Thing")
80
+ assert os.listdir(abspath(temp_dir)) == ["test.json"]
81
+
82
+ def test_extract_zip():
83
+ """
84
+ Tests the extract_zip function.
85
+ """
86
+ # Get file paths
87
+ zip_file = abspath(join(pft_test.BASIC_DIRECTORY, "archive.zip"))
88
+ text_directory = abspath(join(pft_test.BASIC_DIRECTORY, "text"))
89
+ non_zip_file = abspath(join(text_directory, "unicode.txt"))
90
+ # Test extracting a zip file
91
+ with tempfile.TemporaryDirectory() as temp_dir:
92
+ assert python_file_tools.extract_zip(zip_file, temp_dir)
93
+ assert sorted(os.listdir(temp_dir)) == ["DELETE.txt", "Internal", "metadata.json"]
94
+ internal_dir = abspath(join(temp_dir, "Internal"))
95
+ assert sorted(os.listdir(internal_dir)) == ["Text1.txt", "Text2.txt"]
96
+ text_file = abspath(join(temp_dir, "DELETE.txt"))
97
+ assert python_file_tools.read_text_file(text_file) == "Delete Me!"
98
+ # Test extracting a zip file with an added container directory
99
+ with tempfile.TemporaryDirectory() as temp_dir:
100
+ assert python_file_tools.extract_zip(zip_file, temp_dir, create_folder=True)
101
+ assert os.listdir(temp_dir) == ["archive"]
102
+ archive_dir = abspath(join(temp_dir, "archive"))
103
+ assert sorted(os.listdir(archive_dir)) == ["DELETE.txt", "Internal", "metadata.json"]
104
+ internal_dir = abspath(join(archive_dir, "Internal"))
105
+ assert sorted(os.listdir(internal_dir)) == ["Text1.txt", "Text2.txt"]
106
+ text_file = abspath(join(internal_dir, "Text1.txt"))
107
+ assert python_file_tools.read_text_file(text_file) == "This is text!"
108
+ # Test is the container directory already exists
109
+ with tempfile.TemporaryDirectory() as temp_dir:
110
+ duplicate_dir = abspath(join(temp_dir, "archive"))
111
+ os.mkdir(duplicate_dir)
112
+ assert python_file_tools.extract_zip(zip_file, temp_dir, create_folder=True)
113
+ assert sorted(os.listdir(temp_dir)) == ["archive", "archive-2"]
114
+ archive_dir = abspath(join(temp_dir, "archive-2"))
115
+ assert sorted(os.listdir(archive_dir)) == ["DELETE.txt", "Internal", "metadata.json"]
116
+ internal_dir = abspath(join(archive_dir, "Internal"))
117
+ assert sorted(os.listdir(internal_dir)) == ["Text1.txt", "Text2.txt"]
118
+ text_file = abspath(join(internal_dir, "Text2.txt"))
119
+ assert python_file_tools.read_text_file(text_file) == "Another File."
120
+ # Test extracting zip while deleting unwanted files
121
+ with tempfile.TemporaryDirectory() as temp_dir:
122
+ assert python_file_tools.extract_zip(zip_file, temp_dir, delete_files=["DELETE.txt"])
123
+ assert sorted(os.listdir(temp_dir)) == ["Internal", "metadata.json"]
124
+ internal_dir = abspath(join(temp_dir, "Internal"))
125
+ assert sorted(os.listdir(internal_dir)) == ["Text1.txt", "Text2.txt"]
126
+ text_file = abspath(join(temp_dir, "metadata.json"))
127
+ assert python_file_tools.read_json_file(text_file) == {"title":"Zip Test"}
128
+ # Test extracting zip while removing internal directory
129
+ with tempfile.TemporaryDirectory() as temp_dir:
130
+ delete = ["DELETE.txt", "metadata.json"]
131
+ assert python_file_tools.extract_zip(zip_file, temp_dir, create_folder=True, remove_internal=True, delete_files=delete)
132
+ assert os.listdir(temp_dir) == ["archive"]
133
+ archive_dir = abspath(join(temp_dir, "archive"))
134
+ assert sorted(os.listdir(archive_dir)) == ["Text1.txt", "Text2.txt"]
135
+ # Test that directory is not removed with external files
136
+ with tempfile.TemporaryDirectory() as temp_dir:
137
+ assert python_file_tools.extract_zip(zip_file, temp_dir, remove_internal=True)
138
+ assert sorted(os.listdir(temp_dir)) == ["DELETE.txt", "Internal", "metadata.json"]
139
+ internal_dir = abspath(join(temp_dir, "Internal"))
140
+ assert sorted(os.listdir(internal_dir)) == ["Text1.txt", "Text2.txt"]
141
+ # Test if the extracted files already exist
142
+ with tempfile.TemporaryDirectory() as temp_dir:
143
+ python_file_tools.write_text_file(abspath(join(temp_dir, "Text1.txt")), "A")
144
+ python_file_tools.write_text_file(abspath(join(temp_dir, "Text2.txt")), "B")
145
+ delete = ["DELETE.txt", "metadata.json"]
146
+ assert python_file_tools.extract_zip(zip_file, temp_dir, remove_internal=True, delete_files=delete)
147
+ assert sorted(os.listdir(temp_dir)) == ["Text1.txt", "Text2.txt", "archive"]
148
+ text_file = abspath(join(temp_dir, "Text1.txt"))
149
+ assert python_file_tools.read_text_file(text_file) == "A"
150
+ sub_dir = abspath(join(temp_dir, "archive"))
151
+ text_file = abspath(join(sub_dir, "Text1.txt"))
152
+ assert python_file_tools.read_text_file(text_file) == "This is text!"
153
+ text_file = abspath(join(sub_dir, "Text2.txt"))
154
+ assert python_file_tools.read_text_file(text_file) == "Another File."
155
+ # Test that paired files remain connected when there's a file name conflict
156
+ base_dir = pft_test.ZIP_CONFLICT_DIRECTORY
157
+ with tempfile.TemporaryDirectory() as temp_dir:
158
+ extract_dir = abspath(join(temp_dir, "extract"))
159
+ shutil.copytree(base_dir, extract_dir)
160
+ zip_file = abspath(join(extract_dir, "blue.zip"))
161
+ assert python_file_tools.extract_zip(zip_file, extract_dir, remove_internal=True)
162
+ files = sorted(os.listdir(extract_dir))
163
+ assert len(files) == 5
164
+ assert files[0] == "blue"
165
+ assert files[1] == "blue.jpg"
166
+ assert files[2] == "blue.json"
167
+ assert files[3] == "blue.zip"
168
+ assert files[4] == "folder"
169
+ assert sorted(os.listdir(abspath(join(extract_dir, "folder")))) == ["outside.txt"]
170
+ sub_dir = abspath(join(extract_dir, "blue"))
171
+ assert sorted(os.listdir(sub_dir)) == ["blue.json", "blue.png", "folder"]
172
+ assert sorted(os.listdir(abspath(join(sub_dir, "folder")))) == ["internal.txt"]
173
+ # Test if an invalid zip file is given
174
+ with tempfile.TemporaryDirectory() as temp_dir:
175
+ assert not python_file_tools.extract_zip(non_zip_file, temp_dir)
176
+ assert not python_file_tools.extract_zip("/non/existant/", temp_dir)
177
+ assert os.listdir(temp_dir) == []
178
+
179
+ def test_extract_file_from_zip():
180
+ """
181
+ Tests the extract_file_from_zip function.
182
+ """
183
+ # Get file paths
184
+ zip_file = abspath(join(pft_test.BASIC_DIRECTORY, "archive.zip"))
185
+ text_directory = abspath(join(pft_test.BASIC_DIRECTORY, "text"))
186
+ non_zip_file = abspath(join(text_directory, "unicode.txt"))
187
+ # Test extracting a file from a zip file
188
+ with tempfile.TemporaryDirectory() as temp_dir:
189
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "metadata.json")
190
+ assert os.listdir(temp_dir) == ["metadata.json"]
191
+ assert abspath(join(extracted, os.pardir)) == abspath(temp_dir)
192
+ assert basename(extracted) == "metadata.json"
193
+ assert python_file_tools.read_json_file(extracted) == {"title":"Zip Test"}
194
+ # Test extracting file from a subdirectory
195
+ with tempfile.TemporaryDirectory() as temp_dir:
196
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "Text1.txt", True)
197
+ assert os.listdir(temp_dir) == ["Text1.txt"]
198
+ assert abspath(join(extracted, os.pardir)) == abspath(temp_dir)
199
+ assert basename(extracted) == "Text1.txt"
200
+ assert python_file_tools.read_text_file(extracted) == "This is text!"
201
+ # Test if requested file is not present in the zip file
202
+ with tempfile.TemporaryDirectory() as temp_dir:
203
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "Nothing.txt", True)
204
+ assert extracted is None
205
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "Text1.txt")
206
+ assert extracted is None
207
+ assert os.listdir(temp_dir) == []
208
+ # Test that directories cannot be extracted
209
+ with tempfile.TemporaryDirectory() as temp_dir:
210
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "Internal")
211
+ assert extracted is None
212
+ assert os.listdir(temp_dir) == []
213
+ # Test if the extracted file already exists
214
+ with tempfile.TemporaryDirectory() as temp_dir:
215
+ python_file_tools.write_text_file(abspath(join(temp_dir, "DELETE.txt")), "A")
216
+ extracted = python_file_tools.extract_file_from_zip(zip_file, temp_dir, "DELETE.txt")
217
+ assert sorted(os.listdir(temp_dir)) == ["DELETE-2.txt", "DELETE.txt"]
218
+ assert abspath(join(extracted, os.pardir)) == abspath(temp_dir)
219
+ assert basename(extracted) == "DELETE-2.txt"
220
+ assert python_file_tools.read_text_file(extracted) == "Delete Me!"
221
+ # Test extracting from an invalid zip file
222
+ with tempfile.TemporaryDirectory() as temp_dir:
223
+ extracted = python_file_tools.extract_file_from_zip(non_zip_file, temp_dir, "DELETE.txt")
224
+ assert extracted is None
225
+ extracted = python_file_tools.extract_file_from_zip("/non/existant/file", temp_dir, "DELETE.txt")
226
+ assert extracted is None
227
+ assert os.listdir(temp_dir) == []
228
+
229
+ def test_create_zip():
230
+ """
231
+ Tests the create_zip function.
232
+ """
233
+ # Get file paths
234
+ json_directory = abspath(join(pft_test.BASIC_DIRECTORY, "json"))
235
+ non_zip_file = abspath(join(json_directory, "unicode.json"))
236
+ # Test creating a zip file
237
+ with tempfile.TemporaryDirectory() as temp_dir:
238
+ created_zip = abspath(join(temp_dir, "created.zip"))
239
+ assert python_file_tools.create_zip(json_directory, created_zip)
240
+ assert python_file_tools.extract_zip(created_zip, temp_dir)
241
+ assert sorted(os.listdir(temp_dir)) == ["created.zip", "latin1.JSON", "unicode.json"]
242
+ json_file = abspath(join(temp_dir, "latin1.JSON"))
243
+ assert python_file_tools.read_json_file(json_file) == {"new": "Títle"}
244
+ # Test creating a zip file with internal directories
245
+ with tempfile.TemporaryDirectory() as temp_dir:
246
+ created_zip = abspath(join(temp_dir, "created.zip"))
247
+ assert python_file_tools.create_zip(pft_test.BASIC_DIRECTORY, created_zip)
248
+ assert python_file_tools.extract_zip(created_zip, temp_dir)
249
+ assert sorted(os.listdir(temp_dir)) == ["archive.zip", "created.zip", "html", "json", "text"]
250
+ internal_dir = abspath(join(temp_dir, "json"))
251
+ assert sorted(os.listdir(internal_dir)) == ["latin1.JSON", "unicode.json"]
252
+ internal_dir = abspath(join(temp_dir, "html"))
253
+ assert sorted(os.listdir(internal_dir)) == ["basic.html", "unformatted.html"]
254
+ internal_dir = abspath(join(temp_dir, "text"))
255
+ assert sorted(os.listdir(internal_dir)) == ["cp437.TXT", "latin1.txt", "unicode.txt"]
256
+ text_file = abspath(join(internal_dir, "cp437.TXT"))
257
+ assert python_file_tools.read_text_file(text_file) == "This is cp437."
258
+ # Test adding a mimetype file
259
+ with tempfile.TemporaryDirectory() as temp_dir:
260
+ created_zip = abspath(join(temp_dir, "created.zip"))
261
+ assert python_file_tools.create_zip(json_directory, created_zip, mimetype="Thing")
262
+ assert python_file_tools.extract_zip(created_zip, temp_dir)
263
+ assert sorted(os.listdir(temp_dir)) == ["created.zip", "latin1.JSON", "mimetype", "unicode.json"]
264
+ text_file = abspath(join(temp_dir, "mimetype"))
265
+ assert python_file_tools.read_text_file(text_file) == "Thing"
266
+
267
+ def test_get_all_files():
268
+ """
269
+ Tests the get_all_files function.
270
+ """
271
+ # Get file paths
272
+ basic_directory = pft_test.BASIC_DIRECTORY
273
+ text_directory = abspath(join(basic_directory, "text"))
274
+ html_directory = abspath(join(basic_directory, "html"))
275
+ json_directory = abspath(join(basic_directory, "json"))
276
+ # Test finding files in a folder without subdirectories
277
+ files = python_file_tools.find_all_files(text_directory)
278
+ assert len(files) == 3
279
+ assert basename(files[0]) == "cp437.TXT"
280
+ assert basename(files[1]) == "latin1.txt"
281
+ assert basename(files[2]) == "unicode.txt"
282
+ assert python_file_tools.find_files_of_type(text_directory, ".png") == []
283
+ assert python_file_tools.find_files_of_type(text_directory, ".json") == []
284
+ assert python_file_tools.find_files_of_type(basic_directory, ".txt", False) == []
285
+ # Test finding files while including subdirectories
286
+ files = python_file_tools.find_all_files(basic_directory)
287
+ assert len(files) == 8
288
+ assert basename(files[0]) == "archive.zip"
289
+ assert abspath(join(files[0], os.pardir)) == basic_directory
290
+ assert basename(files[1]) == "basic.html"
291
+ assert abspath(join(files[1], os.pardir)) == html_directory
292
+ assert basename(files[2]) == "unformatted.html"
293
+ assert abspath(join(files[2], os.pardir)) == html_directory
294
+ assert basename(files[3]) == "latin1.JSON"
295
+ assert abspath(join(files[3], os.pardir)) == json_directory
296
+ assert basename(files[4]) == "unicode.json"
297
+ assert abspath(join(files[4], os.pardir)) == json_directory
298
+ assert basename(files[5]) == "cp437.TXT"
299
+ assert abspath(join(files[5], os.pardir)) == text_directory
300
+ assert basename(files[6]) == "latin1.txt"
301
+ assert abspath(join(files[6], os.pardir)) == text_directory
302
+ assert basename(files[7]) == "unicode.txt"
303
+ assert abspath(join(files[7], os.pardir)) == text_directory
304
+ # Test finding files while ignoring subdirectories
305
+ files = python_file_tools.find_all_files(basic_directory, False)
306
+ assert len(files) == 1
307
+ assert basename(files[0]) == "archive.zip"
308
+ assert abspath(join(files[0], os.pardir)) == basic_directory
309
+
310
+ def test_find_files_of_type():
311
+ """
312
+ Tests the find_files_of_type function.
313
+ """
314
+ # Get file paths
315
+ basic_directory = pft_test.BASIC_DIRECTORY
316
+ text_directory = abspath(join(basic_directory, "text"))
317
+ html_directory = abspath(join(basic_directory, "html"))
318
+ json_directory = abspath(join(basic_directory, "json"))
319
+ # Test finding all files of a given extension
320
+ files = python_file_tools.find_files_of_type(text_directory, ".txt")
321
+ assert len(files) == 3
322
+ assert basename(files[0]) == "cp437.TXT"
323
+ assert basename(files[1]) == "latin1.txt"
324
+ assert basename(files[2]) == "unicode.txt"
325
+ assert python_file_tools.find_files_of_type(text_directory, ".png") == []
326
+ assert python_file_tools.find_files_of_type(text_directory, ".json") == []
327
+ assert python_file_tools.find_files_of_type(basic_directory, ".txt", False) == []
328
+ # Test finding files while including subdirectories
329
+ files = python_file_tools.find_files_of_type(basic_directory, ".txt")
330
+ assert len(files) == 3
331
+ files = python_file_tools.find_files_of_type(basic_directory, [".json", ".zip"])
332
+ assert len(files) == 3
333
+ assert basename(files[0]) == "archive.zip"
334
+ assert abspath(join(files[0], os.pardir)) == basic_directory
335
+ assert basename(files[1]) == "latin1.JSON"
336
+ assert abspath(join(files[1], os.pardir)) == json_directory
337
+ assert basename(files[2]) == "unicode.json"
338
+ assert abspath(join(files[2], os.pardir)) == json_directory
339
+ # Test finding files with inverted extension
340
+ files = python_file_tools.find_files_of_type(basic_directory, [".txt"], inverted=True)
341
+ assert len(files) == 5
342
+ assert basename(files[0]) == "archive.zip"
343
+ assert abspath(join(files[0], os.pardir)) == basic_directory
344
+ assert basename(files[1]) == "basic.html"
345
+ assert abspath(join(files[1], os.pardir)) == html_directory
346
+ assert basename(files[2]) == "unformatted.html"
347
+ assert abspath(join(files[2], os.pardir)) == html_directory
348
+ assert basename(files[3]) == "latin1.JSON"
349
+ assert abspath(join(files[3], os.pardir)) == json_directory
350
+ assert basename(files[4]) == "unicode.json"
351
+ assert abspath(join(files[4], os.pardir)) == json_directory
352
+ files = python_file_tools.find_files_of_type(basic_directory, [".txt", ".json", ".htm", ".html"], inverted=True)
353
+ assert len(files) == 1
354
+ assert basename(files[0]) == "archive.zip"
355
+ assert abspath(join(files[0], os.pardir)) == basic_directory
356
+
357
+ def test_directory_contains():
358
+ """
359
+ Tests the directory_contains function.
360
+ """
361
+ # Get file paths
362
+ basic_directory = pft_test.BASIC_DIRECTORY
363
+ text_directory = abspath(join(basic_directory, "text"))
364
+ json_directory = abspath(join(basic_directory, "json"))
365
+ # Test if a directory contains files without checking subdirectories
366
+ assert python_file_tools.directory_contains(basic_directory, ".zip", False)
367
+ assert python_file_tools.directory_contains(text_directory, ".txt")
368
+ assert python_file_tools.directory_contains(text_directory, [".txt", ".json"])
369
+ assert not python_file_tools.directory_contains(text_directory, ".t")
370
+ assert not python_file_tools.directory_contains(text_directory, ".json")
371
+ assert not python_file_tools.directory_contains(text_directory, [".json", ".png"])
372
+ assert not python_file_tools.directory_contains(basic_directory, ".txt", False)
373
+ # Test if directory contains files while checking subdirectories
374
+ assert python_file_tools.directory_contains(basic_directory, ".zip")
375
+ assert python_file_tools.directory_contains(basic_directory, ".json")
376
+ assert python_file_tools.directory_contains(basic_directory, [".txt", ".png"])
377
+ assert not python_file_tools.directory_contains(basic_directory, ".png")
378
+ assert not python_file_tools.directory_contains(basic_directory, [".jpeg", ".png", ".pdf"])
379
+
380
+ def test_get_file_friendly_text():
381
+ """
382
+ Tests the get_file_friendly_text function.
383
+ """
384
+ # Test replacing invalid characters
385
+ assert python_file_tools.get_file_friendly_text(r"A < B > C") == "A - B - C"
386
+ assert python_file_tools.get_file_friendly_text(r'1 " 2 " 3') == "1 - 2 - 3"
387
+ assert python_file_tools.get_file_friendly_text(r"A\B/C | 123") == "A-B-C - 123"
388
+ assert python_file_tools.get_file_friendly_text(r"a*b?c") == "a-b-c"
389
+ assert python_file_tools.get_file_friendly_text(r"abcd..") == "abcd"
390
+ assert python_file_tools.get_file_friendly_text(r"abcd . .") == "abcd"
391
+ assert python_file_tools.get_file_friendly_text("ABCDE") == "ABCDE"
392
+ # Test removing reserved file names
393
+ assert python_file_tools.get_file_friendly_text(r"CON") == "0"
394
+ assert python_file_tools.get_file_friendly_text(r"prn") == "0"
395
+ assert python_file_tools.get_file_friendly_text(r"AUX") == "0"
396
+ assert python_file_tools.get_file_friendly_text(r"nul") == "0"
397
+ assert python_file_tools.get_file_friendly_text(r"com1") == "0"
398
+ assert python_file_tools.get_file_friendly_text(r"COM2") == "0"
399
+ assert python_file_tools.get_file_friendly_text(r"com5") == "0"
400
+ assert python_file_tools.get_file_friendly_text(r"lpt1") == "0"
401
+ assert python_file_tools.get_file_friendly_text(r"LPT5") == "0"
402
+ assert python_file_tools.get_file_friendly_text(r"CONTENT") == "CONTENT"
403
+ assert python_file_tools.get_file_friendly_text(r"aprn") == "aprn"
404
+ assert python_file_tools.get_file_friendly_text(r"com6") == "com6"
405
+ assert python_file_tools.get_file_friendly_text(r"LPT6") == "LPT6"
406
+ assert python_file_tools.get_file_friendly_text(r"LPT0") == "LPT0"
407
+ # Test replacing different types of whitespace and hyphens
408
+ assert python_file_tools.get_file_friendly_text(r"A-B⎼C") == "A-B-C"
409
+ assert python_file_tools.get_file_friendly_text("1\n2\t3") == "1 2 3"
410
+ # Test replacing multiple hyphens or whitespace
411
+ assert python_file_tools.get_file_friendly_text(r"A-----B") == "A-B"
412
+ assert python_file_tools.get_file_friendly_text(r"1 2") == "1 2"
413
+ assert python_file_tools.get_file_friendly_text(r"a - -? - b") == "a - b"
414
+ assert python_file_tools.get_file_friendly_text(r"A- -*- -Z") == "A-Z"
415
+ # Test replacing special structures
416
+ assert python_file_tools.get_file_friendly_text(r"A:B") == "A - B"
417
+ assert python_file_tools.get_file_friendly_text(r"abc...") == "abc…"
418
+ assert python_file_tools.get_file_friendly_text(r"123. . . . ") == "123…"
419
+ assert python_file_tools.get_file_friendly_text(r". . . A . . . . . .") == "… A … …"
420
+ assert python_file_tools.get_file_friendly_text(r"A -> B") == "A to B"
421
+ assert python_file_tools.get_file_friendly_text(r"B -----> C") == "B to C"
422
+ assert python_file_tools.get_file_friendly_text(r"1->3") == "1-3"
423
+ # Test removing hanging hyphens
424
+ assert python_file_tools.get_file_friendly_text(r"A- B") == "A B"
425
+ assert python_file_tools.get_file_friendly_text(r"C -D") == "C D"
426
+ assert python_file_tools.get_file_friendly_text(r"a? z") == "a z"
427
+ assert python_file_tools.get_file_friendly_text(r"A *Z") == "A Z"
428
+ # Test removing whitespace and hyphens from ends of string
429
+ assert python_file_tools.get_file_friendly_text(r" ABC ") == "ABC"
430
+ assert python_file_tools.get_file_friendly_text(r"- - 123 - -") == "123"
431
+ assert python_file_tools.get_file_friendly_text(r" ?? az * * ") == "az"
432
+ # Test replacing diacritic characters in ASCII only mode
433
+ assert python_file_tools.get_file_friendly_text("Áéíóú") == "Áéíóú"
434
+ assert python_file_tools.get_file_friendly_text("ÀÁÂÃÄÅ", True) == "AAAAAA"
435
+ assert python_file_tools.get_file_friendly_text("ÈÉÊË", True) == "EEEE"
436
+ assert python_file_tools.get_file_friendly_text("ÌÍÎÏ", True) == "IIII"
437
+ assert python_file_tools.get_file_friendly_text("ÑŃÒÓÔÕÖ", True) == "NNOOOOO"
438
+ assert python_file_tools.get_file_friendly_text("ÙÚÛÜÝŸ", True) == "UUUUYY"
439
+ assert python_file_tools.get_file_friendly_text("àáâãäå", True) == "aaaaaa"
440
+ assert python_file_tools.get_file_friendly_text("èéêë", True) == "eeee"
441
+ assert python_file_tools.get_file_friendly_text("ìíîï", True) == "iiii"
442
+ assert python_file_tools.get_file_friendly_text("ńñòóôõö", True) == "nnooooo"
443
+ assert python_file_tools.get_file_friendly_text("ùúûüýÿ", True) == "uuuuyy"
444
+ # Test non-ASCII characters are removed in ASCII only mode
445
+ assert python_file_tools.get_file_friendly_text("$.AAA☺") == "$.AAA☺"
446
+ assert python_file_tools.get_file_friendly_text("$.☺abz", True) == "abz"
447
+ assert python_file_tools.get_file_friendly_text("[A] @;`^{} (Z)", True) == "[A] - (Z)"
448
+ assert python_file_tools.get_file_friendly_text("0 % % % 9!", True) == "0 - 9!"
449
+ # Test if the final filename has no length
450
+ assert python_file_tools.get_file_friendly_text("@#$%^&*-=", True) == "0"
451
+ assert python_file_tools.get_file_friendly_text("---") == "0"
452
+ assert python_file_tools.get_file_friendly_text(" ") == "0"
453
+ assert python_file_tools.get_file_friendly_text("") == "0"
454
+ assert python_file_tools.get_file_friendly_text(None) == "0"
455
+
456
+ def test_get_available_filename():
457
+ """
458
+ Tests the get_available_filename function.
459
+ """
460
+ # Test getting a filename with invalid characters
461
+ directory = pft_test.MULTI_TYPE_DIRECTORY
462
+ assert python_file_tools.get_available_filename("a.txt", "Name?", directory) == "Name"
463
+ assert python_file_tools.get_available_filename(["a.txt"], ".Náme.", directory) == ".Náme"
464
+ assert python_file_tools.get_available_filename("a.txt", ".Náme.", directory, True) == "Name"
465
+ # Test getting the filename if the desired filename already exists
466
+ assert python_file_tools.get_available_filename("a.txt", "fíle", directory, True) == "file-2"
467
+ assert python_file_tools.get_available_filename(["a.TXT", "a.html"], "file", directory) == "file-3"
468
+ # Test if filename exists with filename but different capitalization
469
+ assert python_file_tools.get_available_filename("a.txt", "OTHER", directory) == "OTHER-2"
470
+ # Test if the filename exists, but with a different extension
471
+ assert python_file_tools.get_available_filename("thing.png", "other", directory) == "other"
472
+ assert python_file_tools.get_available_filename(["a.png", "b.jpg"], "file", directory) == "file"
473
+ # Test with invalid directory
474
+ assert python_file_tools.get_available_filename(".txt", "abc", "/non/existant/dir/") is None
475
+
476
+ def test_rename_file():
477
+ """
478
+ Tests the rename_file function.
479
+ """
480
+ with tempfile.TemporaryDirectory() as temp_dir:
481
+ # Create test file
482
+ file = abspath(join(temp_dir, "file.txt"))
483
+ python_file_tools.write_text_file(file, "test text")
484
+ # Test renaming file
485
+ file = python_file_tools.rename_file(file, "Náme?")
486
+ assert abspath(join(file, os.pardir)) == temp_dir
487
+ assert basename(file) == "Náme.txt"
488
+ # Test renaming file with only ASCII characters allowed
489
+ file = python_file_tools.rename_file(file, ".Náme", True)
490
+ assert abspath(join(file, os.pardir)) == temp_dir
491
+ assert basename(file) == "Name.txt"
492
+ # Test renaming file to its current name
493
+ file = python_file_tools.rename_file(file, "Name??????????????")
494
+ assert basename(file) == "Name.txt"
495
+ # Test renaming file to name of existing file
496
+ file = abspath(join(temp_dir, "new.txt"))
497
+ python_file_tools.write_text_file(file, "new text")
498
+ file = python_file_tools.rename_file(file, "Name")
499
+ assert basename(file) == "Name-2.txt"
500
+ assert sorted(os.listdir(temp_dir)) == ["Name-2.txt", "Name.txt"]
501
+ # Test renaming same filename but different extension
502
+ file = abspath(join(temp_dir, "Image.png"))
503
+ python_file_tools.write_text_file(file, "image text")
504
+ file = python_file_tools.rename_file(file, ":Name:")
505
+ assert basename(file) == "Name.png"
506
+ # Test that renamed files still contain the correct data
507
+ assert sorted(os.listdir(temp_dir)) == ["Name-2.txt", "Name.png", "Name.txt"]
508
+ file = abspath(join(temp_dir, "Name.txt"))
509
+ assert python_file_tools.read_text_file(file) == "test text"
510
+ file = abspath(join(temp_dir, "Name-2.txt"))
511
+ assert python_file_tools.read_text_file(file) == "new text"
512
+ file = abspath(join(temp_dir, "Name.png"))
513
+ assert python_file_tools.read_text_file(file) == "image text"
514
+ # Test renaming invalid file
515
+ file = abspath(join(temp_dir, "non-existant"))
516
+ assert python_file_tools.rename_file(file, "new") is None
517
+ assert python_file_tools.rename_file("/non/existant/file", "new") is None