source: orange/Orange/OrangeCanvas/registry/cache.py @ 11098:b743937fe90a

Revision 11098:b743937fe90a, 1.5 KB checked in by Ales Erjavec <ales.erjavec@…>, 19 months ago (diff)

Added new widget/category description classes and new widget discovery.

This includes a Qt item model interface for the registry.

Line 
1"""
2Widget Registry cache.
3
4"""
5
6import os
7import cPickle
8import logging
9
10from .. import config
11
12log = logging.getLogger(__name__)
13
14
15def registry_cache_filename():
16    """Return the pickled registry cache filename. Also make sure the
17    containing directory is created if it does not exists.
18
19    """
20    cache_dir = config.cache_dir()
21    default = os.path.join(cache_dir, "registry-cache.pck")
22    cache_filename = config.rc.get("registry.registry-cache", default)
23    dirname = os.path.dirname(cache_filename)
24    if not os.path.exists(dirname):
25        log.info("Creating directory %r", dirname)
26        os.makedirs(dirname)
27    return cache_filename
28
29
30def registry_cache():
31    """Return the registry cache dictionary.
32    """
33    filename = registry_cache_filename()
34    log.debug("Loading widget registry cache (%r).", filename)
35    if os.path.exists(filename):
36        try:
37            return cPickle.load(open(filename, "rb"))
38        except Exception:
39            log.error("Could not load registry cache.", exc_info=True)
40
41    return {}
42
43
44def save_registry_cache(cache):
45    """Save (pickle) the registry cache. Return True on success,
46    False otherwise.
47
48    """
49    filename = registry_cache_filename()
50    log.debug("Saving widget registry cache with %i entries (%r).",
51              len(cache), filename)
52    try:
53        cPickle.dump(cache, open(filename, "wb"))
54        return True
55    except Exception:
56        log.error("Could not save registry cache", exc_info=True)
57    return False
Note: See TracBrowser for help on using the repository browser.