commit 50b90e5e2a23c7fe6a20635824c59fd82bbde956 Author: José Carlos Cuevas Date: Tue May 25 17:57:03 2010 +0200 Importación inicial al árbol diff --git a/jsoninspector.glade b/jsoninspector.glade new file mode 100644 index 0000000..bb6743d --- /dev/null +++ b/jsoninspector.glade @@ -0,0 +1,119 @@ + + + + + + + 400 + 400 + JSON Inspector + center + + + + True + + + True + + + True + _Archivo + True + + + True + + + gtk-open + True + True + True + + + + + + True + + + + + gtk-quit + True + True + True + + + + + + + + + + True + Ay_uda + True + + + True + + + gtk-about + True + True + True + + + + + + + + + False + 0 + + + + + 300 + 300 + True + True + treestore1 + + + 1 + + + + + True + 2 + + + True + No hay JSON cargado + + + 0 + + + + + False + 2 + + + + + + + 100 + 1 + 10 + 10 + + diff --git a/jsoninspector.py b/jsoninspector.py new file mode 100644 index 0000000..17dba7d --- /dev/null +++ b/jsoninspector.py @@ -0,0 +1,115 @@ +#!/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) + + self.window = self.builder.get_object("MainWindow") + self.window.show_all() + + # We get the logic and help object + 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) + + else: + + label.set_text("No hay JSON cargado") + + # Nos deshacemos del dialogo + chooser.destroy() + + 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): + """ + Holds the some external logic of the app and persisten variables all across + the execution + """ + def __init__(self): + self.json = None + + def loadjson(self, filename): + """ + Loads a JSON file string and creates the respective object + """ + 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 + + +# Ejecucion del programa principal + +if __name__ == "__main__": + + logicObject = LogicObject() + mainWindow = MainWindowMethods(logicObject) + gtk.main()