-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.py
More file actions
192 lines (144 loc) · 5.14 KB
/
utils.py
File metadata and controls
192 lines (144 loc) · 5.14 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import logging
import os
import time
import urllib
import uuid
from distutils.version import LooseVersion as versioner
from os import makedirs
from os.path import join, split, exists, splitext
from PIL import Image
from django.conf import settings
from django.db import models
from requests.compat import quote
logger = logging.getLogger(__name__)
#this is gross, but requests doesn't import quote_plus into compat,
#so we re-implement it here
def quote_plus(s, safe=''):
"""Quote the query fragment of a URL; replacing ' ' with '+'"""
if ' ' in s:
s = quote(s, safe + ' ')
return s.replace(' ', '+')
return quote(s, safe)
def uniquer(seq, idfun=None):
if idfun is None:
def idfun(x):
return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if marker in seen:
continue
seen[marker] = 1
result.append(item)
return result
def get_version(package):
versions = package.version_set.exclude(upload_time=None)
try:
return versions.latest()
except models.ObjectDoesNotExist:
return None
def get_pypi_version(package):
versions = []
for v_str in package.version_set.values_list('number', flat=True):
v = versioner(v_str)
comparable = True
for elem in v.version:
if isinstance(elem, str):
comparable = False
if comparable:
versions.append(v)
if versions:
return str(sorted(versions)[-1])
return ''
def normalize_license(license):
""" Handles when:
* No license is passed
* Made up licenses are submitted
* Official PyPI trove classifier licenses
* Common abbreviations of licenses
"""
if license is None:
return "UNKNOWN"
if license.strip() in settings.LICENSES:
return license.strip()
if len(license.strip()) > 20:
return "Custom"
return license.strip()
def prepare_thumbnails(image_path):
image = Image.open(image_path)
image_dir, image_filename = split(image_path)
thumbs_dir = join(image_dir, 'thumbs')
if not exists(thumbs_dir):
makedirs(thumbs_dir)
croped_thumbnail = crop_image(image, settings.PROJECT_IMAGE_THUMBNAIL_RATIO)
thumbnail_path = join(thumbs_dir, image_filename)
croped_thumbnail.save(thumbnail_path, quality=settings.PROJECT_IMAGE_THUMBNAIL_QUALITY)
logger.info("Thumbnail {}".format(thumbnail_path))
shrinked_thumbnails = [
croped_thumbnail.resize(thumb_size, Image.ANTIALIAS)
for thumb_size in settings.PROJECT_IMAGE_THUMBNAIL_SIZES
if thumb_size[0] < croped_thumbnail.size[0]
]
for thumb in shrinked_thumbnails:
name, ext = splitext(image_filename)
thumb_path = join(
thumbs_dir,
"{name}_{size}{ext}".format(
name=name,
size="x".join(map(str, thumb.size)),
ext=ext
)
)
logger.info("Thumbnail {}".format(thumb_path))
thumb.save(thumb_path, quality=settings.PROJECT_IMAGE_THUMBNAIL_QUALITY)
def crop_image(image, ratio):
ratio_x, ratio_y = ratio
x, y = image.size
x_unit = x / float(ratio_x)
y_unit = y / float(ratio_y)
if x_unit > y_unit: # to wide
new_x = ratio_x * y_unit # so new_x use more narrow y_unit
new_y = ratio_y * y_unit
else: # to high
new_x = ratio_x * x_unit
new_y = ratio_y * x_unit # so new_y use shorter x_unit
padding_x = (x - new_x) / 2
padding_y = (y - new_y) / 2
box = (padding_x, padding_y, x - padding_x, y - padding_y)
return image.crop(box)
def download_file(file_url, dest_path, file_name=None):
if not file_name:
file_name = generate_unique_file_name()
file_path = join_path_with_file_name(dest_path, file_name)
makedirs(dest_path, exist_ok=True)
urllib.request.urlretrieve(file_url, file_path)
return file_name
def get_file_maintype_from_url(file_url):
return urllib.request.urlopen(file_url).info().get_content_maintype()
def get_file_subtype_from_url(file_url):
return urllib.request.urlopen(file_url).info().get_content_subtype()
def get_image_name(file_type):
return f"{int(round(time.time()*1000))}.{file_type}"
def generate_unique_file_name():
return str(uuid.uuid4())
def rename_file(file_dir, old_name, new_name):
file_path_old = join_path_with_file_name(file_dir, old_name)
file_path_new = join_path_with_file_name(file_dir, new_name)
os.rename(file_path_old, file_path_new)
return file_path_new
def join_path_with_file_name(path, file_name):
if not path.endswith('/'):
path += '/'
return f"{path}{file_name}"
def split_path_for_path_and_name(file_path):
return file_path.rsplit("/", 1)
def cut_domain_name_from_url(file_url):
return file_url.split("/", 3)[-1]
def delete_file_from_media(image_path):
folder_path, image_name = split_path_for_path_and_name(image_path)
image_name = image_name.split(".")[0]
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.find(image_name) != -1:
os.remove(join(root, file))