Python API

From DevWiki

Revision as of 14:57, 3 April 2011 by Riegersn (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Applications

Contents


Overview

Boxee Applications fully support the use and integration of Python. Currently, Boxee is running Python 2.4. Code can be integrated into the UI by placing python scripts into the root of your applications directory. See Application Anatomy for more details. Additionally python code can execute from within the Window XML Files themselves by way of Control Events and CDATA tags.


MC Module

Boxee has built the 'mc' module, available to all applications, for integration with your applications and most of its controls. With built-in methods to modify button and label text, make controls visible/hidden, modify list containers and their content and much more.


Usage Examples

Button OnClick Event

  1. <control type="button" id="7002">
  2.    <width>115</width>
  3.    <height>60</height>
  4.    <label>Search</label>
  5.    <onclick lang="python">
  6. <![CDATA[
  7. mc.ShowDialogNotification('You clicked the search button!')
  8. ]]>
  9.    </onclick>
  10. </control>

Window OnLoad Event

  1. <window type="window" id="14000">
  2.    <defaultcontrol always="true">1000</defaultcontrol>
  3.    <allowoverlay>no</allowoverlay>
  4.    <onload lang="python">
  5. <![CDATA[
  6. config = mc.GetApp().GetLocalConfig()
  7. path = config.GetValue("feed"):
  8. mc.GetActiveWindow().GetList(120).SetContentURL(path)
  9. ]]>
  10.    </onload>
  11.    <controls>
  12.       <control type="list" id="120">
  13.          ...
  14.       </control>
  15.    </controls>
  16. </window>

Importing from Root

Python in applications root directory:
myapp/myscript.py

  1. import mc
  2. def login():
  3.    uname = mc.ShowDialogKeyboard("Enter Username", "")
  4.    pword = mc.ShowDialogKeyboard("Enter Password", "", True)
  5.    if uname and pword:
  6.       http = mc.Http()
  7.       params = "user=%s&pass=%s" % (uname, pword)
  8.       http.Post("http://mysite.com/login/", params)
  9.       responseCookie = str(http.GetHttpHeader("Set-cookie"))
  10.       if 'PHPSESSID=' in responseCookie:
  11.          return True
  12.    return False

Importing the script from within your application:

  1. <control type="button" id="7002">
  2.    <width>115</width>
  3.    <height>60</height>
  4.    <label>LOGIN</label>
  5.    <onclick lang="python">
  6. <![CDATA[
  7. import myscript
  8. result = myscript.login()
  9. if result:
  10.    mc.ShowDialogNotification("You are now authenticated!")
  11. ]]>
  12.    </onclick>
  13. </control>

Launching with Python

It is possible to launch your applications with a python script instead of a window. First order of business is to modify your descriptor.xml and set the startWindow tag to the script you'd like loaded when your application is launched.

  1. <app>
  2.   ...
  3.   <type>skin</type>
  4.   <platform>all</platform>
  5.   <minversion>0.9.21</minversion>
  6.   <startWindow>myscript</startWindow>
  7.   <test-app>true</test-app>
  8. </app>

Make sure that myscript.py is located in the root if your app directory.
myapp/myscript.py

  1. import mc
  2. # do stuff
  3. # launch your main application window
  4. mc.ActivateWindow(14001)


Things to Remember

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox