Merge lp:~flimm/quickly/better-new into lp:quickly

Proposed by David D Lowe
Status: Merged
Merge reported by: Rick Spencer
Merged at revision: not available
Proposed branch: lp:~flimm/quickly/better-new
Merge into: lp:quickly
Diff against target: 274 lines (+62/-85)
4 files modified
data/templates/ubuntu-application/project_root/bin/project_name (+27/-26)
data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py (+14/-24)
data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py (+16/-28)
data/templates/ubuntu-application/project_root/python/helpers.py (+5/-7)
To merge this branch: bzr merge lp:~flimm/quickly/better-new
Reviewer Review Type Date Requested Status
Rick Spencer Approve
Review via email: mp+21384@code.launchpad.net

Description of the change

Replaced New methods with __new__, it's more elegant.

To post a comment you must log in.
Revision history for this message
Rick Spencer (rick-rickspencer3) wrote :

awesome ... we will need to make 'add dialog' work this way as well, but should be simple. next will be updating the docs.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'data/templates/ubuntu-application/project_root/bin/project_name'
--- data/templates/ubuntu-application/project_root/bin/project_name 2010-03-09 09:36:08 +0000
+++ data/templates/ubuntu-application/project_root/bin/project_name 2010-03-15 16:53:12 +0000
@@ -31,31 +31,41 @@
3131
32from python_name import (32from python_name import (
33 Aboutcamel_case_nameDialog, Preferencescamel_case_nameDialog)33 Aboutcamel_case_nameDialog, Preferencescamel_case_nameDialog)
34from python_name.helpers import make_window34from python_name.helpers import get_builder
3535
3636
37class camel_case_nameWindow(gtk.Window):37class camel_case_nameWindow(gtk.Window):
38 __gtype_name__ = "camel_case_nameWindow"38 __gtype_name__ = "camel_case_nameWindow"
3939
40 def __init__(self):40 # To construct a new instance of this method, the following notable
41 """Construct a camel_case_nameWindow.41 # methods are called in this order:
4242 # __new__(cls)
43 This function is typically not called directly. Creation a43 # __init__(self)
44 camel_case_nameWindow requires rereading the associated ui file and44 # finish_initializing(self, builder)
45 parsing the ui definition externally, and then calling45 # __init__(self)
46 camel_case_nameWindow.finish_initializing().46 #
4747 # For this reason, it's recommended you leave __init__ empty and put
48 Use the convenience function Newcamel_case_nameWindow to create48 # your inialization code in finish_intializing
49 camel_case_nameWindow object.49
50 def __new__(cls):
51 """Special static method that's automatically called by Python when
52 constructing a new instance of this class.
53
54 Returns a fully instantiated camel_case_nameWindow object.
50 """55 """
51 pass56 builder = get_builder('camel_case_nameWindow')
57 new_object = builder.get_object("python_name_window")
58 new_object.finish_initializing(builder)
59 return new_object
5260
53 def finish_initializing(self, builder):61 def finish_initializing(self, builder):
54 """Called after we've finished initializing.62 """Called while initializing this instance in __new__
5563
56 finish_initalizing should be called after parsing the UI definition64 finish_initalizing should be called after parsing the UI definition
57 and creating a camel_case_nameWindow object with it in order to finish65 and creating a camel_case_nameWindow object with it in order to finish
58 initializing the start of the new camel_case_nameWindow instance.66 initializing the start of the new camel_case_nameWindow instance.
67
68 Put your initilization code in here and leave __init__ undefined.
59 """69 """
60 # Get a reference to the builder and set up the signals.70 # Get a reference to the builder and set up the signals.
61 self.builder = builder71 self.builder = builder
@@ -80,13 +90,13 @@
8090
81 def about(self, widget, data=None):91 def about(self, widget, data=None):
82 """Display the about box for project_name."""92 """Display the about box for project_name."""
83 about = Aboutcamel_case_nameDialog.NewAboutcamel_case_nameDialog()93 about = Aboutcamel_case_nameDialog.Aboutcamel_case_nameDialog()
84 response = about.run()94 response = about.run()
85 about.destroy()95 about.destroy()
8696
87 def preferences(self, widget, data=None):97 def preferences(self, widget, data=None):
88 """Display the preferences window for project_name."""98 """Display the preferences window for project_name."""
89 prefs = Preferencescamel_case_nameDialog.NewPreferencescamel_case_nameDialog()99 prefs = Preferencescamel_case_nameDialog.Preferencescamel_case_nameDialog()
90 response = prefs.run()100 response = prefs.run()
91 if response == gtk.RESPONSE_OK:101 if response == gtk.RESPONSE_OK:
92 # Make any updates based on changed preferences here.102 # Make any updates based on changed preferences here.
@@ -102,15 +112,6 @@
102 # Clean up code for saving application state should be added here.112 # Clean up code for saving application state should be added here.
103 gtk.main_quit()113 gtk.main_quit()
104114
105
106def Newcamel_case_nameWindow():
107 """Newcamel_case_nameWindow - returns a fully instantiated
108 camel_case_nameWindow object. Use this function rather than
109 creating a camel_case_nameWindow directly.
110 """
111 return make_window('camel_case_nameWindow', "python_name_window")
112
113
114if __name__ == "__main__":115if __name__ == "__main__":
115 # Support for command line options.116 # Support for command line options.
116 import logging117 import logging
@@ -127,6 +128,6 @@
127 logging.debug('logging enabled')128 logging.debug('logging enabled')
128129
129 # Run the application.130 # Run the application.
130 window = Newcamel_case_nameWindow()131 window = camel_case_nameWindow()
131 window.show()132 window.show()
132 gtk.main()133 gtk.main()
133134
=== modified file 'data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py'
--- data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py 2010-02-10 05:54:07 +0000
+++ data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py 2010-03-15 16:53:12 +0000
@@ -5,7 +5,7 @@
55
6import gtk6import gtk
77
8from python_name.helpers import make_window8from python_name.helpers import get_builder
99
10import gettext10import gettext
11from gettext import gettext as _11from gettext import gettext as _
@@ -14,26 +14,26 @@
14class Aboutcamel_case_nameDialog(gtk.AboutDialog):14class Aboutcamel_case_nameDialog(gtk.AboutDialog):
15 __gtype_name__ = "Aboutcamel_case_nameDialog"15 __gtype_name__ = "Aboutcamel_case_nameDialog"
1616
17 def __init__(self):17 def __new__(cls):
18 """Construct an Aboutcamel_case_nameDialog.18 """Special static method that's automatically called by Python when
1919 constructing a new instance of this class.
20 This function is typically not called directly. Creation of an20
21 Aboutcamel_case_nameDialog requires redeading the associated UI file21 Returns a fully instantiated Aboutcamel_case_nameDialog object.
22 and parsing the UI definition externally, and then calling
23 Aboutcamel_case_nameDialog.finish_initializing().
24
25 Use the convenience function NewAboutcamel_case_nameDialog to create
26 NewAboutcamel_case_nameDialog objects.
27 """22 """
28 pass23 builder = get_builder('Aboutcamel_case_nameDialog')
24 new_object = builder.get_object("about_python_name_dialog")
25 new_object.finish_initializing(builder)
26 return new_object
2927
30 def finish_initializing(self, builder):28 def finish_initializing(self, builder):
31 """Called after we've finished initializing.29 """Called while initializing this instance in __new__
3230
33 finish_initalizing should be called after parsing the ui definition31 finish_initalizing should be called after parsing the ui definition
34 and creating a Aboutcamel_case_nameDialog object with it in order to32 and creating a Aboutcamel_case_nameDialog object with it in order to
35 finish initializing the start of the new Aboutcamel_case_nameDialog33 finish initializing the start of the new Aboutcamel_case_nameDialog
36 instance.34 instance.
35
36 Put your initialization code in here and leave __init__ undefined.
37 """37 """
38 # Get a reference to the builder and set up the signals.38 # Get a reference to the builder and set up the signals.
39 self.builder = builder39 self.builder = builder
@@ -42,17 +42,7 @@
42 # Code for other initialization actions should be added here.42 # Code for other initialization actions should be added here.
4343
4444
45def NewAboutcamel_case_nameDialog():
46 """Returns a fully instantiated Aboutcamel_case_nameDialog object.
47
48 Use this function rather than creating a Aboutcamel_case_nameDialog
49 instance directly.
50 """
51 return make_window(
52 'Aboutcamel_case_nameDialog', "about_python_name_dialog")
53
54
55if __name__ == "__main__":45if __name__ == "__main__":
56 dialog = NewAboutcamel_case_nameDialog()46 dialog = Aboutcamel_case_nameDialog()
57 dialog.show()47 dialog.show()
58 gtk.main()48 gtk.main()
5949
=== modified file 'data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py'
--- data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py 2010-02-10 05:54:07 +0000
+++ data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py 2010-03-15 16:53:12 +0000
@@ -7,7 +7,7 @@
7from desktopcouch.records.record import Record7from desktopcouch.records.record import Record
8import gtk8import gtk
99
10from python_name.helpers import make_window10from python_name.helpers import get_builder
1111
12import gettext12import gettext
13from gettext import gettext as _13from gettext import gettext as _
@@ -17,26 +17,26 @@
17 __gtype_name__ = "Preferencescamel_case_nameDialog"17 __gtype_name__ = "Preferencescamel_case_nameDialog"
18 preferences = {}18 preferences = {}
1919
20 def __init__(self):20 def __new__(cls):
21 """Construct a Preferencescamel_case_nameDialog.21 """Special static method that's automatically called by Python when
2222 constructing a new instance of this class.
23 This function is typically not called directly. Creation of a23
24 Preferencescamel_case_nameDialog requires rereading the associated UI24 Returns a fully instantiated Preferencescamel_case_nameDialog object.
25 file and parsing the UI definition extrenally, and then calling
26 Preferencescamel_case_nameDialog.finish_initializing().
27
28 Use the convenience function NewPreferencescamel_case_nameDialog to
29 create NewAboutcamel_case_nameDialog objects.
30 """25 """
31 pass26 builder = get_builder('Preferencescamel_case_nameDialog')
27 new_object = builder.get_object("preferences_python_name_dialog")
28 new_object.finish_initializing(builder)
29 return new_object
3230
33 def finish_initializing(self, builder):31 def finish_initializing(self, builder):
34 """Called after we've finished initializing.32 """Called while initializing this instance in __new__
3533
36 finish_initalizing should be called after parsing the ui definition34 finish_initalizing should be called after parsing the ui definition
37 and creating a Aboutcamel_case_nameDialog object with it in order to35 and creating a Preferencescamel_case_nameDialog object with it in order to
38 finish initializing the start of the new Aboutcamel_case_nameDialog36 finish initializing the start of the new Perferencescamel_case_nameDialog
39 instance.37 instance.
38
39 Put your initialization code in here and leave __init__ undefined.
40 """40 """
4141
42 # Get a reference to the builder and set up the signals.42 # Get a reference to the builder and set up the signals.
@@ -105,19 +105,7 @@
105 # Restore any changes to self._preferences here.105 # Restore any changes to self._preferences here.
106 pass106 pass
107107
108
109def NewPreferencescamel_case_nameDialog():
110 """Returns a fully instantiated Preferencescamel_case_nameDialog object.
111
112 Use this function rather than creating a Preferencescamel_case_nameDialog
113 instance directly.
114 """
115 return make_window(
116 'Preferencescamel_case_nameDialog',
117 "preferences_python_name_dialog")
118
119
120if __name__ == "__main__":108if __name__ == "__main__":
121 dialog = NewPreferencescamel_case_nameDialog()109 dialog = Preferencescamel_case_nameDialog()
122 dialog.show()110 dialog.show()
123 gtk.main()111 gtk.main()
124112
=== modified file 'data/templates/ubuntu-application/project_root/python/helpers.py'
--- data/templates/ubuntu-application/project_root/python/helpers.py 2010-03-03 14:27:09 +0000
+++ data/templates/ubuntu-application/project_root/python/helpers.py 2010-03-15 16:53:12 +0000
@@ -18,12 +18,12 @@
18from gettext import gettext as _18from gettext import gettext as _
19gettext.textdomain('project_name')19gettext.textdomain('project_name')
2020
21def make_window(builder_file_name, window_name):21def get_builder(builder_file_name):
22 """Return a fully-instantiated window or dialog.22 """Return a fully-instantiated gtk.Builder instance from specified ui
2323 file
24
24 :param builder_file_name: The name of the builder file, without extension.25 :param builder_file_name: The name of the builder file, without extension.
25 Assumed to be in the 'ui' directory under the data path.26 Assumed to be in the 'ui' directory under the data path.
26 :param window_name: The name of the window or dialog in the builder file.
27 """27 """
28 # Look for the ui file that describes the user interface.28 # Look for the ui file that describes the user interface.
29 ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))29 ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
@@ -33,6 +33,4 @@
33 builder = gtk.Builder()33 builder = gtk.Builder()
34 builder.set_translation_domain('project_name')34 builder.set_translation_domain('project_name')
35 builder.add_from_file(ui_filename)35 builder.add_from_file(ui_filename)
36 dialog = builder.get_object(window_name)36 return builder
37 dialog.finish_initializing(builder)
38 return dialog

Subscribers

People subscribed via source and target branches