Development Team/Almanac/sugar.datastore.datastore: Difference between revisions
No edit summary |
m moved Walter is a wanker 12/Almanac/sugar.datastore.datastore to Development Team/Almanac/sugar.datastore.datastore over redirect: revert |
||
| (20 intermediate revisions by 9 users not shown) | |||
| Line 1: | Line 1: | ||
{{ | {{Almanac}} | ||
{{ | {{Almanac TOC}} | ||
= High Level Functionality of the Datastore = | = High Level Functionality of the Datastore = | ||
| Line 32: | Line 32: | ||
=== How do I provide a query to the datastore.find() method so that I can find datastore objects with a particular property? === | === How do I provide a query to the datastore.find() method so that I can find datastore objects with a particular property? === | ||
'''IMPORTANT''' Do not rely on datastore.find() method in all cases, its behaviour could be changed in future releases;<br>there is only on thing for sure: to treat jobjects that were created within activity use:<br>datastore.find({'activity_id': <activity-id-of(and only of)-current-activity>, ...}) | |||
The datastore package includes a find() helper function that allows you to find things in the datastore. Belows is a very simple example use of find where I simply search for journal entries that have a title of 'file1.txt'. I think print out the title and file path for each retreived entry. | The datastore package includes a find() helper function that allows you to find things in the datastore. Belows is a very simple example use of find where I simply search for journal entries that have a title of 'file1.txt'. I think print out the title and file path for each retreived entry. | ||
| Line 41: | Line 44: | ||
print "Number of Objects: " + str(num_objects) | print "Number of Objects: " + str(num_objects) | ||
for i in xrange ( | for i in xrange (num_objects): | ||
print "File Path: " + ds_objects[i].get_file_path() | print "File Path: " + ds_objects[i].get_file_path() | ||
print "Title: " + ds_objects[i].metadata['title'] | print "Title: " + ds_objects[i].metadata['title'] | ||
| Line 74: | Line 77: | ||
#loop through and delete each datastore object found. | #loop through and delete each datastore object found. | ||
for i in xrange ( | for i in xrange (num_objects): | ||
print 'DELETING ' + ds_objects[i].metadata['title'] + '...' | print 'DELETING ' + ds_objects[i].metadata['title'] + '...' | ||
ds_objects[i].destroy() | ds_objects[i].destroy() | ||
| Line 95: | Line 98: | ||
#loop through list of values and print them out. | #loop through list of values and print them out. | ||
for | for x in unique_val_array: | ||
print | print x | ||
</pre> | </pre> | ||
| Line 119: | Line 122: | ||
print '------------------------MOUNTS--------------------------------' | print '------------------------MOUNTS--------------------------------' | ||
ds_mounts = datastore.mounts() | ds_mounts = datastore.mounts() | ||
for | for x in ds_mounts: | ||
print '---------MOUNT---------' | print '---------MOUNT---------' | ||
print 'title: '+ | print 'title: '+ x['title'] | ||
print 'uri: '+ | print 'uri: '+ x['uri'] | ||
print 'id: ' + | print 'id: ' + x['id'] | ||
</pre> | </pre> | ||
| Line 133: | Line 136: | ||
= Class: DSObject = | = Class: DSObject = | ||
=== How do I access the metadata entries for a datastore object? === | |||
Every DSObject created by datastore.create() has a metadata property. This property refers to a [[Low-level_Activity_API#Meta_Data|DS Object]], which contains the metadata for your datastore object. The code below shows how to read metadata values by referring to the metadata property of a DSObject. metadata is a Python dictionary object. | |||
<pre> | |||
from sugar.datastore import datastore | |||
... | |||
#my_dsobject is of type datastore.DSObject | |||
#object_id is a datastore object (e.g., returned from the datastore Chooser) | |||
my_dsobject = datastore.get(object_id) | |||
#Access the 'description' property | |||
print my_dsobject.metadata['description'] | |||
</pre> | |||
This code snippet shows how to create a pixbuf from the preview entry in the datastore: | |||
<pre> | |||
from sugar.datastore import datastore | |||
... | |||
#my_dsobject is of type datastore.DSObject | |||
#object_id is a datastore object | |||
my_dsobject = datastore.get(object_id) | |||
pixbufloader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/png') | |||
#Access the 'preview' property | |||
#Note that the preview image is 300x225 | |||
pixbufloader.write(dsobject.metadata['preview']) | |||
pixbufloader.close() | |||
pixbuf = pixbufloader.get_pixbuf() | |||
</pre> | |||
=== How do I create new metadata entries or reassign metadata for a datastore object that has been created? === | === How do I create new metadata entries or reassign metadata for a datastore object that has been created? === | ||