AEX - App Engine eXchange
Home Home Contact us Contact
home News Projects Tests Files Follow manatlan on Twitter Sign in
Last edition : 2009/04/09 15:39

AEX

A simple library to export/import GAE models thru xml serialization.

AEX exposes 2 methods :

  • exporte(*objects) : will generate (yield) a xml flow (using the to_xml()) of all "objects". object can be a Model class, in this case it will query all entities, or object can be some entities.
  • importe(objects,file=None,buffer=None,delete=False) : will import in models defined in objects, the xml flow from a file if file is defined, or from a buffer if buffer is defined. 'delete' will make a delete all before importing.

exporte use

Consider this model :

class MyModel(db.Model):
    name = db.StringProperty()

You can do :

from aex import exporte

buf="\n".join( exporte(MyModel) )

which will create a string buf containing the XML data of MyModel's entities. Note that xml is encoded in UTF-8.

If you want to create a XML file, you can do :

open("MyModel.xml","w+").writelines( exporte(MyModel) )

If you want to export many models in one XML, you can do like that :

buf="\n".join( exporte(MyModel, MyModel2, MyModel3 ) )

Note thant you pass model class in the exporte method. If it's a model class, all entities of this model will be exported. If you want to export only some entities you can give a query instance like that :

buf="\n".join( exporte(MyModel, MyModel2.all().filter("name = ","me"), MyModel3 ) )

In the case, all entities of MyModel and MyModel3, and entities of Model2 which have name like "me", will be exported.

importe use

Import can be done like this, if you import the xml thru a buffer string :

from aex import importe

importe( MyModel, buffer=buf)

or like this, if you import a xml file :

importe( MyModel, file="MyModel.xml")

If the xml contains many models, you should give them to import method, like this :

importe( [MyModel, MyModel2, MyModel3], file="MyModel.xml")

Note that imported data will always be added in models. If you want to "delete before add", just add the switch delete in import method like this :

importe( [MyModel, MyModel2, MyModel3], file="MyModel.xml", delete=True)

Models will be deleted, and filled with data from the xml file.

Important

AEX is young, very young. It works pretty well for me, but could not work for you ... or it could damage your datas.

It may contains BUG. I accept patches ;-)

Links

RSS Python Powered Get Ubuntu
©opyleft 2008-2019 - manatlan