def export(self): '把备忘录内容导出成指定格式' try: memo_dic = {} # 使用字典,优化速度 if len(self.memo_list) == 0: print(ColorMe('备忘录中没有记录,请去添加纪录').red()) else: for memo in self.memo_list: # print(ColorMe(f'{memo.id} {memo.date:5} -- {memo.name}--{memo.thing}').green()) memo_dic[memo.id] = [memo.date, memo.thing] ex = Export(memo_dic) ex_path = { 'pdf': 'export_pdf', 'txt': 'export_txt', 'xlsx': 'export_excle', 'word': 'export_word' } print( ColorMe(''' 支持的格式为: pdf、word、xlsx、txt ''').red()) choice_input = input('请输入要到出的格式').strip() if choice_input in ex_path: getattr(ex, ex_path.get(choice_input))() self.log.info('导出成功') else: self.log.warning('没有该选项') except: self.log.warning('导出失败')
def regist(self): '注册账号,注册成功后,生产配置文件,并写入密码文件' while True: reg_username = input(ColorMe('请输入注册的用户名,按q退出').yellow()) if reg_username == 'q': break elif self.__pwd.get(reg_username): print(ColorMe('用户名已经存在,请换一个用户名').red()) continue reg_password1 = input(ColorMe('请输入注册的密码').yellow()) reg_password2 = input(ColorMe('请再次输入注册的密码').yellow()) if reg_password1 != reg_password2: print('两次密码不一致,请重新输入') continue self.__pwd[reg_username] = reg_password1 self.pwd_save() # 1.保存账号密码到pwd文件中 self.log.info(f'{reg_username}-注册成功') dic = { reg_username: { # 'path': os.path.join(BASE_DIR, 'db'), 'path': '${base_dir}', 'file_name': reg_username, 'db_type': 'db' } } self.conf.write(dic) # 2.保存数据库的路径到配置文件中 self.conf.save() print('注册成功') break
def print_all(self): '打印所有备忘录' if len(self.memo_list) == 0: print(ColorMe('备忘录中没有记录,请去添加纪录').red()) for memo in self.memo_list: print(ColorMe(f'{memo.id} {memo.date:5} --{memo.thing}').green()) self.log.info('输出所有的日志')
def save(self): '保存memo_list到文件中' self.log.debug(self.db_path) with open(self.db_path, 'wb') as f: f.write(pickle.dumps(self.memo_list)) print(ColorMe('文件保存成功').red()) self.log.info('文件保存成功')
def main(): # 配置密码文件 与 配置文件的绝对路径 log = lh_log('main') pwd_db_path = os.path.join(BASE_DIR, 'db', 'pwd.db') conf_path = os.path.join(BASE_DIR, 'conf', 'conf.txt') admin = AdminMemo(pwd_db_path, conf_path) # 生成对象 # 获取终端输入的命令:python 程序名 -u name -指令 使用装饰器来验证密码 sys_li = sys.argv if len(sys_li) == 1 or sys_li[1] == '-h': # 如果参数为 -h 或 只有文件的时候,跳转到帮助文档 admin.help() elif sys_li[1] == '-r': # 注册函数 admin.regist() else: # 登入进去进行各种操作 login_result = admin.login(sys_li[2], sys_li[4]) if login_result['status']: # 账号密码验证通过 if sys_li[5] in admin.menus: if hasattr(admin, admin.menus.get(sys_li[5])): run = getattr(admin, admin.menus.get(sys_li[5], None)) if run: run() else: log.waring('系统menus错误,请检查') else: print('无该选项,请使用python main.py -h 核对后输入') else: # 账号密码错误,不同的状态 print(ColorMe(login_result['statusText']).red())
def modify(self): '修改某条备忘录' self.print_all() self.log.info('修改备忘录') try: index_input = int(input('请输入需要修改的编号:').strip()) flag = True # for 循环结束标志位 for memo in self.memo_list: if not flag: # 在while退出的时候,就不再for循环了,节约计算时间。 break if index_input == memo.id: while 1: words_input = input( '请输入要修改的字段(date或thing) q返回:').strip() if words_input == 'thing': new_thing = input('请输入一个新thing:') memo.thing = new_thing print('修改成功') self.save() return True elif words_input == 'date': new_date = input('请输入一个新date:2017-01-01:').strip() memo.date = parser.parse(new_date).strftime( '%Y-%m-%d %X') print('修改成功') self.save() return True # elif words_input == 'name': # new_name = input('请输入一个新name').strip() # memo.name = new_name # print('修改成功') # return True elif words_input == 'q': flag = False break else: print(ColorMe('输入有误').red()) else: # 是fou的else print(ColorMe('没有该编号,请重新输入').red()) except Exception as e: print('输入有误,请重新输入')
def add(self): '添加一条数据,存储到Memo对象中' try: thing = input('请输入事件:') if self.add_input(thing): memo = Memo(*self.add_input(thing)) print(memo.date, memo.thing) # 处理x.id 自增加,利用最后一条memo的id + 1 if len(self.memo_list) == 0: memo.id = 0 else: memo.id = int(self.memo_list[-1]._id) + 1 self.memo_list.append(memo) self.save() self.log.info(ColorMe('添加一条数据成功').red()) except Exception as e: self.log.warning('添加事件,出现未知错误') print(ColorMe('添加失败,请重新输入').red(), e)
def delete(self): '删除一条记录' try: if len(self.memo_list) == 0: print('已经没了,你还删') else: self.print_all() num = int(input('请输入要删除的编号')) for memo in self.memo_list: if memo.id == num: self.memo_list.remove(memo) self.log.info('事件,删除成功') print(ColorMe('删除成功').red()) self.print_all() self.save() except Exception as e: print('删除出错,请重新输入')
def search_date(self): '根据' memo_dic = {} input_time = input('请输入一个月份eg(2017 1 1)') year, month, day = input_time.split(' ') year, month, day = int(year), int(month), int(day) print(year, month, day) if year == 0: # for memo in self.memo_list: memo_dic[memo.id] = {memo.date: memo.thing} print(json.dumps(memo_dic, ensure_ascii=False)) return json.dumps(memo_dic, ensure_ascii=False) elif month == 0: for memo in self.memo_list: if parser.parse(memo.date).year == year: memo_dic[memo.id] = {memo.date: memo.thing} print(json.dumps(memo_dic, ensure_ascii=False)) return json.dumps(memo_dic, ensure_ascii=False) elif day == 0: for memo in self.memo_list: if parser.parse(memo.date).year == year and parser.parse( memo.date).month == month: memo_dic[memo.id] = {memo.date: memo.thing} print(json.dumps(memo_dic, ensure_ascii=False)) return json.dumps(memo_dic, ensure_ascii=False) elif year != 0 and month != 0 and day != 0: for memo in self.memo_list: if parser.parse(memo.date).year == year and parser.parse( memo.date).month == month and parser.parse( memo.date).day == day: memo_dic[memo.id] = {memo.date: memo.thing} print(json.dumps(memo_dic, ensure_ascii=False)) return json.dumps(memo_dic, ensure_ascii=False) self.log.info('按照时间搜索日志') for memo in self.memo_list: if parser.parse(memo.date).month == input_time: # if input_time == memo.date : print( ColorMe( f'{memo.id}--{memo.date:5}--{memo.thing}').yellow())
def print_menu(self): '打印菜单' for k, v in self.menus.items(): print(ColorMe(f' {k} {v}').blue()) self.log.info('打印菜单')