Sugar.datastore.datastore: Difference between revisions
Line 32: | Line 32: | ||
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'] |
||
</pre> |
|||
=== How do I delete datastore entries with a particular property? === |
|||
The following code shows a function that contains a property name and value for the metadata of a datastore object. The function searches out and deletes any datastore entries where the metadata property matches the value passed. |
|||
<pre> |
|||
def _delete_dsobjects(self, prop, val): |
|||
ds_objects, num_objects = datastore.find({prop: val}) |
|||
for i in xrange (0, num_objects, 1): |
|||
print 'DELETING ' + ds_objects[i].metadata['title'] + '...' |
|||
ds_objects[i].destroy() |
|||
datastore.delete(ds_objects[i].object_id) |
|||
</pre> |
</pre> |
||
Revision as of 20:25, 20 June 2008
Sugar Almanac for Developers |
---|
Sugar Almanac Main Page Package: sugar |
Package: sugar.activity |
Package: sugar.graphics |
Package: sugar.datastore |
Logging |
Notes on using Python Standard Logging in Sugar |
Internationalization |
Datastore Helper Functions
How do I create a new datastore object?
In addition to the write_file() method, which allows sugar's activity code to save your activity by creating a datastore object, you can explicitly create datastore objects in your activity code using the datastore.create() helper function. This function will return an object of type DSObject. The following code shows how a new datastore object is created and then actually written to the datastore. Other sections will discuss how this datastore object can actually be assigned to files and populated with useful metadata.
#This method creates a datastore object that will be saved for later use by this activity. def _create_ds_object(self): #my_dsobject is of type datastore.DSObject my_dsobject = datastore.create() # ... you can put any code to change your datastore file and metadata here ... #Persist this newly created datastore object in the datastore for later access. datastore.write(my_dsobject) return my_dsobject
How do I provide a query to the datastore.find() method so that I can find datastore objects with a particular quality?
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.
ds_objects, num_objects = datastore.find({'title':'file1.txt'}) print '------------------------- QUERY RESULTS ---------------------------' print "Number of Objects: " + str(num_objects) for i in xrange (0, num_objects, 1): print "File Path: " + ds_objects[i].get_file_path() print "Title: " + ds_objects[i].metadata['title']
How do I delete datastore entries with a particular property?
The following code shows a function that contains a property name and value for the metadata of a datastore object. The function searches out and deletes any datastore entries where the metadata property matches the value passed.
def _delete_dsobjects(self, prop, val): ds_objects, num_objects = datastore.find({prop: val}) for i in xrange (0, num_objects, 1): print 'DELETING ' + ds_objects[i].metadata['title'] + '...' ds_objects[i].destroy() datastore.delete(ds_objects[i].object_id)
Class: DSObject
How do I create new metadata entries or reassign metadata for a datastore object that has been created?
Every DSObject created by datastore.create() has a metadata property. This property refers to a DSMetadata object, which contains the metadata for your datastore object. The code below shows how to assign metadata values by referring to the metadata property of a DSObject.
#my_dsobject is of type datastore.DSObject my_dsobject = datastore.create() #Map the 'filename' property to a specific filename my_dsobject.metadata['filename'] = 'krugman-ebooks.txt' datastore.write(my_dsobject)
How do I save a simple text file to the datastore?
The function below takes a filename and file text and saves a new datastore object. When you look in to the datastore using the Journal activity, the object will appear with the title saved in the filename variable. Notice that when we actually write the file, it is written to the 'instance' directory, which means the file is only temporarily stored in that location. This is fine because the datastore should copy this file.
#### Method: _write_textfile, which creates a simple text file # with filetext as the data put in the file. # @Returns: a DSObject representing the file in the datastore. def _write_textfile(self, filename, filetext=''): # Create a datastore object file_dsobject = datastore.create() # Write any metadata (here we specifically set the title of the file and # specify that this is a plain text file). file_dsobject.metadata['title'] = filename file_dsobject.metadata['mime_type'] = 'text/plain' #Write the actual file to the data directory of this activity's root. file_path = os.path.join(self.get_activity_root(), 'instance', filename) f = open(file_path, 'w') try: f.write(filetext) finally: f.close() #Set the file_path in the datastore. file_dsobject.set_file_path(file_path) datastore.write(file_dsobject) return file_dsobject
Class: DSMetadata
Notes
<references />