python文档管理工具程序,基于python的密码管理工具

#!/usr/bin/env python

#-*- coding:utf-8 -*-

#Filename: manage_passwd.py

'''this file can help you manage all of your acounts and passwds that are hard to remember. with this tool, you just need to memorize the name of your website or app account. Introducton: 1.manpwd is a softlink to this file which has beed made a system command in /usr/bin/manpwd 2.the file which contains your password contents are encrypted, so if it somehow obtained by anyone, it's hard for them to decrypt it. And before that, you can change them. 3.in the following text, [] stands for the content you need to input Usage: 1.manpwd new --- newly create a record file. 2.manpwd add [name] --- add a record. [name] is the website's or app's name. if the record files are not created, add action is failed. name can't be same with recorded ones, or action os failed. "manpwd del" and "manpwd mod" are both suitable 3.manpwd del [name] --- del a record. if [name] is not in record, action failed. 4.manpwd mod [name] --- modify a record. 5.manpwd qry [name] --- query a specified name's information Record Structure itemname: ... username: ... email: ... password: ... '''

import sys

import pickle

import os

import platform

import base64

RECORD_FILE = '/home/xieyuan/.passwd_record/record.new'

TEMPFILE = RECORD_FILE + '.tmp'

class PasswdManager:

def __init__(self):

self.user_file = RECORD_FILE

self.record = {}

self.usage = ''' Usage: 1.manpwd new --- newly create a record file. 2.manpwd add [name] --- add a record. when names conflict, fail 3.manpwd del [name] --- del a record. name must be in record 4.manpwd mod [name] --- modify a record. empty items be noticed 5.manpwd qry [name] --- display a set of information of [name] 6.manpwd qry --- display all the names, in case you forget them'''

def Dispatch(self):

if len(sys.argv) == 2:

if sys.argv[1] == 'new':

self.CreateFile()

elif sys.argv[1] == 'qry':

self.QueryReocrd('')

else:

print(self.usage)

elif len(sys.argv) == 3:

if sys.argv[1] == 'add':

self.AddRecord(sys.argv[2])

elif sys.argv[1] == 'del':

self.DelRecord(sys.argv[2])

elif sys.argv[1] == 'mod':

self.ModRecord(sys.argv[2])

elif sys.argv[1] == 'qry':

self.QueryReocrd(sys.argv[2])

else:

print(self.usage)

else:

print(self.usage)

def isRecordExists(self):

return os.path.exists(self.user_file)

def DecodeFile(self):

f = open(self.user_file, 'r+')

filestring = f.read()

newstring = base64.decodestring(filestring)

ftmp = open(TEMPFILE, 'w+')

ftmp.write(newstring)

f.close()

ftmp.close()

with open(TEMPFILE, 'rb') as f:

self.record = pickle.load(f)

os.remove(TEMPFILE)

def EncodeFile(self):

with open(self.user_file, 'wb') as f:

pickle.dump(self.record, f)

f = open(self.user_file, 'r+')

filestring = f.read()

newstring = base64.encodestring(filestring)

f = open(self.user_file, 'w+')

f.write(newstring)

f.close()

def CreateFile(self):

#first see if the file already exists

if os.path.exists(self.user_file):

self.FeedBack('record file', 'already exists')

else:

os.mknod(self.user_file)

self.FeedBack('create record', 'ok!')

def AddRecord(self, name):

if self.isRecordExists():

#if file not empty, load first

if os.path.getsize(RECORD_FILE) > 0:

self.DecodeFile()

#if file empty, write directly

if name in self.record:

self.FeedBack('names', 'conflict...')

else:

user = raw_input('username: ')

email = raw_input('email: ')

passwd = raw_input('password: ')

self.record[name] = (user, email, passwd)

self.EncodeFile()

self.FeedBack('add record', 'ok!')

else:

self.FeedBack('record', 'not found...')

def DelRecord(self, name):

if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:

self.DecodeFile()

if name in self.record:

del self.record[name]

self.EncodeFile()

self.FeedBack('Delete record', 'ok')

else:

self.FeedBack('record', 'not exists')

else:

self.FeedBack('record', 'not exists')

def ModRecord(self, name):

if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:

self.DecodeFile()

if name in self.record:

user = raw_input('username: ')

email = raw_input('email: ')

passwd = raw_input('password: ')

if user == '' or email == '' or passwd == '':

print('there is empty item(s), sure to modify?[y/n]')

sure = raw_input()

if not sure == 'y':

self.FeedBack('Modify record', 'abort')

return

self.record[name] = (user, email, passwd)

self.EncodeFile()

self.FeedBack('Modify record', 'ok')

else:

self.FeedBack('record', 'not exists')

else:

self.FeedBack('record', 'not exists')

def QueryReocrd(self, name):

if self.isRecordExists() and os.path.getsize(RECORD_FILE) > 0:

self.DecodeFile()

if not name == '':

if name in self.record:

self.ShowRecord(self.record[name])

else:

self.FeedBack('record', 'not exists')

else:

for key in self.record.keys():

# ShowRecord(self.record[key])

print(key + ' ')

else:

self.FeedBack('record', 'not exists')

def FeedBack(self, typeme='', infostr=''):

print(typeme + ' ' + infostr)

def ShowRecord(self, tup):

print('username: {0}\nemail: {1}\npassword: {2}\n'.format(tup[0], tup[1], tup[2]))

if __name__ == '__main__':

app = PasswdManager()

app.Dispatch()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值