diff options
author | Ignacio Rodríguez <ignaciorodriguez@sugarlabs.org> | 2013-06-23 22:44:23 (GMT) |
---|---|---|
committer | Ignacio Rodríguez <ignaciorodriguez@sugarlabs.org> | 2013-06-23 22:44:23 (GMT) |
commit | f5dd20f0532b7abb7802ae1b1fc81dd048b60b08 (patch) | |
tree | 73330ae0ac73654a29504b26f6ef34834cc69627 /Widgets.py | |
parent | fae14d7aee6206e6be7797f0213f73c0e3e8e6ef (diff) |
Objetivo: Busqueda con grep
Diffstat (limited to 'Widgets.py')
-rw-r--r-- | Widgets.py | 150 |
1 files changed, 145 insertions, 5 deletions
@@ -21,7 +21,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import os -import codecs +import commands import Pep8 from pyflakes.scripts import pyflakes @@ -677,7 +677,7 @@ class DialogoProyecto(Gtk.Dialog): class DialogoBuscar(Gtk.Dialog): def __init__(self, view, parent_window = None, - title = "Buscar Texto", texto=None): + title = "Buscar Texto", texto=None, lineanum=None): Gtk.Dialog.__init__(self, title = title, @@ -691,6 +691,7 @@ class DialogoBuscar(Gtk.Dialog): self.entrada = Gtk.Entry() self.view = view + self.lineanum = lineanum boton_anterior = Gtk.Button('Buscar anterior') boton_siguiente = Gtk.Button('Buscar siguiente') @@ -702,8 +703,15 @@ class DialogoBuscar(Gtk.Dialog): tabla.attach(Gtk.Label('Texto a buscar:'), 0, 1, 0, 1) tabla.attach(self.entrada, 1, 2, 0, 1) - if texto: self.entrada.set_text(texto) - + if texto: + self.entrada.set_text(texto) + if self.lineanum: + buffer = view.get_buffer() + inicio = buffer.get_iter_at_line(self.lineanum - 1) + fin = buffer.get_iter_at_line(self.lineanum) + buffer.select_range(inicio, fin) + self.view.scroll_to_iter(fin, 0.1, 1, 1, 1) + self.vbox.add(tabla) button_box.add(boton_anterior) @@ -724,8 +732,14 @@ class DialogoBuscar(Gtk.Dialog): buffer = self.view.get_buffer() inicio, fin = buffer.get_bounds() + if self.lineanum: + inicio = buffer.get_iter_at_line(self.lineanum - 1) + texto_actual = buffer.get_text(inicio, fin, 0) - posicion = buffer.get_iter_at_mark(buffer.get_insert()) + if self.lineanum: + posicion = inicio + else: + posicion = buffer.get_iter_at_mark(buffer.get_insert()) if texto: if texto in texto_actual: @@ -1901,3 +1915,129 @@ class DialogoEliminar(Gtk.Dialog): label.show() self.vbox.add(label) + +class BusquedaGrep(Gtk.Dialog): + """ + Dialogo con un TreeView para busquedas con Grep + """ + + def __init__(self, archivo, parent_window = None): + + if os.path.isdir(archivo): + tipo = "la carpeta" + else: + tipo = "el archivo" + + Gtk.Dialog.__init__(self, + title = "Buscar en %s %s" % (tipo, os.path.basename(archivo)), + parent = parent_window, + flags = Gtk.DialogFlags.MODAL, + buttons = [ + "Aceptar", Gtk.ResponseType.ACCEPT, + "Cancelar", Gtk.ResponseType.CANCEL]) + + self.set_size_request(700, 500) + self.archivo = archivo + self.directorio = False + if os.path.isdir(archivo): + self.directorio = True + + self.treeview = self.get_treeview() + self.entry = Gtk.Entry() + self.entry.connect("changed", self.actualizar) + self.box = Gtk.VBox() + self.box.pack_start(self.entry, False, False, 0) + + self.scroll = Gtk.ScrolledWindow() + self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC, + Gtk.PolicyType.AUTOMATIC) + self.scroll.add(self.treeview) + + self.box.pack_end(self.scroll, True, True, 0) + + self.scroll.set_size_request(700, 450) + self.vbox.add(self.box) + self.box.show_all() + + def get_treeview(self): + + tree = Gtk.TreeView() + # Línea, Documento, Texto + modelo = Gtk.ListStore(int, str, str, str, str) + tree.set_model(modelo) + + columna1 = Gtk.TreeViewColumn("Archivo", Gtk.CellRendererText(), text=1) + columna2 = Gtk.TreeViewColumn("Línea N°", Gtk.CellRendererText(), text=0) + columna3 = Gtk.TreeViewColumn("Texto", Gtk.CellRendererText(), text=2) + + columna1.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) + columna2.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) + columna3.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) + + tree.append_column(columna1) + tree.append_column(columna2) + tree.append_column(columna3) + + return tree + + def actualizar(self, widget): + + texto = widget.get_text() + if not self.directorio: + comando = "cat %s | grep -n '%s'" % (self.archivo, texto) + else: + os.chdir(self.archivo) + comando = "grep -r -n '%s'" % (texto) + + resultado = commands.getoutput(comando) + lineas = resultado.splitlines() + + modelo = self.treeview.get_model() + modelo.clear() + + for linea in lineas: + resultados = linea.split(":") + if not self.directorio: + if len(resultados) < 2: + continue + if len(resultados) >= 2: + try: + dato1 = int(resultados[0]) + except: + continue + dato2 = os.path.basename(self.archivo) + dato3 = resultados[1] + if len(resultados) > 2: + misdatos = resultados[1:len(resultados) + 1] + dato3 = misdatos[0] + misdatos.remove(dato3) + for txt in misdatos: + dato3 = dato3 + ":" + txt + else: + if len(resultados) < 3: + continue + + if len(resultados) >= 3: + try: + dato1 = int(resultados[1]) + except: + continue + + dato2 = resultados[0] + dato3 = resultados[2] + + if len(resultados) > 3: + misdatos = resultados[2:len(resultados) + 1] + dato3 = misdatos[0] + misdatos.remove(dato3) + for txt in misdatos: + dato3 = dato3 + ":" + txt + + dato3 = dato3.replace("\t", "") + dato3 = dato3.replace(" ", "") + if self.directorio: + direccion = os.path.join(self.archivo, dato2) + else: + direccion = os.path.join(self.archivo) + + modelo.insert(-1, [dato1, dato2, dato3, direccion, texto]) |