Importación inicial al árbol
This commit is contained in:
commit
50b90e5e2a
2 changed files with 234 additions and 0 deletions
119
jsoninspector.glade
Normal file
119
jsoninspector.glade
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?xml version="1.0"?>
|
||||
<interface>
|
||||
<requires lib="gtk+" version="2.16"/>
|
||||
<!-- interface-naming-policy project-wide -->
|
||||
<object class="GtkTreeStore" id="treestore1"/>
|
||||
<object class="GtkWindow" id="MainWindow">
|
||||
<property name="width_request">400</property>
|
||||
<property name="height_request">400</property>
|
||||
<property name="title" translatable="yes">JSON Inspector</property>
|
||||
<property name="window_position">center</property>
|
||||
<signal name="delete_event" handler="onMainWindowDelete"/>
|
||||
<child>
|
||||
<object class="GtkVBox" id="vbox1">
|
||||
<property name="visible">True</property>
|
||||
<child>
|
||||
<object class="GtkMenuBar" id="menubar1">
|
||||
<property name="visible">True</property>
|
||||
<child>
|
||||
<object class="GtkMenuItem" id="menuitem1">
|
||||
<property name="visible">True</property>
|
||||
<property name="label" translatable="yes">_Archivo</property>
|
||||
<property name="use_underline">True</property>
|
||||
<child type="submenu">
|
||||
<object class="GtkMenu" id="menu1">
|
||||
<property name="visible">True</property>
|
||||
<child>
|
||||
<object class="GtkImageMenuItem" id="imagemenuitem2">
|
||||
<property name="label">gtk-open</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="use_underline">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<signal name="activate" handler="onOpenMenuClicked"/>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
|
||||
<property name="visible">True</property>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkImageMenuItem" id="imagemenuitem5">
|
||||
<property name="label">gtk-quit</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="use_underline">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
<signal name="activate" handler="onExitMenuClicked"/>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkMenuItem" id="menuitem4">
|
||||
<property name="visible">True</property>
|
||||
<property name="label" translatable="yes">Ay_uda</property>
|
||||
<property name="use_underline">True</property>
|
||||
<child type="submenu">
|
||||
<object class="GtkMenu" id="menu3">
|
||||
<property name="visible">True</property>
|
||||
<child>
|
||||
<object class="GtkImageMenuItem" id="imagemenuitem10">
|
||||
<property name="label">gtk-about</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="use_underline">True</property>
|
||||
<property name="use_stock">True</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkTreeView" id="treeview1">
|
||||
<property name="width_request">300</property>
|
||||
<property name="height_request">300</property>
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="model">treestore1</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">1</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkStatusbar" id="statusbar1">
|
||||
<property name="visible">True</property>
|
||||
<property name="spacing">2</property>
|
||||
<child>
|
||||
<object class="GtkLabel" id="StatusLabel">
|
||||
<property name="visible">True</property>
|
||||
<property name="label" translatable="yes">No hay JSON cargado</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="position">0</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="position">2</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<object class="GtkAdjustment" id="adjustment1">
|
||||
<property name="upper">100</property>
|
||||
<property name="step_increment">1</property>
|
||||
<property name="page_increment">10</property>
|
||||
<property name="page_size">10</property>
|
||||
</object>
|
||||
</interface>
|
115
jsoninspector.py
Normal file
115
jsoninspector.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue