/usr/share/nautilus-compare/utils.py is in nautilus-compare 0.0.4+po1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #!/usr/bin/python
# -*- coding: utf-8 -*-
# nautilus-compare --- Context menu extension for Nautilus file manager
# Copyright (C) 2011 Guido Tabbernuk <boamaod@gmail.com>
#
# 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 3 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. If not, see <http://www.gnu.org/licenses/>.
import os
import xdg.BaseDirectory
import ConfigParser
APP = 'nautilus-compare'
# settings
CONFIG_FILES = [os.path.join(xdg.BaseDirectory.xdg_config_home, APP + ".conf"), os.path.join("/etc", APP + ".conf")]
CONFIG_FILE = CONFIG_FILES[0]
SETTINGS_MAIN = 'Settings'
DIFF_PATH = 'diff_engine_path'
DIFF_PATH_3WAY = 'diff_engine_path_3way'
DIFF_PATH_MULTI = 'diff_engine_path_multi'
COMPARATORS = 'defined_comparators'
# ordered by preference
PREDEFINED_ENGINES = ['meld', 'kdiff3', 'diffuse', 'kompare', 'fldiff', 'tkdiff']
DEFAULT_DIFF_ENGINE = "meld"
# where comparator engines are sought
COMPARATOR_PATHS = ['/usr/bin', '/usr/local/bin']
class NautilusCompareConfig:
diff_engine = DEFAULT_DIFF_ENGINE
diff_engine_3way = DEFAULT_DIFF_ENGINE
diff_engine_multi = ""
engines = []
config = None
def load(self):
'''Loads config options if available. If not, creates them using the best heuristics availabe.'''
self.config = ConfigParser.ConfigParser()
# allow system-wide default settings from /etc/*
if os.path.isfile(CONFIG_FILES[0]):
self.config.read(CONFIG_FILES[0])
else:
self.config.read(CONFIG_FILES[1])
# read from start or flush from the point where cancelled
try:
self.diff_engine = self.config.get(SETTINGS_MAIN, DIFF_PATH)
self.diff_engine_3way = self.config.get(SETTINGS_MAIN, DIFF_PATH_3WAY)
self.diff_engine_multi = self.config.get(SETTINGS_MAIN, DIFF_PATH_MULTI)
self.engines = eval(self.config.get(SETTINGS_MAIN, COMPARATORS))
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
# maybe settings were half loaded when exception was thrown
try:
self.config.add_section(SETTINGS_MAIN)
except ConfigParser.DuplicateSectionError:
pass
self.add_missing_predefined_engines()
# add choice for "engine not enabled"
# (always needed, because at least self.engines cannot be loaded)
self.engines.insert(0, "")
# if default engine is not installed, replace with preferred installed engine
if len(self.engines) > 0:
if self.diff_engine not in self.engines:
self.diff_engine = self.engines[0]
if self.diff_engine_3way not in self.engines:
self.diff_engine_3way = self.engines[0]
if self.diff_engine_multi not in self.engines:
self.diff_engine_multi = self.engines[0]
self.engines.sort()
self.config.set(SETTINGS_MAIN, DIFF_PATH, self.diff_engine)
self.config.set(SETTINGS_MAIN, DIFF_PATH_3WAY, self.diff_engine_3way)
self.config.set(SETTINGS_MAIN, DIFF_PATH_MULTI, self.diff_engine_multi)
self.config.set(SETTINGS_MAIN, COMPARATORS, self.engines)
with open(CONFIG_FILE, 'wb') as f:
self.config.write(f)
def add_missing_predefined_engines(self):
'''Adds predefined engines which are installed, but missing in engines list.'''
system_utils = []
for path in COMPARATOR_PATHS:
system_utils += os.listdir(path)
for engine in PREDEFINED_ENGINES:
if engine not in self.engines and engine in system_utils:
self.engines.append(engine)
def save(self):
'''Saves config options'''
try:
self.config.add_section(SETTINGS_MAIN)
except ConfigParser.DuplicateSectionError:
pass
self.config.set(SETTINGS_MAIN, DIFF_PATH, self.diff_engine)
self.config.set(SETTINGS_MAIN, DIFF_PATH_3WAY, self.diff_engine_3way)
self.config.set(SETTINGS_MAIN, DIFF_PATH_MULTI, self.diff_engine_multi)
if self.diff_engine not in self.engines:
self.engines.append(self.diff_engine)
if self.diff_engine_3way not in self.engines:
self.engines.append(self.diff_engine_3way)
if self.diff_engine_multi not in self.engines:
self.engines.append(self.diff_engine_multi)
self.config.set(SETTINGS_MAIN, COMPARATORS, self.engines)
with open(CONFIG_FILE, 'wb') as f:
self.config.write(f)
|