diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/autolatex/plugins/sublime-text-2/utils |
Initial commit
Diffstat (limited to 'support/autolatex/plugins/sublime-text-2/utils')
6 files changed, 878 insertions, 0 deletions
diff --git a/support/autolatex/plugins/sublime-text-2/utils/__init__.py b/support/autolatex/plugins/sublime-text-2/utils/__init__.py new file mode 100644 index 0000000000..adaad3518f --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/__init__.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/__init__.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +__all__ = [ ] diff --git a/support/autolatex/plugins/sublime-text-2/utils/debug.py b/support/autolatex/plugins/sublime-text-2/utils/debug.py new file mode 100644 index 0000000000..ffba72fdfe --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/debug.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/utils/utils.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import pprint + +def dbg(*variables): + pp = pprint.PrettyPrinter(indent=2) + pp.pprint(variables) + exit(255) + +def dbg_struct(var): + print dir(var) + exit(255) diff --git a/support/autolatex/plugins/sublime-text-2/utils/latex_log_parser.py b/support/autolatex/plugins/sublime-text-2/utils/latex_log_parser.py new file mode 100644 index 0000000000..d68b43c14e --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/latex_log_parser.py @@ -0,0 +1,188 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/utils/latex_log_parser.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +#--------------------------------- +# IMPORTS +#--------------------------------- + +# Import standard python libs +import os +import re +# Import AutoLaTeX libraries +import utils + +#--------------------------------- +# INTERNATIONALIZATION +#--------------------------------- + +import gettext +_T = gettext.gettext + + + +class TeXWarning: + def __init__(self, filename, extension, line, message): + self._data = {} + if extension: + self._filename = filename.strip()+extension + else: + self._filename = filename.strip() + self._linenumber = line + expr = re.compile("[\n\r\f\t ]+") + self._message = re.sub(expr, ' ', message) + + def append(self, message): + self._message = self._message + message + + def set_data(self, key, value): + self._data[key] = value + + def get_data(self, key): + return self._data[key] + + def get_all_data(self): + return self._data + + def get_filename(self): + return self._filename + + def get_line_number(self): + return self._linenumber + + def get_message(self): + return self._message + + def set_message(self, message): + self._message = message + + def __str__(self): + s = str(self._filename)+":"+str(self._linenumber)+":"+str(self._message)+"\n" + if self._data: + s = s + self._data + return s + +class Parser: + + def __init__(self, log_file): + self._directory = os.path.dirname(log_file) + # + self._warnings = [] + # Parsing the log file + regex_start = re.compile("^\\!\\!\\!\\!\\[BeginWarning\\](.*)$") + regex_end = re.compile("^\\!\\!\\!\\!\\[EndWarning\\]") + regex_warn = re.compile("^(.*?):([^:]*):([0-9]+):\\s*(.*?)\\s*$") + f = open(log_file, 'r') + current_log_block = '' + warning = False + line = f.readline() + while line: + if warning: + mo = re.match(regex_end, line) + if mo: + mo = re.match(regex_warn, current_log_block) + if mo: + w = TeXWarning( + mo.group(1), + mo.group(2), + mo.group(3), + mo.group(4)) + self._warnings.append(w) + warning = False + current_log_block = '' + else: + l = line + if not l.endswith(".\n"): + l = l.rstrip() + current_log_block = current_log_block + l + else: + mo = re.match(regex_start, line) + if mo: + l = mo.group(1) + if not l.endswith(".\n"): + l = l.rstrip() + current_log_block = l + warning = True + line = f.readline() + + if warning and current_log_block: + mo = re.match(regex_warn, current_log_block) + if mo: + w = TeXWarning( + mo.group(1), + mo.group(2), + mo.group(3), + mo.group(4)) + self._warnings.append(w) + + def __str__(self): + text = "" + for w in self._warnings: + text = text + str(w) + "\n" + return text + + def get_undefined_citation_warnings(self): + regex = re.compile( + "^.*citation\\s*\\`([^']+)\\'.+undefined.*$", + re.I|re.S) + warnings = [] + for warning in self._warnings: + message = warning.get_message() + mo = re.match(regex, message) + if mo: + warning.set_message( + warning.get_filename()+":"+ + str(warning.get_line_number())+": "+ + (_T("Citation '%s' undefined") % mo.group(1))) + warnings.append(warning) + return warnings + + def get_undefined_reference_warnings(self): + regex = re.compile( + "^.*reference\\s*\\`([^']+)\\'.+undefined.*$", + re.I|re.S) + warnings = [] + for warning in self._warnings: + message = warning.get_message() + mo = re.match(regex, message) + if mo: + warning.set_message( + warning.get_filename()+":"+ + str(warning.get_line_number())+": "+ + (_T("Reference '%s' undefined") % mo.group(1))) + warnings.append(warning) + return warnings + + def get_multidefined_label_warnings(self): + regex = re.compile( + "^.*label\\s*\\`([^']+)\\'.+multiply\\s+defined.*$", + re.I|re.S) + warnings = [] + for warning in self._warnings: + message = warning.get_message() + mo = re.match(regex, message) + if mo: + warning.set_message( + warning.get_filename()+":"+ + str(warning.get_line_number())+": "+ + (_T("Label '%s' multiply defined") % mo.group(1))) + warnings.append(warning) + return warnings + diff --git a/support/autolatex/plugins/sublime-text-2/utils/runner.py b/support/autolatex/plugins/sublime-text-2/utils/runner.py new file mode 100644 index 0000000000..d5725e00cb --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/runner.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/utils/runner.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import os +import re +import subprocess + +# Try to use the threading library if it is available +try: + import threading as _threading +except ImportError: + import dummy_threading as _threading + +import utils + +# List of all the runners +_all_runners = [] + +def kill_all_runners(): + global _all_runners + tab = _all_runners + _all_runners = [] + for r in tab: + r.cancel() + +# Launch AutoLaTeX inside a thread, and wait for the result +class Listener(object): + def get_runner_progress(self): + return False + def on_runner_add_ui(self): + pass + def on_runner_remove_ui(self): + pass + def on_runner_progress(self, amount, comment): + pass + def on_runner_finalize_execution(self, retcode, output, latex_warnings): + pass + +# Launch AutoLaTeX inside a thread, and wait for the result +class Runner(_threading.Thread): + + # listener is the listener on the events + # directory is the path to set as the current path + # directive is the AutoLaTeX command + # params are the CLI options for AutoLaTeX + def __init__(self, listener, directory, directive, params): + _threading.Thread.__init__(self) + assert listener + self.daemon = True + self._listener = listener + self._directory = directory + self._cmd = [ utils.AUTOLATEX_BINARY, '--file-line-warning' ] + params + if directive: + self._cmd.append(directive) + self._has_progress = False + self._subprocess = None + + # Cancel the execution + def cancel(self): + if self._subprocess: + self._subprocess.terminate() + self._subprocess = None + if self._has_progress: + # Remove the info bar from the inside of the UI thread + self._listener.on_runner_remove_ui() + # Update the rest of the UI from the inside of the UI thread + self._listener.on_runner_finalize_execution(0, '', []) + + # Run the thread + def run(self): + global _all_runners + _all_runners.append(self) + progress_line_pattern = None + + self._has_progress = self._listener.get_runner_progress() + + if self._has_progress: + # Add the progress UI + self._listener.on_runner_add_ui() + # Update the command line to obtain the progress data + self._cmd.append('--progress=n') + # Compile a regular expression to extract the progress amount + progress_line_pattern = re.compile("^\\[\\s*([0-9]+)\\%\\]\\s+[#.]+(.*)$") + + # Launch the subprocess + os.chdir(self._directory) + self._subprocess = subprocess.Popen(self._cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output = '' + if self._subprocess: + if self._has_progress: + # Use the info bar to draw the progress of the task + if self._subprocess: + self._subprocess.poll() + # Loop until the subprocess is dead + while self._subprocess and self._subprocess.returncode is None: + if self._subprocess and not self._subprocess.stdout.closed: + # Read a line from STDOUT and extract the progress amount + if self._subprocess: + self._subprocess.stdout.flush() + if self._subprocess: + line = self._subprocess.stdout.readline() + if line: + mo = re.match(progress_line_pattern, line) + if mo: + amount = (float(mo.group(1)) / 100.) + comment = mo.group(2).strip() + self._listener.on_runner_progress(amount, comment) + + if self._subprocess: + self._subprocess.poll() + # Kill the subprocess if + proc = self._subprocess + if proc: + retcode = proc.returncode + # Read the error output of AutoLaTeX + proc.stderr.flush() + for line in proc.stderr: + output = output + line + proc.stdout.close() + proc.stderr.close() + + else: + # Silent execution of the task + out, err = self._subprocess.communicate() if self._subprocess else ('','') + retcode = self._subprocess.returncode if self._subprocess else 0 + output = err + + # Stop because the subprocess was cancelled + if not self._subprocess: + if self in _all_runners: + _all_runners.remove(self) + return 0 + self._subprocess = None + + # If AutoLaTeX had failed, the output is assumed to + # be the error message. + # If AutoLaTeX had not failed, the output may contains + # "warning" notifications. + latex_warnings = [] + if retcode == 0: + regex_expr = re.compile("^\\!\\!(.+?):(W[0-9]+):[^:]+:\\s*(.+?)\\s*$") + for output_line in re.split("[\n\r]+", output): + mo = re.match(regex_expr, output_line) + if mo: + latex_warnings.append([mo.group(3),mo.group(1), mo.group(2)]) + output = '' # Output is no more interesting + + if self._has_progress: + # Remove the info bar from the inside of the UI thread + self._listener.on_runner_remove_ui() + + # Update the rest of the UI from the inside of the UI thread + self._listener.on_runner_finalize_execution(retcode, output, latex_warnings) + if self in _all_runners: + _all_runners.remove(self) + diff --git a/support/autolatex/plugins/sublime-text-2/utils/runner_command.py b/support/autolatex/plugins/sublime-text-2/utils/runner_command.py new file mode 100644 index 0000000000..7b602c4826 --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/runner_command.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/utils/runner_command.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import os +import sublime +import utils, runner +import gettext + +#--------------------------------- +# INTERNATIONALIZATION +#--------------------------------- + +_T = gettext.gettext + + +class AbstractRunnerCommand(runner.Listener): + + def __init__(self): + runner.Listener.__init__(self) + self._thread = None + self._show_progress = True + + def cancel_task(self): + if self._thread: + self._thread.cancel() + + def start_task(self, show_progress, encoding, working_dir, directive, params): + self._encoding = encoding + self._show_progress = show_progress + + # Default the to the current files directory if no working directory was given + if (working_dir == "" and self.window.active_view() + and self.window.active_view().file_name()): + working_dir = os.path.dirname(self.window.active_view().file_name()) + + if not hasattr(self, 'output_view'): + # Try not to call get_output_panel until the regexes are assigned + self.output_view = self.window.get_output_panel("autolatex") + + self.output_view.settings().set("result_file_regex", "^(.*?):([0-9]+):(?:([0-9]+):)?\\s*(.*?)\\s*$") + self.output_view.settings().set("result_line_regex", "^l\\.([0-9]+)\\s+") + self.output_view.settings().set("result_base_dir", working_dir) + + # Call get_output_panel a second time after assigning the above + # settings, so that it'll be picked up as a result buffer + self.window.get_output_panel("autolatex") + + # Show the progress + if self._show_progress: + sublime.status_message(_T("Building [%d%%]") % int(0)) + + autolatex_directory = utils.find_AutoLaTeX_directory(working_dir) + self._thread = runner.Runner( + self, + autolatex_directory, + directive, + params) + self._thread.start() + + + def get_runner_progress(self): + return self._show_progress + + def on_runner_add_ui(self): + sublime.status_message(_T("Building [%d%%]") % 0) + show_panel_on_build = sublime.load_settings("Preferences.sublime-settings").get("show_panel_on_build", True) + if show_panel_on_build: + self.window.run_command("show_panel", {"panel": "output.autolatex"}) + + def on_runner_remove_ui(self): + pass + + def on_runner_progress(self, amount, comment): + if comment: + sublime.status_message(_T("Building [%d%%]: %s") % (int(amount * 100), comment)) + else: + sublime.status_message(_T("Building [%d%%]") % int(amount * 100)) + + def on_runner_finalize_execution(self, retcode, output, latex_warnings): + messages = [] + if retcode != 0: + messages.append(output) + else: + for warning in latex_warnings: + messages.append(warning[1]+":1: "+warning[0]) + + for message in messages: + try: + message = message.decode(self._encoding) + except: + message = _T("[Decode error - output not %s]") % self._encoding + + # Normalize newlines, Sublime Text always uses a single \n separator + # in memory. + message = message.replace('\r\n', '\n').replace('\r', '\n') + + selection_was_at_end = (len(self.output_view.sel()) == 1 + and self.output_view.sel()[0] + == sublime.Region(self.output_view.size())) + + self.output_view.set_read_only(False) + edit = self.output_view.begin_edit() + self.output_view.insert(edit, self.output_view.size(), message) + if selection_was_at_end: + self.output_view.show(self.output_view.size()) + self.output_view.end_edit(edit) + self.output_view.set_read_only(True) + + if retcode!=0: + sublime.status_message(_T("Build finished with an error")) + elif len(latex_warnings) == 0: + sublime.status_message(_T("Build finished")) + else: + sublime.status_message(_T("Build finished with %d warnings") % len(latex_warnings)) + + # Set the selection to the start, so that next_result will work as expected + edit = self.output_view.begin_edit() + self.output_view.sel().clear() + self.output_view.sel().add(sublime.Region(0)) + self.output_view.end_edit(edit) diff --git a/support/autolatex/plugins/sublime-text-2/utils/utils.py b/support/autolatex/plugins/sublime-text-2/utils/utils.py new file mode 100644 index 0000000000..452b4049ee --- /dev/null +++ b/support/autolatex/plugins/sublime-text-2/utils/utils.py @@ -0,0 +1,325 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# autolatex/utils/utils.py +# Copyright (C) 2013 Stephane Galland <galland@arakhne.org> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +#--------------------------------- +# IMPORTS +#--------------------------------- + +# Import standard python libs +import os +import sys +import subprocess +import ConfigParser +import StringIO +import gettext + +#--------------------------------- +# UTILITY FUNCTION +#--------------------------------- + +# Search an executable in the PATH +def which(cmd): + # can't search the path if a directory is specified + assert not os.path.dirname(cmd) + extensions = os.environ.get("PATHEXT", "").split(os.pathsep) + for directory in os.environ.get("PATH", "").split(os.pathsep): + base = os.path.join(directory, cmd) + options = [base] + [(base + ext) for ext in extensions] + for filename in options: + if os.path.exists(filename): + return filename + return None + +# Search a module in the sys.path or in search_part +def resolve_module_path(path, search_path=[]): + if not os.path.isabs(path): + for p in search_path: + full_name = os.path.join(p, path) + if os.path.exists(full_name): + return full_name + for p in sys.path: + full_name = os.path.join(p, path) + if os.path.exists(full_name): + return full_name + return path + +#--------------------------------- +# CONSTANTS +#--------------------------------- + +# Level of verbosity of AutoLaTeX +DEFAULT_LOG_LEVEL = '--quiet' + +# String that is representing an empty string for the AutoLaTeX backend. +CONFIG_EMPTY_VALUE = '<<<<empty>>>>' + +# Paths +AUTOLATEX_PLUGIN_PATH = None +AUTOLATEX_DEV_PATH = None +AUTOLATEX_INSTALL_PATH = None +AUTOLATEX_PO_PATH = None # Default locale path +AUTOLATEX_PM_PATH = None +TOOLBAR_ICON_PATH = None +NOTEBOOK_ICON_PATH = None +TABLE_ICON_PATH = None + +# Binary files +AUTOLATEX_BINARY = None +DEFAULT_AUTOLATEX_BINARY = None +AUTOLATEX_BACKEND_BINARY = None +DEFAULT_AUTOLATEX_BACKEND_BINARY = None + +def init_plugin_configuration(plugin_file, po_name, search_path=[]): + global AUTOLATEX_PLUGIN_PATH + global AUTOLATEX_DEV_PATH + global AUTOLATEX_INSTALL_PATH + global AUTOLATEX_PO_PATH + global AUTOLATEX_PM_PATH + global AUTOLATEX_BINARY + global AUTOLATEX_BACKEND_BINARY + global DEFAULT_AUTOLATEX_BINARY + global DEFAULT_AUTOLATEX_BACKEND_BINARY + global TOOLBAR_ICON_PATH + global NOTEBOOK_ICON_PATH + global TABLE_ICON_PATH + + bin_autolatex = which('autolatex') + + # Build the plugin's filename + plugin_file = resolve_module_path(plugin_file, search_path) + + # Path of the plugin + AUTOLATEX_PLUGIN_PATH = os.path.dirname(plugin_file) + + # Path to the development directory + pdev_path = os.path.realpath(os.path.join(AUTOLATEX_PLUGIN_PATH, + os.path.basename(os.path.splitext(plugin_file)[0])+'.py')) + dev_path = os.path.dirname(pdev_path) + while dev_path and pdev_path and pdev_path!=dev_path and not os.path.exists( + os.path.join(dev_path, 'autolatex.pl')): + pdev_path = dev_path + dev_path = os.path.dirname(dev_path) + if dev_path and os.path.isfile(os.path.join(dev_path, 'autolatex.pl')): + AUTOLATEX_DEV_PATH = dev_path + else: + dev_path = os.path.realpath(bin_autolatex) + if dev_path: + AUTOLATEX_DEV_PATH = os.path.dirname(dev_path) + else: + AUTOLATEX_DEV_PATH = None + + + # Path to the install directory + if AUTOLATEX_DEV_PATH: + AUTOLATEX_INSTALL_PATH = AUTOLATEX_DEV_PATH + else: + path = os.path.realpath(bin_autolatex) + AUTOLATEX_INSTALL_PATH = os.path.dirname(path) + + # Path to PO files + AUTOLATEX_PO_PATH = None + if AUTOLATEX_DEV_PATH: + path = os.path.join(AUTOLATEX_DEV_PATH, 'po') + if os.path.exists(os.path.join(path, 'fr', 'LC_MESSAGES', po_name+'.mo')): + AUTOLATEX_PO_PATH = path + + # Path to PM files + AUTOLATEX_PM_PATH = os.path.join(AUTOLATEX_INSTALL_PATH, 'pm') + + # Binary file + AUTOLATEX_BINARY = bin_autolatex + if AUTOLATEX_DEV_PATH: + path = os.path.join(dev_path, 'autolatex.pl') + if os.path.exists(path): + AUTOLATEX_BINARY = path + DEFAULT_AUTOLATEX_BINARY = AUTOLATEX_BINARY + + AUTOLATEX_BACKEND_BINARY = which('autolatex-backend') + if AUTOLATEX_DEV_PATH: + path = os.path.join(dev_path, 'autolatex-backend.pl') + if os.path.exists(path): + AUTOLATEX_BACKEND_BINARY = path + DEFAULT_AUTOLATEX_BACKEND_BINARY = AUTOLATEX_BACKEND_BINARY + + # Icons paths + TOOLBAR_ICON_PATH = os.path.join(AUTOLATEX_PLUGIN_PATH, 'icons', '24') + NOTEBOOK_ICON_PATH = os.path.join(AUTOLATEX_PLUGIN_PATH, 'icons', '16') + TABLE_ICON_PATH = os.path.join(AUTOLATEX_PLUGIN_PATH, 'icons', '16') + + # Init internationalization tools + gettext.bindtextdomain(po_name, AUTOLATEX_PO_PATH) + gettext.textdomain(po_name) + +def make_toolbar_icon_path(name): + assert TOOLBAR_ICON_PATH + return os.path.join(TOOLBAR_ICON_PATH, name) + +def make_notebook_icon_path(name): + assert NOTEBOOK_ICON_PATH + return os.path.join(NOTEBOOK_ICON_PATH, name) + +def make_table_icon_path(name): + assert TABLE_ICON_PATH + return os.path.join(TABLE_ICON_PATH, name) + + + +def backend_get_translators(directory): + os.chdir(directory) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'get', 'translators'], stdout=subprocess.PIPE ) + data = process.communicate()[0] + string_in = StringIO.StringIO(data) + config = ConfigParser.ConfigParser() + config.readfp(string_in) + string_in.close() + return config + +def backend_get_loads(directory): + os.chdir(directory) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'get', 'loads', ], stdout=subprocess.PIPE ) + data = process.communicate()[0] + string_in = StringIO.StringIO(data) + config = ConfigParser.ConfigParser() + config.readfp(string_in) + string_in.close() + return config + +def backend_get_configuration(directory, level, section): + os.chdir(directory) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'get', 'config', level, section], stdout=subprocess.PIPE ) + data = process.communicate()[0] + string_in = StringIO.StringIO(data) + config = ConfigParser.ConfigParser() + config.readfp(string_in) + string_in.close() + return config + +def backend_get_images(directory): + os.chdir(directory) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'get', 'images'], stdout=subprocess.PIPE ) + data = process.communicate()[0] + string_in = StringIO.StringIO(data) + config = ConfigParser.ConfigParser() + config.readfp(string_in) + string_in.close() + return config + +def backend_set_loads(directory, load_config): + os.chdir(directory) + string_out = StringIO.StringIO() + load_config.write(string_out) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'set', 'loads', ], stdin=subprocess.PIPE) + process.communicate(input=string_out.getvalue()) + string_out.close() + return process.returncode == 0 + +def backend_set_configuration(directory, level, settings): + os.chdir(directory) + string_out = StringIO.StringIO() + settings.write(string_out) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'set', 'config', level, 'false' ], stdin=subprocess.PIPE) + process.communicate(input=string_out.getvalue()) + string_out.close() + return process.returncode == 0 + +def backend_set_images(directory, settings): + os.chdir(directory) + string_out = StringIO.StringIO() + settings.write(string_out) + process = subprocess.Popen( [AUTOLATEX_BACKEND_BINARY, 'set', 'images', 'false' ], stdin=subprocess.PIPE) + process.communicate(input=string_out.getvalue()) + string_out.close() + return process.returncode == 0 + + +def first_of(*values): + for value in values: + if value is not None: + return value + return None + +def get_autolatex_user_config_directory(): + if os.name == 'posix': + return os.path.join(os.path.expanduser("~"), ".autolatex") + elif os.name == 'nt': + return os.path.join(os.path.expanduser("~"),"Local Settings","Application Data","autolatex") + else: + return os.path.join(os.path.expanduser("~"), "autolatex") + +def get_autolatex_user_config_file(): + directory = get_autolatex_user_config_directory() + if os.path.isdir(directory): + return os.path.join(directory, 'autolatex.conf') + if os.name == 'posix': + return os.path.join(os.path.expanduser("~"), ".autolatex") + elif os.name == 'nt': + return os.path.join(os.path.expanduser("~"),"Local Settings","Application Data","autolatex.conf") + else: + return os.path.join(os.path.expanduser("~"), "autolatex.conf") + +def get_autolatex_document_config_file(directory): + if os.name == 'posix': + return os.path.join(directory, ".autolatex_project.cfg") + else: + return os.path.join(directory, "autolatex_project.cfg") + +# Test if a given string is a standard extension for TeX document +def is_TeX_extension(ext): + ext = ext.lower() + if ext == '.tex' or ext =='.latex': + return True + else: + return False + +# Replies if the active document is a TeX document +def is_TeX_document(filename): + if filename: + ext = os.path.splitext(filename)[-1] + return is_TeX_extension(ext) + return False + +# Try to find the directory where an AutoLaTeX configuration file is +# located. The search is traversing the parent directory from the current +# document. +def find_AutoLaTeX_directory(current_document): + adir = None + if os.path.isdir(current_document): + directory = current_document + else: + directory = os.path.dirname(current_document) + directory = os.path.abspath(directory) + document_dir = directory + cfgFile = get_autolatex_document_config_file(directory) + previousFile = '' + while previousFile != cfgFile and not os.path.exists(cfgFile): + directory = os.path.dirname(directory) + previousFile = cfgFile + cfgFile = get_autolatex_document_config_file(directory) + + if previousFile != cfgFile: + adir = os.path.dirname(cfgFile) + else: + ext = os.path.splitext(current_document)[-1] + if is_TeX_extension(ext): + adir = document_dir + + return adir + |