Skip to content
Snippets Groups Projects
xmlview.py 3.20 KiB
from gi.repository import GObject, Gtk, Pango
from xml.etree import ElementTree as ET

class Section:
    def __init__(self, name, start, end):
        self.name = name
        self.start = start
        self.end = end

class Word:
    def __init__(self, text, start, end):
        self.text = text
        self.start = start
        self.end = end

class XmlView(Gtk.ScrolledWindow):
    def __init__(self, filename):
        super(XmlView, self).__init__()
        self.sections = []
        self.words = []

        self.view = Gtk.TextView()
        self.view.set_editable(False)
        self.view.set_cursor_visible(False)
        self.buffer = self.view.get_buffer()

        self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
        self.add_with_viewport(self.view)

        #self.inactive_section = self.buffer.create_tag("inactive_section", background="#ffffff")
        self.active_section = self.buffer.create_tag("active_section", background="red")
        self.section_title = self.buffer.create_tag("section_title", scale=2, weight=Pango.Weight.BOLD, justification=Gtk.Justification.CENTER)
        self.subsection_title = self.buffer.create_tag("subsection_title", scale=1.5, weight=Pango.Weight.BOLD, justification=Gtk.Justification.CENTER)

        self.parse_xml(filename)

        self.last_section = None
        self.show_section(0)

    def get_view(self):
        return self

    def parse_sequence(self, sequence, name):
        self.buffer.insert_with_tags(self.buffer.get_end_iter(), 'Sequence %s\n' % name, self.subsection_title)

        text = str(sequence.text)
        for node in sequence:
            text += node.text
            text += node.tail
        for line in text.split('\n'):
            line = line.strip()
            if line != '':
                self.buffer.insert_with_tags(self.buffer.get_end_iter(), '   %s\n' % line)

    def parse_section(self, section):
        name = section.get('id')
        section_start = self.buffer.create_mark('section-start %s' % name, self.buffer.get_end_iter(), True)
        self.buffer.insert_with_tags(self.buffer.get_end_iter(), 'Section %s\n' % section.get('id'), self.section_title)

        num = 1
        for sequence in section.findall('./sequence'):
            self.parse_sequence(sequence, section.get('id') + '.' + str(num))
            num += 1
        self.sections.append(Section(name, section_start, self.buffer.create_mark('section-end %s' % name, self.buffer.get_end_iter(), True)))

    def parse_xml(self, filename):
        root = ET.parse(filename)
        treestore = Gtk.TreeStore(str)
        for section in root.findall(".//section"):
            self.parse_section(section)
        return treestore

    def show_section(self, section):
        if self.last_section != None:
            self.buffer.remove_tag(self.active_section, 
                    self.buffer.get_iter_at_mark(self.sections[self.last_section].start), 
                    self.buffer.get_iter_at_mark(self.sections[self.last_section].end))
        self.last_section = section
        self.buffer.apply_tag(self.active_section, 
                self.buffer.get_iter_at_mark(self.sections[section].start), 
                self.buffer.get_iter_at_mark(self.sections[section].end))