boxee :: forums  

Go Back   boxee :: forums > boxee developers > boxee applications

Reply
 
Thread Tools Display Modes
  #1  
Old January 6th, 2010, 10:56 PM
joshuajonah joshuajonah is offline
Senior Member
 
Join Date: Jan 2009
Posts: 185
Default [Info] Persistent storage and global variables in apps

Devbobo posted an interesting question about how to keep data structures in memory for use throughout an app. So I figure we need a little rundown on persistance options in Boxee apps.

The Problem
When you define a variable in one event of an app XML, it is not accessible from other events in the same XML file. Each event is treated as a fresh Python script instance and therefore has no access to items defined in others.

Example:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
test_variable = "Exists"
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
print test_variable                # <- gives you an error that it is undefined
]]></onclick>
        </control>
Eventually you're going to have to find a way to store values short term for the session, or permanently.

Some Solutions
We have a number of ways we can hold a variable throughout the XML starting with the simplest:

Storing variables in LocalConfig an app has a class instance called a LocalConfig, this is a place for settings in an app. It is quite limited, but is usually just enough for most apps. It is a simple key -> value storage that is limited to strings and is kinda slow. This data will only be available for the session. Example usage using our previous example:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
mc.GetApp().GetLocalConfig().SetValue("test_variable", "Exists")
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
print mc.GetApp().GetLocalConfig().GetValue("test_variable") # <- prints "Exists" like you'd expect
]]></onclick>
        </control>
Relevant Boxee mc class docs

Using file storage This is by far the slowest, but permanently persistent, and relatively simple. Example code:
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
f = open('test_variable.txt', 'w')
f.write('Exists')
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
f = open('test_variable.txt', 'rb')
f.read()              # <- prints "Exists"
]]></onclick>
        </control>
Relevant Python docs

Using sqlite you can use sqlite as a DB in Boxee apps, boxee apps have the permission to create sqlite.db files in the app folders on the client machine. This is not only permanently persistent, but also very flexible as it is an entire relational database system. This comes at the cost of it being slightly heavy and complex. Also because of the separate scripts, you must open a new cursor for every script (not ideal but does the job):
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
from sqlite3 import *
conn = connect('appid.db')
curs = conn.cursor()
curs.execute("insert into sometable values ('test_variable', 'Exists')")
conn.commit()
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
from sqlite3 import *
curs = connect('appid.db').cursor()
curs.execute("select * from sometable")
for row in curs:
    print row
# prints "('test_variable', 'Exists')"
]]></onclick>
        </control>
Relevant Python docs

Anything You Want you could use memcached, CouchDB, MySQL, Postgres, Tokyo Cabinet, etc. But there's a catch. Your client will have to have this software, as well as the appropriate Python bindings installed to connect to these databases. Boxee uses sqlite for it's persistent storage, so you are safe to assume that sqlite is around. The others are not required and not installed with Boxee.

Hopefully this helps open your mind about what is possible with apps. Just try to remember that under all the complexity, it's just Python. Need to scrape a webpage? Make an FTP connection? Scan a barcode? Order a pizza? There's a Python package for just about anything you can think of, and they're all usable inside a Boxee App.

Considers starting a Dominoes pizza order and delivery app
__________________
A Guy Who Makes Boxee Apps Intermittently

Last edited by joshuajonah; January 7th, 2010 at 12:12 AM.
Reply With Quote
  #2  
Old January 7th, 2010, 06:09 AM
DPK DPK is offline
Super Moderator
 
Join Date: Feb 2009
Posts: 1,606
Default

Quote:
Originally Posted by joshuajonah View Post
Storing variables in LocalConfig - an app has a class instance called a LocalConfig, this is a place for settings in an app. It is quite limited, but is usually just enough for most apps. It is a simple key -> value storage that is limited to strings and is kinda slow. This data will only be available for the session.

My only critique would be that it will last longer than the current session. I've stored settings w/ the LocalConfig that would persist past the initial session. Overall excellent review though of the different methods. Stickied this topic for you.
__________________
Common issues w/ Boxee:
Beta Important Info | FAQ & Support Requests | Hulu Mature Content | Search

UnBoxeed app development:
Comics.com | ESPN360 | Weather

For more info, follow app development on Twitter!
Reply With Quote
  #3  
Old January 7th, 2010, 09:23 AM
joshuajonah joshuajonah is offline
Senior Member
 
Join Date: Jan 2009
Posts: 185
Default

Nice thanx.

Yes, the variables are kept throughout the Boxee session, not just the app session. But when you restart Boxee, you're back to square one.
__________________
A Guy Who Makes Boxee Apps Intermittently
Reply With Quote
  #4  
Old March 24th, 2010, 10:17 AM
russk russk is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default Writing to the file is not working

Hi,

I am trying to store some values to a file using the method as you mentioned. It gives me a error Invalid Mode: W. Please help me out.

Thanks.
Reply With Quote
  #5  
Old March 24th, 2010, 10:54 AM
agentlame's Avatar
agentlame agentlame is offline
Super Moderator
 
Join Date: Aug 2008
Location: Cleveland, OH
Posts: 3,709
Send a message via AIM to agentlame
Default

We need to see the code to help.

BTW, this info is inaccurate.

An application's configuration does persist across Boxee sessions.

More here:
http://developer.boxee.tv/App_Class#GetLocalConfig
__________________
personal: boxee | twitter | boxee hq
boxee: boxee chat | boxee wiki | boxee plugins

i run boxee 0.9.20 on: ubuntu 9.10, windows xp sp3 (virtual machine), windows 7 and a neuros link hacked to all hell.

what super powers does a 'super mod' have? the 'super ban'... that, and laser vision.
Reply With Quote
  #6  
Old March 24th, 2010, 11:35 AM
russk russk is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

This is the code...


Code:
<onload lang="python"><![CDATA[
try:
        import os
        f = open('test.txt', 'w')
	f.write('test')
	f.close()
]]></onload>
Reply With Quote
  #7  
Old March 24th, 2010, 12:04 PM
agentlame's Avatar
agentlame agentlame is offline
Super Moderator
 
Join Date: Aug 2008
Location: Cleveland, OH
Posts: 3,709
Send a message via AIM to agentlame
Default

Quote:
Originally Posted by russk View Post
This is the code...


Code:
<onload lang="python"><![CDATA[
try:
        import os
        f = open('test.txt', 'w')
	f.write('test')
	f.close()
]]></onload>
Yeah, don't do it that way.

Code:
config = mc.GetApp().GetLocalConfig()
config.SetValue('test', 'value')
print config.GetValue('test')
EDIT:
Like I said, this post is wrong about session persistence. I use this exact system for storing login in credentials across app sessions AND Boxee sessions. It works fine.
__________________
personal: boxee | twitter | boxee hq
boxee: boxee chat | boxee wiki | boxee plugins

i run boxee 0.9.20 on: ubuntu 9.10, windows xp sp3 (virtual machine), windows 7 and a neuros link hacked to all hell.

what super powers does a 'super mod' have? the 'super ban'... that, and laser vision.

Last edited by agentlame; March 24th, 2010 at 12:07 PM.
Reply With Quote
  #8  
Old March 24th, 2010, 12:27 PM
russk russk is offline
Junior Member
 
Join Date: Jan 2010
Posts: 6
Default

Thanks for the Reply.

But I want to make a generic playlist app that reads the feed link from a file stored locally. So i need to read a file as well as write to it. So that i need not change the xml everytime.
Reply With Quote
  #9  
Old March 28th, 2010, 12:28 PM
Fuzzgtp Fuzzgtp is offline
Super Moderator
 
Join Date: Apr 2009
Posts: 832
Default

Quote:
Originally Posted by joshuajonah View Post
[SIZE="1"]

Using sqlite you can use sqlite as a DB in Boxee apps, boxee apps have the permission to create sqlite.db files in the app folders on the client machine. This is not only permanently persistent, but also very flexible as it is an entire relational database system. This comes at the cost of it being slightly heavy and complex. Also because of the separate scripts, you must open a new cursor for every script (not ideal but does the job):
Code:
<?xml version="1.0"?>
<window id="14000">
	<onload lang="python"><![CDATA[
from sqlite3 import *
conn = connect('appid.db')
curs = conn.cursor()
curs.execute("insert into sometable values ('test_variable', 'Exists')")
conn.commit()
]]></onload>
        <control type="button">
            <onclick lang="python"><![CDATA[
from sqlite3 import *
curs = connect('appid.db').cursor()
curs.execute("select * from sometable")
for row in curs:
    print row
# prints "('test_variable', 'Exists')"
]]></onclick>
        </control>
Relevant Python docs
Have you actually gotten sqlite to work in Boxee?

Quote:
12:27:31 T:8076 M:1761574912 NOTICE: ImportError
12:27:31 T:8076 M:1761574912 NOTICE: :
12:27:31 T:8076 M:1761574912 NOTICE: No module named sqlite3
__________________
The Boxee Repository - An All-In-One Solution for your 3rd party repositories.
Like Fuzzthed.com on Facebook
Reply With Quote
  #10  
Old March 28th, 2010, 12:32 PM
darcilicious darcilicious is offline
Super Moderator
 
Join Date: Apr 2009
Location: Orbiting Venus
Posts: 10,086
Default

Looks like sqlite wasn't installed where/such that your python install can find it.
__________________
Note: This is a users/community forum. For official support, fill out a Support Request. For User Guides & more, see the Support Site.

Boxee beta 0.9.23.15885 on: HP TouchSmart 600xt/Windows 7 x64 & HP m8400f Vista x86 HTPC running Intelliremote, SageTV 7.1 beta, Orb/MP-1, Subsonic, & AirVideo, with an Olevia 247TFHD & Onyko TX-SR606 with a Harmony 550. Boxee Box v1.2.0.20310 with a 32" Panasonic TC-L32X2; and finally, an HP EX490 MediaSmart Server/WHS+MediaSonic for 12TB of storage.
Reply With Quote
Reply

Bookmarks

Tags
database, persistence, pizza, sqlite, storage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:56 AM.


skin based on greenzero from vBSkins.com

 
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2011, vBulletin Solutions, Inc.