From 50b90e5e2a23c7fe6a20635824c59fd82bbde956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Tue, 25 May 2010 17:57:03 +0200 Subject: [PATCH] =?UTF-8?q?Importaci=C3=B3n=20inicial=20al=20=C3=A1rbol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsoninspector.glade | 119 ++++++++++++++++++++++++++++++++++++++++++++ jsoninspector.py | 115 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 jsoninspector.glade create mode 100644 jsoninspector.py 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()