~aptdaemon-developers/aptdaemon/smart-error

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""app -- Apt-Xapian-Index integration providing application abstraction"""
# Copyright (c) 2012 Sebastian Heinlein <devel@glatzor.de>
#
# 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
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

__author__  = "Sebastian Heinlein <devel@glatzor.de>"

import logging
import os
import weakref

from gi.repository import Gio

__ALL__ = ("get_package_desc", "APP_INSTALL_DATA", "AXI_DATABASE")

APP_INSTALL_DATA = "/usr/share/app-install/desktop"
AXI_DATABASE = "/var/lib/apt-xapian-index/index"

log = logging.getLogger("sessioninstaller")

try:
    import xapian
    os.stat(APP_INSTALL_DATA)
    axi = xapian.Database("/var/lib/apt-xapian-index/index")
except (ImportError, OSError, xapian.DatabaseOpeningError):
    log.warning("Falling back to package information")
    axi = None

_desktop_cache = weakref.WeakValueDictionary()

def _load(file_name):
    path = os.path.join(APP_INSTALL_DATA, file_name)
    return Gio.DesktopAppInfo.new_from_filename(path)

def get_app_info(pkg):
    """Return a list of Gio.DesktopAppInfo instances for the applications
    provided by the given package.

    :param pkg: The name of the package
    :raises: KeyError if information cannot be provided for the package
    """
    result = []
    found = False
    matches = axi.postlist("XP" + pkg)
    for match in matches:
        found = True
        doc = axi.get_document(match.docid)
        for term_iter in doc.termlist():
            app = False
            if term_iter.term.startswith("XDF"):
                file_name = term_iter.term[3:]
                entry = _desktop_cache.setdefault(file_name, _load(file_name))
                result.append(entry)
    if not found:
        raise KeyError("Package %s isn't known to the application mapper" % pkg)
    return result

# vim:ts=4:sw=4:et