Skip to content

Commit

Permalink
Change loaders registration to associate priority with MIME type
Browse files Browse the repository at this point in the history
This changes the loader registration to associate a loader priority
with the MIME type.  This is more flexible than just associating a
priority with a loader itself.

Despite the difference, the API for ginga.util.loader has not changed.
  • Loading branch information
ejeschke committed Nov 28, 2023
1 parent 13f2d89 commit 2004e10
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
18 changes: 7 additions & 11 deletions ginga/rv/Control.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def _check_open(errmsg):
"\nPlease choose an opener or cancel, for file:\n"
"{1:}".format(mimetype, filepath))
self.gui_do(self.gui_choose_file_opener, errmsg, openers,
_open_file, '*', filepath)
_open_file, mimetype, filepath)

future = Future.Future()
future.freeze(_check_open, warnmsg)
Expand Down Expand Up @@ -2505,17 +2505,13 @@ def choose_opener_cb(self, w, rsp, wgts, openers, open_cb, mimetype):
bnch = bnchs[0]
self.ds.remove_dialog(w)

if wgts.choice is not None and wgts.choice.get_state():
if (wgts.choice is not None and wgts.choice.get_state() and
mimetype is not None):
# user wants us to remember their choice
if mimetype != '*':
# loader is not registered for this mimetype, so go ahead
# and do it
loader.add_opener(bnch.opener, [mimetype],
priority=bnch.priority, note=bnch.note)
else:
# multiple loaders for the same mimetype--
# remember setting by prioritizing choice
bnch.priority = -1
# loader is not registered for this mimetype, so go ahead
# and do it
loader.add_opener(bnch.opener, [mimetype],
priority=-99, note=bnch.note)

self.nongui_do(open_cb, bnch.opener)
self.__next_dialog()
Expand Down
5 changes: 0 additions & 5 deletions ginga/rv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,8 @@ def main(self, options, args):

try:
from ginga.util.io import io_fits
from ginga.util import loader
if fitspkg != 'choose':
assert io_fits.use(fitspkg) is True
# opener name is not necessarily the same
opener = loader.get_opener(io_fits.fitsLoaderClass.name)
# set this opener as the priority one
opener.priority = -99

except Exception as e:
logger.warning(
Expand Down
8 changes: 7 additions & 1 deletion ginga/util/io/io_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ginga.table.AstroTable import AstroTable

from ginga.misc import Bunch
from ginga.util import iohelper
from ginga.util import iohelper, loader
from ginga.util.io import io_base

fits_configured = False
Expand Down Expand Up @@ -55,6 +55,9 @@ def use(fitspkg, raise_err=True):
if have_astropy:
fitsLoaderClass = AstropyFitsFileHandler
fits_configured = True
# set this opener as the priority one
loader.add_opener(fitsLoaderClass, fitsLoaderClass.mimetypes,
priority=-99, note=fitsLoaderClass.__doc__)
return True

elif raise_err:
Expand All @@ -67,6 +70,9 @@ def use(fitspkg, raise_err=True):
if have_fitsio:
fitsLoaderClass = FitsioFileHandler
fits_configured = True
# set this opener as the priority one
loader.add_opener(fitsLoaderClass, fitsLoaderClass.mimetypes,
priority=-99, note=fitsLoaderClass.__doc__)
return True

elif raise_err:
Expand Down
22 changes: 11 additions & 11 deletions ginga/util/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,16 @@ def add_opener(opener, mimetypes, priority=0, note=''):
global loader_by_mimetype, loader_registry
if opener.name in loader_registry:
loader_rec = loader_registry[opener.name]
loader_rec.mimetypes.update(set(mimetypes))
else:
loader_rec = Bunch.Bunch(name=opener.name, opener=opener,
mimetypes=mimetypes, priority=priority,
note=note)
mimetypes=set(mimetypes), note=note)
loader_registry[opener.name] = loader_rec

mimetype_rec = Bunch.Bunch(opener=loader_rec, priority=priority)
for mimetype in mimetypes:
bnchs = loader_by_mimetype.setdefault(mimetype, [])
if loader_rec not in bnchs:
bnchs.append(loader_rec)
bnchs.sort(key=lambda bnch: bnch.priority)
bnchs = loader_by_mimetype.setdefault(mimetype, {})
bnchs[opener.name] = mimetype_rec


def get_opener(name):
Expand All @@ -128,14 +127,15 @@ def get_openers(mimetype):
files, with all but the best (matching) priority removed.
"""
global loader_by_mimetype
bnchs = loader_by_mimetype.setdefault(mimetype, [])
if len(bnchs) <= 1:
return bnchs
bnchs = loader_by_mimetype.setdefault(mimetype, {})
if len(bnchs) == 0:
return []

# if there is more than one possible opener, return the list of
# those that have the best (i.e. lowest) equal priority
priority = min([bnch.priority for bnch in bnchs])
return [bnch for bnch in bnchs if bnch.priority <= priority]
priority = min([bnch.priority for bnch in bnchs.values()])
return [loader_registry[name] for name, bnch in bnchs.items()
if bnch.priority <= priority]


def get_all_openers():
Expand Down

0 comments on commit 2004e10

Please sign in to comment.