home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / apt / gtk / widgets.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  16.0 KB  |  436 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''GObject-powered progress classes and a GTK+ status widget.'''
  5. from gettext import gettext as _
  6. import os
  7. import time
  8. import pygtk
  9. pygtk.require('2.0')
  10. import gtk
  11. import glib
  12. import gobject
  13. import pango
  14. import vte
  15. import apt
  16. import apt_pkg
  17.  
  18. def mksig(params = (), run = gobject.SIGNAL_RUN_FIRST, rettype = gobject.TYPE_NONE):
  19.     '''Simplified Create a gobject signal.
  20.  
  21.     This allows us to write signals easier, because we just need to define the
  22.     type of the parameters (in most cases).
  23.  
  24.     ``params`` is a tuple which defines the types of the arguments.
  25.     '''
  26.     return (run, rettype, params)
  27.  
  28.  
  29. class GOpProgress(gobject.GObject, apt.progress.OpProgress):
  30.     '''Operation progress with GObject signals.
  31.  
  32.     Signals:
  33.  
  34.         * status-changed(str: operation, int: percent)
  35.         * status-started()  - Not Implemented yet
  36.         * status-finished()
  37.  
  38.     '''
  39.     __gsignals__ = {
  40.         'status-changed': mksig((str, int)),
  41.         'status-started': mksig(),
  42.         'status-finished': mksig() }
  43.     
  44.     def __init__(self):
  45.         apt.progress.OpProgress.__init__(self)
  46.         gobject.GObject.__init__(self)
  47.         self._context = glib.main_context_default()
  48.  
  49.     
  50.     def update(self, percent):
  51.         '''Called to update the percentage done'''
  52.         self.emit('status-changed', self.op, percent)
  53.         while self._context.pending():
  54.             self._context.iteration()
  55.  
  56.     
  57.     def done(self):
  58.         '''Called when all operation have finished.'''
  59.         self.emit('status-finished')
  60.  
  61.  
  62.  
  63. class GInstallProgress(gobject.GObject, apt.progress.InstallProgress):
  64.     '''Installation progress with GObject signals.
  65.  
  66.     Signals:
  67.  
  68.         * status-changed(str: status, int: percent)
  69.         * status-started()
  70.         * status-finished()
  71.         * status-timeout()
  72.         * status-error()
  73.         * status-conffile()
  74.  
  75.     '''
  76.     INSTALL_TIMEOUT = 300
  77.     __gsignals__ = {
  78.         'status-changed': mksig((str, int)),
  79.         'status-started': mksig(),
  80.         'status-timeout': mksig(),
  81.         'status-error': mksig(),
  82.         'status-conffile': mksig(),
  83.         'status-finished': mksig() }
  84.     
  85.     def __init__(self, term):
  86.         apt.progress.InstallProgress.__init__(self)
  87.         gobject.GObject.__init__(self)
  88.         self.finished = False
  89.         self.time_last_update = time.time()
  90.         self.term = term
  91.         reaper = vte.reaper_get()
  92.         reaper.connect('child-exited', self.childExited)
  93.         self.env = [
  94.             'VTE_PTY_KEEP_FD=%s' % self.writefd,
  95.             'DEBIAN_FRONTEND=gnome',
  96.             'APT_LISTCHANGES_FRONTEND=gtk']
  97.         self._context = glib.main_context_default()
  98.  
  99.     
  100.     def childExited(self, term, pid, status):
  101.         '''Called when a child process exits'''
  102.         self.apt_status = os.WEXITSTATUS(status)
  103.         self.finished = True
  104.  
  105.     
  106.     def error(self, pkg, errormsg):
  107.         '''Called when an error happens.
  108.  
  109.         Emits: status-error()
  110.         '''
  111.         self.emit('status-error')
  112.  
  113.     
  114.     def conffile(self, current, new):
  115.         '''Called during conffile.
  116.  
  117.         Emits: status-conffile()
  118.         '''
  119.         self.emit('status-conffile')
  120.  
  121.     
  122.     def startUpdate(self):
  123.         '''Called when the update starts.
  124.  
  125.         Emits: status-started()
  126.         '''
  127.         self.emit('status-started')
  128.  
  129.     
  130.     def finishUpdate(self):
  131.         '''Called when the update finished.
  132.  
  133.         Emits: status-finished()
  134.         '''
  135.         self.emit('status-finished')
  136.  
  137.     
  138.     def statusChange(self, pkg, percent, status):
  139.         '''Called when the status changed.
  140.  
  141.         Emits: status-changed(status, percent)
  142.         '''
  143.         self.time_last_update = time.time()
  144.         self.emit('status-changed', status, percent)
  145.  
  146.     
  147.     def updateInterface(self):
  148.         '''Called periodically to update the interface.
  149.  
  150.         Emits: status-timeout() [When a timeout happens]
  151.         '''
  152.         apt.progress.InstallProgress.updateInterface(self)
  153.         while self._context.pending():
  154.             self._context.iteration()
  155.         if self.time_last_update + self.INSTALL_TIMEOUT < time.time():
  156.             self.emit('status-timeout')
  157.         
  158.  
  159.     
  160.     def fork(self):
  161.         '''Fork the process.'''
  162.         return self.term.forkpty(envv = self.env)
  163.  
  164.     
  165.     def waitChild(self):
  166.         '''Wait for the child process to exit.'''
  167.         while not self.finished:
  168.             self.updateInterface()
  169.         return self.apt_status
  170.  
  171.  
  172.  
  173. class GDpkgInstallProgress(apt.progress.DpkgInstallProgress, GInstallProgress):
  174.     '''An InstallProgress for local installations.
  175.  
  176.     Signals:
  177.  
  178.         * status-changed(str: status, int: percent)
  179.         * status-started()  - Not Implemented yet
  180.         * status-finished()
  181.         * status-timeout() - When the maintainer script hangs
  182.         * status-error() - When an error happens
  183.         * status-conffile() - On Conffile
  184.     '''
  185.     
  186.     def run(self, debfile):
  187.         '''Install the given package.'''
  188.         apt.progress.DpkgInstallProgress.run(self, debfile)
  189.  
  190.     
  191.     def updateInterface(self):
  192.         '''Called periodically to update the interface.
  193.  
  194.         Emits: status-timeout() [When a timeout happens]'''
  195.         apt.progress.DpkgInstallProgress.updateInterface(self)
  196.         if self.time_last_update + self.INSTALL_TIMEOUT < time.time():
  197.             self.emit('status-timeout')
  198.         
  199.  
  200.  
  201.  
  202. class GFetchProgress(gobject.GObject, apt.progress.FetchProgress):
  203.     '''A Fetch Progress with GObject signals.
  204.  
  205.     Signals:
  206.  
  207.         * status-changed(str: description, int: percent)
  208.         * status-started()
  209.         * status-finished()
  210.     '''
  211.     __gsignals__ = {
  212.         'status-changed': mksig((str, int)),
  213.         'status-started': mksig(),
  214.         'status-finished': mksig() }
  215.     
  216.     def __init__(self):
  217.         apt.progress.FetchProgress.__init__(self)
  218.         gobject.GObject.__init__(self)
  219.         self._continue = True
  220.         self._context = glib.main_context_default()
  221.  
  222.     
  223.     def start(self):
  224.         self.emit('status-started')
  225.  
  226.     
  227.     def stop(self):
  228.         self.emit('status-finished')
  229.  
  230.     
  231.     def cancel(self):
  232.         self._continue = False
  233.  
  234.     
  235.     def pulse(self):
  236.         apt.progress.FetchProgress.pulse(self)
  237.         currentItem = self.currentItems + 1
  238.         if currentItem > self.totalItems:
  239.             currentItem = self.totalItems
  240.         
  241.         if self.currentCPS > 0:
  242.             text = _('Downloading file %(current)li of %(total)li with %(speed)s/s') % {
  243.                 'current': currentItem,
  244.                 'total': self.totalItems,
  245.                 'speed': apt_pkg.SizeToStr(self.currentCPS) }
  246.         else:
  247.             text = _('Downloading file %(current)li of %(total)li') % {
  248.                 'current': currentItem,
  249.                 'total': self.totalItems }
  250.         self.emit('status-changed', text, self.percent)
  251.         while self._context.pending():
  252.             self._context.iteration()
  253.         return self._continue
  254.  
  255.  
  256.  
  257. class GtkAptProgress(gtk.VBox):
  258.     '''Graphical progress for installation/fetch/operations.
  259.  
  260.     This widget provides a progress bar, a terminal and a status bar for
  261.     showing the progress of package manipulation tasks.
  262.     '''
  263.     
  264.     def __init__(self):
  265.         gtk.VBox.__init__(self)
  266.         self.set_spacing(6)
  267.         self._expander = gtk.Expander(_('Details'))
  268.         self._terminal = vte.Terminal()
  269.         self._expander.add(self._terminal)
  270.         self._progressbar = gtk.ProgressBar()
  271.         self._label = gtk.Label()
  272.         attr_list = pango.AttrList()
  273.         attr_list.insert(pango.AttrStyle(pango.STYLE_ITALIC, 0, -1))
  274.         self._label.set_attributes(attr_list)
  275.         self._label.set_ellipsize(pango.ELLIPSIZE_END)
  276.         self._label.set_alignment(0, 0)
  277.         self.pack_start(self._progressbar, False)
  278.         self.pack_start(self._label, False)
  279.         self.pack_start(self._expander, False)
  280.         self._progress_open = GOpProgress()
  281.         self._progress_open.connect('status-changed', self._on_status_changed)
  282.         self._progress_open.connect('status-started', self._on_status_started)
  283.         self._progress_open.connect('status-finished', self._on_status_finished)
  284.         self._progress_fetch = GFetchProgress()
  285.         self._progress_fetch.connect('status-changed', self._on_status_changed)
  286.         self._progress_fetch.connect('status-started', self._on_status_started)
  287.         self._progress_fetch.connect('status-finished', self._on_status_finished)
  288.         self._progress_install = GInstallProgress(self._terminal)
  289.         self._progress_install.connect('status-changed', self._on_status_changed)
  290.         self._progress_install.connect('status-started', self._on_status_started)
  291.         self._progress_install.connect('status-finished', self._on_status_finished)
  292.         self._progress_install.connect('status-timeout', self._on_status_timeout)
  293.         self._progress_install.connect('status-error', self._on_status_timeout)
  294.         self._progress_install.connect('status-conffile', self._on_status_timeout)
  295.         self._progress_dpkg_install = GDpkgInstallProgress(self._terminal)
  296.         self._progress_dpkg_install.connect('status-changed', self._on_status_changed)
  297.         self._progress_dpkg_install.connect('status-started', self._on_status_started)
  298.         self._progress_dpkg_install.connect('status-finished', self._on_status_finished)
  299.         self._progress_dpkg_install.connect('status-timeout', self._on_status_timeout)
  300.         self._progress_dpkg_install.connect('status-error', self._on_status_timeout)
  301.         self._progress_dpkg_install.connect('status-conffile', self._on_status_timeout)
  302.  
  303.     
  304.     def clear(self):
  305.         '''Reset all status information.'''
  306.         self._label.set_label('')
  307.         self._progressbar.set_fraction(0)
  308.         self._expander.set_expanded(False)
  309.  
  310.     
  311.     def open(self):
  312.         '''Return the cache opening progress handler.'''
  313.         return self._progress_open
  314.  
  315.     open = property(open)
  316.     
  317.     def install(self):
  318.         '''Return the install progress handler.'''
  319.         return self._progress_install
  320.  
  321.     install = property(install)
  322.     
  323.     def dpkg_install(self):
  324.         '''Return the install progress handler for dpkg.'''
  325.         return self._dpkg_progress_install
  326.  
  327.     dpkg_install = property(dpkg_install)
  328.     
  329.     def fetch(self):
  330.         '''Return the fetch progress handler.'''
  331.         return self._progress_fetch
  332.  
  333.     fetch = property(fetch)
  334.     
  335.     def _on_status_started(self, progress):
  336.         '''Called when something starts.'''
  337.         self._on_status_changed(progress, _('Starting...'), 0)
  338.         while gtk.events_pending():
  339.             gtk.main_iteration()
  340.  
  341.     
  342.     def _on_status_finished(self, progress):
  343.         '''Called when something finished.'''
  344.         self._on_status_changed(progress, _('Complete'), 100)
  345.         while gtk.events_pending():
  346.             gtk.main_iteration()
  347.  
  348.     
  349.     def _on_status_changed(self, progress, status, percent):
  350.         '''Called when the status changed.'''
  351.         self._label.set_text(status)
  352.         if percent is None:
  353.             self._progressbar.pulse()
  354.         else:
  355.             self._progressbar.set_fraction(percent / 100)
  356.         while gtk.events_pending():
  357.             gtk.main_iteration()
  358.  
  359.     
  360.     def _on_status_timeout(self, progress):
  361.         '''Called when timeout happens.'''
  362.         self._expander.set_expanded(True)
  363.         while gtk.events_pending():
  364.             gtk.main_iteration()
  365.  
  366.     
  367.     def cancel_download(self):
  368.         '''Cancel a currently running download.'''
  369.         self._progress_fetch.cancel()
  370.  
  371.     
  372.     def show_terminal(self, expanded = False):
  373.         '''Show the expander for the terminal.
  374.  
  375.         Show an expander with a terminal widget which provides a way
  376.         to interact with dpkg
  377.         '''
  378.         self._expander.show()
  379.         self._terminal.show()
  380.         self._expander.set_expanded(expanded)
  381.         while gtk.events_pending():
  382.             gtk.main_iteration()
  383.  
  384.     
  385.     def hide_terminal(self):
  386.         '''Hide the expander with the terminal widget.'''
  387.         self._expander.hide()
  388.         while gtk.events_pending():
  389.             gtk.main_iteration()
  390.  
  391.     
  392.     def show(self):
  393.         '''Show the Box'''
  394.         gtk.HBox.show(self)
  395.         self._label.show()
  396.         self._progressbar.show()
  397.         while gtk.events_pending():
  398.             gtk.main_iteration()
  399.  
  400.  
  401.  
  402. def _test():
  403.     '''Test function'''
  404.     import sys
  405.     DebPackage = DebPackage
  406.     import apt.debfile
  407.     win = gtk.Window()
  408.     apt_progress = GtkAptProgress()
  409.     win.set_title('GtkAptProgress Demo')
  410.     win.add(apt_progress)
  411.     apt_progress.show()
  412.     win.show()
  413.     cache = apt.cache.Cache(apt_progress.open)
  414.     pkg = cache['xterm']
  415.     if pkg.isInstalled:
  416.         pkg.markDelete()
  417.     else:
  418.         pkg.markInstall()
  419.     apt_progress.show_terminal(True)
  420.     
  421.     try:
  422.         cache.commit(apt_progress.fetch, apt_progress.install)
  423.     except Exception:
  424.         exc = None
  425.         print >>sys.stderr, 'Exception happened:', exc
  426.  
  427.     if len(sys.argv) > 1:
  428.         deb = DebPackage(sys.argv[1], cache)
  429.         deb.install(apt_progress.dpkg_install)
  430.     
  431.     gtk.main()
  432.  
  433. if __name__ == '__main__':
  434.     _test()
  435.  
  436.