Commit 0b66cd80 authored by Kai Willadsen's avatar Kai Willadsen

Update recent files handling to use a enum for type

From a user POV this should be a no-op, but it makes reasoning about
the values in use easier, and allows typing.
parent a5808e04
......@@ -36,7 +36,6 @@ from gi.repository import Gtk
from . import melddoc
from . import tree
from . import misc
from . import recent
from .ui import gnomeglade
from .ui import emblemcellrenderer
......@@ -45,6 +44,7 @@ from decimal import Decimal
from meld.conf import _
from meld.misc import all_same
from meld.recent import RecentType
from meld.settings import bind_settings, meldsettings, settings
......@@ -645,7 +645,7 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
for d in self.model.value_paths(root)]
else:
uris = []
return recent.TYPE_FOLDER, uris
return RecentType.Folder, uris
def recursively_update( self, path ):
"""Recursively update from tree path 'path'.
......
......@@ -30,7 +30,6 @@ from meld.conf import _
from . import meldbuffer
from . import melddoc
from . import misc
from . import recent
from . import undo
from .ui import gnomeglade
......@@ -39,6 +38,7 @@ from meld.matchers.diffutil import Differ, merged_chunk_order
from meld.matchers.helpers import CachedSequenceMatcher
from meld.matchers.merge import Merger
from meld.patchdialog import PatchDialog
from meld.recent import RecentType
from meld.settings import bind_settings, meldsettings
from meld.sourceview import LanguageManager, get_custom_encoding_candidates
from meld.ui.findbar import FindBar
......@@ -1070,7 +1070,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
def get_comparison(self):
uris = [b.data.gfile for b in self.textbuffer[:self.num_panes]]
return recent.TYPE_FILE, uris
return RecentType.File, uris
def file_loaded(self, loader, result, user_data):
......
......@@ -17,7 +17,7 @@
from meld.conf import _
from . import filediff
from meld.matchers import merge
from . import recent
from meld.recent import RecentType
class FileMerge(filediff.FileDiff):
......@@ -31,7 +31,7 @@ class FileMerge(filediff.FileDiff):
def get_comparison(self):
comp = filediff.FileDiff.get_comparison(self)
return recent.TYPE_MERGE, comp[1]
return RecentType.Merge, comp[1]
def _merge_files(self):
yield _("[%s] Merging files") % self.label_text
......
......@@ -31,6 +31,7 @@ from . import task
from meld.conf import _
from meld.settings import settings
from meld.recent import RecentType
log = logging.getLogger(__name__)
......@@ -106,7 +107,7 @@ class MeldDoc(LabeledObjectMixin, GObject.GObject):
self.emit('state-changed', self._state, value)
self._state = value
def get_comparison(self):
def get_comparison(self) -> RecentType:
"""Get the comparison type and URI(s) being compared"""
pass
......
......@@ -14,8 +14,6 @@
# 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
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import GLib
......@@ -27,14 +25,13 @@ from . import filediff
from . import filemerge
from . import melddoc
from . import newdifftab
from . import recent
from . import task
from . import vcview
from .ui import gnomeglade
from .ui import notebooklabel
from meld.conf import _
from meld.recent import recent_comparisons
from meld.recent import RecentType, recent_comparisons
from meld.settings import interface_settings, settings
from meld.windowstate import SavedWindowState
......@@ -623,7 +620,8 @@ class MeldWindow(gnomeglade.Component):
def append_vcview(self, location, auto_compare=False):
doc = vcview.VcView()
self._append_page(doc, "meld-version-control")
location = location[0] if isinstance(location, list) else location
if isinstance(location, (list, tuple)):
location = location[0]
doc.set_location(location.get_path())
if auto_compare:
doc.scheduler.add_task(doc.auto_compare)
......@@ -631,15 +629,13 @@ class MeldWindow(gnomeglade.Component):
def append_recent(self, uri):
comparison_type, gfiles, flags = recent_comparisons.read(uri)
if comparison_type == recent.TYPE_MERGE:
tab = self.append_filemerge(gfiles)
elif comparison_type == recent.TYPE_FOLDER:
tab = self.append_dirdiff(gfiles)
elif comparison_type == recent.TYPE_VC:
# Files should be a single-element iterable
tab = self.append_vcview(gfiles[0])
else: # comparison_type == recent.TYPE_FILE:
tab = self.append_filediff(gfiles)
comparison_method = {
RecentType.File: self.append_filediff,
RecentType.Folder: self.append_dirdiff,
RecentType.Merge: self.append_filemerge,
RecentType.VersionControl: self.append_vcview,
}
tab = comparison_method[comparison_type](gfiles)
self.notebook.set_current_page(self.notebook.page_num(tab.widget))
recent_comparisons.add(tab)
return tab
......
......@@ -25,6 +25,7 @@ infrastructure that that's actually what we opened.
"""
import configparser
import enum
import os
import sys
import tempfile
......@@ -37,11 +38,12 @@ import meld.misc
from meld.conf import _
TYPE_FILE = "File"
TYPE_FOLDER = "Folder"
TYPE_VC = "Version control"
TYPE_MERGE = "Merge"
COMPARISON_TYPES = (TYPE_FILE, TYPE_FOLDER, TYPE_VC, TYPE_MERGE)
class RecentType(enum.Enum):
File = "File"
Folder = "Folder"
VersionControl = "Version control"
Merge = "Merge"
def unicodeify(s):
......@@ -81,7 +83,7 @@ class RecentFiles(object):
The passed flags are currently ignored. In the future these are to be
used for extra initialisation not captured by the tab itself.
"""
comp_type, gfiles = tab.get_comparison()
recent_type, gfiles = tab.get_comparison()
# While Meld handles comparisons including None, recording these as
# recently-used comparisons just isn't that sane.
......@@ -90,6 +92,7 @@ class RecentFiles(object):
uris = [f.get_uri() for f in gfiles]
names = [f.get_parse_name() for f in gfiles]
comp_type = recent_type.value
# If a (type, uris) comparison is already registered, then re-add
# the corresponding comparison file
......@@ -146,7 +149,9 @@ class RecentFiles(object):
raise ValueError("Invalid recent comparison file")
comp_type = config.get("Comparison", "type")
if comp_type not in COMPARISON_TYPES:
try:
recent_type = RecentType(comp_type)
except ValueError:
raise ValueError("Invalid recent comparison file")
if config.has_option("Comparison", "uris"):
......@@ -157,7 +162,7 @@ class RecentFiles(object):
for p in tuple(config.get("Comparison", "paths").split(";"))])
flags = tuple()
return comp_type, gfiles, flags
return recent_type, gfiles, flags
def _write_recent_file(self, comp_type, uris):
# TODO: Use GKeyFile instead, and return a Gio.File. This is why we're
......
......@@ -34,13 +34,13 @@ from gi.repository import Pango
from meld import melddoc
from meld import misc
from meld import recent
from meld import tree
from meld import vc
from meld.ui import gnomeglade
from meld.ui import vcdialogs
from meld.conf import _
from meld.recent import RecentType
from meld.settings import settings, bind_settings
from meld.vc import _null
from meld.vc._vc import Entry
......@@ -326,7 +326,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
def get_comparison(self):
uris = [Gio.File.new_for_path(self.location)]
return recent.TYPE_VC, uris
return RecentType.VersionControl, uris
def recompute_label(self):
self.label_text = os.path.basename(self.location)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment