[meld] Move FilterEntry to its own module to avoid circular imports
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld] Move FilterEntry to its own module to avoid circular imports
- Date: Fri, 30 Nov 2012 19:28:36 +0000 (UTC)
commit ad2a9208ec22b481225e02814ccf08b6a7282326
Author: Kai Willadsen <kai willadsen gmail com>
Date: Fri Oct 26 05:54:14 2012 +1000
Move FilterEntry to its own module to avoid circular imports
meld/filters.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++
meld/meldapp.py | 87 ++++++---------------------------------------------
meld/preferences.py | 10 +++---
3 files changed, 102 insertions(+), 82 deletions(-)
---
diff --git a/meld/filters.py b/meld/filters.py
new file mode 100644
index 0000000..e7f0999
--- /dev/null
+++ b/meld/filters.py
@@ -0,0 +1,87 @@
+### Copyright (C) 2011-2012 Kai Willadsen <kai willadsen 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 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; if not, write to the Free Software
+### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+import re
+
+from . import misc
+
+
+class FilterEntry(object):
+
+ __slots__ = ("label", "active", "filter", "filter_string")
+
+ REGEX, SHELL = 0, 1
+
+ def __init__(self, label, active, filter, filter_string):
+ self.label = label
+ self.active = active
+ self.filter = filter
+ self.filter_string = filter_string
+
+ @classmethod
+ def _compile_regex(cls, regex):
+ try:
+ compiled = re.compile(regex + "(?m)")
+ except re.error:
+ compiled = None
+ return compiled
+
+ @classmethod
+ def _compile_shell_pattern(cls, pattern):
+ bits = pattern.split()
+ if len(bits) > 1:
+ regexes = [misc.shell_to_regex(b)[:-1] for b in bits]
+ regex = "(%s)$" % "|".join(regexes)
+ elif len(bits):
+ regex = misc.shell_to_regex(bits[0])
+ else:
+ # An empty pattern would match everything, so skip it
+ return None
+
+ try:
+ compiled = re.compile(regex)
+ except re.error:
+ compiled = None
+
+ return compiled
+
+ @classmethod
+ def parse(cls, string, filter_type):
+ elements = string.split("\t")
+ if len(elements) < 3:
+ return None
+ name, active = elements[0], bool(int(elements[1]))
+ filter_string = " ".join(elements[2:])
+ compiled = FilterEntry.compile_filter(filter_string, filter_type)
+ if compiled is None:
+ active = False
+ return FilterEntry(name, active, compiled, filter_string)
+
+ @classmethod
+ def compile_filter(cls, filter_string, filter_type):
+ if filter_type == FilterEntry.REGEX:
+ compiled = FilterEntry._compile_regex(filter_string)
+ elif filter_type == FilterEntry.SHELL:
+ compiled = FilterEntry._compile_shell_pattern(filter_string)
+ else:
+ raise ValueError("Unknown filter type")
+ return compiled
+
+ def __copy__(self):
+ new = type(self)(self.label, self.active, None, self.filter_string)
+ if self.filter is not None:
+ new.filter = re.compile(self.filter.pattern, self.filter.flags)
+ return new
diff --git a/meld/meldapp.py b/meld/meldapp.py
index 9c80965..18b150b 100644
--- a/meld/meldapp.py
+++ b/meld/meldapp.py
@@ -16,89 +16,19 @@
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
### USA.
-
import optparse
import os
-import re
from gettext import gettext as _
import gobject
import gtk
-import misc
-import preferences
+from . import filters
+from . import preferences
version = "1.7.0"
-class FilterEntry(object):
-
- __slots__ = ("label", "active", "filter", "filter_string")
-
- REGEX, SHELL = 0, 1
-
- def __init__(self, label, active, filter, filter_string):
- self.label = label
- self.active = active
- self.filter = filter
- self.filter_string = filter_string
-
- @classmethod
- def _compile_regex(cls, regex):
- try:
- compiled = re.compile(regex + "(?m)")
- except re.error:
- compiled = None
- return compiled
-
- @classmethod
- def _compile_shell_pattern(cls, pattern):
- bits = pattern.split()
- if len(bits) > 1:
- regexes = [misc.shell_to_regex(b)[:-1] for b in bits]
- regex = "(%s)$" % "|".join(regexes)
- elif len(bits):
- regex = misc.shell_to_regex(bits[0])
- else:
- # An empty pattern would match everything, so skip it
- return None
-
- try:
- compiled = re.compile(regex)
- except re.error:
- compiled = None
-
- return compiled
-
- @classmethod
- def parse(cls, string, filter_type):
- elements = string.split("\t")
- if len(elements) < 3:
- return None
- name, active = elements[0], bool(int(elements[1]))
- filter_string = " ".join(elements[2:])
- compiled = FilterEntry.compile_filter(filter_string, filter_type)
- if compiled is None:
- active = False
- return FilterEntry(name, active, compiled, filter_string)
-
- @classmethod
- def compile_filter(cls, filter_string, filter_type):
- if filter_type == FilterEntry.REGEX:
- compiled = FilterEntry._compile_regex(filter_string)
- elif filter_type == FilterEntry.SHELL:
- compiled = FilterEntry._compile_shell_pattern(filter_string)
- else:
- raise ValueError("Unknown filter type")
- return compiled
-
- def __copy__(self):
- new = type(self)(self.label, self.active, None, self.filter_string)
- if self.filter is not None:
- new.filter = re.compile(self.filter.pattern, self.filter.flags)
- return new
-
-
class MeldApp(gobject.GObject):
__gsignals__ = {
@@ -114,9 +44,9 @@ class MeldApp(gobject.GObject):
self.prefs = preferences.MeldPreferences()
self.prefs.notify_add(self.on_preference_changed)
self.file_filters = self._parse_filters(self.prefs.filters,
- FilterEntry.SHELL)
+ filters.FilterEntry.SHELL)
self.text_filters = self._parse_filters(self.prefs.regexes,
- FilterEntry.REGEX)
+ filters.FilterEntry.REGEX)
def create_window(self):
self.window = meldwindow.MeldWindow()
@@ -124,14 +54,17 @@ class MeldApp(gobject.GObject):
def on_preference_changed(self, key, val):
if key == "filters":
- self.file_filters = self._parse_filters(val, FilterEntry.SHELL)
+ self.file_filters = self._parse_filters(val,
+ filters.FilterEntry.SHELL)
self.emit('file-filters-changed')
elif key == "regexes":
- self.text_filters = self._parse_filters(val, FilterEntry.REGEX)
+ self.text_filters = self._parse_filters(val,
+ filters.FilterEntry.REGEX)
self.emit('text-filters-changed')
def _parse_filters(self, string, filt_type):
- filt = [FilterEntry.parse(l, filt_type) for l in string.split("\n")]
+ filt = [filters.FilterEntry.parse(l, filt_type) for l
+ in string.split("\n")]
return [f for f in filt if f is not None]
def diff_files_callback(self, option, opt_str, value, parser):
diff --git a/meld/preferences.py b/meld/preferences.py
index 6ac6d28..287b58f 100644
--- a/meld/preferences.py
+++ b/meld/preferences.py
@@ -23,7 +23,7 @@ import gtk
from ui import gnomeglade
from ui import listwidget
-import meldapp
+import filters
import misc
import paths
from util import prefs
@@ -45,7 +45,7 @@ class FilterList(listwidget.ListWidget):
self.valid_icon_celldata)
for filtstring in getattr(self.prefs, self.key).split("\n"):
- filt = meldapp.FilterEntry.parse(filtstring, filter_type)
+ filt = filters.FilterEntry.parse(filtstring, filter_type)
if filt is None:
continue
valid = filt.filter is not None
@@ -70,7 +70,7 @@ class FilterList(listwidget.ListWidget):
self.model[path][1] = not ren.get_active()
def on_pattern_edited(self, ren, path, text):
- filt = meldapp.FilterEntry.compile_filter(text, self.filter_type)
+ filt = filters.FilterEntry.compile_filter(text, self.filter_type)
valid = filt is not None
self.model[path][2] = text
self.model[path][3] = valid
@@ -135,13 +135,13 @@ class PreferencesDialog(gnomeglade.Component):
# file filters
self.filefilter = FilterList(self.prefs, "filters",
- meldapp.FilterEntry.SHELL)
+ filters.FilterEntry.SHELL)
self.file_filters_tab.pack_start(self.filefilter.widget)
self.checkbutton_ignore_symlinks.set_active( self.prefs.ignore_symlinks)
# text filters
self.textfilter = FilterList(self.prefs, "regexes",
- meldapp.FilterEntry.REGEX)
+ filters.FilterEntry.REGEX)
self.text_filters_tab.pack_start(self.textfilter.widget)
self.checkbutton_ignore_blank_lines.set_active( self.prefs.ignore_blank_lines )
# encoding
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]