Merge lp:~ralsina/ubuntuone-control-panel/opt-parsing into lp:ubuntuone-control-panel

Proposed by Roberto Alsina
Status: Merged
Approved by: Roberto Alsina
Approved revision: 267
Merged at revision: 267
Proposed branch: lp:~ralsina/ubuntuone-control-panel/opt-parsing
Merge into: lp:ubuntuone-control-panel
Diff against target: 269 lines (+161/-36)
6 files modified
bin/ubuntuone-control-panel-qt (+1/-30)
run-tests (+1/-1)
ubuntuone/controlpanel/gui/qt/main/__init__.py (+36/-4)
ubuntuone/controlpanel/gui/qt/main/tests/__init__.py (+17/-0)
ubuntuone/controlpanel/gui/qt/main/tests/test_main.py (+105/-0)
ubuntuone/controlpanel/gui/qt/uniqueapp/linux.py (+1/-1)
To merge this branch: bzr merge lp:~ralsina/ubuntuone-control-panel/opt-parsing
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
dobey (community) Approve
Diego Sarmentero (community) Approve
Review via email: mp+94599@code.launchpad.net

Commit message

* Cleanup the ubuntuone-control-panel-qt script moving all logic into the main module.
* Adds tests for main()
* Parse Qt options correctly
* Move some code from UniqueApp to main where it belongs, and add tests for it
* Migrate to argparse

Description of the change

* Cleanup the ubuntuone-control-panel-qt script moving all logic into the main module.
* Adds tests for main()
* Parse Qt options correctly
* Move some code from UniqueApp to main where it belongs, and add tests for it
* Migrate to argparse

To post a comment you must log in.
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve
Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Any chance you migrate to argparse while you're at this branch? :-)

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Thanks for this work!

Could you please re-align all the options in parse_args? The multi-line statements are misaligned, for example, lines 80 to 84 should be:

80 + result.add_argument("--switch-to", dest="switch_to",
81 + metavar="PANEL_NAME", default="",
82 + help="Start Ubuntu One in the "
83 + "PANEL_NAME tab. Possible values are: "
84 + "dashboard, volumes, devices, applications")

or (the one I prefer):

80 + result.add_argument("--switch-to", dest="switch_to",
81 + metavar="PANEL_NAME", default="",
82 + help="Start Ubuntu One in the PANEL_NAME tab. "
83 + "Possible values are: dashboard, volumes, devices, applications")

(the chosen pattern should be applied to the rest of the arguments).

review: Needs Fixing
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

(the examples above can be properly read -regarding spacing- in the generated email with the review)

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks good!

review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

Attempt to merge into lp:ubuntuone-control-panel failed due to conflicts:

text conflict in bin/ubuntuone-control-panel-qt

267. By Roberto Alsina

merged trunk, resolved conflict

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bin/ubuntuone-control-panel-qt'
--- bin/ubuntuone-control-panel-qt 2012-02-28 14:49:21 +0000
+++ bin/ubuntuone-control-panel-qt 2012-02-28 16:47:17 +0000
@@ -25,37 +25,8 @@
25 import qt4reactor25 import qt4reactor
26 qt4reactor.install()26 qt4reactor.install()
2727
28
29from optparse import OptionParser
30
31from ubuntuone.controlpanel.gui.qt import main28from ubuntuone.controlpanel.gui.qt import main
3229
3330
34def parser_options():
35 """Parse command line parameters."""
36 usage = "Usage: %prog [option]"
37 result = OptionParser(usage=usage)
38 result.add_option("", "--switch-to", dest="switch_to", type="string",
39 metavar="PANEL_NAME", default="",
40 help="Start Ubuntu One in the "
41 "PANEL_NAME tab. Possible values are: "
42 "dashboard, volumes, devices, applications")
43 result.add_option("-a", "--alert", dest="alert", action="store_true",
44 default=False, help="Start Ubuntu One "
45 "alerting the user to its presence.")
46 result.add_option("--minimized", dest="minimized", action="store_true",
47 default=False, help="Start Ubuntu One "
48 "only in the notification area, with no visible window. "
49 "Implies --with-icon")
50 result.add_option("--with-icon", dest="with_icon", action="store_true",
51 default=False, help="Start Ubuntu One "
52 "with an icon in the notification area.")
53 return result
54
55
56if __name__ == "__main__":31if __name__ == "__main__":
57 parser = parser_options()32 main.main(sys.argv)
58 (options, args) = parser.parse_args(sys.argv)
59 main.main(switch_to=options.switch_to,
60 alert=options.alert, minimized=options.minimized,
61 with_icon=options.with_icon)
6233
=== modified file 'run-tests'
--- run-tests 2012-02-06 20:49:41 +0000
+++ run-tests 2012-02-28 16:47:17 +0000
@@ -16,7 +16,7 @@
16# You should have received a copy of the GNU General Public License along16# You should have received a copy of the GNU General Public License along
17# with this program. If not, see <http://www.gnu.org/licenses/>.17# with this program. If not, see <http://www.gnu.org/licenses/>.
1818
19QT_TESTS_PATH=ubuntuone/controlpanel/gui/qt/tests19QT_TESTS_PATH="ubuntuone/controlpanel/gui/qt/tests, ubuntuone/controlpanel/gui/qt/main/tests"
20GTK_TESTS_PATH=ubuntuone/controlpanel/gui/gtk/tests20GTK_TESTS_PATH=ubuntuone/controlpanel/gui/gtk/tests
21DBUS_TESTS_PATH=ubuntuone/controlpanel/dbustests21DBUS_TESTS_PATH=ubuntuone/controlpanel/dbustests
22WINDOWS_TESTS=test_windows.py22WINDOWS_TESTS=test_windows.py
2323
=== modified file 'ubuntuone/controlpanel/gui/qt/main/__init__.py'
--- ubuntuone/controlpanel/gui/qt/main/__init__.py 2012-02-06 21:10:10 +0000
+++ ubuntuone/controlpanel/gui/qt/main/__init__.py 2012-02-28 16:47:17 +0000
@@ -16,10 +16,12 @@
1616
17"""Provide the correct ui main module."""17"""Provide the correct ui main module."""
1818
19import argparse
19import sys20import sys
2021
21from PyQt4 import QtCore22from PyQt4 import QtCore
2223
24from ubuntuone.controlpanel.gui.qt.gui import start
23# Module used to include the resources into this file25# Module used to include the resources into this file
24# Unused import images_rc, pylint: disable=W061126# Unused import images_rc, pylint: disable=W0611
25from ubuntuone.controlpanel.gui.qt.ui import images_rc27from ubuntuone.controlpanel.gui.qt.ui import images_rc
@@ -36,19 +38,49 @@
36# pylint: enable=C010338# pylint: enable=C0103
3739
3840
39def main(switch_to='', alert=False, minimized=False, with_icon=False):41def parser_options():
42 """Parse command line parameters."""
43 result = argparse.ArgumentParser()
44 result.add_argument("--switch-to", dest="switch_to",
45 metavar="PANEL_NAME", default="",
46 help="Start Ubuntu One in the "
47 "PANEL_NAME tab. Possible values are: "
48 "dashboard, volumes, devices, applications")
49 result.add_argument("-a", "--alert", dest="alert", action="store_true",
50 default=False,
51 help="Start Ubuntu One "
52 "alerting the user to its presence.")
53 result.add_argument("--minimized", dest="minimized", action="store_true",
54 default=False,
55 help="Start Ubuntu One "
56 "only in the notification area, with no "
57 "visible window. Implies --with-icon")
58 result.add_argument("--with-icon", dest="with_icon", action="store_true",
59 default=False,
60 help="Start Ubuntu One "
61 "with an icon in the notification area.")
62 return result
63
64
65def main(args):
40 """Start the Qt mainloop and open the main window."""66 """Start the Qt mainloop and open the main window."""
41 # The following cannot be imported outside this function67 # The following cannot be imported outside this function
42 # because u1trial already provides a reactor.68 # because u1trial already provides a reactor.
4369
44 app = UniqueApplication(sys.argv, "ubuntuone-control-panel")70 args = ['ubuntuone-installer'] + args[1:]
71 app = UniqueApplication(args, "ubuntuone-control-panel")
72 parser = parser_options()
73 # Use only the arguments that are not recognized by Qt
74 args = parser.parse_args(args=[unicode(x) for x in app.arguments()[1:]])
75 _ = args.switch_to
76 _ = args.alert
77 minimized = args.minimized
78 with_icon = args.with_icon
45 source.main(app)79 source.main(app)
4680
47 qss = QtCore.QResource(":/ubuntuone.qss")81 qss = QtCore.QResource(":/ubuntuone.qss")
48 app.setStyleSheet(qss.data())82 app.setStyleSheet(qss.data())
4983
50 from ubuntuone.controlpanel.gui.qt.gui import start
51
52 # Unused variable 'window', 'icon', pylint: disable=W061284 # Unused variable 'window', 'icon', pylint: disable=W0612
53 icon, window = start(lambda: source.main_quit(app),85 icon, window = start(lambda: source.main_quit(app),
54 minimized=minimized, with_icon=with_icon)86 minimized=minimized, with_icon=with_icon)
5587
=== added directory 'ubuntuone/controlpanel/gui/qt/main/tests'
=== added file 'ubuntuone/controlpanel/gui/qt/main/tests/__init__.py'
--- ubuntuone/controlpanel/gui/qt/main/tests/__init__.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/controlpanel/gui/qt/main/tests/__init__.py 2012-02-28 16:47:17 +0000
@@ -0,0 +1,17 @@
1# -*- coding: utf-8 -*-
2#
3# Copyright 2011-2012 Canonical Ltd.
4#
5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranties of
11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12# PURPOSE. See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program. If not, see <http://www.gnu.org/licenses/>.
16
17"""The test suite for the Qt UI for the control panel for Ubuntu One."""
018
=== added file 'ubuntuone/controlpanel/gui/qt/main/tests/test_main.py'
--- ubuntuone/controlpanel/gui/qt/main/tests/test_main.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/controlpanel/gui/qt/main/tests/test_main.py 2012-02-28 16:47:17 +0000
@@ -0,0 +1,105 @@
1# -*- coding: utf-8 -*-
2#
3# Copyright 2012 Canonical Ltd.
4#
5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranties of
11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12# PURPOSE. See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program. If not, see <http://www.gnu.org/licenses/>.
16
17"""Tests for the control panel's Qt main."""
18
19from twisted.internet.defer import inlineCallbacks
20
21from ubuntuone.controlpanel.gui.qt import main
22from ubuntuone.controlpanel.tests import TestCase
23
24
25class FakeApplication(object):
26
27 """A fake application."""
28
29 def __init__(self):
30 self.args = None
31 self.style = None
32
33 def __call__(self, argv, *args, **kwargs):
34 """Fake arg filtering function."""
35 if '-title' in argv:
36 argv.remove('-title')
37 self.args = (argv, args, kwargs)
38 return self
39
40 # pylint: disable=C0103
41 def setStyleSheet(self, *args, **kwargs):
42 """Fake setStyleSheet."""
43 self.style = (args, kwargs)
44 # pylint: enable=C0103
45
46 def arguments(self):
47 """Fake arguments function."""
48 return self.args[0]
49
50 def exec_(self):
51 """Fake event loop."""
52 pass
53
54
55class FakeStart(object):
56
57 """Fake start function."""
58
59 def __init__(self):
60 self.args = None
61
62 def __call__(self, *args, **kwargs):
63 self.args = (args, kwargs)
64 return None, None
65
66
67class MainTestCase(TestCase):
68
69 """Test the argument parsing and passing in main."""
70
71 @inlineCallbacks
72 def setUp(self):
73 yield super(MainTestCase, self).setUp()
74 self.app = FakeApplication()
75 self.start = FakeStart()
76 self.patch(main, "UniqueApplication", self.app)
77 self.patch(main, "start", self.start)
78
79 def test_wm_class(self):
80 """Test that we set the 1st argument, used for WM_CLASS, correctly."""
81 main.main([])
82 self.assertEqual(self.app.args,
83 (['ubuntuone-installer'], ('ubuntuone-control-panel',), {}))
84
85 def test_title_not_fail(self):
86 """Ensure -title is removed before it gets to OptParser."""
87 main.main(["blah", "-title"])
88 # Did not crash!
89
90 def test_minimized_option(self):
91 """Ensure the --minimized option is parsed and passed correctly."""
92 main.main(["blah", "--minimized"])
93 self.assertEqual(self.start.args[1],
94 {'minimized': True, 'with_icon': False})
95
96 def test_with_icon_option(self):
97 """Ensure the --minimized option is parsed and passed correctly."""
98 main.main(["blah", "--with-icon"])
99 self.assertEqual(self.start.args[1],
100 {'minimized': False, 'with_icon': True})
101
102 def test_style_loads(self):
103 """Ensure the stylesheet is loaded."""
104 main.main([])
105 self.assertTrue(self.app.style)
0106
=== modified file 'ubuntuone/controlpanel/gui/qt/uniqueapp/linux.py'
--- ubuntuone/controlpanel/gui/qt/uniqueapp/linux.py 2012-02-23 19:49:25 +0000
+++ ubuntuone/controlpanel/gui/qt/uniqueapp/linux.py 2012-02-28 16:47:17 +0000
@@ -26,4 +26,4 @@
26 new_instance = QtCore.pyqtSignal()26 new_instance = QtCore.pyqtSignal()
2727
28 def __init__(self, argv, key):28 def __init__(self, argv, key):
29 super(UniqueApplication, self).__init__(['ubuntuone-installer'] + argv)29 super(UniqueApplication, self).__init__(argv)

Subscribers

People subscribed via source and target branches