<commit_before>#!/usr/bin/python
# -*- encoding: utf-8 -*-

# Copyright (c) 2009 <NAME> <<EMAIL>>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import sys
import gtk
import locale
import gtksourceview2
import traceback
import pango

try: import glib
except ImportError: pass

import TexPane
import PreviewPane
import Importer
import Motion
import Biblio
import Preferences
import UpdateCheck
import Template


class GummiGUI:

	def __init__(self):

		self.filename = None
		self.exitinterrupt = False
		self.CWD = CWD

		builder = gtk.Builder()
		builder.add_from_file(CWD + "/gui/gummi.glade")
		builder.connect_signals(self) #split signals?
		self.builder = builder

		self.mainwindow = builder.get_object("mainwindow")
		self.toolbar = builder.get_object("toolbar")
		self.mainnotebook = builder.get_object("main_notebook")
		self.editorscroll = builder.get_object("editor_scroll")
		self.drawarea = builder.get_object("preview_drawarea")
		self.preview_toolbar = builder.get_object("preview_toolbar")
		self.errorfield = builder.get_object("errorfield")
		self.searchwindow = builder.get_object("searchwindow")
		self.searchentry = builder.get_object("searchentry")
		self.backwards = builder.get_object("toggle_backwards")
		self.matchcase = builder.get_object("toggle_matchcase")
		self.statuslight = builder.get_object("tool_statuslight")
		self.statusbar = builder.get_object("statusbar")
		self.statusbar_cid = self.statusbar.get_context_id("Gummi")
		self.errorfield.modify_font(pango.FontDescription("monospace 8"))
		self.recent1 = builder.get_object("menu_recent1")
		self.recent2 = builder.get_object("menu_recent2")
		self.recent3 = builder.get_object("menu_recent3")
		self.hpaned = builder.get_object("hpaned")

		self.menu_statusbar = builder.get_object("menu_statusbar")
		self.menu_statusbar.set_active(True)
		self.menu_toolbar = builder.get_object("menu_toolbar")
		self.menu_toolbar.set_active(True)
		self.menu_hlayout = builder.get_object("menu_hlayout")
		self.menu_vlayout = builder.get_object("menu_vlayout")

		self.box_image = builder.get_object("box_image")
		self.box_table = builder.get_object("box_table")
		self.box_matrix = builder.get_object("box_matrix")
		self.image_pane = builder.get_object("image_pane")
		self.table_pane = builder.get_object("table_pane")
		self.matrix_pane = builder.get_object("matrix_pane")

		self.tempdir = os.environ.get("TMPDIR", "/tmp")

		mainwidth = self.mainwindow.get_size()[0]
		self.hpaned.set_position(mainwidth/2)

		self.config = Preferences.Preferences(self)
		self.editorpane = TexPane.TexPane(self.config)
		self.previewpane = PreviewPane.PreviewPane(self.builder, self.drawarea, self.preview_toolbar)
		self.importer = Importer.Importer(self.editorpane, builder)
		self.motion = Motion.Motion(self.config, self.editorpane, self.previewpane, self.errorfield, self.statuslight, self.tempdir)
		self.editorscroll.add(self.editorpane.editorviewer)
		self.biblio = Biblio.Biblio(self.config, self.editorpane, self.motion, builder)

		self.create_initial_document()
		self.mainwindow.show_all()

	def create_initial_document(self):
		if len(sys.argv) > 1:
			self.filename = sys.argv[1]
			self.load_file(self.filename)
		else:
			self.filename = self.tempdir + "/gummi-default"
			self.editorpane.fill_buffer(self.config.get_string("tex_defaulttext"))
			self.motion.create_environment(self.filename)
			os.chdir(os.environ['HOME'])
		self.setup_recentfiles()

	def decode_text(self, filename):
		loadfile = open(filename, "r")
		content = loadfile.read()
		encoding = locale.getdefaultlocale()[1]
		try: decoded_content = content.decode(encoding)
		except (UnicodeError, TypeError):
			try: decoded_content = content.decode("iso-8859-1", 'replace')
			except (UnicodeError, TypeError):
				decoded_content = content.decode("ascii", 'replace')
		loadfile.close()
		return decoded_content

	def encode_text(self, text):
		encoding = locale.getdefaultlocale()[1]
		try: encoded_content = text.encode(encoding)
		except (UnicodeError, TypeError):
			try: encoded_content = text.encode("iso-8859-1", 'replace')
			except (UnicodeError, TypeError):
				encoded_content = text.encode("ascii", 'replace')
		return encoded_content

	def update_statusbar(self, message):
		self.statusbar.push(self.statusbar_cid, message)

	def set_window_title(self, filename):
		self.mainwindow.set_title \
					("[" + os.path.basename(filename) + "] - Gummi")

	def on_menu_new_activate(self, menuitem, data=None):
		if self.check_for_save(): self.on_menu_save_activate(None, None)
		self.editorpane.fill_buffer(Preferences.DEFAULT_TEXT)
		self.editorpane.editorbuffer.set_modified(False)
		self.filename = None
		self.motion.create_environment(self.tempdir + "/gummi-new")

	def on_menu_template_activate(self, menuitem, data=None):
		self.template_doc = Template.Template(self.builder, CWD)

	def on_menu_open_activate(self, menuitem, data=None):
		if os.getcwd() == self.tempdir:
			os.chdir(os.environ['HOME'])
		if self.check_for_save(): self.on_menu_save_activate(None, None)<commit_msg>removed brackets<commit_after>#!/usr/bin/python
# -*- encoding: utf-8 -*-

# Copyright (c) 2009 <NAME> <<EMAIL>>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
import sys
import gtk
import locale
import gtksourceview2
import traceback
import pango

try: import glib
except ImportError: pass

import TexPane
import PreviewPane
import Importer
import Motion
import Biblio
import Preferences
import UpdateCheck
import Template


class GummiGUI:

	def __init__(self):

		self.filename = None
		self.exitinterrupt = False
		self.CWD = CWD

		builder = gtk.Builder()
		builder.add_from_file(CWD + "/gui/gummi.glade")
		builder.connect_signals(self) #split signals?
		self.builder = builder

		self.mainwindow = builder.get_object("mainwindow")
		self.toolbar = builder.get_object("toolbar")
		self.mainnotebook = builder.get_object("main_notebook")
		self.editorscroll = builder.get_object("editor_scroll")
		self.drawarea = builder.get_object("preview_drawarea")
		self.preview_toolbar = builder.get_object("preview_toolbar")
		self.errorfield = builder.get_object("errorfield")
		self.searchwindow = builder.get_object("searchwindow")
		self.searchentry = builder.get_object("searchentry")
		self.backwards = builder.get_object("toggle_backwards")
		self.matchcase = builder.get_object("toggle_matchcase")
		self.statuslight = builder.get_object("tool_statuslight")
		self.statusbar = builder.get_object("statusbar")
		self.statusbar_cid = self.statusbar.get_context_id("Gummi")
		self.errorfield.modify_font(pango.FontDescription("monospace 8"))
		self.recent1 = builder.get_object("menu_recent1")
		self.recent2 = builder.get_object("menu_recent2")
		self.recent3 = builder.get_object("menu_recent3")
		self.hpaned = builder.get_object("hpaned")

		self.menu_statusbar = builder.get_object("menu_statusbar")
		self.menu_statusbar.set_active(True)
		self.menu_toolbar = builder.get_object("menu_toolbar")
		self.menu_toolbar.set_active(True)
		self.menu_hlayout = builder.get_object("menu_hlayout")
		self.menu_vlayout = builder.get_object("menu_vlayout")

		self.box_image = builder.get_object("box_image")
		self.box_table = builder.get_object("box_table")
		self.box_matrix = builder.get_object("box_matrix")
		self.image_pane = builder.get_object("image_pane")
		self.table_pane = builder.get_object("table_pane")
		self.matrix_pane = builder.get_object("matrix_pane")

		self.tempdir = os.environ.get("TMPDIR", "/tmp")

		mainwidth = self.mainwindow.get_size()[0]
		self.hpaned.set_position(mainwidth/2)

		self.config = Preferences.Preferences(self)
		self.editorpane = TexPane.TexPane(self.config)
		self.previewpane = PreviewPane.PreviewPane(self.builder, self.drawarea, self.preview_toolbar)
		self.importer = Importer.Importer(self.editorpane, builder)
		self.motion = Motion.Motion(self.config, self.editorpane, self.previewpane, self.errorfield, self.statuslight, self.tempdir)
		self.editorscroll.add(self.editorpane.editorviewer)
		self.biblio = Biblio.Biblio(self.config, self.editorpane, self.motion, builder)

		self.create_initial_document()
		self.mainwindow.show_all()

	def create_initial_document(self):
		if len(sys.argv) > 1:
			self.filename = sys.argv[1]
			self.load_file(self.filename)
		else:
			self.filename = self.tempdir + "/gummi-default"
			self.editorpane.fill_buffer(self.config.get_string("tex_defaulttext"))
			self.motion.create_environment(self.filename)
			os.chdir(os.environ['HOME'])
		self.setup_recentfiles()

	def decode_text(self, filename):
		loadfile = open(filename, "r")
		content = loadfile.read()
		encoding = locale.getdefaultlocale()[1]
		try: decoded_content = content.decode(encoding)
		except (UnicodeError, TypeError):
			try: decoded_content = content.decode("iso-8859-1", 'replace')
			except (UnicodeError, TypeError):
				decoded_content = content.decode("ascii", 'replace')
		loadfile.close()
		return decoded_content

	def encode_text(self, text):
		encoding = locale.getdefaultlocale()[1]
		try: encoded_content = text.encode(encoding)
		except (UnicodeError, TypeError):
			try: encoded_content = text.encode("iso-8859-1", 'replace')
			except (UnicodeError, TypeError):
				encoded_content = text.encode("ascii", 'replace')
		return encoded_content

	def update_statusbar(self, message):
		self.statusbar.push(self.statusbar_cid, message)

	def set_window_title(self, filename):
		self.mainwindow.set_title \
					(os.path.basename(filename) + " - Gummi")

	def on_menu_new_activate(self, menuitem, data=None):
		if self.check_for_save(): self.on_menu_save_activate(None, None)
		self.editorpane.fill_buffer(Preferences.DEFAULT_TEXT)
		self.editorpane.editorbuffer.set_modified(False)
		self.filename = None
		self.motion.create_environment(self.tempdir + "/gummi-new")

	def on_menu_template_activate(self, menuitem, data=None):
		self.template_doc = Template.Template(self.builder, CWD)

	def on_menu_open_activate(self, menuitem, data=None):
		if os.getcwd() == self.tempdir:
			os.chdir(os.environ['HOME'])
		if self.check_for_save(): self.on_menu_save_activate(None, None)
{'fraction_non_alphanumeric': 0.1112118713932399, 'fraction_numerical': 0.004781533388293487, 'mean_word_length': 5.480235042735043, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7415195', 'n_tokens_mistral': 4173, 'n_tokens_neox': 3848, 'n_words': 858}