-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
67 lines (53 loc) · 2 KB
/
helpers.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
import hashlib
import io
import re
import unicodedata
from configuration import *
from writer.writers import find_writer_for_ext
def file_get_extension(file, strip_dot=False):
"""
Returns extension (w/ or w/o dot, specified by strip_dot)
and the filename without the extension as tuple
"""
filename, ext = os.path.splitext(file)
return (ext, filename) if not strip_dot else (ext.replace('.', ''), filename)
def file_get_hash_md5(file, block_size=2**16):
md5 = hashlib.md5()
with io.open(file, 'r', encoding='utf-8') as file:
while True:
d_chunk = file.read(block_size)
if not d_chunk:
break
md5.update(d_chunk)
return md5.digest()
def contents_get_hash_md5(contents):
md5 = hashlib.md5()
md5.update(contents)
return md5.hexdigest()
def safe_create_dir(file):
dir_name = os.path.dirname(file)
if not os.path.exists(dir_name):
os.makedirs(dir_name, exist_ok=True)
# Taken from https://github.com/django/django/blob/master/django/utils/text.py
def slugify(value, allow_unicode=False):
"""
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase. Also strip leading and trailing whitespace.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
def write_disk_meta(meta_dict):
"""
Write meta_converted to actual file
"""
outfile_path = os.path.join(input_dir, meta_dict['slug'] + '.{type}'.format(type=default_meta_type))
writer_meta = find_writer_for_ext(default_meta_type)()
meta_converted = writer_meta.write(meta_dict)
with io.open(outfile_path, 'w') as meta_file:
meta_file.write(meta_converted)