|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 |
|
5 from Tkinter import * |
|
6 from tkSimpleDialog import * |
|
7 import sys,time,random,string,MySQLdb |
|
8 |
|
9 # Configuració |
|
10 legi_el = 'mysql' |
|
11 |
|
12 # Conf - mysql |
|
13 database = 'lingvigilo' |
|
14 dbuser = 'lingvigilouser' |
|
15 dbpass = 'carrinclo' |
|
16 dbtable_vortoj = 'vortoj_ru_ca' |
|
17 dbtable_frazoj = 'frazoj_ru_ca' |
|
18 |
|
19 # Conf - fitxer |
|
20 dbasefile = 'vortoj-ru-ca.txt' |
|
21 frazodosiero = 'frazoj.txt' |
|
22 |
|
23 # Prendre les paraules d'una base de dades MySQL |
|
24 def getMySQL(): |
|
25 db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass, |
|
26 db=database) |
|
27 cursor = db.cursor() |
|
28 cursor.execute("SELECT * FROM "+dbtable_vortoj) |
|
29 res_vortoj = cursor.fetchall() |
|
30 res_vortoj_unicode = [] |
|
31 for vorto in res_vortoj: |
|
32 vorto_vorto = unicode(vorto[1],'utf8') |
|
33 vorto_tipo = unicode(vorto[2],'utf8') |
|
34 vorto_traduko = unicode(vorto[3],'utf8') |
|
35 |
|
36 # Parà metres opcionals |
|
37 if vorto[4] == None: |
|
38 vorto_param1 = '' |
|
39 else: |
|
40 vorto_param1 = unicode(vorto[4],'utf8') |
|
41 |
|
42 if vorto[5] == None: |
|
43 vorto_param2 = '' |
|
44 else: |
|
45 vorto_param2 = unicode(vorto[5],'utf8') |
|
46 |
|
47 res_vortoj_unicode.append( |
|
48 (vorto_vorto, vorto_tipo, vorto_traduko, |
|
49 vorto_param1, vorto_param2)) |
|
50 db.close() |
|
51 return res_vortoj_unicode |
|
52 |
|
53 # Prendre les paraules d'un fitxer |
|
54 def getFile(): |
|
55 dbase = open(dbasefile,"r") |
|
56 |
|
57 lines = dbase.readlines() |
|
58 dbase.close() |
|
59 |
|
60 res_vortoj = [] |
|
61 for line in lines: |
|
62 line = line.rstrip('\n') |
|
63 vortoj = string.split(unicode(line.rstrip('\n'),'utf8'),'\t') |
|
64 res_vortoj.append(vortoj) |
|
65 |
|
66 return res_vortoj |
|
67 |
|
68 if legi_el == 'file': |
|
69 print "Legante vortojn el dosiero..." |
|
70 vortoj = getFile() |
|
71 else: |
|
72 print "Legante vortojn el MySQLa datumbazo..." |
|
73 vortoj = getMySQL() |
|
74 |
|
75 print "Legitaj", len(vortoj), "vortoj." |
|
76 |
|
77 def AldoniFrazonDosieren(vorto,teksto): |
|
78 outfile = open(frazodosiero,"a") |
|
79 |
|
80 lineout = vorto + "\t" + teksto +"\n" |
|
81 outfile.write(lineout.encode('utf8')) |
|
82 |
|
83 outfile.close() |
|
84 |
|
85 def AldoniFrazonDatumbazen(vorto,teksto): |
|
86 db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass, |
|
87 db=database) |
|
88 cursor = db.cursor() |
|
89 print ("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)" |
|
90 " VALUES('%s', '%s');" % (vorto, teksto)) |
|
91 cursor.execute("INSERT INTO "+dbtable_frazoj+" (vorto, frazo)"+ |
|
92 " VALUES('%s', '%s');" % (vorto.encode('utf8'), |
|
93 teksto.encode('utf8'))) |
|
94 db.close() |
|
95 |
|
96 |
|
97 def LoopFunction(): |
|
98 index = random.randint(0, len(vortoj)-1) |
|
99 vorto = vortoj[index][0] |
|
100 teksto = askstring("Frazilon", "Skribu frazon por vorto '%s'" % vorto) |
|
101 if teksto == None: |
|
102 print 'Fi del programa' |
|
103 sys.exit() |
|
104 AldoniFrazonDatumbazen(vorto, teksto) |
|
105 root.after(1*1000,LoopFunction) |
|
106 |
|
107 |
|
108 # Main program |
|
109 root=Tk() |
|
110 root.after(10,LoopFunction) |
|
111 Message(root, text='Programo por lerni lingvon').pack(); |
|
112 root.mainloop() |