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