def previous_week_backlog(self, current_week, user_id): prev_week = current_week + relativedelta(days=-7) pw_backlog = 0 try: data = IndividualReport.objects.get( week=prev_week, gcase_user_id=user_id).values('pw_backlog') if data: pw_backlog = data.pw_backlog log.info('Data is %s ' % pw_backlog) except Exception, ex: log.exception("SQL Error encountered in client list.." + str(ex))
def main(): start_time = datetime.datetime.now() initLogger() try: # we prepare options we want _options = load_options() if _options.log_level != None: logging.getLogger().setLevel(int(_options.log_level)) motu_api.execute_request(_options) except Exception as e: log.error("Execution failed: %s", e) if hasattr(e, 'reason'): log.info(' . reason: %s', e.reason) if hasattr(e, 'code'): log.info(' . code %s: ', e.code) if hasattr(e, 'read'): try: log.log(utils_log.TRACE_LEVEL, ' . detail:\n%s', e.read()) except: pass log.debug('-' * 60) log.debug("Stack trace exception is detailed hereafter:") exc_type, exc_value, exc_tb = sys.exc_info() x = traceback.format_exception(exc_type, exc_value, exc_tb) for stack in x: log.debug(' . %s', stack.replace('\n', '')) log.debug('-' * 60) log.log(utils_log.TRACE_LEVEL, 'System info is provided hereafter:') system, node, release, version, machine, processor = platform.uname() log.log(utils_log.TRACE_LEVEL, ' . system : %s', system) log.log(utils_log.TRACE_LEVEL, ' . node : %s', node) log.log(utils_log.TRACE_LEVEL, ' . release : %s', release) log.log(utils_log.TRACE_LEVEL, ' . version : %s', version) log.log(utils_log.TRACE_LEVEL, ' . machine : %s', machine) log.log(utils_log.TRACE_LEVEL, ' . processor: %s', processor) log.log(utils_log.TRACE_LEVEL, ' . python : %s', sys.version) log.log(utils_log.TRACE_LEVEL, ' . client : %s', get_client_version()) log.log(utils_log.TRACE_LEVEL, '-' * 60) sys.exit(ERROR_CODE_EXIT) finally: log.debug("Elapsed time : %s", str(datetime.datetime.now() - start_time))
def render_kerned(self,log,text,output_name): log.debug("Entry") # Nice and big so the kerning actually has an effect error = FT.Set_Pixel_Sizes(self.facep,0,48) self.assertEqual(error,0) dest = Image.new('L',(300,80)) pen_x, pen_y = 0, 60 delta = FT.Vector() use_kerning = FT.HAS_KERNING(self.face) previous = None for n in xrange(len(text)): glyph_index = FT.Get_Char_Index(self.facep,ord(text[n])) self.assertNotEqual(glyph_index,0) if use_kerning and previous: error = FT.Get_Kerning( self.face, previous, glyph_index, FT.KERNING_DEFAULT, ctypes.byref(delta) ) self.assertEqual(error,0) pen_x += FT.IFromF26Dot6(delta.x) error = FT.Load_Glyph(self.facep,glyph_index,FT.LOAD_RENDER) self.assertEqual(error,0) glyph = self.face.glyph.contents # PIL doesn't like drawing spaces if glyph.bitmap.width > 0 and glyph.bitmap.rows > 0: color_image = Image.new('L',(glyph.bitmap.width,glyph.bitmap.rows),255) glyph_image = self.image_from_bitmap(glyph.bitmap) log.debug("Drawing character %s at pen loc (%d,%d) offset by " "bitmap origin (%d,%d)",text[n],pen_x,pen_y, glyph.bitmap_left,glyph.bitmap_top) dest.paste(color_image, (pen_x+glyph.bitmap_left,pen_y-glyph.bitmap_top), glyph_image) # glyph==mask pen_x += FT.IFromF26Dot6(glyph.advance.x) pen_y += FT.IFromF26Dot6(glyph.advance.y) previous = glyph_index test_output = self.get_output_path(output_name) dest.save(test_output) log.info(" Image saved to %s",test_output)
def measured_transformed_render(self,log,text,output_name): log.debug("Entry") def get_glyph_info(text): """ Returns an array of (glyph_index,(x,y),glyph_p) tuples """ pen = FT.Vector(FT.ToF26Dot6(0),FT.ToF26Dot6(0)) # In F26Dot6 this time return_value = [ ] use_kerning = FT.HAS_KERNING(self.face) previous = None # The measuring part for char in unicode(text): glyph_index = FT.Get_Char_Index(self.facep,ord(char)) self.assertNotEqual(glyph_index,0) if use_kerning and previous: delta = FT.Vector() error = FT.Get_Kerning(self.facep,previous,glyph_index, FT.KERNING_DEFAULT,ctypes.byref(delta)) self.assertEqual(error,0) log.debug("Applying kerning delta of %f",FT.FromF26Dot6(delta.x)) pen.x += delta.x error = FT.Load_Glyph(self.facep,glyph_index,FT.LOAD_DEFAULT) self.assertEqual(error,0) glyph_p = FT.Glyph() error = FT.Get_Glyph(self.face.glyph,ctypes.byref(glyph_p)) self.assertEqual(error,0) log.debug("Before transform, character %s's advance is (%f,%f)",repr(char), FT.FromFixed(glyph_p.contents.advance.x), FT.FromFixed(glyph_p.contents.advance.y)) error = FT.Glyph_Transform(glyph_p,None,ctypes.byref(pen)) self.assertEqual(error,0) return_value.append((glyph_index,(pen.x,pen.y),glyph_p)) log.debug("Character %s's advance is (%f,%f)",repr(char), FT.FromFixed(glyph_p.contents.advance.x), FT.FromFixed(glyph_p.contents.advance.y)) pen.x += FT.ToF26Dot6(FT.FromFixed(glyph_p.contents.advance.x)) log.debug("After character %s, pen is (%f,%f)",repr(char), FT.FromF26Dot6(pen.x),FT.FromF26Dot6(pen.y)) previous = glyph_index return return_value def calculate_bbox(glyph_info): """ Returns bbox in grid-fitted pixel coordinates in F26Dot6 format. """ bbox = FT.BBox() bbox.xMin = bbox.yMin = FT.ToFixed(16000) bbox.xMax = bbox.yMax = FT.ToFixed(-16000) glyph_bbox = FT.BBox() for (glyph_index,(pos_x,pos_y),glyph_p) in glyph_info: FT.Glyph_Get_CBox(glyph_p,FT.GLYPH_BBOX_PIXELS, ctypes.byref(glyph_bbox)) log.debug("bbox for glyph_index %d is (%f,%f) (%f,%f)",glyph_index, glyph_bbox.xMin,glyph_bbox.yMin,glyph_bbox.xMax,glyph_bbox.yMax) if glyph_bbox.xMin < bbox.xMin: bbox.xMin = glyph_bbox.xMin if glyph_bbox.yMin < bbox.yMin: bbox.yMin = glyph_bbox.yMin if glyph_bbox.xMax > bbox.xMax: bbox.xMax = glyph_bbox.xMax if glyph_bbox.yMax > bbox.yMax: bbox.yMax = glyph_bbox.yMax if bbox.xMin > bbox.xMax: bbox.xMin = bbox.xMax = 0 if bbox.yMin > bbox.yMax: bbox.yMin = bbox.yMax = 0 return bbox error = FT.Set_Pixel_Sizes(self.facep,0,48) self.assertEqual(error,0) my_target_width = 300 my_target_height = 80 angle = math.radians(10.0) dest = Image.new('L',(my_target_width,my_target_height)) glyph_info = get_glyph_info(text) log.debug("Got glyphs and positions") bbox = calculate_bbox(glyph_info) log.debug("Calculated bbox is (%f,%f), (%f,%f)", FT.FromF26Dot6(bbox.xMin),FT.FromF26Dot6(bbox.yMin), FT.FromF26Dot6(bbox.xMax),FT.FromF26Dot6(bbox.yMax)) string_width = bbox.xMax - bbox.xMin string_height = bbox.yMax - bbox.yMin log.debug("String size is (%d,%d)",string_width,string_height) start = FT.Vector( FT.ToF26Dot6((my_target_width - string_width)/2), FT.ToF26Dot6((my_target_height - string_height)/2) ) matrix = FT.Matrix( FT.ToFixed(math.cos(angle)), FT.ToFixed(-math.sin(angle)), FT.ToFixed(math.sin(angle)), FT.ToFixed(math.cos(angle)) ) log.debug("Set matrix to %f %f / %f %f",FT.FromFixed(matrix.xx), FT.FromFixed(matrix.xy),FT.FromFixed(matrix.yx),FT.FromFixed(matrix.yy)) for (glyph_index,(x,y),glyph_p) in glyph_info: # This is the variable from the tutorial named "image" # ... an unforunate choice of name perhaps. image_p = FT.Glyph() error = FT.Glyph_Copy(glyph_p,ctypes.byref(image_p)) self.assertEqual(error,0) error = FT.Glyph_Transform(image_p,ctypes.byref(matrix),ctypes.byref(start)) self.assertEqual(error,0) FT.Glyph_Get_CBox(image_p,FT.GLYPH_BBOX_PIXELS,ctypes.byref(bbox)) if bbox.xMax <= 0 or bbox.xMin >= my_target_width or \ bbox.yMax <= 0 or bbox.yMin >= my_target_height: log.debug("Skipping output of glyph; outside bbox") # The tutorial code has a memory leak here -- I chose # to insert the required Done_Glyph call even though it's # not in the tutorial FT.Done_Glyph(image_p) continue error = FT.Glyph_To_Bitmap( ctypes.byref(image_p), FT.RENDER_MODE_NORMAL,None, 1 ) self.assertEqual(error,0) bitmap_glyph_p = ctypes.cast(image_p,ctypes.POINTER(FT.BitmapGlyphRec)) bitmap_glyph = bitmap_glyph_p.contents if bitmap_glyph.bitmap.width > 0 and bitmap_glyph.bitmap.rows > 0: color_image = Image.new('L',(bitmap_glyph.bitmap.width,bitmap_glyph.bitmap.rows),255) glyph_image = self.image_from_bitmap(bitmap_glyph.bitmap) log.debug("Drawing glyph at loc (%d,%d)", bitmap_glyph.left,my_target_height-bitmap_glyph.top) dest.paste(color_image, (bitmap_glyph.left,my_target_height-bitmap_glyph.top), glyph_image) # glyph==mask FT.Done_Glyph(image_p) test_output = self.get_output_path(output_name) dest.save(test_output) log.info(" Image saved to %s",test_output) for (glyph_index,(bee,bah),glyph_p) in glyph_info: FT.Done_Glyph(glyph_p)
def measure_bbox_render(self,log,text,output_name): log.debug("Entry") def get_glyph_positions(text): pen_x, pen_y = 0, 0 glyph_positions = [ ] use_kerning = FT.HAS_KERNING(self.face) previous = None # The measuring part for char in unicode(text): glyph_index = FT.Get_Char_Index(self.facep,ord(char)) self.assertNotEqual(glyph_index,0) if use_kerning and previous: delta = FT.Vector() error = FT.Get_Kerning(self.facep,previous,glyph_index, FT.KERNING_DEFAULT,ctypes.byref(delta)) self.assertEqual(error,0) pen_x += FT.IFromF26Dot6(delta.x) error = FT.Load_Glyph(self.facep,glyph_index,FT.LOAD_DEFAULT) self.assertEqual(error,0) glyph_p = FT.Glyph() error = FT.Get_Glyph(self.face.glyph,ctypes.byref(glyph_p)) self.assertEqual(error,0) glyph_positions.append((glyph_p,(pen_x,pen_y))) pen_x += FT.IFromF26Dot6(self.face.glyph.contents.advance.x) previous = glyph_index return glyph_positions def calculate_bbox(glyph_positions): bbox = FT.BBox() bbox.xMin = bbox.yMin = FT.ToFixed(16000) bbox.xMax = bbox.yMax = FT.ToFixed(-16000) glyph_bbox = FT.BBox() for (glyph_p,(pos_x,pos_y)) in glyph_positions: FT.Glyph_Get_CBox(glyph_p,FT.GLYPH_BBOX_PIXELS, ctypes.byref(glyph_bbox)) glyph_bbox.xMin += pos_x glyph_bbox.yMin += pos_y glyph_bbox.xMax += pos_x glyph_bbox.yMax += pos_y if glyph_bbox.xMin < bbox.xMin: bbox.xMin = glyph_bbox.xMin if glyph_bbox.yMin < bbox.yMin: bbox.yMin = glyph_bbox.yMin if glyph_bbox.xMax > bbox.xMax: bbox.xMax = glyph_bbox.xMax if glyph_bbox.yMax > bbox.yMax: bbox.yMax = glyph_bbox.yMax if bbox.xMin > bbox.xMax: bbox.xMin = bbox.xMax = 0 if bbox.yMin > bbox.yMax: bbox.yMin = bbox.yMax = 0 return bbox error = FT.Set_Pixel_Sizes(self.facep,0,48) self.assertEqual(error,0) my_target_width = 300 my_target_height = 80 dest = Image.new('L',(my_target_width,my_target_height)) glyph_positions = get_glyph_positions(text) log.debug("Got glyphs and positions") bbox = calculate_bbox(glyph_positions) log.debug("Calculated bbox is (%d,%d), (%d,%d)", bbox.xMin,bbox.yMin,bbox.xMax,bbox.yMax) string_width = bbox.xMax - bbox.xMin string_height = bbox.yMax - bbox.yMin log.debug("String size is (%d,%d)",string_width,string_height) start_x = FT.ToF26Dot6((my_target_width - string_width)/2) start_y = FT.ToF26Dot6((my_target_height - string_height)/2) pen = FT.Vector() for (glyph_p,(pos_x,pos_y)) in glyph_positions: pen.x = start_x + FT.ToF26Dot6(pos_x) pen.y = start_y + FT.ToF26Dot6(pos_y) log.debug("Pen is at (%f,%f)",FT.FromF26Dot6(pen.x),FT.FromF26Dot6(pen.y)) error = FT.Glyph_To_Bitmap( ctypes.byref(glyph_p), FT.RENDER_MODE_NORMAL,ctypes.byref(pen), 0 ) self.assertEqual(error,0) bitmap_glyph_p = ctypes.cast(glyph_p,ctypes.POINTER(FT.BitmapGlyphRec)) bitmap_glyph = bitmap_glyph_p.contents if bitmap_glyph.bitmap.width > 0 and bitmap_glyph.bitmap.rows > 0: color_image = Image.new('L',(bitmap_glyph.bitmap.width,bitmap_glyph.bitmap.rows),255) glyph_image = self.image_from_bitmap(bitmap_glyph.bitmap) log.debug("Drawing glyph at loc (%d,%d)", bitmap_glyph.left,my_target_height-bitmap_glyph.top) dest.paste(color_image, (bitmap_glyph.left,my_target_height-bitmap_glyph.top), glyph_image) # glyph==mask test_output = self.get_output_path(output_name) dest.save(test_output) log.info(" Image saved to %s",test_output) for (glyph_p,(bee,bah)) in glyph_positions: FT.Done_Glyph(glyph_p)
def onMessage(self, author_id=None, message_object=None, thread_id=None, thread_type=ThreadType.USER, **kwargs): toggle = client.fetchThreadMessages( thread_id=client.uid, limit=1) # client.uid means its our own acc for message in toggle: pText = message.text.lower() if ("online" in pText): self.markAsRead(author_id) log.info("Message {} from {} in {}".format(message_object, thread_id, thread_type)) msgText = message_object.text.lower() if msgText == "say hello, delpi bot!" or msgText == "say hi to them, delpi bot! ": client.send( Message(text="Hello! I am delpi bot, created and managed by " "@Jam Emmanuel Arevalo Villarosa, written in " "python, using the " "python FBChat API! I am currently in progress, " "i would appreciate it if you all could give " "suggestions as to what i can do, thank you! :)"), thread_id=thread_id, thread_type=thread_type) elif msgText == "tell me the weather today" or msgText == "whats the weather like today?" or msgText == "tell me the weather" or msgText == "": i = requests.get( "https://api.darksky.net/forecast/b2f13b2632333395c57e9dbb5f082cda/14.8528, 120.8154" ) data = i.json() for key, value in dict.items(data): if key != "currently": pass else: a = value val = str(a['windSpeed']) t = round(int((a["temperature"]) - 32) / 1.8) temp = str(round(int((a["temperature"]) - 32) / 1.8)) precipProb = str(a['precipProbability']) if 30 <= t <= 32: client.sendRemoteImage( "https://scontent.fmnl8-1.fna.fbcdn.net/v/t1.15752-0/p280x" "280/88225254_655687121857925_6008140458004316160_n.png?_nc" "_cat=107&_nc_sid=b96e70&_nc_ohc=_6zpM_NdEu0AX9pxo1N&_nc_ht=" "scontent.fmnl8-1.fna&oh=24fd7c985f65333a985eed448c22aab9&oe=5EFCD9D3", message=Message( text="What's the weather like today?\n" + "*OVERCAST*\n" + "Wind Speed: " + val + "\n" + "Rain probability: " + precipProb + "\n" + "Temperature (C): " + temp + "°C \n" + "It's getting hot!"), thread_id=thread_id, thread_type=thread_type, ) elif 31 <= t <= 35: client.send(Message( text="What's the weather like today?\n " + "*OVERCAST*\n" + "Windspeed: " + val + "\n" + "Rain probability: " + precipProb + "\n" + "Temperature (C): " + temp + "°C \n" + "It's getting REALLY hot! Make sure to drink water!" ), thread_id=thread_id, thread_type=thread_type) elif t <= 30: client.send( Message(text="What's the weather like today?\n " + "*OVERCAST*\n" + "Windspeed: " + val + "\n" + "Rain probability: " + precipProb + "\n" + "Temperature (C): " + temp + "°C \n" + "Normal temperature!"), thread_id=thread_id, thread_type=thread_type) elif 20 <= t <= 25: client.send( Message(text="What's the weather like today?\n " + "*OVERCAST*\n" + "Windspeed: " + val + "\n" + "Rain probability: " + precipProb + "\n" + "Temperature (C): " + temp + "°C \n" + "It's so cold today!"), thread_id=thread_id, thread_type=thread_type) elif msgText == "am i a dumbbell" or msgText == "dumbbell ba ako": rand = random.randrange(1, 100) if 1 < rand < 15: client.send(Message( text= "Pare isa ka sa mga pinaka-malaking dumbbell sa lahat, sorry to tell you the truth." ), thread_id=thread_id, thread_type=thread_type) elif 15 < rand < 25: client.send( Message(text="Pabigat ka, pero hindi ka sobrang pabigat."), thread_id=thread_id, thread_type=thread_type) elif 25 < rand < 50: client.send(Message( text="Nagiging dumbbell ka kapag malungkot or pagod."), thread_id=thread_id, thread_type=thread_type) elif 50 < rand < 75: client.send(Message( text="Dumbbell ka talaga, pero kapag tinatamad ka lang."), thread_id=thread_id, thread_type=thread_type) elif 75 < rand < 80: client.send( Message(text="Dumbbell ka, pero nakakabawi pa rin."), thread_id=thread_id, thread_type=thread_type) elif 80 < rand < 100: client.send( Message(text="It's your lucky day! Hindi ka dumbbell!"), thread_id=thread_id, thread_type=thread_type) elif msgText == "ano commands" or "ano commands nung bot" in msgText: client.send(Message( text="COMMANDS:\n" "weather: just say 'tell me the weather today.'\n" "" "" "dumbbell-o-meter: just say 'dumbbell ba ako?'\n" "More explanation of the features @ : https://pastebin.com/6yUEA0sR" ), thread_id=thread_id, thread_type=thread_type) def sendMsgg(): if author_id != self.uid: self.send(Message(text="Hello!"), thread_id=thread_id, thread_type=thread_type) self.markAsDelivered(author_id, thread_id) if "online" in pText: sendMsgg()
def get(self, request): self.context_dict = {} data = request.GET.copy() self.context_dict = self.set_duration(data, self.context_dict, -1) date_filter = self.set_date_filter(data, 'week') self.context_dict['period'] = '( %04d/%02d ~ %04d/%02d )' % ( data['from_year'], data['from_month'], data['to_year'], data['to_month']) backlog_list = None total_list = None total_report = [] try: kwargs = {} args = () user_list = GcaseUser.objects.filter( user__is_active=1, case_handler=True).select_related('user').order_by('id') week_list = IndividualReport.objects.values( 'week').distinct().filter(date_filter, *args, **kwargs).order_by('-week') total_list = IndividualReport.objects.filter( date_filter, *args, **kwargs).values('week', 'gcase_user_id', 'pw_backlog', 'incoming', 'so', 'so_partner').order_by('-week') log.info(total_list) # total_list = IndividualReport.objects.filter(date_filter, *args, **kwargs).values('week','gcase_user_id').\ # annotate(total_pw_backlog=Sum('pw_backlog'),total_incoming=Sum('incoming'), # total_so=Sum('so'),total_so_partner=Sum('so_partner') # ).order_by('-week') for item in week_list: log.info('Week is %s' % item['week']) total = {} total['week'] = item['week'] user_wise = [] for user in user_list: user_report = self.set_total_info(user, item['week'], total_list) user_wise.append(user_report) total['user_wise'] = user_wise total_report.append(total) for data in total_report: log.info(data['week']) log.info('---------------------------------------------') log.info(data['user_wise']) self.context_dict['week_list'] = week_list self.context_dict['user_list'] = user_list self.context_dict['user_count'] = len(user_list) self.context_dict['total_report'] = total_report except Exception, ex: log.exception("SQL Error Encountered in jd search. " + str(ex))