home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / computerjanitor / cruft.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.7 KB  |  139 lines

  1. # cruft.py - base class for different kinds of cruft
  2. # Copyright (C) 2008  Canonical, Ltd.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16.  
  17. import computerjanitor
  18.  
  19.  
  20. class Cruft(object):
  21.  
  22.     """One piece of cruft to be cleaned out.
  23.  
  24.     A piece of cruft can be a file, a package, a configuration tweak
  25.     that is missing, or something else.
  26.     
  27.     This is a base class, which does nothing. Subclasses do the
  28.     actual work.
  29.     
  30.     """
  31.  
  32.     def get_prefix(self):
  33.         """Return the unique prefix used to group this type of cruft.
  34.         
  35.         For example, the .deb package called 'foo' would have a prefix
  36.         of 'deb'. This way, the package foo is not confused with the
  37.         file foo, or the username foo.
  38.         
  39.         Subclasses SHOULD define this. The default implementation
  40.         returns the name of the class, which is rarely useful to
  41.         the user.
  42.         
  43.         """
  44.         
  45.         return self.__class__.__name__
  46.     
  47.     def get_prefix_description(self):
  48.         """Return human-readable description of class of cruft."""
  49.         return self.get_description()
  50.     
  51.     def get_shortname(self):
  52.         """Return the name of this piece of cruft.
  53.         
  54.         The name should be something that the user will understand.
  55.         For example, it might be the name of a package, or the full
  56.         path to a file.
  57.         
  58.         The name should be unique within the unique prefix returned
  59.         by get_prefix. The prefix MUST NOT be included by this method,
  60.         the get_name() method does that instead. The intent is that
  61.         get_shortname() will be used by the user interface in contexts
  62.         where the prefix is shown separately from the short name,
  63.         and get_name() when a single string is used.
  64.         
  65.         Subclasses MUST define this. The default implementation
  66.         raises an exception.
  67.         
  68.         """
  69.  
  70.         raise computerjanitor.UnimplementedMethod(self.get_shortname)
  71.  
  72.     def get_name(self):
  73.         """Return prefix plus name.
  74.         
  75.         See get_prefix() and get_shortname() for a discussion of
  76.         the prefix and the short name. This method will return
  77.         the prefix, a colon, and the short name.
  78.  
  79.         The long name will used to store state/configuration data:
  80.         _this_ package should not be removed.
  81.         
  82.         """
  83.  
  84.         return "%s:%s" % (self.get_prefix(), self.get_shortname())
  85.         
  86.     def get_description(self):
  87.         """Return a description of this piece of cruft.
  88.         
  89.         This may be arbitrarily long. The user interface will take
  90.         care of breaking it into lines or otherwise presenting it to
  91.         the user in a nice manner. The description should be plain
  92.         text, in UTF-8.
  93.         
  94.         The default implementation returns the empty string. Subclasses
  95.         MAY override this as they wish.
  96.         
  97.         """
  98.         
  99.         return ""
  100.     
  101.     def get_disk_usage(self):
  102.         """Return amount of disk space reserved by this piece of cruft.
  103.         
  104.         The unit is bytes.
  105.         
  106.         The disk space in question should be the amount that will be
  107.         freed if the cruft is cleaned up. The amount may be an estimate
  108.         (read: guess). It is intended to be shown to the user to help
  109.         them decide what to remove and what to keep.
  110.         
  111.         This will also be used by the user interface to better
  112.         estimate how much remaining time there is when cleaning
  113.         up a lot of cruft.
  114.         
  115.         For some types of cruft, this is not applicable and they should
  116.         return None. The base class implementation does that, so
  117.         subclasses MUST define this method if it is useful for them to
  118.         return something else.
  119.         
  120.         The user interface will distinguish between None (not
  121.         applicable) and 0 (no disk space being used).
  122.         
  123.         """
  124.         
  125.         return None
  126.     
  127.     def cleanup(self):
  128.         """Clean up this piece of cruft.
  129.         
  130.         Depending on the type of cruft, this may mean removing files,
  131.         packages, modifying configuration files, or something else.
  132.         
  133.         The default implementation raises an exception. Subclasses
  134.         MUST override this.
  135.         
  136.         """
  137.         
  138.         raise computerjanitor.UnimplementedMethod(self.cleanup)
  139.