Overview

Introduction

Pyjon.Versionning is a little helper to use mercurial to version objects in your application.

Those objects can be stored in a database for example, allowing changes from the filesystem to be synchronised in the database and the other way around (while maintaining an history).

Versionning managers are classes that get a repository folder and initialize it if not existing yet.

The base managers available are :

Basically, the default classes assume you have a payload or payload_xml field in your objects, and this field will get written to file.

If you want to have other field (or version your whole object serializing it as json for example), you have to create your own repository class basen on the base one.

Example using a simple json serializer:

import simplejson
from myapp import get_vehicles, get_people, get_adresses
from pyjon.versionning import Repository

# for an sqlalchemy repository :
# class MyRepository(SARepository):
# for a normal one (or manual fetching) :
class MyRepository(Repository):
     def get_file_content(self, item):
        # we will assume your objects have an "serializable_fields" attribute
        # this is to support multiple object types (each object type will be stored in a separate folder)
        output_dict = dict()
        for field in item.serializable_fields:
            output_dict[field] = getattr(item, field)

        return simplejson.dumps(output_dict)

    def update_content(self, item, value):
        input_dict = simplejson.loads(value)
        for field in item.serializable_fields:
            setattr(item, field, input_dict[field])

        return simplejson.dumps(my_output_dict)

    # this is used to init the repository with all existing data
    # not necessary with SARepository as it will do it himself with the given classes and dbsession
    def get_all_objects(self):
        for item in get_vehicle():
            yield item

        for item in get_people():
            yield item

        for item in get_adresses():
            yield item

To call it, here is how:

# it's thread safe, so you can define it as a global var (imported like "from my_app.versionning import repository")
repository = MyRepository('./repository',
                          default_user="our_great_app <system@ourgreatapp.com>")

# if you have an SARepository, here is how to call it:
from my_app.model import session, vehicles, people, adresses
repository = MyRepository('./repository', session,
                          [vehicles, people, adresses],
                          default_user="our_great_app <system@ourgreatapp.com>")

# now, before using an item in your app (to check changes in the repo), just do that:
repository.check_item(item)

# after saving or creating an item:
repository.store_item(item) # you can define user= to specify the user who did that

# after deleting an item:
repository.delete_item(item) # you can define user= to specify the user who did that