datastore.filesystem

filesystem datastore implementation.

Tested with:
  • Journaled HFS+ (Mac OS X 10.7.2)
class datastore.filesystem.FileSystemDatastore(root, case_sensitive=True)

Bases: datastore.core.basic.Datastore

Simple flat-file datastore.

FileSystemDatastore will store objects in independent files in the host’s filesystem. The FileSystemDatastore is initialized with a root path, under which to store all objects. Each object will be stored under its own file: root/key.obj

The key portion also replaces namespace parameter delimiters (:) with slashes, creating several nested directories. For example, storing objects under root path ‘/data’ with the following keys:

Key('/Comedy:MontyPython/Actor:JohnCleese')
Key('/Comedy:MontyPython/Sketch:ArgumentClinic')
Key('/Comedy:MontyPython/Sketch:CheeseShop')
Key('/Comedy:MontyPython/Sketch:CheeseShop/Character:Mousebender')

will yield the file structure:

/data/Comedy/MontyPython/Actor/JohnCleese.obj
/data/Comedy/MontyPython/Sketch/ArgumentClinic.obj
/data/Comedy/MontyPython/Sketch/CheeseShop.obj
/data/Comedy/MontyPython/Sketch/CheeseShop/Character/Mousebender.obj

Implementation Notes:

Separating key namespaces (and their parameters) within directories allows granular querying for under a specific key. For example, a query with key:

Key('/data/Comedy:MontyPython/Sketch:CheeseShop')

will query for all objects under Sketch:CheeseShop independently of queries for:

Key('/data/Comedy:MontyPython/Sketch')

Also, using the .obj extension gets around the ambiguity of having both a CheeseShop object and directory:

/data/Comedy/MontyPython/Sketch/CheeseShop.obj
/data/Comedy/MontyPython/Sketch/CheeseShop/

Hello World:

>>> import datastore.filesystem
>>>
>>> ds = datastore.filesystem.FileSystemDatastore('/tmp/.test_datastore')
>>>
>>> hello = datastore.Key('hello')
>>> ds.put(hello, 'world')
>>> ds.contains(hello)
True
>>> ds.get(hello)
'world'
>>> ds.delete(hello)
>>> ds.get(hello)
None
contains(key)

Returns whether the object named by key exists. Optimized to only check whether the file object exists.

Args:
key: Key naming the object to check.
Returns:
boalean whether the object exists
delete(key)

Removes the object named by key.

Args:
key: Key naming the object to remove.
get(key)

Return the object named by key or None if it does not exist.

Args:
key: Key naming the object to retrieve
Returns:
object or None
ignore_list = []
object_extension = '.obj'
object_path(key)

return the object path for key.

path(key)

Returns the path for given key

put(key, value)

Stores the object value named by key.

Args:
key: Key naming value value: the object to store.
query(query)

Returns an iterable of objects matching criteria expressed in query FSDatastore.query queries all the .obj files within the directory specified by the query.key.

Args:
query: Query object describing the objects to return.
Raturns:
Cursor with all objects matching criteria
relative_object_path(key)

Returns the relative path for object pointed by key.

relative_path(key)

Returns the relative path for given key

datastore.filesystem.ensure_directory_exists(directory)

Ensures directory exists. May make directory and intermediate dirs. Raises RuntimeError if directory is a file.

Previous topic

datastore.core.util

This Page