Friday, June 9, 2017

simple username/password manager

**********passwordmrg.py **********

import os
import sqlite3 as lite
import sqlite3
import sys



class SQLiteStorePipeline:
    # filename = 'data.sqlite'

    def __init__(self,filename):
        self.conn = None
        self.filename = filename

    def queryList(self, table):
        try:

            u = self.conn.execute('select * from ' + table)
            res = u.fetchall()

        except:
            print 'query Failed '
        return res
    def insert(self, table, item):
        try:

            field = table
            # u = self.conn.execute('insert into ' + table + "("+ field +") values('" + item +"'')")
            u = self.conn.execute('insert into ' + table + "("+ field +") values(?)", (item,))
     
            print "inserted "  + item
            self.conn.commit()
        except:
            print 'insert Failed '
       
    def insertRelate(self, table,fields, items):
        try:
            # self.conn.execute('insert into COMPANY values(?,?,?,?,?)',
            #               (item['title'], item['price'], item['link'],item['image'],unicode(domain)))
            field = table
            c1 = fields[0]
            c2 = fields[1]
            c3 = fields[2]
            # u = self.conn.execute('insert into ' + table + "("+ field +") values('" + item +"'')")
            u = self.conn.execute('insert into ' + table + "("+ ",".join(fields) +") values(?,?,?)", (items))
            # u = self.conn.execute('insert into ' + table +" values(?)", (item))
            #print "tt"
            #print u.fetchone()
            print "inserted "
            self.conn.commit()
           

        except:
            print 'insert Failed '
        # return res

    def initialize(self):
        if os.path.exists(self.filename):
            self.conn = sqlite3.connect(self.filename)
        else:
            self.conn = self.create_table()

    def finalize(self):
        if self.conn is not None:
            self.conn.commit()
            self.conn.close()
            self.conn = None

    def create_table(self):
        conn = sqlite3.connect(self.filename)
        conn.execute("""create table pwd
                     (id INTEGER PRIMARY KEY AUTOINCREMENT,pwd text)""")
        conn.execute("""create table usr
                     (id INTEGER PRIMARY KEY AUTOINCREMENT,usr text)""")
        conn.execute("""create table mgr
                     (id INTEGER PRIMARY KEY AUTOINCREMENT,usr text, pwd text, web text)""")
        conn.commit()
        return conn

db = "/home/user1/p.db"
u = SQLiteStorePipeline(db)
u.initialize()
comm = sys.argv[1]

if comm == "listp":
    plist = u.queryList("pwd")
    for i in plist:
        print i
elif comm == "listu":
    plist = u.queryList("usr")
    for i in plist:
        print i
elif comm == "addu":
    user = sys.argv[2]
    u.insert("usr", user)
elif comm == "addp":
    password = sys.argv[2]
    u.insert("pwd", password)
elif comm == "addw":
    user = sys.argv[2]
    password = sys.argv[3]
    web = sys.argv[4]
    fields = ["usr","pwd","web"]
    items = [user,password,web]
    u.insertRelate("mgr", fields,items)
elif comm == "list":
    plist = u.queryList("pwd")
    ulist = u.queryList("usr")
    mlist = u.queryList("mgr")
    password = ""
    user = ""
    for i in mlist:
        # print i
        for j in ulist:
            if str(j[0]) == i[1]:
                user = j[1]

        for k in plist:
            if str(k[0]) == i[2]:
                password = k[1]
        print i[3],user,password





***** usages*****
- add user
$ python passwordmrg.py addu test

- list user
$ python passwordmrg.py listu

- add password
$ python passwordmrg.py addp pass123

- list password
$ python passwordmrg.py listp

- add relation with websit, first 1 is the id of user and second 1 is the id of password
$ python passwordmrg.py addw 1 1 www.facebook.com

- add all user/password/website
$ python passwordmrg.py list
www.facebook.com test pass123
www.gmail.com test newpass
 




No comments:

Post a Comment