home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / apt-xapian-index / plugins / sizes.py < prev    next >
Encoding:
Python Source  |  2009-01-31  |  3.8 KB  |  110 lines

  1. import apt
  2. import xapian
  3. import os, os.path
  4.  
  5. class Sizes:
  6.     def info(self):
  7.         """
  8.         Return general information about the plugin.
  9.  
  10.         The information returned is a dict with various keywords:
  11.          
  12.          timestamp (required)
  13.            the last modified timestamp of this data source.  This will be used
  14.            to see if we need to update the database or not.  A timestamp of 0
  15.            means that this data source is either missing or always up to date.
  16.          values (optional)
  17.            an array of dicts { name: name, desc: description }, one for every
  18.            numeric value indexed by this data source.
  19.  
  20.         Note that this method can be called before init.  The idea is that, if
  21.         the timestamp shows that this plugin is currently not needed, then the
  22.         long initialisation can just be skipped.
  23.         """
  24.         file = apt.apt_pkg.Config.FindFile("Dir::Cache::pkgcache")
  25.         return dict(
  26.                 timestamp = os.path.getmtime(file),
  27.                 values = [
  28.                     dict(name = "installedsize", desc = "installed size"),
  29.                     dict(name = "packagesize", desc = "package size")
  30.                 ])
  31.  
  32.     def doc(self):
  33.         """
  34.         Return documentation information for this data source.
  35.  
  36.         The documentation information is a dictionary with these keys:
  37.           name: the name for this data source
  38.           shortDesc: a short description
  39.           fullDoc: the full description as a chapter in ReST format
  40.         """
  41.         return dict(
  42.             name = "Sizes",
  43.             shortDesc = "package sizes indexed as values",
  44.             fullDoc = """
  45.             The Sizes data source indexes the package size and the installed
  46.             size as the ``packagesize`` and ``installedsize`` Xapian values.
  47.             """
  48.         )
  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.         # Read the value indexes we will use
  62.         values = info['values']
  63.         self.val_inst_size = values.get("installedsize", -1)
  64.         self.val_pkg_size = values.get("packagesize", -1)
  65.  
  66.     def index(self, document, pkg):
  67.         """
  68.         Update the document with the information from this data source.
  69.  
  70.         document  is the document to update
  71.         pkg       is the python-apt Package object for this package
  72.         """
  73.         try:
  74.             instSize = pkg.installedSize
  75.             pkgSize = pkg.packageSize
  76.         except:
  77.             return
  78.  
  79.         if self.val_inst_size != -1:
  80.             document.add_value(self.val_inst_size, xapian.sortable_serialise(instSize));
  81.         if self.val_pkg_size != -1:
  82.             document.add_value(self.val_pkg_size, xapian.sortable_serialise(pkgSize));
  83.  
  84.     def indexDeb822(self, document, pkg):
  85.         """
  86.         Update the document with the information from this data source.
  87.  
  88.         This is alternative to index, and it is used when indexing with package
  89.         data taken from a custom Packages file.
  90.  
  91.         document  is the document to update
  92.         pkg       is the Deb822 object for this package
  93.         """
  94.         try:
  95.             instSize = int(pkg["Installed-Size"])
  96.             pkgSize = int(pkg["Size"])
  97.         except:
  98.             return
  99.  
  100.         if self.val_inst_size != -1:
  101.             document.add_value(self.val_inst_size, xapian.sortable_serialise(instSize));
  102.         if self.val_pkg_size != -1:
  103.             document.add_value(self.val_pkg_size, xapian.sortable_serialise(pkgSize));
  104.  
  105. def init():
  106.     """
  107.     Create and return the plugin object.
  108.     """
  109.     return Sizes()
  110.