×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    Belgium (Europe)
    Posts
    1,352

    Lightbulb test plug-in mechanism (from Mobile Python Book)

    hello ,

    Mobile Python Book introduce a very interesting feature : plug-in module downloaded from the web and import on device !

    Try it and enter test and run hello world from the web
    So you download test.py from my site and write it in e:\python\lib folder and finally import module test directly in your app.

    Code:
    import urllib,appuifw
    
    URL = "http://cyke64.googlepages.com/"
    
    def download_plugin(plugin_name):
        filename = plugin_name + ".py"
        code = urllib.urlopen(URL + filename).read()
        f = file(u"E:\\Python\\Lib\\" + filename, "w")
        f.write(code)
        f.close()
        return __import__(plugin_name)
    
    plugin_name = appuifw.query(u"Give plug-in name", "text")
    print "Downloading plugin", plugin_name
    plugin = download_plugin(plugin_name)
    print "Plugin loaded!"
    plugin.hello()
    This example is inspired from example 109 from Mobile Python Book from Jurgen Scheible and Ville Tullos
    Example 109 has an error You must add at the beginning

    Code:
    import appuifw
    Cyke64
    pys60 1.4.5 and 2.0.0, pygame, PyS60 CE on E90 and 5800 !

    Find my pys60 extension modules on cyke64.googlepages.com

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: test plug-in mechanism (from Mobile Python Book)

    Nice idea but I would use the application's private directory instead to prevent conflicts.

    You can use any directory since import uses the sys.path to find the module. So, you can create a "plugins" subdirectory in your app's directory, add it to path and download the plugins there.

    This idea can also be used to download updates. However, the app would have to be restarted after downloading I think.

  3. #3
    Regular Contributor
    Join Date
    Jan 2007
    Posts
    80

    Re: test plug-in mechanism (from Mobile Python Book)

    Couldn't you just use the reload function

    Ex:
    reload(appuifw)

    after you upgrade it

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: test plug-in mechanism (from Mobile Python Book)

    Hmm, it would be tricky (reloading the module you're currently executing?) but I think it's doable.

    You could place your main code in a separate module imported from default.py. Then the default.py would call a function in the main module and that function would keep running as long as the app would run. If the user would decide to quit, a False would be returned and the default.py would exit. The True would be returned on an update in which case default.py would call reload() and call the main function in main module again.

    Some kind of an internal restart

  5. #5
    Regular Contributor
    Join Date
    Sep 2005
    Location
    Finland, Helsinki
    Posts
    323

    Re: test plug-in mechanism (from Mobile Python Book)

    Quote Originally Posted by y.a.k View Post
    Nice idea but I would use the application's private directory instead to prevent conflicts.

    You can use any directory since import uses the sys.path to find the module. So, you can create a "plugins" subdirectory in your app's directory, add it to path and download the plugins there.
    I'm trying use "plugins" subdirectory inside my app's directory but without complete success. I'm creating a signed SIS-package with ensymble's py2sis-command.

    If I put my 2 test-plugins (test1_plugin.py and test2_plugin.py) to e.g. "E:\Python\plugins" and then sys.path.append("E:\\Python\\plugins")
    those plugins can be __import__:ed successfully.

    If I put them to the apps main directory those plugins are __import__:ed successfully without need to append anything to the sys.path (because "C:\Private\0xUID\" is already in the sys.path).

    But if I create plugins-subdirectory and then try __import__ plugins from there, python claims: ImportError: No module named test1_plugin. (I use "exception harness" to catch traceback in SIS-packaged app.)

    What I'm doing wrong?

    Here is how I add plugins-dir to the sys.path in the last case:
    Code:
        
        self.plugin_dir = os.path.join(os.getcwd(), u"plugins")
        appuifw.note(u"%s" % self.plugin_dir, "info")
        sys.path.append(self.plugin_dir)
        # sys.path contains now C:\Private\0xUID\ AND C:\Private\0xUID\plugins
    And here is the function to get a list of available plugins, works as expected:
    Code:
        
        def get_plugins(self):
            """
            Get a list of available plugins.
            All files starting with a letter and ending with .py
            are considered as a plugin.
            """
            r = re.compile(r'^([a-zA-Z]+[\w]*)\.py$')
            pluginlist = []
            for file in os.listdir(self.plugin_dir):
                matches = r.match(file)
                if matches is not None and matches.groups()[0] != 'BasePlugin':
                    #print matches.groups()[0]
                    pluginlist.append(matches.groups()[0])
                else:
                    pass
                    #print "Not included: " + file
            return pluginlist
    And here is the snippet which hopefully shows how I try to import plugins:

    Code:
            # "plugin" contains plugin's name e.g. test1_plugin
            for plugin in self.get_plugins():
                appuifw.note(u"%s" % plugin, "info")
                # THIS RAISES ImportError:
                self.mods[plugin] =  __import__(plugin)
                plugin_instance = self.mods[plugin].Plugin(self)
                self.insts.append(plugin_instance)
                self.menu_entries.append(((plugin_instance.title), plugin_instance))

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: test plug-in mechanism (from Mobile Python Book)

    I'm using 2nd ed. and getcwd() doesn't give me the app path. I prefer using os.path.split(sys.argv[0])[0] instead.

    And I'm not sure right now but maybe the sys.path cannot take unicode strings? If you use a unicode in one of the components passed to join(), it will give you a unicode string.

    So, you may want to try this (note the sys.argv and no "u" in front of "plugins" string):

    Code:
        self.plugin_dir = os.path.join(os.path.split(sys.argv[0])[0], "plugins")
        appuifw.note(u"%s" % self.plugin_dir, "info")
        sys.path.append(self.plugin_dir)
    Last edited by y.a.k; 2008-05-30 at 08:20.

  7. #7
    Regular Contributor
    Join Date
    Sep 2005
    Location
    Finland, Helsinki
    Posts
    323

    Re: test plug-in mechanism (from Mobile Python Book)

    Quote Originally Posted by y.a.k View Post
    I'm using 2nd ed. and getcwd() doesn't give me the app path. I prefer using os.path.split(sys.argv[0])[0] instead.

    And I'm not sure right now but maybe the sys.path cannot take unicode strings? - -
    Code:
        self.plugin_dir = os.path.join(os.path.split(sys.argv[0])[0], "plugins")
        sys.path.append(self.plugin_dir)
    This worked! Thank you for the tip. (Now I recall some previous issues with unicode/non-unicode strings in file and path names.)

Similar Threads

  1. Nokia Mobile VPN Client
    By marcyl in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2003-12-01, 14:47
  2. How To Access The Mobile Phone Book
    By abhiauradkar in forum Mobile Java General
    Replies: 3
    Last Post: 2003-10-09, 04:54
  3. Transferring address book from mobile phone
    By nkap00r in forum General Development Questions
    Replies: 1
    Last Post: 2003-07-09, 11:18
  4. How to read address book in mobile phone through AT command?
    By rickylee in forum General Messaging
    Replies: 1
    Last Post: 2002-12-14, 19:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×