PDA

View Full Version : Python Interpreter Development "Python Inside"


darkie
2003-10-09, 17:19
first of all 'python 2.3' is embedded into xbmc and you the need to do the following to get it working

extract the directories from python.rar (cvs\xbmc\python\python.rar)
to "xbmc home dir\python\"
and if you want some examples you should extract scripts.rar(cvs\xbmc\scripts\scripts.rar)
to "xbmc home dir\scripts\"

you will now have the next directory structure in xbmc
xbmc
* python
* * * lib
* * * temp
* * * www
* scripts
* * * medusa

you execute scripts thrue the myscripts gui which is under settings->scripts.
currently there is no interaction with xbmc, for example you can't see any output from the python interpreter (this will be added because it's the only way to see where the script stops when an error occours)

included in scripts.rar are the next files

chat-server.py: runs a chat server on port 4000
download_apleasure_zip.py: downloads a file from ftp and places it in your xbmc home dir
download_xskit303_exe.py: downloads another file from ftp
echo_server_port_50007.py: runs an echo server on port 50007
mp3.py: only used for testing and doesn't work
url_fetcher_asyncore.py: used for testing and doesn't work
weather.py: used for testing and doesn't work

medusa\start_medusa: runs an ftp / web and chat server on the ports 8021, 8080 and 8888

if you need any documentation or need more scripts examples you can try the official python website http://www.python.org

Gamester17
2003-10-10, 13:12
darkie, as the coder of xbmc's python interpreter can you tell us all a little more about the techical aspects behind it, things like; is it loaded into a pragma section?, is it or will it be loaded by default?, how much ram does it use?, once the python interpreter is loaded into memory will/can it be unloaded when not used anymore or is reboot nessesary?, once a python script is loaded into memory will/can it be unloaded when not running anymore?, where are you planning to take this development wise?, and how do you plan to implement it in the gui, now & later?

...and other things like that, tia http://www.xboxmediaplayer.de/forums/non-cgi/emoticons/cool.gif

Frodo
2003-10-11, 11:46
>is it loaded into a pragma section?
yes it is

>is it or will it be loaded by default?
no

>how much ram does it use?
when loaded about 350kbyte

> once the python interpreter is loaded into memory will/can it be unloaded when not used anymore
no, sorry?,

>once a python script is loaded into memory will/can it be unloaded when not running anymore?
yes
frodo

Gamester17
2003-10-11, 14:57
> once the python interpreter is loaded into memory will/can it be unloaded when not used anymore
no, sorry?
why? i mean it must be possible somehow, or isn't? this will be a challange for all you devs to figure out http://www.xboxmediaplayer.de/forums/non-cgi/emoticons/wink.gif

Frodo
2003-10-11, 16:13
why?

cause darkie couldnt get it to work (yet)
hopefully he'll manage to fix it

frodo

Gamester17
2003-10-11, 18:35
why? cause darkie couldnt get it to work (yet)
hehe, i meant the technical reason. i already knew that it didn't, i just thought i start an open dev/code discussion here http://www.xboxmediaplayer.de/forums/non-cgi/emoticons/wink.gif

Frodo
2003-10-11, 19:33
k here's why:

python is using lots of static functions & vars
the problem is that static functions & vars dont work with section loading. we didnt find a solution to this yet
there's one solution and thats 2 rewrite python so it doesnt use any static things anymore. but thats a lot of work
so if any compiler/xdk/c++ guru's know a way how to get section loading 2 work with static vars & functions please let us know
frodo

darkie
2003-10-13, 17:43
k, first of all, python is made for working in processes and not threads, that means if you run a program in windows and stop it, all used memory is given back to the operating system.
since the xbox doesn't work with processes used memory that isn't released by python won't be given back to the xbox.

python is managing memory in a special way. it doesn't allocate memory for every object or string that is created, but instead it allocates 256k(called an area) of memory at once and keeps track of all objects, strings that are placed in it.
once you don't need an object anymore, python removes it from the 256k area but doesn't give the memory back to the system, instead when a new object is created, this object is placed in that free part of the area. python does this because it is faster then allocating / deallocating memory.
when more then 256k of memory is needed another area of 256k is created and so on.
all these area's are only released when a proces ends, and there is the problem on the xbox. memory once needed for a script in python is never given back to xbmc.

python as it exists in xbmc is created with 2 pragma section: python and py_rw.
python is a section which contains *c / c++ code and memory that can't change, this section is about 800k.
py_rw contains variable memory that is modified when python is used, this section is about 300k.

at startup of xbmc none of these sections are loaded, python doesn't use any memory at all.

when a python script is selected in xbmc to execute. both python and py_rw are loaded into memory and python is initialized. at this moment 1200k of memory is used.
now the script is beeing executed and if it needs memory for storing stuff a new area (see above) is created.
that means that if a script uses 4 areas, a total of 4 * 256k + 1200k = 2200k of memory is used from the 64mb available on the xbox.

when the script ends, all 4 areas are cleaned so they can be used when an user selects a next script to execute( 4 * 256k of memory isn't given back to the xbox!).
section python will be unloaded by xbmc and py_rw is not unloaded.
at this moment, 1 mb for the areas and 300k for py_rw is in use by python.
if we select another script to run, section python is loaded again. if this scripts would need 750k of memory, python will use the existing areas for it. that means 2200k of memory is used

i didn't unload py_rw because loading it again will reset all memory. if we would unload py_rw and load it when we would need python again. python would think there where no areas created before and therefore creates new areas.
at this moment the 1 mb of areas created for the first script is lost and will never be used again (a memory leak of 1 mb!)

that was the technical part, so what can you expect to come.
well there are still some small bugs to fix (including 256k areas that are never given back to xbmc).
currently i am buzy to integrate some xbmc stuff into xbmc, like a yes / no dialog and the progress dialog. for example, you could use the progress dialog when downloading a file from ftp so an user could see how long it takes before it would finish.

darkie

Frodo
2003-10-15, 09:20
darkie, thx for the explanation.
i still think python in xbmc has a lot of potential
especially when its able to access xbmc stuff like dialogs,windows etc.
things i would like 2 see for python:

1. bind a button control to a script.
so pressing a button will run a script
2. python scripts should b able to use the controls like:
- cguiimage to display an image
- list control
- text area
- button
- spincontrol

3. mapping of keys -> python

with these 3 things one should b able to build almost anything in pyton

frodo

darkie
2003-10-18, 00:59
ok, made a little progress.
with python now, you can use the 4 dialogs (dialogok, dialogyesno, dialogselect, and dialogprogress)


1. bind a button control to a script.
so pressing a button will run a script
2. python scripts should b able to use the controls like:
- cguiimage to display an image
- list control
- text area
- button
- spincontrol

3. mapping of keys -> python

1 and 2 are both possible, but what exactly do you mean with 3. mapping of keys -> python?

darkie
2003-11-08, 01:43
for xbmc there is no documentation atm. but since writing python code for windows is the same as writing it on the xbox you should be able to find most information at python.org (http://www.python.org)

for xbmc specific functions you can take a look at the examples which are in cvs\scripts\

problem for now is that there isn't any error output to xbmc, so you can't see if the script runs ok or not.

darkie
2003-11-30, 21:21
ok, python has the option now to create windows where text and images can be drawn on.
i've just added a small example to the cvs tree so people can have a look at it.

note, it is impossible to close a window at this time with your controller or remote. will be fixed soon.

darkie
2003-12-03, 17:20
just did another update to python.

actions are now send to a window created with python. this means that when you press back on your remote control, the script will recieve action_previous_menu which is defined in keymap.xml.

i have updated windowexample.py to give you some idea.

alx5962
2004-01-30, 12:53
hi

does the pil library is planned to be added in your port darkie?
it's useful to process images.
more infos here : pil website (http://www.pythonware.com/products/pil/)

i don't know at all how python libraries are working, and also i have no ideas how to compile them for xbmc. so more infos about this process could help us all i guess.

thanks

alex

darkie
2004-02-01, 15:41
does the pil library is planned to be added in your port darkie?
maybe, but not at this time. problem is al those libraries take a lot of memory and python is already using to much

about the w.domodal().
if you leave it away, you will see that the python window doesn't stay onscreen because the python script just ends. using w.domodal() or an endless loop will let the script run forever. with the difference that

w.domodal() takes no processor speed and stops when w.close() is called.

an endless loop will take 100 % processor speed and only stops when you break out of the loop

and i'll try to upload new sources to cvs today which makes it possible to use xbmc playlists and add buttons to the window

kexman
2004-02-08, 01:04
hi.

is it possible to use the pysqlite library with the xbmc python impementation.

i've fiddeled around some and tried to get it to work with no luck at all, and i think the problem is that it can't load the _sqllite.pyd library, so the question is: can we load external python libraries into xbmc?

darkie
2004-02-08, 01:42
you can't load external dll's (.pyd) yet. i'm busy with that atm.
only problem is that the dll has to be compiled for the xbox, and _sqllite.pyd is probably not.

alx5962
2004-02-15, 23:17
darkie, i just noticed sometimes when a python script crash, xbmc is totally frozen and so i need to restart the xbox.
is this a known bug?

and what do you think about adding a debug log for python? so it would be easier to know where is the problem when the script crashes the xbox.

darkie
2004-02-16, 00:13
darkie, i just noticed sometimes when a python script crash, xbmc is totally frozen and so i need to restart the xbox.
is this a known bug?

if python crashes xbmc will crash to, can't do anything about that. but python shouldnt crash at all *:d

and what do you think about adding a debug log for python? so it would be easier to know where is the problem when the script crashes the xbox.

good idea. will do that.

alx5962
2004-02-19, 16:24
darkie,
are you planning to modify the windowexample script soon?
as some functions are not documented like :
setnavigation, removecontrol, setfocus for the window object are not very clear for me and the example is the only way to have infos about how to use them...

also, in my script i'm doing a dirty thing : i just add one more controlimage over the previous one to erase the screen in the same class. it uses memory for nothing so and it's too dirty. so how can i do that please?

darkie
2004-02-19, 20:19
is there or will there be any support for the on screen keyboard so i could add a custom names option to the above code?
no. but i can always have a look at it to add it.

darkie,
are you planning to modify the windowexample script soon?
as some functions are not documented like :
setnavigation, removecontrol, setfocus for the window object are not very clear for me and the example is the only way to have infos about how to use them...

no don't have it planned :) . but what still needs to be done is writing better documentation for python (there is none currently).
but since i don't have time at all now, i can't add/create any new code / documentation for python.
maybe someone else is willing to make a start at this?

also, in my script i'm doing a dirty thing : i just add one more controlimage over the previous one to erase the screen in the same class. it uses memory for nothing so and it's too dirty. so how can i do that please?
you can use removecontrol to remove all the controls.
you can also delete the window and create it again to start with a new clean window.

alx5962
2004-02-22, 13:10
hi darkie!

would it be possible to include a version number in the future releases? as you added more (nice) features in the latest versions but some scripts don't work anymore. and so with a version number we could alert people their python version is too old to run the script.

thanks

alex

darkie
2004-02-23, 19:04
just a quick reply, playlist support in python is written but i did not have the time yet to test / complete it. this is the first thing i'll do when i have some time left for xbmc

would it be possible to include a version number in the future releases? as you added more (nice) features in the latest versions but some scripts don't work anymore. and so with a version number we could alert people their python version is too old to run the script.

will do that.

alx5962
2004-03-12, 00:18
support for xpr would be nice, as default skin as no background image in supported format now :( that means all the actual scripts with background image don't work anymore...

support for the new virtual keyboard would be wonderful too !

btw darkie, i tried to use the 0-9 keys of the remote but nothing happened... normal behaviour or do am i a bad coder? :d

Butcher
2004-03-22, 15:07
support for xpr would be nice, as default skin as no background image in supported format now *:( *that means all the actual scripts with background image don't work anymore...
so far as i can tell xpr images should work fine. can you give me an example script that doesn't work (i'm not a python coder).

darkie
2004-03-22, 22:53
support for xpr would be nice, as default skin as no background image in supported format now * *that means all the actual scripts with background image don't work anymore...
you can display files from xpr files, but only if the skin that uses thisxpr is set as the current skin.
to load a picture from such an xpr just use
xbmcgui.controlimage(0,0,720,576, 'background.png')
and not the full path (need to be fixed in windowexample.py)

alx5962
2004-03-28, 22:05
darkie, you're the man * http://www.xboxmediaplayer.de/forums/non-cgi/emoticons/cool.gif

xml parser is working perfectly and the virtual keyboard too!

i'm so damn happy with all these new tools *:)

i started to write a xbox python tutorial i will send it to you if u wish

edit : after some tests, it took me at least 30seconds to parse an 80kb xml file! and for the same script it took 2-3 seconds on windows... is this a memory issue?

darkie
2004-03-30, 13:52
edit : after some tests, it took me at least 30seconds to parse an 80kb xml file! and for the same script it took 2-3 seconds on windows... is this a memory issue
could be a memory issue.
but the biggest problem is that on your windows machine all processor speed is available for the python script
on the xbox the script has to share the processor speed with other threads (for example the gui). it could be set to a higher priority so the script will run faster. but then you could not control your xbox anymore :)

alx5962
2004-04-11, 18:45
darkie, i added some functions to your python lib.
as i have no commit rights in the cvs, can you add my code please?

lib changes (http://www.gueux.net/xbmc-scripts/xbmc/12042004.rar)

edit : added some more functions

Alexpoet
2004-07-19, 01:14
any chance i could get you guys to add a function to the real xbmcgui library named "emulating" that returns false?

it would make things a whole lot easier with my emulator scripts that i'm making. i know it's a lot to ask to change the main functions just to assist in a little tool like stubbed code, but others developing better emulators or whatever could use the function, too. it would just be a way to see that, this particular instance of the script is running on the legitimate platform (when emulating returns false), or else it's being faked and probably needs extra function calls.

this is all i'd need (written python style, anyway):

def emulating(self):
* * return false


to call it, i'd add to the top of my script:
emulating = xbmcgui.emulating()
or, that's short enough, i'd probably just check right on the function:
* *if xbmcgui.emulating(): dothis()
just a humble request....

Alexpoet
2004-07-22, 06:27
any chance we could get a function in xbmc that starts an xbmc picture slideshow (with random as an option, or an alternate function)? i don't think it can be done with the playlists, and it's another function already built into xbmc that would be awesome to be able to trigger with a script.

let me know.

Alexpoet
2004-07-26, 06:27
request: let us cast the starting coords for xbmcgui.controlimage as negative numbers.

please! pretty, pretty please add this feature! i'm begging you!

i know it will be very crazy difficult to write. i know you're all shaking your heads and saying, no, no, that's impossible....

but it would be so helpful to me! i have no other argument (and that's not a good one), but i am emphatic, anyway! that's gotta be worth something!

currently we can draw pictures starting off the screen to the right and bottom (that is, positive numbers larger than the screen's width and height), but if you give it any negative numbers for x and y, it just doesn't draw the picture at all (whereas, i want it to draw the pixels that would be on-screen).

that's all. just...begging. that's all.

alexpoet.

(oh, and getfocus, too. and emulating = false in xbmcgui. but all of those are secondary! negative-value off-screen coords first!)

Alexpoet
2004-08-13, 17:40
darkie,
we seriously need a way in python to interrupt the current media.

specifically a way to play an audio, video, or image file from a python script without losing position in xbmc's currently playing media.

this could either include an automatic pause of xbmc's media (if xbmc can't handle two different media streams at the same time), or you could just overlay the python media and leave it up to the python scripters when to player.pause() and when to leave it running.

this could be useful for timed audio alarms (you're watching "gross pointe blank" and thirty minutes in a buzzer reminds you you're supposed to go to work), for weather warnings (a monitor script could be checking the government weather site and pop up a warning letting you know if a tornado's on the way), or maybe even for something like picture-in-picture.

those would all be uses of the dual simultaneous streams, and if that's not possible i understand (i know it's been requested before). but for at least the first two examples, it would be nice to be able to interrupt whatever is playing without losing your spot.

i can already stop a music playlist to play a buzzer sound, but that buzzer sound replaces the current playlist. and if i relaunch the previous playlist, it starts over from 0.

just some thoughts. let me know what you think.

Alexpoet
2004-08-16, 22:35
how hard would it be to make id3 information about a song available to python scripts? i've been wanting to write a particular script for ages, but kept it on the shelf because i knew i didn't have access to that info.

but i just read a thread on xbox scene by a guy wanting to do something that would be a simple script, if he had that same info. so i thought i'd make it a semi-official feature request. could you provide a function in xbmc that'll python can call to get the id3 information for the currently playing media?

would be very cool.

alexpoet

darkie
2004-08-18, 17:23
we seriously need a way in python to interrupt the current media.

specifically a way to play an audio, video, or image file from a python script without losing position in xbmc's currently playing media.

this could either include an automatic pause of xbmc's media (if xbmc can't handle two different media streams at the same time), or you could just overlay the python media and leave it up to the python scripters when to player.pause() and when to leave it running.
not possible
we can have only one song playing or pausing mplayer. the only way to make your audio alarm is to stop the current file, play the alarm, and start playing the file again. only problem is you can't continue from where the movie stopted, you have to watch it all over again :)

darkie
2004-08-19, 21:04
check current cvs
it has support for id3 tags and imdb.com information (but only if it's available in xbmc's movie database)
and you can recieve events when xbmc starts and stops playing a song + a lot more

solexalex
2004-10-28, 01:54
hi there !
i need to get informations about the file i am planning to play inside xbmc. this file is a stream file so the only thing i know is the url. but as you may know, some of the stream files are not working yet with xbmc, or some are soooo long to open. some others are so bad because of low bitrate or for 'little' connections, some streams may hang because too much high bitrate...
so as you can figure, many differents possibility for a stream file.
my question is, will it be possible to get infos about the stream but before playing it ? when starting, the popup show tons of informations, but it is not possible to re-use it ! can you make an output with file informations
(player.infos(url) -> bitrate, codec, mimetype, length, title,... ... ... ...)
thanks in advance.
alexsolex
nb: i am one of the http://xbmc-stream.gueux.be/ team member. you are welcome to visit us if you like streaming (pc or xbox). i am developping a python script that is based on our database of streams ( http://xbmc-stream.gueux.be/test/pcstream/ )
so if you like it, you should do your best to help me !
see ya

madtw
2005-01-15, 21:09
i was wondering if there is a way to use the icon/big icon/detailed list controls in a python script? i have played around with xbmcgui.controllist but it is somewhat limited compared to the programs, music, and videos "list" display in xbmc.

specifically, i am wondering about how to associate an image with a list item and at the same time associate text with the item. as for the text, i'd like to have the name of the item fade (like music file names that scroll when they are too long for the list item) and i'd like to able to set the text for the second column (where the size of the music file is displayed). has this been exposed in python yet? the other piece to the puzzle is being able to associate a control to change between small icon/big icon/detail views in the list.

if this is something that can already be done, please enlighten me. if it hasn't been done, then i will rethink what i am working on because i could see this taking a while to implement and there are probably more high priority items on the list. :)

madtw
2005-02-09, 07:43
we seriously need a way in python to interrupt the current media.

specifically a way to play an audio, video, or image file from a python script without losing position in xbmc's currently playing media.

this could either include an automatic pause of xbmc's media (if xbmc can't handle two different media streams at the same time), or you could just overlay the python media and leave it up to the python scripters when to player.pause() and when to leave it running.
not possible
we can have only one song playing or pausing mplayer. the only way to make your audio alarm is to stop the current file, play the alarm, and start playing the file again. only problem is you can't continue from where the movie stopted, you have to watch it all over again :)
darkie: if iplayer::gettime() and iplayer::seektime() were exposed to python, would it be possible for a python script to query the currently playing media for its position and reset the position respectively or am i misunderstanding what those methods do?

madtw
2005-02-10, 08:19
i have some questions on this code that i was playing around with...


import os
import string
import time
import xbmc
import xbmcgui

def getcwd():
d = string.replace( os.getcwd(), ";", "" )
return d

class player( xbmc.player ):
def ( self ):
xbmc.player.( self )

def onplaybackstarted( self ):
xbmcgui.dialog().ok( "info", "started" )
fh = file( getcwd() + os.sep + "playback.start", 'w' )
for i in range(0,3):
str = "playback started %d\n"%i
xbmc.output( str )
fh.write( str )
time.sleep( 3.0 )
fh.close()

def onplaybackended( self ):
fh = file( getcwd() + os.sep + "playback.end", 'w' )
fh.write( "playback ended\n" )
fh.close()
xbmcgui.dialog().ok( "info", "done" )

p = player()
p.play( "smb://banshee/mythtv/1002_20050209210000_20050209220000.nuv" )
while p.isplaying():
pass
time.sleep( 3.0 )
xbmcgui.dialog().ok( "info", "quitting" )


when this runs, it starts playing the media file and immediately afterwards i get a dialog saying "started". also, the file is created on disk. when i hit stop, it pauses for around 3 seconds then i get a dialog saying "quitting". i was expecting to get a dialog saying "done", then a dialog saying "quitting". now if i fast forward to the end of the video, i get a pause for 3 seconds, then a dialog saying "done", then a dialog saying "quitting". is the onplaybackended() method only meant to be called when the player() finishes playing without user intervention?

i was hoping to use the onplaybackended() method to do some cleanup. instead, i may have to do something like:


p.play( filepath )
while p.isplaying():
time.sleep(1.0 )
# cleanup code


it might be worth while to have onplaybackended() be passed a boolean parameter where true means playback was ended by user intervention and false means playback ended normally.

darkie
2005-02-10, 17:07
yup, this is the current implementation of onplaybackended. i didn't realise that when i exposed onplaybackended to python.
so currently there is no way for python to check if the player stopped when someone pressed the stop button.

SiW
2005-02-14, 07:33
interest in my python slideshow hack has been expressed, but i don't really have time to work on it properly now so i thought i'd just throw a patch together:

http://www.coolpowers.com/lab/data/xbmc.patch

NewNole2001
2005-09-19, 09:07
if there are any xbmc devs reading this could you pretty pretty please implement "set" methods to complement each of the "get" functions in this file:
xbmc/xbmc/xbmc/lib/libpython/xbmcmodule/infotagmusic.cpp
i would do this myself because it doesn't look like it would be very hard, but i have no way of testing, as i don't have the xdk

this will help make my xm radio script so much better.