-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmediafile_utils.py
99 lines (85 loc) · 3.15 KB
/
mediafile_utils.py
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
# -*- mode: python ; coding: utf-8 -*-
#
# Copyright © 2012–14 Roland Sieker <[email protected]>
#
# Based on deurl-files.py by Damien Elmes <[email protected]>
#
# License: GNU AGPL, version 3 or later;
# http://www.gnu.org/copyleft/agpl.html
#
"""
Helper function to deal with file names.
"""
import os
import re
import shutil
import unicodedata
from aqt import mw
from anki.utils import isMac, stripHTML
class dl_entry():
def __init__(self,abspath,base_name):
self.base_name = base_name
self.file_extension = os.path.splitext(abspath)[1].strip()
self.file_path = os.path.abspath(abspath)
def free_media_name(base, end):
"""Return a useful media name
Return a pair of a file name that can be used for the media file,
and the whole file path. The name is based on the base name and
end, but doesn’t exist, nor does it clash with another file
different only in upper/lower case.
If no name can be found, a ValueError is raised.
"""
base = stripHTML(base)
# Strip the ‘invalidFilenameChars’ by hand.
base = re.sub(r'[\\/:\*?\'"<>\|]', '', base)
base = unicodedata.normalize('NFC', base)
# Looks like the normalization issue has finally been
# solved. Always use NFC versions of file names now.
mdir = mw.col.media.dir()
if not exists_lc(mdir, base + end):
return os.path.join(mdir, base + end), base + end
for i in range(1, 10000):
# Don't be silly. Give up after 9999 tries (by falling out of
# this loop).
long_name = '{0}_{1}{2}'.format(base, i, end)
if not exists_lc(mdir, long_name):
return os.path.join(mdir, long_name), long_name
# The only way we can have arrived here is by unsuccessfully
# trying the 10000 names.
raise ValueError('Could not find free name.')
def exists_lc(path, name):
"""Test if file name clashes with name of extant file.
On Mac OS X we, simply check if the file exists.
On other systems, we check if the name would clashes with an
existing file’s name. That is, we check for files that have the same
name when both are pulled to lower case and Unicode normalized.
"""
# The point is that like this syncing from Linux to Macs/Windows
# and from Linux/Windows to Macs should work savely. Not much to
# do on Macs, then.
if isMac:
return os.path.exists(os.path.join(path, name))
ln_name = unicodedata.normalize('NFC', name.lower())
for fname in os.listdir(path):
if unicodedata.normalize('NFC', fname.lower()) == ln_name:
return True
# After the loop, none found.
return False
def unmunge_to_mediafile(dl_entry):
"""
Move the data to the media folder.
Determine a free media name and move the data there from the
tempfile.
"""
try:
media_path, media_file_name = free_media_name(
dl_entry.base_name, dl_entry.file_extension)
shutil.copy(dl_entry.file_path, media_path)
except :
media_path =''
media_file_name =''
pass
return media_file_name
def get_file_entry(file,base_name):
f_entry = dl_entry(file,base_name)
return f_entry