2010-05-25 17:57:03 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
import pygtk
|
|
|
|
|
|
|
|
try:
|
|
|
|
pygtk.require("2.0")
|
|
|
|
except:
|
|
|
|
print "Versión de PyGTK incorrecta!\n"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
import gtk
|
|
|
|
|
|
|
|
class MainWindowMethods(object):
|
|
|
|
"""
|
|
|
|
Clase que contiene los métodos de la ventana principal
|
|
|
|
"""
|
|
|
|
def __init__(self, logic):
|
|
|
|
"""
|
|
|
|
Carga y conecta el XML de gtkBuilder, además de mostrar la ventana
|
|
|
|
principal
|
|
|
|
"""
|
|
|
|
self.builder = gtk.Builder()
|
|
|
|
self.builder.add_from_file("jsoninspector.glade")
|
|
|
|
self.builder.connect_signals(self)
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Prepara los renderizados de columna y las asigna a los valores
|
2010-05-26 13:58:33 +02:00
|
|
|
cell = gtk.CellRendererText()
|
|
|
|
columns = self.builder.get_object("treeviewcolumn1")
|
|
|
|
columns.pack_start(cell)
|
|
|
|
columns.add_attribute(cell, 'text', 0)
|
|
|
|
treeview = self.builder.get_object("treeview1")
|
2010-05-26 16:34:52 +02:00
|
|
|
# Esta columna es la que tiene los nodos de apertura y cierre
|
2010-05-26 13:58:33 +02:00
|
|
|
treeview.set_expander_column(columns)
|
2010-05-26 16:34:52 +02:00
|
|
|
|
2010-05-26 13:58:33 +02:00
|
|
|
cell = gtk.CellRendererText()
|
|
|
|
columns = self.builder.get_object("treeviewcolumn2")
|
|
|
|
columns.pack_start(cell)
|
|
|
|
columns.add_attribute(cell, 'text', 1)
|
|
|
|
|
2010-05-26 14:36:44 +02:00
|
|
|
cell = gtk.CellRendererText()
|
|
|
|
columns = self.builder.get_object("treeviewcolumn3")
|
|
|
|
columns.pack_start(cell)
|
|
|
|
columns.add_attribute(cell, 'text', 2)
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Obtenemos la ventana y la mostramos completamente
|
2010-05-25 17:57:03 +02:00
|
|
|
self.window = self.builder.get_object("MainWindow")
|
|
|
|
self.window.show_all()
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Obtenemos un enlace al objeto de lógica de aplicación
|
2010-05-25 17:57:03 +02:00
|
|
|
self.logicObj = logic
|
|
|
|
|
|
|
|
def onOpenMenuClicked(self, event):
|
|
|
|
"""
|
|
|
|
El usuario ha presionado Abrir en el menu
|
|
|
|
"""
|
|
|
|
# Creamos el dialogo de abrir fichero
|
|
|
|
chooser = gtk.FileChooserDialog(title=None,
|
|
|
|
action=gtk.FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
buttons=(gtk.STOCK_CANCEL,
|
|
|
|
gtk.RESPONSE_CANCEL,
|
|
|
|
gtk.STOCK_OPEN,
|
|
|
|
gtk.RESPONSE_OK))
|
|
|
|
chooser.set_default_response(gtk.RESPONSE_OK)
|
|
|
|
|
|
|
|
# Lo lanzamos
|
|
|
|
response = chooser.run()
|
|
|
|
|
|
|
|
# Si hemos seleccionado un fichero, lo cargamos
|
|
|
|
if response == gtk.RESPONSE_OK:
|
|
|
|
filename = chooser.get_filename()
|
|
|
|
label = self.builder.get_object("StatusLabel")
|
|
|
|
|
|
|
|
# Según todo haya ido, actualizamos la barra de estado
|
|
|
|
if self.logicObj.loadjson(filename):
|
|
|
|
|
|
|
|
label.set_text(filename)
|
2010-05-26 13:58:33 +02:00
|
|
|
treeStore = self.builder.get_object("treestore1")
|
|
|
|
treeStore.clear()
|
|
|
|
self.logicObj.loadTree(treeStore)
|
|
|
|
|
2010-05-25 17:57:03 +02:00
|
|
|
else:
|
|
|
|
|
|
|
|
label.set_text("No hay JSON cargado")
|
|
|
|
|
|
|
|
# Nos deshacemos del dialogo
|
|
|
|
chooser.destroy()
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
def onCopyJSONClicked(self, widget):
|
|
|
|
"""
|
|
|
|
Se ha pulsado Copiar JSON
|
|
|
|
"""
|
|
|
|
# Mostrar la ventana de texto
|
|
|
|
textWindow = self.builder.get_object("TextWindow")
|
|
|
|
textWindow.show_all()
|
|
|
|
|
|
|
|
def onCopyJSONDelete(self, widget, event):
|
|
|
|
"""
|
|
|
|
Se ha dado a cerrar la ventana de copiar texto
|
|
|
|
"""
|
|
|
|
textView = self.builder.get_object("textview1")
|
|
|
|
textView.hide()
|
|
|
|
|
|
|
|
def onCopyJSONAcceptClicked(self, widget):
|
|
|
|
"""
|
|
|
|
Se ha aceptado el código
|
|
|
|
"""
|
|
|
|
textView = self.builder.get_objects("textview1")
|
|
|
|
jsonBuffer = textView.get_buffer()
|
|
|
|
jsonText = jsonBuffer.get_text()
|
|
|
|
|
|
|
|
textWindow = self.builder.get_object("textview1")
|
|
|
|
textWindow.hide()
|
|
|
|
treestore = self.builder.get_object("treestore1")
|
|
|
|
treestore.clear()
|
|
|
|
if self.logicObj.loadJSONText(jsonText):
|
|
|
|
self.logicObj.loadTree(treestore)
|
|
|
|
|
|
|
|
def onCopyJSONCancelClicked(self, widget):
|
|
|
|
"""
|
|
|
|
Se ha cancelado
|
|
|
|
"""
|
|
|
|
textView = self.builder.get_object("textview1")
|
|
|
|
textView.hide()
|
|
|
|
|
2010-05-25 17:57:03 +02:00
|
|
|
def onExitMenuClicked(self, widget):
|
|
|
|
"""
|
|
|
|
Se ha pulsado salir
|
|
|
|
"""
|
|
|
|
gtk.main_quit()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def onMainWindowDelete(self, widget, event):
|
|
|
|
"""
|
|
|
|
Se ha destruido o mandado cerrar la ventana principal
|
|
|
|
"""
|
|
|
|
gtk.main_quit()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
class LogicObject(object):
|
|
|
|
"""
|
2010-05-26 16:34:52 +02:00
|
|
|
Esta clase define un objeto que mantiene la lógica interna del programa y
|
|
|
|
nos permite un acceso a las variables principales
|
2010-05-25 17:57:03 +02:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.json = None
|
|
|
|
|
|
|
|
def loadjson(self, filename):
|
|
|
|
"""
|
2010-05-26 16:34:52 +02:00
|
|
|
Carga un fichero JSON y crea el objeto respectivo
|
2010-05-25 17:57:03 +02:00
|
|
|
"""
|
|
|
|
f = open(filename, 'r')
|
|
|
|
try:
|
|
|
|
self.json = json.loads(f.read())
|
|
|
|
except ValueError:
|
|
|
|
print "JSON no válido!\n"
|
|
|
|
self.json = None
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
f.close()
|
|
|
|
return True
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
def loadJSONText(self, text):
|
|
|
|
"""
|
|
|
|
Carga un texto JSON
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.json = json.loads(text)
|
|
|
|
except ValueError:
|
|
|
|
print "JSON no válido"
|
|
|
|
self.json = None
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2010-05-26 13:58:33 +02:00
|
|
|
def loadTree(self, treestore):
|
|
|
|
"""
|
2010-05-26 16:34:52 +02:00
|
|
|
Carga el JSON en el tree store para mostrarlo
|
2010-05-26 13:58:33 +02:00
|
|
|
"""
|
|
|
|
for key_val in self.json.keys():
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Si el elemento contiene un diccionario
|
2010-05-26 13:58:33 +02:00
|
|
|
if type(self.json[key_val]) is dict:
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Añadimos el nodo, y obtenemos la referencia
|
2010-05-26 14:36:44 +02:00
|
|
|
parent_node = treestore.append(None, [str(key_val), "", ""])
|
2010-05-26 16:34:52 +02:00
|
|
|
# De manera recursiva, entramos en el diccionario y obtenemos
|
|
|
|
# los nodos, añadidos como hijos de este
|
2010-05-26 13:58:33 +02:00
|
|
|
self._loadTreeRec(treestore, self.json[key_val], parent_node)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Tenemos un nodo hoja, obtenemos el valor y el tipo y lo
|
|
|
|
# añadimos
|
2010-05-26 14:36:44 +02:00
|
|
|
treestore.append(None, [str(key_val),
|
|
|
|
str(self.json[key_val]),
|
|
|
|
str(type(self.json[key_val]))])
|
2010-05-26 13:58:33 +02:00
|
|
|
|
|
|
|
def _loadTreeRec(self, treestore, elems, parent_node):
|
|
|
|
"""
|
2010-05-26 16:34:52 +02:00
|
|
|
Adición recursiva de elementos al árbol
|
2010-05-26 13:58:33 +02:00
|
|
|
"""
|
|
|
|
for key_val in elems.keys():
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Si el elemento contiene un diccionario
|
2010-05-26 13:58:33 +02:00
|
|
|
if type(elems[key_val]) is dict:
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Añadimos el nodo, y obtenemos la referencia
|
2010-05-26 13:58:33 +02:00
|
|
|
new_parent_node = treestore.append(parent_node,
|
|
|
|
[str(key_val),
|
2010-05-26 14:36:44 +02:00
|
|
|
"", ""])
|
2010-05-26 16:34:52 +02:00
|
|
|
# De manera recursiva, entramos en el diccionario y obtenemos
|
|
|
|
# los nodos, añadidos como hijos de este
|
2010-05-26 13:58:33 +02:00
|
|
|
self._loadTreeRec(treestore, elems[key_val], new_parent_node)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2010-05-26 16:34:52 +02:00
|
|
|
# Tenemos un nodo hoja, obtenemos el valor y el tipo y lo
|
|
|
|
# añadimos
|
2010-05-26 14:36:44 +02:00
|
|
|
treestore.append(parent_node, [str(key_val),
|
|
|
|
str(elems[key_val]),
|
|
|
|
str(type(elems[key_val]))])
|
2010-05-26 13:58:33 +02:00
|
|
|
|
|
|
|
|
2010-05-25 17:57:03 +02:00
|
|
|
|
|
|
|
# Ejecucion del programa principal
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
logicObject = LogicObject()
|
|
|
|
mainWindow = MainWindowMethods(logicObject)
|
|
|
|
gtk.main()
|