[pitivi] Refactor the application launcher.



commit ac336a376ad64f8b31c6ab5c6103fd012369fa58
Author: Alessandro Decina <alessandro d gmail com>
Date:   Tue Jun 2 15:54:04 2009 +0200

    Refactor the application launcher.
---
 pitivi/application.py |  180 +++++++++++++++++++++++++++----------------------
 pitivi/check.py       |   21 +------
 2 files changed, 99 insertions(+), 102 deletions(-)

diff --git a/pitivi/application.py b/pitivi/application.py
index de4bc47..3071ffb 100644
--- a/pitivi/application.py
+++ b/pitivi/application.py
@@ -22,9 +22,12 @@
 """
 Main application
 """
-import os
 import gobject
 gobject.threads_init()
+import gtk
+from optparse import OptionParser
+import os
+import sys
 
 from pitivi.pitivigstutils import patch_gst_python
 patch_gst_python()
@@ -46,6 +49,7 @@ from pitivi.log import log
 from pitivi.project import Project
 from pitivi.formatters.format import get_formatter_for_uri
 from pitivi.formatters.base import FormatterError
+from pitivi.ui.mainwindow import PitiviMainWindow
 
 # FIXME : Speedup loading time
 # Currently we load everything in one go
@@ -266,102 +270,114 @@ class Pitivi(Loggable, Signallable):
 
 
 class InteractivePitivi(Pitivi):
-    """ Class for PiTiVi instances that provide user interaction """
+    usage = _("""
+      %prog [PROJECT_FILE]
+      %prog -i [-a] [MEDIA_FILE]...""")
+
+    description = _("""Starts the video editor, optionally loading PROJECT_FILE. If
+    no project is given, %prog creates a new project.
+    Alternatively, when -i is specified, arguments are treated as clips to be
+    imported into the project. If -a is specified, these clips will also be added to
+    the end of the project timeline.""")
+
+    import_help = _("""Import each MEDIA_FILE into the project.""")
+
+    add_help = _("""Add each MEDIA_FILE to timeline after importing.""")
+
+    def run(self, argv):
+        # check for dependencies
+        if not self._checkDependencies():
+            return
+
+        # create the ui
+        self.mainloop = gobject.MainLoop()
+        self.gui = PitiviMainWindow(self)
+        self.gui.show()
+
+        # parse cmdline options
+        parser = self._createOptionParser()
+        options, args = parser.parse_args(argv)
 
-    def __init__(self, sources=[], import_sources=False,
-            add_to_timeline=False, mainloop=None, *args, **kwargs):
-        from pitivi.ui.mainwindow import PitiviMainWindow
-        Pitivi.__init__(self, *args, **kwargs)
-        self._mainloop = None
-        self.mainloop = mainloop
+        # validate options
+        if not options.import_sources and options.add_to_timeline:
+            parser.error("-a requires -i")
+            return
 
-        self._gui = PitiviMainWindow(self)
-        self._gui.show()
+        if not options.import_sources and len(args) > 1:
+            parser.error("invalid arguments")
+            return
 
-        if not import_sources and sources:
-            project = sources[0]
+        if not options.import_sources and args:
+            # load a project file
+            project = args[0]
             self.loadProject(filepath=project)
         else:
-            uris = ["file://" + os.path.abspath(path) for path in sources]
-            if add_to_timeline:
-                self._uris = uris
-                self._duration = self.current.timeline.duration
-                self.current.sources.connect("file_added", self._addSourceCb)
-                self.current.sources.connect("discovery-error", self._discoveryErrorCb)
+            # load the passed filenames, optionally adding them to the timeline
+            # (useful during development)
+            uris = ["file://" + os.path.abspath(path) for path in args]
+            if options.add_to_timeline:
+                self.current.sources.connect("file_added",
+                        self._addSourceCb, uris)
+                self.current.sources.connect("discovery-error",
+                        self._discoveryErrorCb, uris)
             self.current.sources.addUris(uris)
 
-    def _addSourceCb(self, unused_sourcelist, factory):
-        if factory.name in self._uris:
-            self._uris.remove(factory.name)
-            if not self._uris:
-                self.current.sources.disconnect_by_function(self._addSourceCb)
-
-            t = self.current.timeline.addSourceFactory(factory)
-            t.start = self._duration
-            self._duration += t.duration
-
-    def _discoveryErrorCb(self, sourcelist, uri, error, debug):
-        if uri in self._uris:
-            self._uris.remove(uri)
-            if not self._uris:
-                self.current.sources.disconnect_by_function(self._discoveryErrorCb)
-
-    # properties
-
-    def _get_mainloop(self):
-        return self._mainloop
-
-    def _set_mainloop(self, mainloop):
-        if self._mainloop != None:
-            raise Exception("Mainloop already set !")
-        if mainloop == None:
-            mainloop = gobject.MainLoop()
-        self._mainloop = mainloop
-    mainloop = property(_get_mainloop, _set_mainloop,
-                        doc="The MainLoop running the program")
-
-    @property
-    def gui(self):
-        """The user interface"""
-        return self._gui
-
-    # PiTiVi method overrides
+        # run the mainloop
+        self.mainloop.run()
+
     def shutdown(self):
         if Pitivi.shutdown(self):
-            if self.mainloop:
-                self.mainloop.quit()
+            self.mainloop.quit()
             return True
+
         return False
 
-    def run(self):
-        if self.mainloop:
-            self.mainloop.run()
+    def _createOptionParser(self):
+        parser = OptionParser(self.usage, description=self.description)
+        parser.add_option("-i", "--import", help=self.import_help,
+                dest="import_sources", action="store_true", default=False)
+        parser.add_option("-a", "--add-to-timeline", help=self.add_help,
+                action="store_true", default=False)
+
+        return parser
+
 
-usage = _("""
-  %prog [PROJECT_FILE]
-  %prog -i [-a] [MEDIA_FILE]...""")
+    def _checkDependencies(self):
+        missing_deps = initial_checks()
+        if missing_deps:
+            message, detail = missing_deps
+            dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
+                                       buttons=gtk.BUTTONS_OK)
+            dialog.set_markup("<b>"+message+"</b>")
+            dialog.format_secondary_text(detail)
+            dialog.run()
 
-description = _("""Starts the video editor, optionally loading PROJECT_FILE. If
-no project is given, %prog creates a new project.
-Alternatively, when -i is specified, arguments are treated as clips to be
-imported into the project. If -a is specified, these clips will also be added to
-the end of the project timeline.""")
+            return False
+
+        return True
 
-import_help = _("""Import each MEDIA_FILE into the project.""")
+    def _addSourceCb(self, unused_sourcelist, factory, startup_uris):
+        if self._maybePopStartupUri(startup_uris, factory.uri):
+            self.current.timeline.addSourceFactory(factory)
 
-add_help = _("""Add each MEDIA_FILE to timeline after importing.""")
+    def _discoveryErrorCb(self, sourcelist, uri, error, debug, startup_uris):
+        self._maybePopStartupUri(startup_uris, uri)
+
+    def _maybePopStartupUri(self, startup_uris, uri):
+        try:
+            startup_uris.remove(uri)
+        except ValueError:
+            # uri is not a startup uri. This can happen if the user starts
+            # importing sources while sources specified at startup are still
+            # being processed. In practice this will never happen.
+            return False
+
+        if not startup_uris:
+            self.current.sources.disconnect_by_function(self._addSourceCb)
+            self.current.sources.disconnect_by_function(self._discoveryErrorCb)
+
+        return True
 
 def main(argv):
-    """ Start PiTiVi ! """
-    from optparse import OptionParser
-    initial_checks()
-    parser = OptionParser(usage, description=description)
-    parser.add_option("-i", "--import", help=import_help,
-            dest="import_sources", action="store_true", default=False)
-    parser.add_option("-a", "--add-to-timeline", help=add_help, 
-            action="store_true", default=False)
-    options, args = parser.parse_args(argv)
-    ptv = InteractivePitivi(sources=args[1:],
-            import_sources=options.import_sources,
-            add_to_timeline=options.add_to_timeline)
-    ptv.run()
+    ptv = InteractivePitivi()
+    ptv.run(sys.argv[1:])
diff --git a/pitivi/check.py b/pitivi/check.py
index b539a9b..b54e520 100644
--- a/pitivi/check.py
+++ b/pitivi/check.py
@@ -25,31 +25,12 @@ Runtime checks.
 
 import gtk
 import gst
-import sys
 
 from gettext import gettext as _
 
 from pitivi.instance import PiTiVi
 from pitivi.configure import APPNAME, PYGTK_REQ, PYGST_REQ, GST_REQ, GNONLIN_REQ, PYCAIRO_REQ
 
-def initial_checks():
-    """
-    do some basic check
-    If there is a problem, will display the error and exit.
-    """
-    res = _checks()
-    if not res:
-        return
-    message, detail = res
-    # TODO check if we're running graphically
-    dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
-                               buttons=gtk.BUTTONS_OK)
-    dialog.set_markup("<b>"+message+"</b>")
-    dialog.format_secondary_text(detail)
-    dialog.run()
-
-    sys.exit()
-
 def initiate_videosinks():
     """
     Test if the autovideosink element can initiate, return TRUE if it is the
@@ -115,7 +96,7 @@ def check_required_version(modulename):
             return [GNONLIN_REQ, gnlver]
     return [None, None]
 
-def _checks():
+def initial_checks():
     reg = gst.registry_get_default()
     if PiTiVi:
         return (_("%s is already running!") % APPNAME,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]