from os import path
import pickle

outdb = {}
dbpath = path.expanduser("~tutimura/public_html/cgi-bin/passwd/name.txt")

with open(dbpath, "r") as f:
	for line in f.readlines():
		if not line: continue
		no_, reading, name = line.split("\t")
		if name.count(" ") == 2:
			first_n, family_n, note = name.split(" ")
			name = first_n + " " + family_n
		name = name.strip()
		outdb[no_] = {"name": name, "reading": reading}

maildb = {}
mailpath = path.expanduser("~tutimura/public_html/cgi-bin/passwd/mail.txt")
with open(mailpath, "r") as f:
	for line in f.readlines():
		if not line: continue
		line = line.strip()
		tpl = line.split("\t")
		if len(tpl) == 2:
			no_, mailadr = tpl
			dep = note = ""
		elif len(tpl) == 3:
			no_, mailadr, dep = tpl
			note = ""
		elif len(tpl) == 4:
			no_, mailadr, dep, note = tpl
		id_ = mailadr.split("@")[0]
		if no_ in outdb:
			name = outdb[no_]["name"]
			reading = outdb[no_]["reading"]
		else:
			name = ""
			reading = ""
		maildb[id_] = { "name": name, "reading": reading, 
			"no": no_, "dep": dep, "note": note}

with open("name.pickle", "wb") as out:
	pickle.dump(maildb, out)
