2 HCE project, Python bindings, Distributed Tasks Manager application. 3 MediaLimitsHandler Class content main functional for check media limits 6 @file MediaLimitsHandler.py 7 @author Alexander Vybornyh <alexander.hce.cluster@gmail.com> 8 @link: http://hierarchical-cluster-engine.com/ 9 @copyright: Copyright © 2013-2016 IOIX Ukraine 10 @license: http://hierarchical-cluster-engine.com/license/ 28 TEMP_FILE_SUFFIX =
'.jpg' 29 BINARY_IMAGE_SEARCH_STR =
'base64,' 31 MEDIA_LIMITS_TYPE_NAME =
'img' 37 CONTENT_TYPE_NAME =
'Content-Type' 38 MIN_WIDTH_NAME =
'min_width' 39 MAX_WIDTH_NAME =
'max_width' 40 MIN_HEIGHT_NAME =
'min_height' 41 MAX_HEIGHT_NAME =
'max_height' 42 MIN_RATIO =
'min_ratio' 43 MAX_RATIO =
'max_ratio' 44 MIN_COLORS =
'min_colors' 47 'contentType':CONTENT_TYPE_NAME,
48 'minWidth':MIN_WIDTH_NAME,
49 'maxWidth':MAX_WIDTH_NAME,
50 'minHeight':MIN_HEIGHT_NAME,
51 'maxHeight':MAX_HEIGHT_NAME,
54 'minColors':MIN_COLORS}
91 if not isinstance(propertyDict, dict):
92 raise Exception(
"Wrong type of property: %s" % str(
type(property)))
94 for key, value
in propertyDict.items():
95 logger.debug(
"Enter to read property '%s'", str(key))
97 if isinstance(value, dict):
100 logger.debug(
"Wrong type of limits: '%s'. Skipped", str(
type(value)))
102 logger.error(
"Unsupported name of limits: '%s'. Skipped", str(key))
104 logger.debug(
"Leave to read property '%s'", str(key))
106 except Exception, err:
107 logger.error(
"Error: %s", str(err))
120 if propertyString
is None or not isinstance(propertyString, basestring):
121 raise Exception(
'Wrong type %s of property' % str(
type(propertyString)))
123 ret = json.loads(propertyString)
124 except Exception, err:
125 logger.error(
"Initialisation class '%s' was failed. Error: '%s'", self.__class__.__name__, str(err))
138 for key, value
in MediaLimitsHandler.ImageLimits.propertyNamesDict.items():
139 if value
in limitsDict.keys()
and hasattr(imageLimits, key):
140 setattr(imageLimits, key, limitsDict[value])
154 raise Exception(
"Image content wasn't get for save on disk.")
157 tmpf = tempfile.NamedTemporaryFile(suffix=self.
TEMP_FILE_SUFFIX, delete=
False)
158 tmpf.file.write(content)
160 logger.debug(
'Image saved to file: ' + tmpf.name)
163 except Exception, err:
164 logger.debug(
"Error: %s", str(err))
183 ret = base64.b64decode(s)
186 res = requests.get(urlString)
188 except Exception, err:
189 logger.debug(
"Error: %s", str(err))
202 logger.debug(
"Url string: %s", str(urlString))
205 if imageFileName
is not None:
208 imageFile = Image.open(imageFileName)
209 if imageFile
is None:
210 raise Exception(
"Cannot open image file: '%s'", str(imageFileName))
215 imageProperty.format = imageFile.format
217 if len(imageFile.size) > 0:
218 imageProperty.width = imageFile.size[0]
220 if len(imageFile.size) > 1:
221 imageProperty.height = imageFile.size[1]
223 if imageProperty.width > 0
and imageProperty.height > 0:
225 imageProperty.ratio = float(imageProperty.height) / imageProperty.width
227 size = imageProperty.width * imageProperty.height
228 vm = psutil.virtual_memory()
229 logger.debug(
"Image size = %s, available memory = %s", str(size), str(vm.available))
230 if vm.available < size:
231 logger.debug(
"!!! Not enough memory for get colors... Skipped")
233 colors = imageFile.getcolors(size)
234 if colors
is not None:
235 imageProperty.colors = len(colors)
238 except Exception, err:
239 logger.debug(
"Error: %s", str(err))
241 if os.path.isfile(imageFileName):
242 os.remove(imageFileName)
243 logger.debug(
'Remove file: %s', str(imageFileName))
260 if imageProperty.width
is not None and imageLimits.maxWidth
is not None and \
261 int(imageProperty.width) > int(imageLimits.maxWidth):
262 raise Exception(
"Parameter 'width' has not allowed value: %s > %s" % \
263 (str(imageProperty.width), str(imageLimits.maxWidth)))
265 if imageProperty.width
is not None and imageLimits.minWidth
is not None and \
266 int(imageProperty.width) < int(imageLimits.minWidth):
267 raise Exception(
"Parameter 'width' has not allowed value: %s < %s" % \
268 (str(imageProperty.width), str(imageLimits.minWidth)))
270 if imageProperty.height
is not None and imageLimits.maxHeight
is not None and \
271 int(imageProperty.height) > int(imageLimits.maxHeight):
272 raise Exception(
"Parameter 'height' has not allowed value: %s > %s" % \
273 (str(imageProperty.height), str(imageLimits.maxHeight)))
275 if imageProperty.height
is not None and imageLimits.minHeight
is not None and \
276 int(imageProperty.height) < int(imageLimits.minHeight):
277 raise Exception(
"Parameter 'height' has not allowed value: %s < %s" % \
278 (str(imageProperty.height), str(imageLimits.minHeight)))
280 if imageProperty.ratio
is not None and imageLimits.maxRatio
is not None and \
281 float(imageProperty.ratio) > float(imageLimits.maxRatio):
282 raise Exception(
"Parameter 'ratio' has not allowed value: %s > %s" % \
283 (str(imageProperty.ratio), str(imageLimits.maxRatio)))
285 if imageProperty.ratio
is not None and imageLimits.minRatio
is not None and \
286 float(imageProperty.ratio) < float(imageLimits.minRatio):
287 raise Exception(
"Parameter 'ratio' has not allowed value: %s < %s" % \
288 (str(imageProperty.ratio), str(imageLimits.minRatio)))
290 if imageProperty.colors
is not None and imageLimits.minColors
is not None and \
291 int(imageProperty.colors) < int(imageLimits.minColors):
292 raise Exception(
"Parameter 'colors' has not allowed value: %s < %s" % \
293 (str(imageProperty.colors), str(imageLimits.minColors)))
295 if imageProperty.format
is not None and imageLimits.contentType
is not None and \
296 isinstance(imageProperty.format, basestring)
and isinstance(imageLimits.contentType, list):
297 if not imageProperty.format.lower()
in \
298 [contentTypeName.lower()
for contentTypeName
in imageLimits.contentType]:
299 raise Exception(
"Parameter 'format' = '%s' has not allowed value by '%s' = %s" % \
300 (str(imageProperty.format),
301 str(MediaLimitsHandler.ImageLimits.propertyNamesDict[
'contentType']),
302 str(imageLimits.contentType)))
304 except Exception, err:
305 logger.debug(
"%s", str(err))
316 msg =
'Instance %s has:' % (str(
type(obj)))
318 for name, value
in obj.__dict__.items():
319 msg += (
"\n'%s' = %s") % (str(name), str(value))
336 if imageProperty
is None: