Skip to content

Instantly share code, notes, and snippets.

Created April 27, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/11355513 to your computer and use it in GitHub Desktop.
Save anonymous/11355513 to your computer and use it in GitHub Desktop.
tbook.py - telephone-book-like simple db-tool
#!/usr/bin/python
#
# 2012-10-08
#
version = "0.1.6"
import sys, os
data_file="./tbook.dat"
editor="jmacs"
delimit = "[::]"
def read_in():
tbook = {}
try:
f = open(data_file, "r").readlines()
except:
f = open(data_file, "w")
f.write("")
f.close()
return(tbook)
for entry in sorted(f):
try:
name, nr = entry.split(delimit)
tbook[name.strip()] = nr.strip()
except:
continue
return(tbook)
def write_tbook(tbook):
f = open(data_file, "w")
for entry in sorted(tbook):
f.write("%-20s %s %s \n" % (entry, delimit, tbook[entry]))
f.close()
print "> %s entries written to %s " % (len(tbook), data_file)
def list_entries(tbook):
ct = 0
print "\n\n -----------------------+-------------------------"
for entry in sorted(tbook):
ct += 1
print " %-20s | %s " % (entry, tbook[entry])
if ct > 2:
ct = 0
print " ----------------------+-------------------------"
print "\n\n\n-------------------------------------------------------\n"
thelp = """
ShortCut_help
n: -> new entry
s: -> search
e: -> edit tbook-file
h: -> this help
b: -> bash
q: -> quit
"""
txhelp = """
tbook.py - telephone-book-like db-tool
USAGE:
tbook.py execute tbook.py,
creates tbook.dat in current dir
tbook.py /path/to/tbook.dat
executes tbook.py with given file;
if the file doesnt exists, it will be created
tbook.py -h help
OPTIONS
-h help
"""
print """
_ _ _
( )_ ( ) ( )
| ,_) | |_ _ _ | |/')
| | | '_`\ /'_`\ /'_`\ | , <
| |_ _ | |_) )( (_) )( (_) )| |\`\
`\__)(_)(_,__/'`\___/'`\___/'(_) (_)
v: %s
""" % version
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg == "-h":
print txhelp
sys.exit()
else:
data_file = arg
tbook = read_in()
print "> %s entries available" % len(tbook)
sel = "w"
while sel != "q":
tbook = read_in()
if sel == "n":
name = raw_input("> name : ")
tel = raw_input("> tel : ")
tbook[name] = tel
write_tbook(tbook)
sel = "w"
continue
elif sel == "s":
srch = raw_input("> search : ")
print "\n-------------------------------------------------------\n"
for entry in tbook:
if entry.find(srch) > -1:
print " %-20s | %s " % (entry, tbook[entry])
print "\n-------------------------------------------------------\n"
elif sel == "h":
print thelp
sel = "w"
elif sel == "e":
os.system("%s %s" % (editor, data_file))
else:
list_entries(tbook)
print "> n:s:e:b:h:q"
sel = raw_input("> ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment