Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Archivos_y_Directorios.py
blob: c5bf2132af4f4d6a4df7207882b77528a394a101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   Archivos_y_Directorios.py por:
#   Flavio Danesse <fdanesse@gmail.com>
#   CeibalJAM! - Uruguay
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import sqlite3
import urllib
import string
import time

class Archivos_y_Directorios():
    def __init__(self, DIRECTORIO_DATOS):
        self.directorio= DIRECTORIO_DATOS
        self.set_directorio()
        
    def set_directorio(self):
        # Crear Directorio para la base de datos
        if not os.path.exists(self.directorio):
            os.mkdir(self.directorio)
            
    def get_juegos(self):
        return (self.directorio, os.listdir(self.directorio))
    
    def CrearBasededatos (self):
        # Crea la base de datos inicial
        numero_de_juegos = time.time()
        nombre = "Cucarasims%s.db" % (numero_de_juegos)
        nombre_de_base = os.path.join(self.directorio, nombre)
        try:
            coneccion = sqlite3.connect(nombre_de_base)
            basededatos = coneccion.cursor()
            basededatos.execute("""create table Cucas (id INTEGER PRIMARY KEY AUTOINCREMENT,
                sexo text, anios text, dias text, horas text, nutricion text, hidratacion text)""")
            coneccion.commit()
            basededatos.execute("""create table Juego (id INTEGER PRIMARY KEY AUTOINCREMENT,
                anios text, dias text, horas text, puntos text)""")
            coneccion.commit()
            basededatos.execute("""create table Ootecas (id INTEGER PRIMARY KEY AUTOINCREMENT,
                dias text, horas text, huevos text)""")
            coneccion.commit()
            basededatos.close()
            coneccion.close()
            os.chmod(os.path.join(self.directorio, nombre), 0666)
            return nombre_de_base
        except Exception, e:
            return False
        
    def RE_CrearBasededatos (self, base):
    # Crea la base de datos inicial
        nombre_de_base = os.path.join(self.directorio, base)
        try:
            coneccion = sqlite3.connect(nombre_de_base)
            basededatos = coneccion.cursor()
            basededatos.execute("""create table Cucas (id INTEGER PRIMARY KEY AUTOINCREMENT,
                sexo text, anios text, dias text, horas text, nutricion text, hidratacion text)""")
            coneccion.commit()
            basededatos.execute("""create table Juego (id INTEGER PRIMARY KEY AUTOINCREMENT,
                anios text, dias text, horas text, puntos text)""")
            coneccion.commit()
            basededatos.execute("""create table Ootecas (id INTEGER PRIMARY KEY AUTOINCREMENT,
                dias text, horas text, huevos text)""")
            coneccion.commit()
            basededatos.close()
            coneccion.close()
            os.chmod(os.path.join(self.directorio, nombre), 0666)
            return nombre_de_base
        except Exception, e:
            return False
        
    def guardar(self, base=None, datos=None):
        #print "GUARDANDO DATOS"
        datos_de_juego = datos[0] # [anios, dias, horas, puntos]
        datos_de_cucas = datos[1] # [sexo, anios, dias, horas, hambre, sed]
        datos_de_ootecas = datos[2] # [dias, horas, huevos]
        self.borrar_tabla(base)
        self.RE_CrearBasededatos(base)
        # Guardando Datos de tiempo de juego en general
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            try:
                coneccion.text_factory = str #(no funciona en sugar de uruguay)
            except:
                pass
            anios, dias, horas, puntos = datos_de_juego
            item = [0, anios, dias, horas, puntos]
            basededatos.execute ("insert into %s values (?,?,?,?,?)" % ("Juego"), item)
            coneccion.commit()
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Llenar la Tabla Juego"
            
        # Guardando Datos de Cucas
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            try:
                coneccion.text_factory = str #(no funciona en sugar de uruguay)
            except:
                pass
            contador = 0
            for x in range(0, len(datos_de_cucas)):
                datos_bicho = datos_de_cucas[x] # [sexo, anios, dias, horas, hambre, sed]
                sexo = datos_bicho[0]
                anios = datos_bicho[1]
                dias = datos_bicho[2]
                horas = datos_bicho[3]
                hambre = datos_bicho[4]
                sed = datos_bicho[5]
                item = [contador, sexo, anios, dias, horas, hambre, sed]
                basededatos.execute ("insert into %s values (?,?,?,?,?,?,?)" % ("Cucas"), item)
                coneccion.commit()
                contador += 1
                #print "Datos Guardados en Cucas:", item
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Llenar la Tabla Cucas"
            
        # Guardando Datos de Ootecas
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            try:
                coneccion.text_factory = str #(no funciona en sugar de uruguay)
            except:
                pass
            contador = 0
            for x in range(0, len(datos_de_ootecas)):
                datos_ootecas = datos_de_ootecas[x] # [dias, horas, huevos]
                dias = datos_ootecas[0]
                horas = datos_ootecas[1]
                huevos = datos_ootecas[2]
                item = [contador, dias, horas, huevos]
                basededatos.execute ("insert into %s values (?,?,?,?)" % ("Ootecas"), item)
                coneccion.commit()
                contador += 1
                #print "Datos Guardados en Ootecas:", item
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Llenar la Tabla Ootecas"
            
        #print "DATOS GUARDADOS"
        
    def Leer_Base_de_Datos(self, nombre):
        base = os.path.join(self.directorio, nombre)
        datos_juego = None
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            basededatos.execute("select * from %s" % ("Juego"))
            for dato in basededatos:
                datos_juego = dato
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Carga Tabla Juego"
        #print "Leyendo Base", datos_juego
        
            cucas = []
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            basededatos.execute("select * from %s" % ("Cucas"))
            for item in basededatos:
                cucas.append(item)
                #print item
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Carga Tabla Cucas"
            
            Ootecas = []
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            basededatos.execute("select * from %s" % ("Ootecas"))
            for item in basededatos:
                Ootecas.append(item)
                #print item
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Carga Tabla Ootecas"
            
        #print "DATOS:\n", (base, datos_juego, cucas, Ootecas), "\nFIN DATOS:"
        return (base, datos_juego, cucas, Ootecas)
    
        #print "Base Leida"
        
    def borrar_tabla(self, nombre):
        #print "\n\n BORRRAR:", nombre
        try:
            nombre_de_base= os.path.join(self.directorio, nombre)
            os.remove(nombre_de_base)
            #print "Borrar: ", nombre_de_base
        except Exception, e:
            print "Ha Ocurrido un Error al Intentar Borarr un Juego"
            
    def verifica(self, base_abrir=None):
        base = os.path.join(self.directorio, base_abrir)
        datos_juego = None
        try:
            coneccion = sqlite3.connect(base)
            basededatos = coneccion.cursor()
            basededatos.execute("select * from %s" % ("Juego"))
            for dato in basededatos:
                datos_juego = dato
            basededatos.close()
            coneccion.close()
        except Exception, e:
            print "Ha Ocurrido un Error al verifica Tabla Juego"
            
        if not datos_juego:
            os.remove(base)
            #print "base eliminada"