Re: [Py4A] Re: Issue 43 in python-for-android: Android helper class file "androidhelper.py" for IDE development and script to create it

411 views
Skip to first unread message

Hariharan Srinath

unread,
Apr 7, 2012, 2:55:51 PM4/7/12
to python-fo...@googlegroups.com
Dear all,

Sharing "androidhelper.py" for simplifying Python-for-Android SL4A development in IDEs. This creates a "hepler" class derived from the default Android class and defines SL4A R5 functions with API documentation in DocString & uses RPC calls derived from the base android.py.

You can import this file into your scripts in your IDE to use Autocompletion, Documentation popups etc. By structuring your import of androidhelper / android using try/except as explained by Robbie below (and captured in the file comments), your script will run irrespective of whether androidhelper.py is there or not on your phone.

To generate this file, I have written createandroidhelper.py also attached. After some trial & error, I finally chose to use the HTML documentation contained at http://android-scripting.googlecode.com/hg/android/ScriptingLayerForAndroid/assets/sl4adoc.zip to generate androidhelper.py. Note that this currently has issues with 2 API functions which need manual corrections in androidhelper.py - both documented in the file comments.

I'd love to get the community's feedback on these! I guess these could also possibly do with some more testing :-)

Best Regards
Srinath

On Wed, Apr 4, 2012 at 9:44 PM, Robbie Matthews <rjmatt...@gmail.com> wrote:
RpcDoclet does some fancy stuff to combine api and standard javadoc markups into a combined html file.

Looking at TextDocumentationGenerator (in the same folder) might be a better option.

Robbie


On Wed, Apr 4, 2012 at 4:10 PM, Hariharan Srinath <srinathde...@gmail.com> wrote:
Cool! Thanks for this guidance... this is precisely what I was struggling a bit to to understand in android.py :-) 

I'll take a crack at putting together the complete androidhelper class for current R5 API functions over next few days & get back to the group.

Robbie - Yes, I BeautifulSouped the API Wiki page to extract the <pre> sections but after a few tries, had not automated it too much -  had simply done some manual editing.

But given this approach at coding helper classes works, i'll take another crack at better automation... either by parsing the API Wiki using Python or perhaps building something based on your current approach in RpcDoclet.java in the DocumentationGenerator - which probably would be a bit more difficult but possibly more elegant & sustainable.

Regards
Srinath



On Wed, Apr 4, 2012 at 7:13 AM, Robbie Matthews <rjmatt...@gmail.com> wrote:
That's not quite right... here is the tested, partial implementation (attached).

This is how you'd use in in development:
----------------
import androidhelper as android
droid=android.Android()
droid.makeToast("Hello")
----------------

Autocompletion works in idle, and so does the actual class (tested using remote control)

Robbie



On Wed, Apr 4, 2012 at 10:54 AM, Robbie Matthews <rjmatt...@gmail.com> wrote:
Look at the original android.py:
http://code.google.com/p/android-scripting/source/browse/python/ase/android.py

__getattr__ is just a catchall for any undefined methods (which is all of them, by default). It just wraps up the arguments and calls _rpc.

So, for each of your "helper" methods, just pass the arguments on to _rpc:

So your help class would now look like this:

----------
class AndroidHelpe(Android):

    def addContextMenuItem(label, event, eventData=None):
        '''
        addContextMenuItem(
         String label: label for this menu item,
         String event: event that will be generated on menu item click,
         Object eventData[optional])
        Adds a new item to context menu.
        '''
        self._rpc("addContextMenuItem",label,event,eventData)
-------
,
Robbie


On Wed, Apr 4, 2012 at 10:04 AM, Sergey Zelenev <zel...@gmail.com> wrote:
I've tried, but my python skills are not strong enough..

it looks like the helper methods should be returning something like super(Android,self).__getattr__(self.__name__) for each of the methods, but I don't know how to pass the values to getattr


вторник, 3 апреля 2012 г. 18:01:12 UTC-5 пользователь Robbie написал:
I agree. Anyone want to build it?

I assume the original author built something to parse the wiki page to build this... if he made that code available, it could be added to.

Robbie

On Wed, Apr 4, 2012 at 7:36 AM, <python-for-android@googlecode.com> wrote:

Comment #4 on issue 43 by zele...@gmail.com: Android class definition "helper" files to support SL4A development on PCs
http://code.google.com/p/python-for-android/issues/detail?id=43

The class in the module isn't really functional as far as I can tell.

What if we call this module androidhelper.py and import it instead of android.py?

Would something like this work better?

import android as droid
class Android(droid.Android):
   def __init__(self, addr=None):
       super(Android, self).__init__(addr)

   def addContextMenuItem(label, event, eventData=None):
            '''
            addContextMenuItem(
             String label: label for this menu item,
             String event: event that will be generated on menu item click,
             Object eventData[optional])
            Adds a new item to context menu.
            '''
            super(Android,self).addContextMenuItem(label, event, eventData)

    def addOptionsMenuItem(label, event,eventData=None,iconName=None):
            '''
            addOptionsMenuItem(

etc..



This way we can do the following:

import androidhelper as android

droid = android.Android()


In my opinion this would give us the helper and would not break RPC







androidhelper.py
createandroidhelper.py

Robbie Matthews

unread,
Apr 7, 2012, 6:30:26 PM4/7/12
to python-fo...@googlegroups.com
Very cool. Once we've got some feedback on this, I may include this as part of the normal install.

Do you have any objections to this?

Robbie

PS: For copyright notices, I use "Robbie Matthews", not "Robert J Matthews"
--

Beginning Android Tablet Programming

  • Publication Date: November 2, 2011
  • ISBN13: 978-1-4302-3783-9

Robbie Matthews gives a full, no-holds barred introduction to Android  programming for tablets. No previous experience required! Learn More …

Beginning Android Tablet Programming


Robbie Matthews

unread,
Apr 7, 2012, 6:52:30 PM4/7/12
to python-fo...@googlegroups.com

Hariharan Srinath

unread,
Apr 7, 2012, 10:33:41 PM4/7/12
to python-fo...@googlegroups.com

Thank You!

Nope.. no objections - hope it becomes useful.

Best Regards
Srinath

Reply all
Reply to author
Forward
0 new messages