~ubuntu-branches/ubuntu/saucy/apt-xapian-index/saucy

« back to all changes in this revision

Viewing changes to plugins/aliases.py

  • Committer: Bazaar Package Importer
  • Author(s): Enrico Zini
  • Date: 2010-05-16 09:33:58 UTC
  • mfrom: (4.1.5 squeeze)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20100516093358-xvbshas89apqnvgj
Tags: 0.35
* Tolerate (and if --verbose, report) .desktop file with invalid popcon
  fields
* Added missing import. Closes: #581736
* Run update-python-modules -p before updating the index in postinst.
  Closes: #581811

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import xapian
 
2
import os, os.path
 
3
 
 
4
AXI_ALIASES = os.environ.get("AXI_ALIASES", "/etc/apt-xapian-index/aliases/:/usr/share/apt-xapian-index/aliases/")
 
5
 
 
6
def read_db(progress=None):
 
7
    aliases = []
 
8
    maxts = 0
 
9
    for d in AXI_ALIASES.split(":"):
 
10
        if not os.path.isdir(d): continue
 
11
        for f in os.listdir(d):
 
12
            if f[0] == '.': continue
 
13
            fname = os.path.join(d, f)
 
14
            ts = os.path.getmtime(fname)
 
15
            if ts > maxts:
 
16
                maxts = ts
 
17
            if progress: progress.verbose("Reading aliases from %s..." % fname)
 
18
            for line in open(fname):
 
19
                line = line.split()
 
20
                # Skip comments and empty lines
 
21
                if not line or line[0][0] == '#': continue
 
22
                aliases.append(line)
 
23
    return maxts, aliases
 
24
 
 
25
class Aliases:
 
26
    def __init__(self, maxts, db):
 
27
        self.maxts = maxts
 
28
        self.db = db
 
29
 
 
30
    def info(self):
 
31
        """
 
32
        Return general information about the plugin.
 
33
 
 
34
        The information returned is a dict with various keywords:
 
35
 
 
36
         timestamp (required)
 
37
           the last modified timestamp of this data source.  This will be used
 
38
           to see if we need to update the database or not.  A timestamp of 0
 
39
           means that this data source is either missing or always up to date.
 
40
         values (optional)
 
41
           an array of dicts { name: name, desc: description }, one for every
 
42
           numeric value indexed by this data source.
 
43
 
 
44
        Note that this method can be called before init.  The idea is that, if
 
45
        the timestamp shows that this plugin is currently not needed, then the
 
46
        long initialisation can just be skipped.
 
47
        """
 
48
        return dict(timestamp = self.maxts)
 
49
 
 
50
    def init(self, info, progress):
 
51
        """
 
52
        If needed, perform long initialisation tasks here.
 
53
 
 
54
        info is a dictionary with useful information.  Currently it contains
 
55
        the following values:
 
56
 
 
57
          "values": a dict mapping index mnemonics to index numbers
 
58
 
 
59
        The progress indicator can be used to report progress.
 
60
        """
 
61
        pass
 
62
 
 
63
    def send_extra_info(self, db=None, **kw):
 
64
        """
 
65
        Receive extra parameters from the indexer.
 
66
 
 
67
        This may be called more than once, but after init().
 
68
 
 
69
        We are using this to get the database instance
 
70
        """
 
71
        if db is not None:
 
72
            for row in self.db:
 
73
                for a in row[1:]:
 
74
                    db.add_synonym(row[0], a)
 
75
 
 
76
    def doc(self):
 
77
        """
 
78
        Return documentation information for this data source.
 
79
 
 
80
        The documentation information is a dictionary with these keys:
 
81
          name: the name for this data source
 
82
          shortDesc: a short description
 
83
          fullDoc: the full description as a chapter in ReST format
 
84
        """
 
85
        return dict(
 
86
            name = "Package aliases",
 
87
            shortDesc = "aliases for well known programs",
 
88
            fullDoc = """
 
89
            The Aliases data source does not change documents in the index, but
 
90
            adds synonims to the database. Synonims allow to obtain good
 
91
            results while looking for well-know software names, even if such
 
92
            software does not exist in Debian.
 
93
            """
 
94
        )
 
95
 
 
96
    def index(self, document, pkg):
 
97
        """
 
98
        Update the document with the information from this data source.
 
99
 
 
100
        document  is the document to update
 
101
        pkg       is the python-apt Package object for this package
 
102
        """
 
103
        pass
 
104
 
 
105
    def indexDeb822(self, document, pkg):
 
106
        """
 
107
        Update the document with the information from this data source.
 
108
 
 
109
        This is alternative to index, and it is used when indexing with package
 
110
        data taken from a custom Packages file.
 
111
 
 
112
        document  is the document to update
 
113
        pkg       is the Deb822 object for this package
 
114
        """
 
115
        pass
 
116
 
 
117
def init(progress=None, **kw):
 
118
    """
 
119
    Create and return the plugin object.
 
120
    """
 
121
    maxts, db = read_db(progress)
 
122
    if not db: return None
 
123
    return Aliases(maxts, db)