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
1=== modified file 'data/templates/ubuntu-application/project_root/bin/project_name'
2--- data/templates/ubuntu-application/project_root/bin/project_name 2010-03-09 09:36:08 +0000
3+++ data/templates/ubuntu-application/project_root/bin/project_name 2010-03-15 16:53:12 +0000
4@@ -31,31 +31,41 @@
5
6 from python_name import (
7 Aboutcamel_case_nameDialog, Preferencescamel_case_nameDialog)
8-from python_name.helpers import make_window
9+from python_name.helpers import get_builder
10
11
12 class camel_case_nameWindow(gtk.Window):
13 __gtype_name__ = "camel_case_nameWindow"
14-
15- def __init__(self):
16- """Construct a camel_case_nameWindow.
17-
18- This function is typically not called directly. Creation a
19- camel_case_nameWindow requires rereading the associated ui file and
20- parsing the ui definition externally, and then calling
21- camel_case_nameWindow.finish_initializing().
22-
23- Use the convenience function Newcamel_case_nameWindow to create
24- camel_case_nameWindow object.
25+
26+ # To construct a new instance of this method, the following notable
27+ # methods are called in this order:
28+ # __new__(cls)
29+ # __init__(self)
30+ # finish_initializing(self, builder)
31+ # __init__(self)
32+ #
33+ # For this reason, it's recommended you leave __init__ empty and put
34+ # your inialization code in finish_intializing
35+
36+ def __new__(cls):
37+ """Special static method that's automatically called by Python when
38+ constructing a new instance of this class.
39+
40+ Returns a fully instantiated camel_case_nameWindow object.
41 """
42- pass
43+ builder = get_builder('camel_case_nameWindow')
44+ new_object = builder.get_object("python_name_window")
45+ new_object.finish_initializing(builder)
46+ return new_object
47
48 def finish_initializing(self, builder):
49- """Called after we've finished initializing.
50+ """Called while initializing this instance in __new__
51
52 finish_initalizing should be called after parsing the UI definition
53 and creating a camel_case_nameWindow object with it in order to finish
54 initializing the start of the new camel_case_nameWindow instance.
55+
56+ Put your initilization code in here and leave __init__ undefined.
57 """
58 # Get a reference to the builder and set up the signals.
59 self.builder = builder
60@@ -80,13 +90,13 @@
61
62 def about(self, widget, data=None):
63 """Display the about box for project_name."""
64- about = Aboutcamel_case_nameDialog.NewAboutcamel_case_nameDialog()
65+ about = Aboutcamel_case_nameDialog.Aboutcamel_case_nameDialog()
66 response = about.run()
67 about.destroy()
68
69 def preferences(self, widget, data=None):
70 """Display the preferences window for project_name."""
71- prefs = Preferencescamel_case_nameDialog.NewPreferencescamel_case_nameDialog()
72+ prefs = Preferencescamel_case_nameDialog.Preferencescamel_case_nameDialog()
73 response = prefs.run()
74 if response == gtk.RESPONSE_OK:
75 # Make any updates based on changed preferences here.
76@@ -102,15 +112,6 @@
77 # Clean up code for saving application state should be added here.
78 gtk.main_quit()
79
80-
81-def Newcamel_case_nameWindow():
82- """Newcamel_case_nameWindow - returns a fully instantiated
83- camel_case_nameWindow object. Use this function rather than
84- creating a camel_case_nameWindow directly.
85- """
86- return make_window('camel_case_nameWindow', "python_name_window")
87-
88-
89 if __name__ == "__main__":
90 # Support for command line options.
91 import logging
92@@ -127,6 +128,6 @@
93 logging.debug('logging enabled')
94
95 # Run the application.
96- window = Newcamel_case_nameWindow()
97+ window = camel_case_nameWindow()
98 window.show()
99 gtk.main()
100
101=== modified file 'data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py'
102--- data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py 2010-02-10 05:54:07 +0000
103+++ data/templates/ubuntu-application/project_root/python/Aboutcamel_case_nameDialog.py 2010-03-15 16:53:12 +0000
104@@ -5,7 +5,7 @@
105
106 import gtk
107
108-from python_name.helpers import make_window
109+from python_name.helpers import get_builder
110
111 import gettext
112 from gettext import gettext as _
113@@ -14,26 +14,26 @@
114 class Aboutcamel_case_nameDialog(gtk.AboutDialog):
115 __gtype_name__ = "Aboutcamel_case_nameDialog"
116
117- def __init__(self):
118- """Construct an Aboutcamel_case_nameDialog.
119-
120- This function is typically not called directly. Creation of an
121- Aboutcamel_case_nameDialog requires redeading the associated UI file
122- and parsing the UI definition externally, and then calling
123- Aboutcamel_case_nameDialog.finish_initializing().
124-
125- Use the convenience function NewAboutcamel_case_nameDialog to create
126- NewAboutcamel_case_nameDialog objects.
127+ def __new__(cls):
128+ """Special static method that's automatically called by Python when
129+ constructing a new instance of this class.
130+
131+ Returns a fully instantiated Aboutcamel_case_nameDialog object.
132 """
133- pass
134+ builder = get_builder('Aboutcamel_case_nameDialog')
135+ new_object = builder.get_object("about_python_name_dialog")
136+ new_object.finish_initializing(builder)
137+ return new_object
138
139 def finish_initializing(self, builder):
140- """Called after we've finished initializing.
141+ """Called while initializing this instance in __new__
142
143 finish_initalizing should be called after parsing the ui definition
144 and creating a Aboutcamel_case_nameDialog object with it in order to
145 finish initializing the start of the new Aboutcamel_case_nameDialog
146 instance.
147+
148+ Put your initialization code in here and leave __init__ undefined.
149 """
150 # Get a reference to the builder and set up the signals.
151 self.builder = builder
152@@ -42,17 +42,7 @@
153 # Code for other initialization actions should be added here.
154
155
156-def NewAboutcamel_case_nameDialog():
157- """Returns a fully instantiated Aboutcamel_case_nameDialog object.
158-
159- Use this function rather than creating a Aboutcamel_case_nameDialog
160- instance directly.
161- """
162- return make_window(
163- 'Aboutcamel_case_nameDialog', "about_python_name_dialog")
164-
165-
166 if __name__ == "__main__":
167- dialog = NewAboutcamel_case_nameDialog()
168+ dialog = Aboutcamel_case_nameDialog()
169 dialog.show()
170 gtk.main()
171
172=== modified file 'data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py'
173--- data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py 2010-02-10 05:54:07 +0000
174+++ data/templates/ubuntu-application/project_root/python/Preferencescamel_case_nameDialog.py 2010-03-15 16:53:12 +0000
175@@ -7,7 +7,7 @@
176 from desktopcouch.records.record import Record
177 import gtk
178
179-from python_name.helpers import make_window
180+from python_name.helpers import get_builder
181
182 import gettext
183 from gettext import gettext as _
184@@ -17,26 +17,26 @@
185 __gtype_name__ = "Preferencescamel_case_nameDialog"
186 preferences = {}
187
188- def __init__(self):
189- """Construct a Preferencescamel_case_nameDialog.
190-
191- This function is typically not called directly. Creation of a
192- Preferencescamel_case_nameDialog requires rereading the associated UI
193- file and parsing the UI definition extrenally, and then calling
194- Preferencescamel_case_nameDialog.finish_initializing().
195-
196- Use the convenience function NewPreferencescamel_case_nameDialog to
197- create NewAboutcamel_case_nameDialog objects.
198+ def __new__(cls):
199+ """Special static method that's automatically called by Python when
200+ constructing a new instance of this class.
201+
202+ Returns a fully instantiated Preferencescamel_case_nameDialog object.
203 """
204- pass
205+ builder = get_builder('Preferencescamel_case_nameDialog')
206+ new_object = builder.get_object("preferences_python_name_dialog")
207+ new_object.finish_initializing(builder)
208+ return new_object
209
210 def finish_initializing(self, builder):
211- """Called after we've finished initializing.
212+ """Called while initializing this instance in __new__
213
214 finish_initalizing should be called after parsing the ui definition
215- and creating a Aboutcamel_case_nameDialog object with it in order to
216- finish initializing the start of the new Aboutcamel_case_nameDialog
217+ and creating a Preferencescamel_case_nameDialog object with it in order to
218+ finish initializing the start of the new Perferencescamel_case_nameDialog
219 instance.
220+
221+ Put your initialization code in here and leave __init__ undefined.
222 """
223
224 # Get a reference to the builder and set up the signals.
225@@ -105,19 +105,7 @@
226 # Restore any changes to self._preferences here.
227 pass
228
229-
230-def NewPreferencescamel_case_nameDialog():
231- """Returns a fully instantiated Preferencescamel_case_nameDialog object.
232-
233- Use this function rather than creating a Preferencescamel_case_nameDialog
234- instance directly.
235- """
236- return make_window(
237- 'Preferencescamel_case_nameDialog',
238- "preferences_python_name_dialog")
239-
240-
241 if __name__ == "__main__":
242- dialog = NewPreferencescamel_case_nameDialog()
243+ dialog = Preferencescamel_case_nameDialog()
244 dialog.show()
245 gtk.main()
246
247=== modified file 'data/templates/ubuntu-application/project_root/python/helpers.py'
248--- data/templates/ubuntu-application/project_root/python/helpers.py 2010-03-03 14:27:09 +0000
249+++ data/templates/ubuntu-application/project_root/python/helpers.py 2010-03-15 16:53:12 +0000
250@@ -18,12 +18,12 @@
251 from gettext import gettext as _
252 gettext.textdomain('project_name')
253
254-def make_window(builder_file_name, window_name):
255- """Return a fully-instantiated window or dialog.
256-
257+def get_builder(builder_file_name):
258+ """Return a fully-instantiated gtk.Builder instance from specified ui
259+ file
260+
261 :param builder_file_name: The name of the builder file, without extension.
262 Assumed to be in the 'ui' directory under the data path.
263- :param window_name: The name of the window or dialog in the builder file.
264 """
265 # Look for the ui file that describes the user interface.
266 ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
267@@ -33,6 +33,4 @@
268 builder = gtk.Builder()
269 builder.set_translation_domain('project_name')
270 builder.add_from_file(ui_filename)
271- dialog = builder.get_object(window_name)
272- dialog.finish_initializing(builder)
273- return dialog
274+ return builder

Subscribers

People subscribed via source and target branches