import asyncio from defaultdict import DefaultDict from aiocqhttp import CQHttp from theresistance import TheResistanceCampaign bot = CQHttp(enable_http_post=False) group_message_handler = DefaultDict(default_group_message_handler) private_message_handler = DefaultDict(default_private_message_handler) campaigns = dict() def default_group_message_handler(context): at_me = f"[CQ:at,qq={context['self_id']}]" message = context["message"] group_id = context["group_id"] user_id = context["user_id"] # 忽略没有at机器人的消息 if at_me not in message: return # 忽略已经在其他群进行游戏的QQ号消息,防止无法确定私聊消息应该交给哪个游戏处理 if user_id in private_message_handler: return {"reply": "你已经在其它群的游戏中了……"} # TODO:文字移到外部json文件中 # 预先防止群内已有游戏进行,但此代码应该不会被执行,因为后续群消息应该都交给其它message_handler处理了 if group_id in group_message_handler: print("This line of code should not be excecuted. Please check.") return
def test_check_donot_add_value(): d = DefaultDict() assert EMPTY not in d assert len(d) == 0
def test_empty_default(): d = DefaultDict() assert d[EMPTY] == None assert len(d) == 1
def test_function_default(): d = DefaultDict(lambda x: 0) assert d[EMPTY] == 0 assert len(d) == 1
def test_function_default_with_key_parameter(): d = DefaultDict(lambda x: x+1) assert d[41] == 42 assert len(d) == 1
def test_non_function_default(): d = DefaultDict(89) assert d[EMPTY] == 89 assert len(d) == 1
def report_category( dateStart, dateEnd, timesheets, recipients, filename="/tmp/" + QDateTime.currentDateTime().toString() + "_timeSheetCatSum.xls" ): workbook = Workbook() worksheet = workbook.add_sheet("Category-Project") proj_col = 2 type_col = 1 hr_act_col = 3 sum_col = 4 row = 3 col = 0 col_init = 2 virgin = True project_dict = DefaultDict( DefaultDict( list ) ) for ts in timesheets: project_dict[ts.project()][ts.assetType()].append(ts) # insert Blur logo # worksheet.insert_bitmap('K:/Production/blur_bugs/blur_head_logo.bmp', 0, 0, 100, 100, 1, 1) # Set the column sizes set_col_width(worksheet,0,200) set_col_width(worksheet,proj_col,200) set_col_width(worksheet,type_col,200) set_col_width(worksheet,hr_act_col,150) #Setup the styles styleTitle = style_title() styleHead = style_header() styleBody = style_body() #set this worksheet as the active worksheet worksheet.set_selected(True) #spread sheet titles worksheet.write(row, proj_col+4, "", styleTitle) worksheet.write(row, proj_col+3, "", styleTitle) worksheet.write(row, proj_col+2, "", styleTitle) worksheet.write(row, proj_col+1, "", styleTitle) worksheet.write(row, proj_col , "", styleTitle) worksheet.write(row, proj_col-1, "Time Sheet Summary by Category and Project: %s - %s" % (dateStart.toString('MM/dd/yy'), dateEnd.toString('MM/dd/yy')), styleTitle) row+=1 #spread sheet col headers worksheet.write(row, proj_col, 'Projects', styleHead) worksheet.write(row, type_col, 'Category', styleHead) worksheet.write(row, hr_act_col,'Actual Hours', styleHead) worksheet.write(row, hr_act_col+1,'', styleHead) worksheet.write(row, hr_act_col+2,'', styleHead) worksheet.write(row, hr_act_col+3,'', styleHead) row+=1 # Loop through each project hrs = 0 def psort(a,b): if a == b: return 0 if a.name().toUpper() > b.name().toUpper(): return 1 return -1 for p in sorted(project_dict.keys(),psort): at_dict = project_dict[p] for at, sheets in at_dict.iteritems(): # TODO, change to sort using chronology hrs = 0 for ts in sheets: hrs += ts.scheduledHour() # Write a value for each asset type if at != ts.assetType(): write_vals(worksheet,row,1,[at.name(),p.name(),hrs]) #worksheet.write(row,sum_col,Formula("SUM(D10:D15)")) #not working wtf ...drp row += 1 # Write the last one write_vals(worksheet,row,1,[at.name(),p.name(),hrs]) row += 1 workbook.save(filename) # Send email body = "Attached is a time sheet summary by category and project for " + dateStart.toString('MM/dd/yy') + " - " + dateEnd.toString('MM/dd/yy') + "\n\nthePipe\n" return email_report( recipients, subject="Time sheet summary by category and project: %s - %s" % (dateStart.toString('MM/dd/yy'), dateEnd.toString('MM/dd/yy')), body=body, SS=filename );
def report_missing( datestart, dateend, timesheets, recipients = [], filename="/tmp/" + QDateTime.currentDateTime().toString() + "_timeSheetReportMissing.xls" ): date = datestart dst = datestart.toString('MM/dd/yy') det = dateend.toString('MM/dd/yy') missing = DefaultDict() dates_by_user = DefaultDict(list) # Gather by user and date for ts in timesheets: e = ts.user() d = ts.dateTime().date() dates_by_user[e].append(d) # Get all the employees that should be entering timesheets emps = Employee.select().filter("disabled",QVariant(0)) # Function to sort them by name def emp_sort(a,b): if a == b: return 0 if a.displayName().toUpper() > b.displayName().toUpper(): return 1 return -1 workbook = Workbook() worksheet = workbook.add_sheet("Missing") date_col = 1 user_col = 2 row = 3 col = 0 col_init = 2 virgin = True # insert Blur logo # worksheet.insert_bitmap('K:/Production/blur_bugs/blur_head_logo.bmp', 0, 0, 100, 100, 1, 1) # Set the column sizes set_col_width(worksheet,0,200) set_col_width(worksheet,date_col,200) set_col_width(worksheet,user_col,200) #Setup the styles styleTitle = style_title() styleHead = style_header() styleBody = style_body() #set this worksheet as the active worksheet worksheet.set_selected(True) #spread sheet titles worksheet.write(row, 4, "" , styleTitle) worksheet.write(row, 3, "" , styleTitle) worksheet.write(row, 2, "" , styleTitle) worksheet.write(row, 1, "Missing Time Sheets By Date: %s to %s" % (datestart.toString('MM/dd/yy'), dateend.toString('MM/dd/yy')), styleTitle) row+=1 #spread sheet col headers worksheet.write(row, date_col, 'Date', styleHead) worksheet.write(row, user_col, 'User', styleHead) worksheet.write(row, user_col+1,'', styleHead) worksheet.write(row, user_col+2,'', styleHead) row+=1 # Create email output while date < dateend: ds = date.toString('MM/dd/yy') if date.dayOfWeek() <= 5: write_vals(worksheet,row,date_col,[ds]) row += 1 miss = [] for e in emps: if not dates_by_user.has_key(e) or not dates_by_user[e].count(date): miss.append(e) missing[e] += 1 for e in sorted(miss,emp_sort): write_vals(worksheet,row,user_col,[e.displayName()]) row += 1 date = date.addDays(1) row += 2 worksheet.write(row, 4,"", styleTitle) worksheet.write(row, 3,"", styleTitle) worksheet.write(row, 2,"", styleTitle) worksheet.write(row, 1,"Missing TimeSheet Count : %s to %s" % (datestart.toString('MM/dd/yy'), dateend.toString('MM/dd/yy')), styleTitle) row += 1 worksheet.write(row, user_col-1,'', styleHead) worksheet.write(row, user_col, 'User', styleHead) worksheet.write(row, user_col+1,'Missing', styleHead) worksheet.write(row, user_col+2,'', styleHead) row += 1 for e in sorted(missing.keys(),emp_sort): write_vals(worksheet,row,user_col,[e.displayName(),missing[e]]) row += 1 workbook.save(filename) body = "Attached is a missing time sheets summary for " + datestart.toString('MM/dd/yy') + " - " + dateend.toString('MM/dd/yy') + "\n\nthePipe\n" return email_report( recipients, "Missing timesheets: " + dst + " - " + det, body, SS=filename )
def report_dump(dateStart,dateEnd, timesheets, recipients, filename='/tmp/%s_timeSheetDump.xls' % QDateTime.currentDateTime().toString()): #new spread sheet workbook = Workbook() worksheet = workbook.add_sheet("Timesheet Dump") #Setup the styles styleTitle = style_title() styleHead = style_header() styleBody = style_body() ttl_sch = 0 HrsPerDay = 8 empl_col = 1 date_col = 2 proj_col = 3 type_col = 4 asset_col = 5 comment_col = 6 hr_act_col = 7 days_col = 8 days_8hr_col = 9 row = 6 col = 0 col_init = 2 virgin = True def ts_cmp(a,b): if a.user() != b.user(): return a.user().displayName().toUpper() > b.user().displayName().toUpper() if a.dateTime() != b.dateTime(): return a.dateTime() > b.dateTime() if a.project() != b.project(): return a.project().name().toUpper() > b.project().name().toUpper() return a.assetType().name().toUpper() > b.assetType().name().toUpper() def ts_sort(a,b): if a == b: return 0 if ts_cmp(a,b): return 1 return -1 class DaySummary(object): def __init__(self): self.timeSheetCount = 0 self.totalHours = 0 def addTs(self,hours): self.totalHours += hours self.timeSheetCount += 1 def days(self, hrs): Log( "Hrs %i, totalHours %i, tsCount %i" % (hrs,self.totalHours,self.timeSheetCount) ) if self.timeSheetCount > 1 and self.totalHours > 8: #more than one time sheet entry return hrs / self.totalHours if hrs > 8: return 1 else: return hrs / 8.0 summariesByUserAndDay = DefaultDict( DefaultDict( DaySummary ) ) for ts in sorted(timesheets,ts_sort): print 'Adding %i hrs for %s user on %s date to DaySummary %s' % (ts.scheduledHour(), ts.user().name(), ts.dateTime().date().toString(), unicode(summariesByUserAndDay[ts.user()][ts.dateTime().date().toString()]) ) summariesByUserAndDay[ts.user()][ts.dateTime().date().toString()].addTs( ts.scheduledHour() ) for ts in sorted(timesheets,ts_sort): if virgin: virgin = False set_col_width(worksheet,0,50) set_col_width(worksheet,empl_col,160) set_col_width(worksheet,date_col,100) set_col_width(worksheet,proj_col,160) set_col_width(worksheet,type_col,180) set_col_width(worksheet,asset_col,200) set_col_width(worksheet,hr_act_col,40) set_col_width(worksheet,comment_col,220) set_col_width(worksheet,days_col,40) set_col_width(worksheet,days_8hr_col,65) #set this worksheet as the active worksheet worksheet.set_selected(True) #spread sheet titles set_row_height(worksheet,row-3,70) worksheet.write(row-3, 9, "", styleTitle) worksheet.write(row-3, 8, "", styleTitle) worksheet.write(row-3, 7, "", styleTitle) worksheet.write(row-3, 6, "", styleTitle) worksheet.write(row-3, 5, "", styleTitle) worksheet.write(row-3, 4, "", styleTitle) worksheet.write(row-3, 3, "", styleTitle) worksheet.write(row-3, 2, "", styleTitle) worksheet.write(row-3, 1, "Time Sheet Dump %s - %s" % (dateStart.toString('MM/dd/yy'), dateEnd.toString('MM/dd/yy')), styleTitle) #spread sheet col headers worksheet.write(row-2, empl_col, 'Employee', styleHead) worksheet.write(row-2, date_col, 'Date', styleHead) worksheet.write(row-2, proj_col, 'Projects', styleHead) worksheet.write(row-2, type_col, 'Category', styleHead) worksheet.write(row-2, asset_col, 'Asset', styleHead) worksheet.write(row-2, comment_col, 'Comments', styleHead) worksheet.write(row-2, hr_act_col, 'Hours', styleHead) worksheet.write(row-2, days_col, 'Days', styleHead) worksheet.write(row-2, days_8hr_col, 'Days (8hr)', styleHead) dat = ts.dateTime().date().toString('yyyy/MM/dd ddd') prj = ts.project() type = ts.assetType() emp = ts.user() hr = ts.scheduledHour() #...drp if more than one time sheet entry for this day divid by TOT[k] else if only one entry message days = 0 if ts.dateTime().date().dayOfWeek() > 5: days = 0 #dont count weekends else: days = summariesByUserAndDay[ts.user()][ts.dateTime().date().toString()].days(hr) elem = '' if ts.element() != ts.project(): elem = ts.element().displayName(True) write_vals( worksheet, row, 1, [emp.displayName(), dat, prj.displayName(), type.name(), elem, ts.comment(), hr, days, hr / 8.0] ) row += 1 workbook.save(filename) body = """ Attached is a dump of all time sheets %s - %s. thePipe """ % ( dateStart.toString('MM/dd/yy'), dateEnd.toString('MM/dd/yy') ) return email_report( recipients, "Time sheet dump: %s - %s" % (dateStart.toString('MM/dd/yy'),dateEnd.toString('MM/dd/yy')), body, SS=filename )
def __init__(self, path=".animedb_config"): self['path'] = os.path.abspath(path) DefaultDict.__init__(self) self.load()