~gilir/sessioninstaller/defer-migration

« back to all changes in this revision

Viewing changes to sessioninstaller/utils.py

  • Committer: sebi at glatzor
  • Date: 2010-05-24 06:42:23 UTC
  • Revision ID: sebi@glatzor.de-20100524064223-ewvu1t8zy4t3fswv
Make use of app-install-data and apt-xapian-index to render applications in the confirm dialog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
"""util -- Apt-Xapian-Index integration"""
 
4
# Copyright (c) 2010 Sebastian Heinlein <devel@glatzor.de>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
19
 
 
20
__author__  = "Sebastian Heinlein <devel@glatzor.de>"
 
21
__state__   = "experimental"
 
22
 
 
23
import logging
 
24
import os
 
25
 
 
26
from xdg.DesktopEntry import DesktopEntry
 
27
 
 
28
__ALL__ = ("get_package_desc", "APP_INSTALL_DATA", "AXI_DATABASE")
 
29
 
 
30
APP_INSTALL_DATA = "/usr/share/app-install/desktop"
 
31
AXI_DATABASE = "/var/lib/apt-xapian-index/index"
 
32
 
 
33
log = logging.getLogger("sessioninstaller")
 
34
 
 
35
try:
 
36
    import xapian
 
37
    os.stat(APP_INSTALL_DATA)
 
38
    axi = xapian.Database("/var/lib/apt-xapian-index/index")
 
39
except (ImportError, OSError, xapian.DatabaseOpeningError):
 
40
    log.warning("Falling back to package information")
 
41
    axi = None
 
42
 
 
43
_desktop_cache = {}
 
44
 
 
45
def _load(file_name):
 
46
    path = os.path.join(APP_INSTALL_DATA, file_name)
 
47
    return DesktopEntry(path)
 
48
 
 
49
def get_package_desc(pkg):
 
50
    """Return a pango markup description of the package.
 
51
    If the package provides one or more applications
 
52
    use the name and comment of the applications.
 
53
    """
 
54
    markup = ""
 
55
    if axi:
 
56
        for m in axi.postlist("XP" + pkg.name):
 
57
            doc = axi.get_document(m.docid)
 
58
            for term_iter in doc.termlist():
 
59
                app = False
 
60
                if term_iter.term.startswith("XDF"):
 
61
                    if markup:
 
62
                        markup += "\n\n"
 
63
                    file_name = term_iter.term[3:]
 
64
                    de = _desktop_cache.setdefault(file_name, _load(file_name))
 
65
                    name = de.getName()
 
66
                    comment = de.getComment()
 
67
                    if name and comment:
 
68
                        markup += "<b>%s</b>\n%s" % (name, comment)
 
69
    if not markup:
 
70
        markup = "<b>%s</b>\n%s" % (pkg.name, pkg.summary)
 
71
    return markup
 
72
 
 
73
# vim:ts=4:sw=4:et