|
|
|
@ -1,9 +1,13 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import gi
|
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
|
from gi.repository import Gtk, Gio
|
|
|
|
|
|
|
|
|
|
import json, sys, os.path
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
# Internationalization support
|
|
|
|
|
import gettext
|
|
|
|
@ -11,7 +15,7 @@ import locale
|
|
|
|
|
|
|
|
|
|
APP = "jsoninspector"
|
|
|
|
|
|
|
|
|
|
if os.path.exists('../locale/po') and os.path.exists('../../res'):
|
|
|
|
|
if os.path.exists('../../locale/po') and os.path.exists('../../res'):
|
|
|
|
|
# We're in the development tree
|
|
|
|
|
DIR = "../../locale/po/"
|
|
|
|
|
RESOURCES = "../../res/"
|
|
|
|
@ -39,15 +43,15 @@ class MainWindowMethods(Gtk.Application):
|
|
|
|
|
Main Application object with the main window signals
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, logic):
|
|
|
|
|
Gtk.Application.__init__(self, application_id = "apps.gnome.jsoninspector",
|
|
|
|
|
flags = Gio.ApplicationFlags.FLAGS_NONE)
|
|
|
|
|
Gtk.Application.__init__(self, application_id="apps.gnome.jsoninspector",
|
|
|
|
|
flags=Gio.ApplicationFlags.FLAGS_NONE)
|
|
|
|
|
|
|
|
|
|
# We store the reference to the app logic
|
|
|
|
|
self.logicObj = logic
|
|
|
|
|
|
|
|
|
|
self.connect("activate", self.on_app_start)
|
|
|
|
|
|
|
|
|
|
def on_app_start(self, data = None):
|
|
|
|
|
def on_app_start(self, data=None):
|
|
|
|
|
"""
|
|
|
|
|
Loads the MainWindow widgets, shows it up and starts the main loop
|
|
|
|
|
"""
|
|
|
|
@ -100,14 +104,14 @@ class MainWindowMethods(Gtk.Application):
|
|
|
|
|
|
|
|
|
|
# We update the statusbar accordingly to what has happened
|
|
|
|
|
if self.logicObj.loadjson(filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
label.set_text(filename)
|
|
|
|
|
treeStore = self.builder.get_object("treestore1")
|
|
|
|
|
treeStore.clear()
|
|
|
|
|
self.logicObj.loadTree(treeStore)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
label.set_text(_("No JSON loaded."))
|
|
|
|
|
|
|
|
|
|
# We finish the dialog
|
|
|
|
@ -154,8 +158,8 @@ class MainWindowMethods(Gtk.Application):
|
|
|
|
|
treestore.clear()
|
|
|
|
|
|
|
|
|
|
if self.logicObj.loadJSONText(jsonText):
|
|
|
|
|
|
|
|
|
|
status_label = self.builder.get_object("StatusLabel")
|
|
|
|
|
|
|
|
|
|
status_label = self.builder.get_object("StatusLabel")
|
|
|
|
|
status_label.set_text(_("Loaded from the clipboard."))
|
|
|
|
|
self.logicObj.loadTree(treestore)
|
|
|
|
|
|
|
|
|
@ -186,10 +190,10 @@ class MainWindowMethods(Gtk.Application):
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def onAboutDialogClose(self, widget, event = None):
|
|
|
|
|
def onAboutDialogClose(self, widget, event=None):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def onAboutDialogDeleteEvent(self, widget, event = None):
|
|
|
|
|
def onAboutDialogDeleteEvent(self, widget, event=None):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -209,7 +213,7 @@ class LogicObject(object):
|
|
|
|
|
try:
|
|
|
|
|
self.json = json.loads(f.read())
|
|
|
|
|
except ValueError:
|
|
|
|
|
print _("Not valid JSON!\n")
|
|
|
|
|
print(_("Not valid JSON!\n"))
|
|
|
|
|
self.json = None
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
@ -225,9 +229,9 @@ class LogicObject(object):
|
|
|
|
|
try:
|
|
|
|
|
self.json = json.loads(text)
|
|
|
|
|
except ValueError:
|
|
|
|
|
print _("Not valid JSON")
|
|
|
|
|
print(_("Not valid JSON"))
|
|
|
|
|
self.json = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
@ -236,23 +240,22 @@ class LogicObject(object):
|
|
|
|
|
"""
|
|
|
|
|
Carga el JSON en el tree store para mostrarlo
|
|
|
|
|
"""
|
|
|
|
|
for key_val in self.json.keys():
|
|
|
|
|
for key_val in list(self.json.keys()):
|
|
|
|
|
self.process_node(treestore, self.json[key_val], key_val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _loadTreeRec(self, treestore, elems, parent_node):
|
|
|
|
|
"""
|
|
|
|
|
Adición recursiva de elementos al árbol
|
|
|
|
|
"""
|
|
|
|
|
for key_val in elems.keys():
|
|
|
|
|
for key_val in list(elems.keys()):
|
|
|
|
|
self.process_node(treestore, elems[key_val], key_val, parent_node)
|
|
|
|
|
|
|
|
|
|
def process_node(self, treestore, node, node_key, parent_node = None):
|
|
|
|
|
def process_node(self, treestore, node, node_key, parent_node=None):
|
|
|
|
|
# Si el elemento contiene un diccionario
|
|
|
|
|
if type(node) is dict:
|
|
|
|
|
|
|
|
|
|
# Añadimos el nodo, y obtenemos la referencia
|
|
|
|
|
new_node = treestore.append(parent_node, [unicode(node_key), u"", u""])
|
|
|
|
|
new_node = treestore.append(parent_node, [str(node_key), "", ""])
|
|
|
|
|
# De manera recursiva, entramos en el diccionario y obtenemos
|
|
|
|
|
# los nodos, añadidos como hijos de este
|
|
|
|
|
self._loadTreeRec(treestore, node, new_node)
|
|
|
|
@ -260,20 +263,20 @@ class LogicObject(object):
|
|
|
|
|
elif type(node) is list:
|
|
|
|
|
count = 0
|
|
|
|
|
# Añadimos el nodo, y obtenemos la referencia
|
|
|
|
|
new_node = treestore.append(parent_node,
|
|
|
|
|
[unicode(node_key),
|
|
|
|
|
new_node = treestore.append(parent_node,
|
|
|
|
|
[str(node_key),
|
|
|
|
|
"", ""])
|
|
|
|
|
for elem in node:
|
|
|
|
|
if type(elem) is dict:
|
|
|
|
|
another_node = treestore.append(new_node,
|
|
|
|
|
[unicode(node_key) + u"[" + unicode(count) + u"]",
|
|
|
|
|
"", unicode(type(elem))])
|
|
|
|
|
[str(node_key) + "[" + str(count) + "]",
|
|
|
|
|
"", str(type(elem))])
|
|
|
|
|
self._loadTreeRec(treestore, elem, another_node)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
treestore.append(new_node, [unicode(node_key) + u"[" + unicode(count) + u"]",
|
|
|
|
|
unicode(elem),
|
|
|
|
|
unicode(type(elem))])
|
|
|
|
|
treestore.append(new_node, [str(node_key) + "[" + str(count) + "]",
|
|
|
|
|
str(elem),
|
|
|
|
|
str(type(elem))])
|
|
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
@ -281,16 +284,14 @@ class LogicObject(object):
|
|
|
|
|
|
|
|
|
|
# Tenemos un nodo hoja, obtenemos el valor y el tipo y lo
|
|
|
|
|
# añadimos
|
|
|
|
|
treestore.append(parent_node, [unicode(node_key),
|
|
|
|
|
unicode(node),
|
|
|
|
|
unicode(type(node))])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
treestore.append(parent_node, [str(node_key),
|
|
|
|
|
str(node),
|
|
|
|
|
str(type(node))])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Main procedure
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logicObject = LogicObject()
|
|
|
|
|
|
|
|
|
|
mainWindow = MainWindowMethods(logicObject)
|
|
|
|
|