Python Forum
password repository script using csv for site, pw repository
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
password repository script using csv for site, pw repository
#1
This is my first (self-contained) python script with (what I feel is) a practical use. It basically (1.) allows you to look up a password (via a csv file) for a site.

For example your account on foo.com has the pw 'bar'. You enter 'foo' into the script and it outputs
Output:
password for foo is bar
. (2.) If there is no entry for foo.com, the script provides an option to create an entry. Nothing extravagant.

My confidence is a little shot. Although I feel the script should work...I'm scared to test it... Cry

I've included an assortment of comments in the script. I also worry about passing parameters in nested functions?

#!usr/bin/env python3

#pwtext0.py

import os, csv, sys
from functools import partial

#set up input 'filter'
def asker(question,type,errmsg):
	while True:
		user_input = input(question)
		if user_input != '':
			try:
				user_input == type(user_input)
			except ValueError:
				print(f'{user_input} is not {type}')
				print(f'{user_input} {errmsg}.')
				continue
			else:
				print(f'{user_input} is acceptable')
				return user_input
		else:
			print(f'{question} cannot be blank.')
			continue

def prime():
	using = 0#simple counter
	while True:
		os.chdir('/home/me/Desktop')
		print(os.getcwd())
		#repository just to 'smooth' user experience
		#gain experience in design really...not vital.
		asked = partial(asker,type=str,errmsg='Please enter a string.')
		repository=asked('enter \'start\' to access repository')
		repository.strip().lower()
		#check if user want to proceed
		if repository == 'start' or repository == 's':
			using += 1
			#user elects to proceed
			#run search for user's site password with get_pw()
			get_pw(repository)
			
			repository = asked('\'start\' to go again...anything else to leave...')
			if repository == 'start' or repository == 's':
				continue
		else:
			#user enters something other than 'start'/'s'
			#ask to confirm that they want to quit
			repository =asked('are you sure you want to quit?\n\'yes\' to exit, \'no\' to start')
			repository.strip().lower()
			#user changes their mind. Wants to looks up password afterall
			if repository == 'no' or repository =='n':
				get_pw(repository)
			#user wants to quit.
			else:
				print(f'you used system {using} times(s)')
				sys.sleep(2)
				sys.exit()

def get_pw(foo):
	#ask for site address
	asked = partial(asker,type=str,errmsg='Please enter a string.')
	site_name = asked('Please enter site name...')
	#strip whitespace, lowercase for uniform purposes
	site_name.strip().lower()
	
	print('Did you include an tld ext in the site name? .com? .edu? etc?')
	ext_remover = asked('enter \'yes\' or \'no\'')
	#since most ppl won't follow directions and will include tld exts
	ext_remover.strip().lower()
	if ext_remover == 'yes' or ext_remover == 'y':
		remove_tld(site_name)
	else:
		print('good job...;) ')
	
	filen = 'pwkpr.csv'
	
	#create lists to look up info in csv file.
	#a regrex workaround
	sn_list = []
	pw_list = []
	with open(filen, 'a+',newline='') as f:
		file_read = csv.reader(f)
		for line in file_read:
			sname = line[0]#get site_names
			pword = line[1]#get pws
			sn_list.append(sname)
			pw_list.append(pword)
		locate_site_row = sn_list.index(site_name)
	
	#in case an entry does not exist, use site_name value to create entry in add_entry()
	#return site_name value not needed because add_entry() is inside get_pw()
		
		'''
		NEXT IF-STATEMENT is VITAL
		if there is no entry/record for the site
		then the variable presumably should be blank
		(I'm probably wrong here...too scared to test)
		MAKE RECORD using add_entry()
		'''
		if locate_site_row =='':
			add_entry(site_name)

		#if record exists, just look it up
		else:
			locate_pw = pw_list[locate_site_row]
			print(f'password for {site_name} is {locate_pw}')

#just practice with splitext()...works
def remove_tld(site):
	name, tld = os.path.splitext(site)
	print(f'site is {name}.')
	return name

#func to create an entry if none exists. no parameter needed?
def add_entry():
	pw = asked('Please enter password.')
	pw = pw.strip()
	#delimiter should be necessary to add comma
	add_line = csv.writer(f, delimiter=',')
	#site_name value from get_pw()
	add_line.writerow(site_name, pw)

#just a best practices?
#insure that file is run standalone
if __name__=='__main__':
	print('running as main')
	prime()
else:
	print(f'{__name__ } won\'t be run as it not the main file.')
Here is error:
Error:
Traceback (most recent call last): File "pwtext0.py", line 129, in <module> prime() File "pwtext0.py", line 41, in prime get_pw(repository) File "pwtext0.py", line 90, in get_pw locate_site_row = sn_list.index(site_name) ValueError: 'foo.com' is not in list
I feel it should work...feedback please (apologies if I'm violating any board rules...not aware that I am)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,727 May-18-2021, 06:42 PM
Last Post: Skaperen
  How to find the list of dependencies not present in the remote artifactory repository csplrj 6 4,635 Mar-30-2018, 03:43 PM
Last Post: csplrj
  Help Building Repository shakeandbake5555 2 4,369 May-23-2017, 10:09 PM
Last Post: shakeandbake5555

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020