Merge lp:~jml/quickly/template-cleanup into lp:quickly

Proposed by Jonathan Lange
Status: Merged
Merged at revision: not available
Proposed branch: lp:~jml/quickly/template-cleanup
Merge into: lp:quickly
Diff against target: 663 lines (+237/-202)
7 files modified
data/templates/ubuntu-application/bin/project_name (+42/-44)
data/templates/ubuntu-application/create.py (+1/-0)
data/templates/ubuntu-application/python/Aboutcamel_case_nameDialog.py (+26/-32)
data/templates/ubuntu-application/python/Preferencescamel_case_nameDialog.py (+75/-75)
data/templates/ubuntu-application/python/dialog_camel_case_nameDialog.py (+30/-37)
data/templates/ubuntu-application/python/helpers.py (+34/-0)
data/templates/ubuntu-application/python/python_nameconfig.py (+29/-14)
To merge this branch: bzr merge lp:~jml/quickly/template-cleanup
Reviewer Review Type Date Requested Status
Didier Roche-Tolomelli Approve
Review via email: mp+16684@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

This branch changes the Ubuntu application templates to be a little nicer for the average Python programmer.

 - All excess whitespace is deleted
 - PEP 8 is followed where possible
 - Repeated code has been extracted into a 'helpers' module
 - Spelling mistakes have been corrected
 - Docstrings now conform to Python's docstring standards.
 - Comments are written as full sentences.
 - Double underscore prefixes are now single underscore prefixes

Hope this helps,
jml

lp:~jml/quickly/template-cleanup updated
408. By Jonathan Lange

Install the helpers file.

Revision history for this message
Didier Roche-Tolomelli (didrocks) wrote :

Hey Jonathan, thanks for your path.

I really have to find a vim script to format PEP8 compliant a python code (I know, you use emacs and don't care ;))

Thanks for this part:
647 - if __python_name_data_directory__.startswith('/'):
648 - pathname = __python_name_data_directory__
649 - else:
650 - pathname = os.path.dirname(__file__) + '/' + __python_name_data_directory__
651 + # Get pathname absolute or relative.
652 + path = os.path.join(
653 + os.path.dirname(__file__), __python_name_data_directory__)
I didn't know that joining two absolute path only take the second. Handy trick :)

Just a question, what is the __all__ in python_nameconfig.py is for? I only know this for autoimporting module in __init__.py, and you seem to still import function one by one:
from python_name.python_nameconfig import get_data_file
So, I don't really see its usefulness ;)

also, why importing multiple module in a same line is wrong?
(cf commit 404)

BTW, merged, thanks! I'll try to go file by file to include PEP8 compliance, but this is a long term work :)

review: Approve
Revision history for this message
Jonathan Lange (jml) wrote :

On Thu, Dec 31, 2009 at 7:53 PM, Didier Roche <email address hidden> wrote:
> Review: Approve
> Hey Jonathan, thanks for your path.
>
> I really have to find a vim script to format PEP8 compliant a python code (I know, you use emacs and don't care ;))
>

Heh heh.

> Thanks for this part:
> 647     - if __python_name_data_directory__.startswith('/'):
> 648     - pathname = __python_name_data_directory__
> 649     - else:
> 650     - pathname = os.path.dirname(__file__) + '/' + __python_name_data_directory__
> 651     + # Get pathname absolute or relative.
> 652     + path = os.path.join(
> 653     + os.path.dirname(__file__), __python_name_data_directory__)
> I didn't know that joining two absolute path only take the second. Handy trick :)
>
> Just a question, what is the __all__ in python_nameconfig.py is for? I only know this for autoimporting module in __init__.py, and you seem to still import function one by one:
> from python_name.python_nameconfig import get_data_file
> So, I don't really see its usefulness ;)
>

Ahh OK. It's there to indicate which names in the module are really
public. We don't have to have it if you don't want. Launchpad uses it
to check that people aren't using private stuff (we have a utility
called "importfascist" that does this).

> also, why importing multiple module in a same line is wrong?
> (cf commit 404)
>

PEP 8 says so.

> BTW, merged, thanks! I'll try to go file by file to include PEP8 compliance, but this is a long term work :)
>

Going file-by-file is the best approach. I changed all of these
because I wanted to start a Quickly project and not have a bunch of
red warnings on my screen. :)

jml

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/templates/ubuntu-application/bin/project_name'
2--- data/templates/ubuntu-application/bin/project_name 2009-12-26 14:40:43 +0000
3+++ data/templates/ubuntu-application/bin/project_name 2009-12-31 02:05:23 +0000
4@@ -8,104 +8,102 @@
5 import os
6 import gtk
7
8-# add project root directory (enable symlink, and trunk execution)
9-project_bin_directory = os.path.dirname(os.path.realpath(sys.argv[0]))
10-project_root_directory_name = os.path.dirname(project_bin_directory)
11-project_root_directory = os.path.abspath(project_root_directory_name)
12-if os.path.exists(os.path.join(project_root_directory, 'python_name')) and project_root_directory not in sys.path:
13- sys.path.insert(0, project_root_directory)
14- os.putenv('PYTHONPATH', project_root_directory) # for subprocesses
15-
16-from python_name import Aboutcamel_case_nameDialog, Preferencescamel_case_nameDialog
17-from python_name.python_nameconfig import getdatapath
18+# Add project root directory (enable symlink, and trunk execution).
19+PROJECT_ROOT_DIRECTORY = os.path.abspath(
20+ os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
21+
22+if (os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, 'python_name'))
23+ and PROJECT_ROOT_DIRECTORY not in sys.path):
24+ sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
25+ os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
26+
27+from python_name import (
28+ Aboutcamel_case_nameDialog, Preferencescamel_case_nameDialog)
29+from python_name.helpers import make_window
30+
31
32 class camel_case_nameWindow(gtk.Window):
33 __gtype_name__ = "camel_case_nameWindow"
34
35 def __init__(self):
36- """__init__ - This function is typically not called directly.
37- Creation a camel_case_nameWindow requires redeading the associated ui
38- file and parsing the ui definition extrenally,
39- and then calling camel_case_nameWindow.finish_initializing().
40+ """Construct a camel_case_nameWindow.
41+
42+ This function is typically not called directly. Creation a
43+ camel_case_nameWindow requires rereading the associated ui file and
44+ parsing the ui definition externally, and then calling
45+ camel_case_nameWindow.finish_initializing().
46
47 Use the convenience function Newcamel_case_nameWindow to create
48 camel_case_nameWindow object.
49-
50 """
51 pass
52
53 def finish_initializing(self, builder):
54- """finish_initalizing should be called after parsing the ui definition
55+ """Called after we've finished initializing.
56+
57+ finish_initalizing should be called after parsing the UI definition
58 and creating a camel_case_nameWindow object with it in order to finish
59 initializing the start of the new camel_case_nameWindow instance.
60-
61 """
62- #get a reference to the builder and set up the signals
63+ # Get a reference to the builder and set up the signals.
64 self.builder = builder
65 self.builder.connect_signals(self)
66
67- #uncomment the following code to read in preferences at start up
68+ # Uncomment the following code to read in preferences at start up.
69 #dlg = Preferencescamel_case_nameDialog.NewPreferencescamel_case_nameDialog()
70 #self.preferences = dlg.get_preferences()
71
72- #code for other initialization actions should be added here
73+ # Code for other initialization actions should be added here.
74
75 def about(self, widget, data=None):
76- """about - display the about box for project_name """
77+ """Display the about box for project_name."""
78 about = Aboutcamel_case_nameDialog.NewAboutcamel_case_nameDialog()
79 response = about.run()
80 about.destroy()
81
82 def preferences(self, widget, data=None):
83- """preferences - display the preferences window for project_name """
84+ """Display the preferences window for project_name."""
85 prefs = Preferencescamel_case_nameDialog.NewPreferencescamel_case_nameDialog()
86 response = prefs.run()
87 if response == gtk.RESPONSE_OK:
88- #make any updates based on changed preferences here
89+ # Make any updates based on changed preferences here.
90 pass
91 prefs.destroy()
92
93 def quit(self, widget, data=None):
94- """quit - signal handler for closing the camel_case_nameWindow"""
95+ """Signal handler for closing the camel_case_nameWindow."""
96 self.destroy()
97
98 def on_destroy(self, widget, data=None):
99- """on_destroy - called when the camel_case_nameWindow is close. """
100- #clean up code for saving application state should be added here
101-
102+ """Called when the camel_case_nameWindow is closed."""
103+ # Clean up code for saving application state should be added here.
104 gtk.main_quit()
105
106+
107 def Newcamel_case_nameWindow():
108 """Newcamel_case_nameWindow - returns a fully instantiated
109 camel_case_nameWindow object. Use this function rather than
110 creating a camel_case_nameWindow directly.
111 """
112-
113- #look for the ui file that describes the ui
114- ui_filename = os.path.join(getdatapath(), 'ui', 'camel_case_nameWindow.ui')
115- if not os.path.exists(ui_filename):
116- ui_filename = None
117-
118- builder = gtk.Builder()
119- builder.add_from_file(ui_filename)
120- window = builder.get_object("python_name_window")
121- window.finish_initializing(builder)
122- return window
123+ return make_window('camel_case_nameWindow', "python_name_window")
124+
125
126 if __name__ == "__main__":
127- #support for command line options
128- import logging, optparse
129+ # Support for command line options.
130+ import logging
131+ import optparse
132 parser = optparse.OptionParser(version="%prog %ver")
133- parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages")
134+ parser.add_option(
135+ "-v", "--verbose", action="store_true", dest="verbose",
136+ help="Show debug messages")
137 (options, args) = parser.parse_args()
138
139- #set the logging level to show debug messages
140+ # Set the logging level to show debug messages.
141 if options.verbose:
142 logging.basicConfig(level=logging.DEBUG)
143 logging.debug('logging enabled')
144
145- #run the application
146+ # Run the application.
147 window = Newcamel_case_nameWindow()
148 window.show()
149 gtk.main()
150-
151
152=== modified file 'data/templates/ubuntu-application/create.py'
153--- data/templates/ubuntu-application/create.py 2009-12-29 17:18:44 +0000
154+++ data/templates/ubuntu-application/create.py 2009-12-31 02:05:23 +0000
155@@ -120,6 +120,7 @@
156 quicklyutils.file_from_template(template_python_dir, "Aboutcamel_case_nameDialog.py", target_python_dir, substitutions)
157 quicklyutils.file_from_template(template_python_dir, "Preferencescamel_case_nameDialog.py", target_python_dir, substitutions)
158 quicklyutils.file_from_template(template_python_dir, "python_nameconfig.py", target_python_dir, substitutions)
159+quicklyutils.file_from_template(template_python_dir, 'helpers.py', target_python_dir, substitutions)
160
161 # copy the files needed for packaging
162 quicklyutils.file_from_template(abs_path, "project_root/setup.py", ".", substitutions)
163
164=== modified file 'data/templates/ubuntu-application/python/Aboutcamel_case_nameDialog.py'
165--- data/templates/ubuntu-application/python/Aboutcamel_case_nameDialog.py 2009-12-26 14:40:43 +0000
166+++ data/templates/ubuntu-application/python/Aboutcamel_case_nameDialog.py 2009-12-31 02:05:23 +0000
167@@ -3,59 +3,53 @@
168 # This file is in the public domain
169 ### END LICENSE
170
171-import sys
172-import os
173 import gtk
174
175-from python_name.python_nameconfig import getdatapath
176+from python_name.helpers import make_window
177+
178
179 class Aboutcamel_case_nameDialog(gtk.AboutDialog):
180 __gtype_name__ = "Aboutcamel_case_nameDialog"
181
182 def __init__(self):
183- """__init__ - This function is typically not called directly.
184- Creation of a Aboutcamel_case_nameDialog requires redeading the associated ui
185- file and parsing the ui definition extrenally,
186- and then calling Aboutcamel_case_nameDialog.finish_initializing().
187-
188- Use the convenience function NewAboutcamel_case_nameDialog to create
189+ """Construct an Aboutcamel_case_nameDialog.
190+
191+ This function is typically not called directly. Creation of an
192+ Aboutcamel_case_nameDialog requires redeading the associated UI file
193+ and parsing the UI definition externally, and then calling
194+ Aboutcamel_case_nameDialog.finish_initializing().
195+
196+ Use the convenience function NewAboutcamel_case_nameDialog to create
197 NewAboutcamel_case_nameDialog objects.
198-
199 """
200 pass
201
202 def finish_initializing(self, builder):
203- """finish_initalizing should be called after parsing the ui definition
204- and creating a Aboutcamel_case_nameDialog object with it in order to finish
205- initializing the start of the new Aboutcamel_case_nameDialog instance.
206-
207+ """Called after we've finished initializing.
208+
209+ finish_initalizing should be called after parsing the ui definition
210+ and creating a Aboutcamel_case_nameDialog object with it in order to
211+ finish initializing the start of the new Aboutcamel_case_nameDialog
212+ instance.
213 """
214- #get a reference to the builder and set up the signals
215+ # Get a reference to the builder and set up the signals.
216 self.builder = builder
217 self.builder.connect_signals(self)
218
219- #code for other initialization actions should be added here
220+ # Code for other initialization actions should be added here.
221+
222
223 def NewAboutcamel_case_nameDialog():
224- """NewAboutcamel_case_nameDialog - returns a fully instantiated
225- Aboutcamel_case_nameDialog object. Use this function rather than
226- creating a Aboutcamel_case_nameDialog instance directly.
227-
228+ """Returns a fully instantiated Aboutcamel_case_nameDialog object.
229+
230+ Use this function rather than creating a Aboutcamel_case_nameDialog
231+ instance directly.
232 """
233-
234- #look for the ui file that describes the ui
235- ui_filename = os.path.join(getdatapath(), 'ui', 'Aboutcamel_case_nameDialog.ui')
236- if not os.path.exists(ui_filename):
237- ui_filename = None
238-
239- builder = gtk.Builder()
240- builder.add_from_file(ui_filename)
241- dialog = builder.get_object("about_python_name_dialog")
242- dialog.finish_initializing(builder)
243- return dialog
244+ return make_window(
245+ 'Aboutcamel_case_nameDialog', "about_python_name_dialog")
246+
247
248 if __name__ == "__main__":
249 dialog = NewAboutcamel_case_nameDialog()
250 dialog.show()
251 gtk.main()
252-
253
254=== modified file 'data/templates/ubuntu-application/python/Preferencescamel_case_nameDialog.py'
255--- data/templates/ubuntu-application/python/Preferencescamel_case_nameDialog.py 2009-12-26 14:40:43 +0000
256+++ data/templates/ubuntu-application/python/Preferencescamel_case_nameDialog.py 2009-12-31 02:05:23 +0000
257@@ -3,118 +3,118 @@
258 # This file is in the public domain
259 ### END LICENSE
260
261-import sys
262-import os
263-import gtk
264 from desktopcouch.records.server import CouchDatabase
265 from desktopcouch.records.record import Record
266-
267-from python_name.python_nameconfig import getdatapath
268+import gtk
269+
270+from python_name.helpers import make_window
271+
272
273 class Preferencescamel_case_nameDialog(gtk.Dialog):
274 __gtype_name__ = "Preferencescamel_case_nameDialog"
275- prefernces = {}
276+ preferences = {}
277
278 def __init__(self):
279- """__init__ - This function is typically not called directly.
280- Creation of a Preferencescamel_case_nameDialog requires redeading the associated ui
281- file and parsing the ui definition extrenally,
282- and then calling Preferencescamel_case_nameDialog.finish_initializing().
283-
284- Use the convenience function NewPreferencescamel_case_nameDialog to create
285- NewAboutcamel_case_nameDialog objects.
286+ """Construct a Preferencescamel_case_nameDialog.
287+
288+ This function is typically not called directly. Creation of a
289+ Preferencescamel_case_nameDialog requires rereading the associated UI
290+ file and parsing the UI definition extrenally, and then calling
291+ Preferencescamel_case_nameDialog.finish_initializing().
292+
293+ Use the convenience function NewPreferencescamel_case_nameDialog to
294+ create NewAboutcamel_case_nameDialog objects.
295 """
296-
297 pass
298
299 def finish_initializing(self, builder):
300- """finish_initalizing should be called after parsing the ui definition
301- and creating a Aboutcamel_case_nameDialog object with it in order to finish
302- initializing the start of the new Aboutcamel_case_nameDialog instance.
303+ """Called after we've finished initializing.
304+
305+ finish_initalizing should be called after parsing the ui definition
306+ and creating a Aboutcamel_case_nameDialog object with it in order to
307+ finish initializing the start of the new Aboutcamel_case_nameDialog
308+ instance.
309 """
310
311- #get a reference to the builder and set up the signals
312+ # Get a reference to the builder and set up the signals.
313 self.builder = builder
314 self.builder.connect_signals(self)
315
316- #set up couchdb and the preference info
317- self.__db_name = "project_name"
318- self.__database = CouchDatabase(self.__db_name, create=True)
319- self.__preferences = None
320- self.__key = None
321+ # Set up couchdb and the preference info.
322+ self._db_name = "project_name"
323+ self._database = CouchDatabase(self._db_name, create=True)
324+ self._preferences = None
325+ self._key = None
326
327- #set the record type and then initalize the preferences
328- self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/camel_case_name/Preferences"
329- self.__preferences = self.get_preferences()
330- #TODO:code for other initialization actions should be added here
331+ # Set the record type and then initalize the preferences.
332+ self._record_type = (
333+ "http://wiki.ubuntu.com/Quickly/RecordTypes/camel_case_name/"
334+ "Preferences")
335+ self._preferences = self.get_preferences()
336+ # TODO: code for other initialization actions should be added here
337
338 def get_preferences(self):
339- """get_preferences -returns a dictionary object that contain
340- preferences for project_name. Creates a couchdb record if
341- necessary.
342+ """Return a dict of preferences for project_name.
343+
344+ Creates a couchdb record if necessary.
345 """
346-
347- if self.__preferences == None: #the dialog is initializing
348- self.__load_preferences()
349-
350- #if there were no saved preference, this
351- return self.__preferences
352-
353- def __load_preferences(self):
354- #TODO: add prefernces to the self.__preferences dict
355- #default preferences that will be overwritten if some are saved
356- self.__preferences = {"record_type":self.__record_type}
357-
358- results = self.__database.get_records(record_type=self.__record_type, create_view=True)
359-
360+ if self._preferences == None:
361+ # The dialog is initializing.
362+ self._load_preferences()
363+
364+ # If there were no saved preference, this.
365+ return self._preferences
366+
367+ def _load_preferences(self):
368+ # TODO: add preferences to the self._preferences dict default
369+ # preferences that will be overwritten if some are saved
370+ self._preferences = {"record_type": self._record_type}
371+
372+ results = self._database.get_records(
373+ record_type=self._record_type, create_view=True)
374+
375 if len(results.rows) == 0:
376- #no preferences have ever been saved
377- #save them before returning
378- self.__key = self.__database.put_record(Record(self.__preferences))
379+ # No preferences have ever been saved, save them before returning.
380+ self._key = self._database.put_record(Record(self._preferences))
381 else:
382- self.__preferences = results.rows[0].value
383- del self.__preferences['_rev']
384- self.__key = results.rows[0].value["_id"]
385-
386- def __save_preferences(self):
387- self.__database.update_fields(self.__key, self.__preferences)
388+ self._preferences = results.rows[0].value
389+ del self._preferences['_rev']
390+ self._key = results.rows[0].value["_id"]
391+
392+ def _save_preferences(self):
393+ self._database.update_fields(self._key, self._preferences)
394
395 def ok(self, widget, data=None):
396- """ok - The user has elected to save the changes.
397+ """The user has elected to save the changes.
398+
399 Called before the dialog returns gtk.RESONSE_OK from run().
400 """
401
402- #make any updates to self.__preferences here
403- #self.__preferences["preference1"] = "value2"
404- self.__save_preferences()
405+ # Make any updates to self._preferences here. e.g.
406+ #self._preferences["preference1"] = "value2"
407+ self._save_preferences()
408
409 def cancel(self, widget, data=None):
410- """cancel - The user has elected cancel changes.
411+ """The user has elected cancel changes.
412+
413 Called before the dialog returns gtk.RESPONSE_CANCEL for run()
414 """
415-
416- #restore any changes to self.__preferences here
417+ # Restore any changes to self._preferences here.
418 pass
419
420+
421 def NewPreferencescamel_case_nameDialog():
422- """NewPreferencescamel_case_nameDialog - returns a fully instantiated
423- Preferencescamel_case_nameDialog object. Use this function rather than
424- creating a Preferencescamel_case_nameDialog instance directly.
425+ """Returns a fully instantiated Preferencescamel_case_nameDialog object.
426+
427+ Use this function rather than creating a Preferencescamel_case_nameDialog
428+ instance directly.
429 """
430-
431- #look for the ui file that describes the ui
432- ui_filename = os.path.join(getdatapath(), 'ui', 'Preferencescamel_case_nameDialog.ui')
433- if not os.path.exists(ui_filename):
434- ui_filename = None
435-
436- builder = gtk.Builder()
437- builder.add_from_file(ui_filename)
438- dialog = builder.get_object("preferences_python_name_dialog")
439- dialog.finish_initializing(builder)
440- return dialog
441+ return make_window(
442+ 'Preferencescamel_case_nameDialog',
443+ "preferences_python_name_dialog")
444+
445
446 if __name__ == "__main__":
447 dialog = NewPreferencescamel_case_nameDialog()
448 dialog.show()
449 gtk.main()
450-
451
452=== modified file 'data/templates/ubuntu-application/python/dialog_camel_case_nameDialog.py'
453--- data/templates/ubuntu-application/python/dialog_camel_case_nameDialog.py 2009-12-23 07:18:48 +0000
454+++ data/templates/ubuntu-application/python/dialog_camel_case_nameDialog.py 2009-12-31 02:05:23 +0000
455@@ -3,72 +3,65 @@
456 # This file is in the public domain
457 ### END LICENSE
458
459-import sys
460-import os
461 import gtk
462
463-from python_name.python_nameconfig import getdatapath
464+from python_name.helpers import make_window
465+
466
467 class dialog_camel_case_nameDialog(gtk.Dialog):
468 __gtype_name__ = "dialog_camel_case_nameDialog"
469
470 def __init__(self):
471- """__init__ - This function is typically not called directly.
472- Creation of a dialog_camel_case_nameDialog requires redeading the associated ui
473- file and parsing the ui definition extrenally,
474- and then calling dialog_camel_case_nameDialog.finish_initializing().
475-
476- Use the convenience function Newdialog_camel_case_nameDialog to create
477+ """Construct a dialog_camel_case_nameDialog.
478+
479+ This function is typically not called directly. Creation of a
480+ dialog_camel_case_nameDialog requires rereading the associated UI file
481+ and parsing the UI definition externally, and then calling
482+ dialog_camel_case_nameDialog.finish_initializing().
483+
484+ Use the convenience function Newdialog_camel_case_nameDialog to create
485 a dialog_camel_case_nameDialog object.
486-
487 """
488 pass
489
490 def finish_initializing(self, builder):
491- """finish_initalizing should be called after parsing the ui definition
492- and creating a dialog_camel_case_nameDialog object with it in order to finish
493- initializing the start of the new dialog_camel_case_nameDialog instance.
494-
495+ """Called when we're finished initializing.
496+
497+ finish_initalizing should be called after parsing the ui definition
498+ and creating a dialog_camel_case_nameDialog object with it in order to
499+ finish initializing the start of the new dialog_camel_case_nameDialog
500+ instance.
501 """
502- #get a reference to the builder and set up the signals
503+ # Get a reference to the builder and set up the signals.
504 self.builder = builder
505 self.builder.connect_signals(self)
506
507-
508 def ok(self, widget, data=None):
509- """ok - The user has elected to save the changes.
510+ """The user has elected to save the changes.
511+
512 Called before the dialog returns gtk.RESONSE_OK from run().
513-
514 """
515 pass
516
517 def cancel(self, widget, data=None):
518- """cancel - The user has elected cancel changes.
519+ """The user has elected cancel changes.
520+
521 Called before the dialog returns gtk.RESPONSE_CANCEL for run()
522-
523- """
524+ """
525 pass
526
527+
528 def Newdialog_camel_case_nameDialog():
529- """Newdialog_camel_case_nameDialog - returns a fully instantiated
530- dialog-camel_case_nameDialog object. Use this function rather than
531- creating dialog_camel_case_nameDialog instance directly.
532-
533+ """Return a fully instantiated dialog-camel_case_nameDialog object.
534+
535+ Use this function rather than creating dialog_camel_case_nameDialog
536+ instance directly.
537 """
538-
539- #look for the ui file that describes the ui
540- ui_filename = os.path.join(getdatapath(), 'ui', 'dialog_camel_case_nameDialog.ui')
541- if not os.path.exists(ui_filename):
542- ui_filename = None
543-
544- builder = gtk.Builder()
545- builder.add_from_file(ui_filename)
546- dialog = builder.get_object("dialog_name_dialog")
547- dialog.finish_initializing(builder)
548- return dialog
549+ return make_window(
550+ 'dialog_camel_case_nameDialog', "dialog_name_dialog")
551+
552
553 if __name__ == "__main__":
554 dialog = Newdialog_camel_case_nameDialog()
555 dialog.show()
556 gtk.main()
557-
558
559=== added file 'data/templates/ubuntu-application/python/helpers.py'
560--- data/templates/ubuntu-application/python/helpers.py 1970-01-01 00:00:00 +0000
561+++ data/templates/ubuntu-application/python/helpers.py 2009-12-31 02:05:23 +0000
562@@ -0,0 +1,34 @@
563+# -*- coding: utf-8 -*-
564+### BEGIN LICENSE
565+# This file is in the public domain
566+### END LICENSE
567+
568+"""Helpers for an Ubuntu application."""
569+
570+__all__ = [
571+ 'make_window',
572+ ]
573+
574+import os
575+import gtk
576+
577+from python_name.python_nameconfig import get_data_file
578+
579+
580+def make_window(builder_file_name, window_name):
581+ """Return a fully-instantiated window or dialog.
582+
583+ :param builder_file_name: The name of the builder file, without extension.
584+ Assumed to be in the 'ui' directory under the data path.
585+ :param window_name: The name of the window or dialog in the builder file.
586+ """
587+ # Look for the ui file that describes the user interface.
588+ ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
589+ if not os.path.exists(ui_filename):
590+ ui_filename = None
591+
592+ builder = gtk.Builder()
593+ builder.add_from_file(ui_filename)
594+ dialog = builder.get_object(window_name)
595+ dialog.finish_initializing(builder)
596+ return dialog
597
598=== modified file 'data/templates/ubuntu-application/python/python_nameconfig.py'
599--- data/templates/ubuntu-application/python/python_nameconfig.py 2009-12-29 17:31:56 +0000
600+++ data/templates/ubuntu-application/python/python_nameconfig.py 2009-12-31 02:05:23 +0000
601@@ -8,17 +8,35 @@
602 # Do not touch unless you know what you're doing.
603 # you're warned :)
604
605-# where your project will head for your data (for instance, images and ui files)
606-# by default, this is ../data, relative your trunk layout
607+__all__ = [
608+ 'project_path_not_found',
609+ 'get_data_file',
610+ 'get_data_path',
611+ ]
612+
613+# Where your project will look for your data (for instance, images and ui
614+# files). By default, this is ../data, relative your trunk layout
615 __python_name_data_directory__ = '../data/'
616 __license__ = ''
617
618 import os
619
620+
621 class project_path_not_found(Exception):
622- pass
623-
624-def getdatapath():
625+ """Raised when we can't find the project directory."""
626+
627+
628+def get_data_file(*path_segments):
629+ """Get the full path to a data file.
630+
631+ Returns the path to a file underneath the data directory (as defined by
632+ `get_data_path`). Equivalent to os.path.join(get_data_path(),
633+ *path_segments).
634+ """
635+ return os.path.join(get_data_path(), *path_segments)
636+
637+
638+def get_data_path():
639 """Retrieve project_name data path
640
641 This path is by default <python_name_lib_path>/../data/ in trunk
642@@ -26,15 +44,12 @@
643 is specified at installation time.
644 """
645
646- # get pathname absolute or relative
647- if __python_name_data_directory__.startswith('/'):
648- pathname = __python_name_data_directory__
649- else:
650- pathname = os.path.dirname(__file__) + '/' + __python_name_data_directory__
651+ # Get pathname absolute or relative.
652+ path = os.path.join(
653+ os.path.dirname(__file__), __python_name_data_directory__)
654
655- abs_data_path = os.path.abspath(pathname)
656- if os.path.exists(abs_data_path):
657- return abs_data_path
658- else:
659+ abs_data_path = os.path.abspath(path)
660+ if not os.path.exists(abs_data_path):
661 raise project_path_not_found
662
663+ return abs_data_path

Subscribers

People subscribed via source and target branches