tkinter/lingvigilo.py
author viric <viriketo@gmail.com>
Mon, 03 Sep 2012 23:35:31 +0200
branchgo
changeset 7 d5750f4e6e8c
parent 0 6371497b4e53
permissions -rwxr-xr-x
Adding a first attempt at go code

#!/usr/bin/python
# -*- coding: utf-8 -*-


from Tkinter import *
from tkSimpleDialog import *
import sys,time,random,string,MySQLdb

# Configuració
legi_el = 'mysql'

# Conf - mysql
database = 'lingvigilo'
dbuser = 'lingvigilouser'
dbpass = 'carrinclo'
dbtable_vortoj = 'vortoj_ru_ca'
dbtable_frazoj = 'frazoj_ru_ca'

# Conf - fitxer
dbasefile = 'vortoj-ru-ca.txt'
frazodosiero = 'frazoj.txt'

# Prendre les paraules d'una base de dades MySQL
def getMySQL():
	db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,
		db=database)
	cursor = db.cursor()
	cursor.execute("SELECT * FROM "+dbtable_vortoj)
	res_vortoj = cursor.fetchall()
	res_vortoj_unicode = []
	for vorto in res_vortoj:
		vorto_vorto = unicode(vorto[1],'utf8')
		vorto_tipo = unicode(vorto[2],'utf8')
		vorto_traduko = unicode(vorto[3],'utf8')
		
		# Paràmetres opcionals
		if vorto[4] == None:
			vorto_param1 = ''
		else:
			vorto_param1 = unicode(vorto[4],'utf8')

		if vorto[5] == None:
			vorto_param2 = ''
		else:
			vorto_param2 = unicode(vorto[5],'utf8')

		res_vortoj_unicode.append(
			(vorto_vorto, vorto_tipo, vorto_traduko,
			vorto_param1, vorto_param2))
	db.close()
	return res_vortoj_unicode

# Prendre les paraules d'un fitxer
def getFile():
	dbase = open(dbasefile,"r")

	lines = dbase.readlines()
	dbase.close()

	res_vortoj = []
	for line in lines:
		line = line.rstrip('\n')
		vortoj = string.split(unicode(line.rstrip('\n'),'utf8'),'\t')
		res_vortoj.append(vortoj)

	return res_vortoj

if legi_el == 'file':
	print "Legante vortojn el dosiero..."
	vortoj = getFile()
else:
	print "Legante vortojn el MySQLa datumbazo..."
	vortoj = getMySQL()

print "Legitaj", len(vortoj), "vortoj."

def AldoniFrazonDosieren(vorto,teksto):
	outfile = open(frazodosiero,"a")

	lineout = vorto + "\t" + teksto +"\n"
	outfile.write(lineout.encode('utf8'))
	
	outfile.close()

def AldoniFrazonDatumbazen(vorto,teksto):
	db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,
		db=database)
	cursor = db.cursor()
	print ("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)"
		" VALUES('%s', '%s');" % (vorto, teksto))
	cursor.execute("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)"+
		" VALUES('%s', '%s');" % (vorto.encode('utf8'),
		teksto.encode('utf8')))
	db.close()


def LoopFunction():
	index = random.randint(0, len(vortoj)-1)
	vorto = vortoj[index][0]
	teksto = askstring("Frazilon", "Skribu frazon por vorto '%s'" % vorto)
	if teksto == None:
		print 'Fi del programa'
		sys.exit()
	AldoniFrazonDatumbazen(vorto, teksto)
	root.after(1*1000,LoopFunction)


# Main program
root=Tk()
root.after(10,LoopFunction)
Message(root, text='Programo por lerni lingvon').pack();
root.mainloop()