def _checkRGB(self, rgbstr): checkParamsType([(rgbstr, [basestring])]) if isinstance(rgbstr, unicode): rgbstr = rgbstr.encode('utf-8') checkParamsLimit([(rgbstr, ['len(x)==6'])]) if not self.RGB_PATTERN.match(rgbstr): raise BaeValueError("invalid RGB color", rgbstr)
def setCompositeCanvas(self, canvas_width, canvas_height): checkParamsType([(canvas_width, [int])]) checkParamsLimit([(canvas_width, ['0<=x<=10000'])]) checkParamsType([(canvas_height, [int])]) checkParamsLimit([(canvas_height, ['0<=x<=10000'])]) self.__composite_canvas_width = canvas_width self.__composite_canvas_height = canvas_height
def setAnnotatePos(self, x_offset, y_offset): checkParamsType([(x_offset, [int])]) checkParamsLimit([(x_offset, ['x>=0'])]) checkParamsType([(y_offset, [int])]) checkParamsLimit([(y_offset, ['x>=0'])]) self.__operations['x_offset'] = x_offset self.__operations['y_offset'] = y_offset
def setSource(self, image_source): checkParamsType([(image_source, [basestring])]) if isinstance(image_source, unicode): image_source = image_source.encode('utf-8') checkParamsLimit([(image_source, ['0<len(x)<=2048'])]) if self._isUrl(image_source): self.__args['src'] = image_source
def setCompositePos(self, x_offset, y_offset, img_key = 0): checkParamsType([(x_offset, [int])]) checkParamsType([(y_offset, [int])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist"%img_key) self.__strudata['req_data_source'][img_key]['operations']['x_offset'] = x_offset self.__strudata['req_data_source'][img_key]['operations']['y_offset'] = y_offset
def setCropping(self, x, y, width, height): checkParamsType([(x, [int]), (y, [int]), (height, [int]), (width, [int])]) checkParamsLimit([(x, ['0<=x<=10000']), (y, ['0<=x<=10000']), (height, ['0<=x<=10000']), (width, ['0<=x<=10000'])]) self.__args['cut_x'] = x self.__args['cut_y'] = y self.__args['cut_h'] = height self.__args['cut_w'] = width
def setCompositeOpacity(self, opacity, img_key=0): checkParamsType([(opacity, [int, float])]) checkParamsLimit([(opacity, ['0.0<=x<=1.0'])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist" % img_key) self.__strudata['req_data_source'][img_key]['operations'][ 'opacity'] = opacity
def setCompositeAnchor(self, anchor, img_key=0): checkParamsType([(anchor, [int])]) checkParamsLimit([(anchor, ['0<=x<=8'])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist" % img_key) self.__strudata['req_data_source'][img_key]['operations'][ 'anchor_point'] = anchor
def generateVCode(self, vcode_len=4, vcode_pattern=0): checkParamsType([(vcode_len, [int])]) checkParamsLimit([(vcode_len, ['4<=x<=5'])]) checkParamsType([(vcode_pattern, [int])]) checkParamsLimit([(vcode_pattern, ['0<=x<=3'])]) self.__args = {} self.__args['vcservice'] = 0 self.__args['len'] = vcode_len self.__args['setno'] = vcode_pattern return self._common_process(self.__args)
def setCompositePos(self, x_offset, y_offset, img_key=0): checkParamsType([(x_offset, [int])]) checkParamsType([(y_offset, [int])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist" % img_key) self.__strudata['req_data_source'][img_key]['operations'][ 'x_offset'] = x_offset self.__strudata['req_data_source'][img_key]['operations'][ 'y_offset'] = y_offset
def setTranscoding(self, image_type, quality=60): types = {'gif': 2, 'jpg': 1, 'png': 3, 'webp': 4} checkParamsType([(image_type, [basestring]), (quality, [int])]) checkParamsLimit([(quality, ['0<=x<=100'])]) try: self.__args['imgtype'] = types[image_type] except KeyError: raise BaeParamError('Invalid Image Type[%s]' % image_type) if image_type in ['gif', 'jpg']: self.__args['quality'] = quality
def generateVCode(self, vcode_len = 4, vcode_pattern = 0): checkParamsType([(vcode_len, [int])]) checkParamsLimit([(vcode_len, ['4<=x<=5'])]) checkParamsType([(vcode_pattern, [int])]) checkParamsLimit([(vcode_pattern, ['0<=x<=3'])]) self.__args = {} self.__args['vcservice'] = 0 self.__args['len'] = vcode_len self.__args['setno'] = vcode_pattern return self._common_process(self.__args)
def setTranscoding(self, image_type, quality = 60): types = {'gif': 2, 'jpg': 1, 'png': 3, 'webp': 4} checkParamsType([(image_type, [basestring]), (quality, [int])]) checkParamsLimit([(quality, ['0<=x<=100'])]) try: self.__args['imgtype'] = types[image_type] except KeyError: raise BaeParamError('Invalid Image Type[%s]'%image_type) if image_type in ['gif', 'jpg']: self.__args['quality'] = quality
def setAnnotateOpacity(self, opacity): checkParamsType([(opacity, [int, float])]) checkParamsLimit([(opacity, ['0.0<=x<=1.0'])]) if opacity == 1: opacity = '00' elif opacity == 0: opacity = 'ff' else: opacity = hex(int(math.ceil(255 - opacity*255))) opacity = opacity.replace('0x', '') opacity = opacity.upper() self.__operations['opacity'] = opacity
def setAnnotateOpacity(self, opacity): checkParamsType([(opacity, [int, float])]) checkParamsLimit([(opacity, ['0.0<=x<=1.0'])]) if opacity == 1: opacity = '00' elif opacity == 0: opacity = 'ff' else: opacity = hex(int(math.ceil(255 - opacity * 255))) opacity = opacity.replace('0x', '') opacity = opacity.upper() self.__operations['opacity'] = opacity
def verifyVCode(self, vcode_input, vcode_secret): checkParamsType([(vcode_input, [basestring])]) if isinstance(vcode_input, unicode): vcode_input = vcode_input.encode('utf-8') checkParamsLimit([(vcode_input, ['4<=len(x)<=5'])]) checkParamsType([(vcode_secret, [basestring])]) if isinstance(vcode_secret, unicode): vcode_secret = vcode_secret.encode('utf-8') self.__args = {} self.__args['vcservice'] = 1 self.__args['input'] = vcode_input self.__args['vcode'] = vcode_secret return self._common_process(self.__args)
def __init__(self, access_key, secret_key, host, image_source = "http://", debug = False): self.__args = {} self.__args['src'] = image_source self.__ak = access_key self.__sk = secret_key self.__host = host checkParamsType([(self.__ak, [basestring]), (self.__sk, [basestring]), (debug, [bool]), (self.__args['src'], [basestring]), (self.__host, [basestring])]) checkParamsLimit([(image_source, ['0<=len(x)<=2048'])]) if not self.__ak or not self.__sk or not self.__host: raise BaeConstructError("Invalid ak, sk, or image host") self.__requestId = 0 self._debug = debug self.__strudata = None
def setCompositeSource(self, image_source): checkParamsType([(image_source, [basestring])]) if isinstance(image_source, unicode): image_source = image_source.encode('utf-8') checkParamsLimit([(image_source, ['0<=len(x)<=2048'])]) t = {'http_reqpack': {}, 'source_data_type': '1', 'operations': {}, } if self._isUrl(image_source): t['sourcemethod'] = 'GET' t['source_url'] = image_source self.__strudata['req_data_source'].append(t) else: pass
def setZooming(self, zooming_type, value, height_value = 0): checkParamsType([(zooming_type, [int]), (value, [int]), (height_value, [int])]) checkParamsLimit([(zooming_type, ['1<=x<=4'])]) if zooming_type == BaeImage.ZOOMING_TYPE_HEIGHT: checkParamsLimit([(value, ['0<=x<=10000'])]) self.__args['size'] = 'b0_' + str(value) elif zooming_type == BaeImage.ZOOMING_TYPE_WIDTH: checkParamsLimit([(value, ['0<=x<=10000'])]) self.__args['size'] = 'b' + str(value) + '_0' elif zooming_type == BaeImage.ZOOMING_TYPE_PIXELS: checkParamsLimit([(value, ['0<=x<=100000000'])]) self.__args['size'] = 'p' + str(value) elif zooming_type == BaeImage.ZOOMING_TYPE_UNRATIO: checkParamsLimit([(value, ['0<=x<=10000'])]) checkParamsLimit([(height_value, ['0<=x<=10000'])]) self.__args['size'] = 'u' + str(value) + '_' + str(height_value)
def setCompositeSource(self, image_source): checkParamsType([(image_source, [basestring])]) if isinstance(image_source, unicode): image_source = image_source.encode('utf-8') checkParamsLimit([(image_source, ['0<=len(x)<=2048'])]) t = { 'http_reqpack': {}, 'source_data_type': '1', 'operations': {}, } if self._isUrl(image_source): t['sourcemethod'] = 'GET' t['source_url'] = image_source self.__strudata['req_data_source'].append(t) else: pass
def setZooming(self, zooming_type, value, height_value=0): checkParamsType([(zooming_type, [int]), (value, [int]), (height_value, [int])]) checkParamsLimit([(zooming_type, ['1<=x<=4'])]) if zooming_type == BaeImage.ZOOMING_TYPE_HEIGHT: checkParamsLimit([(value, ['0<=x<=10000'])]) self.__args['size'] = 'b0_' + str(value) elif zooming_type == BaeImage.ZOOMING_TYPE_WIDTH: checkParamsLimit([(value, ['0<=x<=10000'])]) self.__args['size'] = 'b' + str(value) + '_0' elif zooming_type == BaeImage.ZOOMING_TYPE_PIXELS: checkParamsLimit([(value, ['0<=x<=100000000'])]) self.__args['size'] = 'p' + str(value) elif zooming_type == BaeImage.ZOOMING_TYPE_UNRATIO: checkParamsLimit([(value, ['0<=x<=10000'])]) checkParamsLimit([(height_value, ['0<=x<=10000'])]) self.__args['size'] = 'u' + str(value) + '_' + str(height_value)
def __init__(self, cache_id, memcache_addr, user, password): checkParamsType([(memcache_addr, [basestring]), (user, [basestring]), (password, [basestring]), (cache_id, [basestring])]) self._ak = user self._sk = password self._appid = cache_id self.nsh = nshead.CNsHead() self.lastcmd, self.lastkey = None, None self.lasterrno, self.lasterrmsg = 0, None self._servers = [] for server in memcache_addr.split(";"): try: host, port = server.split(':') self._servers.append((host, int(port))) except: raise BaeMemcacheParamsError("Invalid memcache_addr") self._index = 0 self._sock = None
def __init__(self, access_key, secret_key, host, image_source="http://", debug=False): self.__args = {} self.__args['src'] = image_source self.__ak = access_key self.__sk = secret_key self.__host = host checkParamsType([(self.__ak, [basestring]), (self.__sk, [basestring]), (debug, [bool]), (self.__args['src'], [basestring]), (self.__host, [basestring])]) checkParamsLimit([(image_source, ['0<=len(x)<=2048'])]) if not self.__ak or not self.__sk or not self.__host: raise BaeConstructError("Invalid ak, sk, or image host") self.__requestId = 0 self._debug = debug self.__strudata = None
def setAnnotateFont(self, name, size, color): checkParamsType([(name, [int])]) checkParamsLimit([(name, ['0<=x<=4'])]) checkParamsType([(size, [int])]) checkParamsLimit([(size, ['0<=x<=100'])]) checkParamsType([(color, [basestring])]) if isinstance(color, unicode): color = color.encode('utf-8') self._checkRGB(color) self.__operations['font_name'] = name self.__operations['font_size'] = size self.__operations['font_color'] = color
def setRotation(self, degree): checkParamsType([(degree, [int])]) checkParamsLimit([(degree, ['0<=x<=360'])]) self.__args['rotate'] = degree
def setAnnotateOutputCode(self, output_code): checkParamsType([(output_code, [int])]) checkParamsLimit([(output_code, ['0<=x<=4'])]) self.__operations['desttype'] = output_code
def setCompositeOpacity(self, opacity, img_key = 0): checkParamsType([(opacity, [int, float])]) checkParamsLimit([(opacity, ['0.0<=x<=1.0'])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist"%img_key) self.__strudata['req_data_source'][img_key]['operations']['opacity'] = opacity
def setQRCodeMargin(self, margin): checkParamsType([(margin, [int])]) checkParamsLimit([(margin, ['1<=x<=100'])]) self.__operations['margin'] = margin
def setQRCodeSize(self, size): checkParamsType([(size, [int])]) checkParamsLimit([(size, ['0<x<=100'])]) self.__operations['size'] = size
def setCompositeQuality(self, quality): checkParamsType([(quality, [int])]) checkParamsLimit([(quality, ['0<=x<=100'])]) self.__composite_quality = quality
def setQuality(self, quality=60): checkParamsType([(quality, [int])]) checkParamsLimit([(quality, ['0<=x<=100'])]) self.__args['quality'] = quality
def setHue(self, hue): checkParamsType([(hue, [int])]) checkParamsLimit([(hue, ['1<=x<=100'])]) self.__args['hue'] = hue
def setSaturation(self, saturation): checkParamsType([(saturation, [int])]) checkParamsLimit([(saturation, ['1<=x<=100'])]) self.__args['saturation'] = saturation
def setLightness(self, lightness): checkParamsType([(lightness, [int])]) checkParamsLimit([(lightness, ['x>=1'])]) self.__args['lightness'] = lightness
def setCompositeOutputCode(self, output_code): checkParamsType([(output_code, [int])]) checkParamsLimit([(output_code, ['0<=x<4'])]) self.__composite_desttype = output_code
def setQRCodeLevel(self, level): checkParamsType([(level, [int])]) checkParamsLimit([(level, ['1<=x<=4'])]) self.__operations['level'] = level
def setQRCodeText(self, text): checkParamsType([(text, [basestring])]) if isinstance(text, unicode): text = text.encode('utf-8') checkParamsLimit([(text, ['1<=len(x)<=500'])]) self.__source_data['data1'] = base64.b64encode(text)
def setQRCodeVersion(self, version): checkParamsType([(version, [int])]) checkParamsLimit([(version, ['0<=x<=30'])]) self.__operations['version'] = version
def setAnnotateQuality(self, quality): checkParamsType([(quality, [int])]) checkParamsLimit([(quality, ['0<=x<=100'])]) self.__operations['quality'] = quality
def setCompositeAnchor(self, anchor, img_key = 0): checkParamsType([(anchor, [int])]) checkParamsLimit([(anchor, ['0<=x<=8'])]) if img_key >= len(self.__strudata['req_data_source']): raise BaeValueError("invalid img_key, image %d isn't exist"%img_key) self.__strudata['req_data_source'][img_key]['operations']['anchor_point'] = anchor
def setContrast(self, contrast): checkParamsType([(contrast, [int])]) checkParamsLimit([(contrast, ['0<=x<=1'])]) self.__args['contrast'] = contrast