How can I work with several common user interfaces?

I am working on a python application that runs on two different platforms, namely regular desktop Linux and Maemo 4. We use PyGTK on both platforms, but on Maemo there are a bunch of small settings to make them look beautiful, which are implemented as follows:

if util.platform.MAEMO: # do something fancy for maemo else: # regular pygtk 

About 15 of these statements are necessary to make the user interface look and work well on Maemo 4.

It has been very manageable all this time. The problem is that a new version of Maemo (5, aka fremantle) appeared a while ago, and it has some big differences compared to Maemo 4. I don’t want to add a bunch of checks throughout the GUI to get all 3 platforms that work well with the same code base because it will be messy. I also do not want to create a copy of the GUI source code for each platform and just change it for a specific platform (I would like to use as much code as possible).

So, what are the ways to have slightly different user interfaces for different platforms based on the same key interface? I do not think this is a question with python or Maemo, I just wanted to know how to do this.

+7
python user-interface pygtk code-reuse maemo
source share
3 answers

You can complete most of this in a factory:

 def createSpec(): if util.platform.MAEMO: return Maemo4Spec() elif util.platform.MAEMO5: return Maemo5Spec() return StandardPyGTKSpec() 

Then, somewhere at the beginning of the code, you simply call it factory:

  spec = createSpec() 

Now, wherever you had conditions, you simply call the desired function:

  spec.drawComboBox() 

While drawComboBox() handles something specific to the platform, you should be in good shape.

+10
source share

You can highlight platform-specific things that you need to do for small sequentially named functions inside the platform module, create the correct function name using the platform you are working on, and then getattr right and call it. Then the if / else template will be closed.

0
source share

I created a separate module to handle all my specialization between regular Linux, Maemo 4.1 and Maemo 5. It detects which features are available and allows the program to degrade properly.

for example

  def _fremantle_hildonize_window(app, window): oldWindow = window newWindow = hildon.StackableWindow() oldWindow.get_child().reparent(newWindow) app.add_window(newWindow) return newWindow def _hildon_hildonize_window(app, window): oldWindow = window newWindow = hildon.Window() oldWindow.get_child().reparent(newWindow) app.add_window(newWindow) return newWindow def _null_hildonize_window(app, window): return window try: hildon.StackableWindow hildonize_window = _fremantle_hildonize_window except AttributeError: try: hildon.Window hildonize_window = _hildon_hildonize_window except AttributeError: hildonize_window = _null_hildonize_window 

For more information, see Remote, Gonert, ejpi, or Quicknote source code for a file named hildonize.py https://garage.maemo.org/plugins/ggit/browse.php/?p=gc-dialer;a=blob;f= src / hildonize.py ;

Another example from One Ring GObject Utils (go_utils.py)

  def _old_timeout_add_seconds(timeout, callback): return gobject.timeout_add(timeout * 1000, callback) def _timeout_add_seconds(timeout, callback): return gobject.timeout_add_seconds(timeout, callback) try: gobject.timeout_add_seconds timeout_add_seconds = _timeout_add_seconds except AttributeError: timeout_add_seconds = _old_timeout_add_seconds 
0
source share

All Articles