# Python imports
import os
import tempfile
from xml.etree import ElementTree as ET
# Lib imports
# Application imports
from utils.style_properties import StyleProperties
class SourceviewSignalsMixin:
def update_sample_view(self):
"""
Update the sample shown in the GUI.
To do this we must write the scheme to disk and reload it from there.
"""
self.write_scheme(self.temp_scheme_file, self.temp_scheme_id)
self.scheme_manager.force_rescan()
new_scheme = self.scheme_manager.get_scheme(self.temp_scheme_id)
self.src_buffer.set_style_scheme(new_scheme);
def load_scheme(self, scheme_id_or_file):
xml_tree = None
if os.path.isfile(scheme_id_or_file):
directory = os.path.dirname(scheme_id_or_file)
if directory not in self.scheme_manager.get_search_path():
self.scheme_manager.prepend_search_path(directory)
with open(scheme_id_or_file, 'r') as f:
xml_tree = ET.parse(f)
if xml_tree.getroot().tag == 'style-scheme':
this_scheme = self.scheme_manager.get_scheme( xml_tree.getroot().attrib['id'] )
if not this_scheme: return False
testFilename = this_scheme.get_filename()
if testFilename != scheme_id_or_file:
text = 'There was a problem opening the file\n\nYou appear to have schemes with the same IDs in different directories\n'
self.message_dialog(Gtk.MessageType.ERROR, text, buttons = Gtk.ButtonsType.NONE, additional_buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL))
return False
self.orig_scheme_file = scheme_id_or_file
else:
this_scheme = self.scheme_manager.get_scheme(scheme_id_or_file)
if not this_scheme: return False
self.currentScheme = this_scheme
self.name_entry.set_text( this_scheme.get_name() )
self.authr_entry.set_text( ', '.join( this_scheme.get_authors() ) )
self.desc_entry.set_text( this_scheme.get_description() )
self.id_entry.set_text( this_scheme.get_id() )
scheme_file = self.currentScheme.get_filename()
with open(scheme_file, 'r') as f:
xml_tree = ET.parse(f)
style_elems = xml_tree.findall('style')
self.all_styles_dict.clear()
for style_elem in style_elems:
this_style = self.currentScheme.get_style(style_elem.attrib['name'])
styleProps = StyleProperties()
styleProps.from_gtk_source_style(this_style)
self.all_styles_dict[style_elem.attrib['name']] = styleProps;
self.src_buffer.set_style_scheme(self.currentScheme);
# set up temp file so the sample view can be updated
self.temp_scheme_id = f"{this_scheme.get_id()}_temp"
self.temp_scheme_file = f"{tempfile.gettempdir()}/{self.temp_scheme_id}.xml"
return True
def write_scheme(self, location, scheme_id):
output = f'\n'
output += f' {self.authr_entry.get_text()}\n'
output += f' {self.desc_entry.get_text()}\n\n'
for k, v in self.all_styles_dict.items():
output += f' \n'
output += '\n'
with open(location, 'w') as f:
try:
f.write(output)
except:
return False
return True