~ubuntu-branches/ubuntu/raring/software-center/raring-proposed

« back to all changes in this revision

Viewing changes to apt_xapian_index_plugin/software_center.py

  • Committer: Package Import Robot
  • Author(s): Michael Vogt
  • Date: 2012-10-11 15:33:05 UTC
  • mfrom: (195.1.18 quantal)
  • Revision ID: package-import@ubuntu.com-20121011153305-fm5ln7if3rpzts4n
Tags: 5.4.1.1
* lp:~mvo/software-center/reinstall-previous-purchase-token-fix:
  - fix reinstall previous purchases that have a system-wide
    license key LP: #1065481
* lp:~mvo/software-center/lp1060106:
  - Add missing gettext init for utils/update-software-center-agent
    (LP: #1060106)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# add software-center custom metadata to the index
 
2
 
 
3
import apt
 
4
import os
 
5
import sys
 
6
import xapian
 
7
 
 
8
sys.path.insert(0, "/usr/share/software-center")
 
9
from softwarecenter.enums import (
 
10
    CustomKeys,
 
11
    XapianValues,
 
12
    )
 
13
from softwarecenter.db.update import (
 
14
    WEIGHT_DESKTOP_NAME,
 
15
    get_pkgname_terms,
 
16
    )
 
17
from softwarecenter.distro import get_distro
 
18
 
 
19
 
 
20
class SoftwareCenterMetadataPlugin:
 
21
 
 
22
    def info(self):
 
23
        """
 
24
        Return general information about the plugin.
 
25
 
 
26
        The information returned is a dict with various keywords:
 
27
 
 
28
         timestamp (required)
 
29
           the last modified timestamp of this data source.  This will be used
 
30
           to see if we need to update the database or not.  A timestamp of 0
 
31
           means that this data source is either missing or always up to date.
 
32
         values (optional)
 
33
           an array of dicts { name: name, desc: description }, one for every
 
34
           numeric value indexed by this data source.
 
35
 
 
36
        Note that this method can be called before init.  The idea is that, if
 
37
        the timestamp shows that this plugin is currently not needed, then the
 
38
        long initialisation can just be skipped.
 
39
        """
 
40
        file = apt.apt_pkg.config.find_file("Dir::Cache::pkgcache")
 
41
        if not os.path.exists(file):
 
42
            return dict(timestamp=0)
 
43
        return dict(timestamp=os.path.getmtime(file))
 
44
 
 
45
    def init(self, info, progress):
 
46
        """
 
47
        If needed, perform long initialisation tasks here.
 
48
 
 
49
        info is a dictionary with useful information.  Currently it contains
 
50
        the following values:
 
51
 
 
52
          "values": a dict mapping index mnemonics to index numbers
 
53
 
 
54
        The progress indicator can be used to report progress.
 
55
        """
 
56
        self.indexer = xapian.TermGenerator()
 
57
 
 
58
    def doc(self):
 
59
        """
 
60
        Return documentation information for this data source.
 
61
 
 
62
        The documentation information is a dictionary with these keys:
 
63
          name: the name for this data source
 
64
          shortDesc: a short description
 
65
          fullDoc: the full description as a chapter in ReST format
 
66
        """
 
67
        return dict(
 
68
            name="SoftwareCenterMetadata",
 
69
            shortDesc="SoftwareCenter meta information",
 
70
            fullDoc="""
 
71
            Software-center metadata
 
72
            It uses the prefixes:
 
73
              AA for the Application name
 
74
              AP for the Package name
 
75
              AC for the categories
 
76
              AT to "application" for applications
 
77
            It sets the following xapian values from the software-center
 
78
            enums:
 
79
              XapianValues.ICON
 
80
              XapianValues.ICON_NEEDS_DOWNLOAD
 
81
              XapianValues.ICON_URL
 
82
              XapianValues.SCREENSHOT_URLS
 
83
              XapianValues.THUMBNAIL_URL
 
84
            """)
 
85
 
 
86
    def index(self, document, pkg):
 
87
        """
 
88
        Update the document with the information from this data source.
 
89
 
 
90
        document  is the document to update
 
91
        pkg       is the python-apt Package object for this package
 
92
        """
 
93
        ver = pkg.candidate
 
94
        # if there is no version or the AppName custom key is not
 
95
        # found we can skip the pkg
 
96
        if ver is None or not CustomKeys.APPNAME in ver.record:
 
97
            return
 
98
        # we want to index the following custom fields:
 
99
        #   XB-AppName,
 
100
        #   XB-Icon,
 
101
        #   XB-Screenshot-Url,
 
102
        #   XB-Thumbnail-Url,
 
103
        #   XB-Category
 
104
        if CustomKeys.APPNAME in ver.record:
 
105
            name = ver.record[CustomKeys.APPNAME]
 
106
            self.indexer.set_document(document)
 
107
            # add s-c values/terms for the name
 
108
            document.add_term("AA"+name)
 
109
            document.add_value(XapianValues.APPNAME, name)
 
110
            for t in get_pkgname_terms(pkg.name):
 
111
                document.add_term(t)
 
112
            self.indexer.index_text_without_positions(
 
113
                name, WEIGHT_DESKTOP_NAME)
 
114
            # we pretend to be an application
 
115
            document.add_term("AT" + "application")
 
116
            # and we inject a custom component value to indicate "independent"
 
117
            document.add_value(XapianValues.ARCHIVE_SECTION, "independent")
 
118
        if CustomKeys.ICON in ver.record:
 
119
            icon = ver.record[CustomKeys.ICON]
 
120
            document.add_value(XapianValues.ICON, icon)
 
121
            # calculate the url and add it (but only if there actually is
 
122
            # a url)
 
123
            try:
 
124
                distro = get_distro()
 
125
                if distro:
 
126
                    base_uri = ver.uri
 
127
                    # new python-apt returns None instead of StopIteration
 
128
                    if base_uri:
 
129
                        url = distro.get_downloadable_icon_url(base_uri, icon)
 
130
                        document.add_value(XapianValues.ICON_URL, url)
 
131
            except StopIteration:
 
132
                pass
 
133
        if CustomKeys.SCREENSHOT_URLS in ver.record:
 
134
            screenshot_url = ver.record[CustomKeys.SCREENSHOT_URLS]
 
135
            document.add_value(XapianValues.SCREENSHOT_URLS, screenshot_url)
 
136
        if CustomKeys.THUMBNAIL_URL in ver.record:
 
137
            url = ver.record[CustomKeys.THUMBNAIL_URL]
 
138
            document.add_value(XapianValues.THUMBNAIL_URL, url)
 
139
        if CustomKeys.CATEGORY in ver.record:
 
140
            categories_str = ver.record[CustomKeys.CATEGORY]
 
141
            for cat in categories_str.split(";"):
 
142
                if cat:
 
143
                    document.add_term("AC" + cat.lower())
 
144
 
 
145
    def indexDeb822(self, document, pkg):
 
146
        """
 
147
        Update the document with the information from this data source.
 
148
 
 
149
        This is alternative to index, and it is used when indexing with package
 
150
        data taken from a custom Packages file.
 
151
 
 
152
        document  is the document to update
 
153
        pkg       is the Deb822 object for this package
 
154
        """
 
155
        # NOTHING here, does not make sense for non-downloadable data
 
156
        return
 
157
 
 
158
 
 
159
def init():
 
160
    """
 
161
    Create and return the plugin object.
 
162
    """
 
163
    return SoftwareCenterMetadataPlugin()