为restful客户机和服务器应用程序创建面向资源的体系结构。
docar的Python项目详细描述
python-docar为定义传递的消息提供声明性语法 在客户端和服务器应用程序之间。通过移动web应用程序的焦点 对于交换的文档,它提供了实现文档的可能性 面向架构。这样,您可以将文档(消息)映射到 由uri标识的资源。这对于实现 一个restful api。在django等框架的上下文中,docar充当 支持库。
在服务器端,这些文档可以映射到底层模型存储。 此时python-docar支持django orm后端和http后端。 其他模型映射器,如sqlalchemy或mongodb后端映射器是可能的。 目前MongoDB后端正在开发中。
相同的文档定义可以在客户端重用。未来 它将附带一个http客户机,它可以编写消息并正确地发送它们 远离服务器应用程序。这些文件可以在 客户端和服务器端。
目前唯一支持的消息格式是非常简单的json格式。 计划提供更多的方言(如XML)或 重写内置序列化程序。
有关完整文档,请build it yourself或访问联机 阅读文档:http://readthedocs.org/docs/python-docar/
报告github的issue tracker上的任何问题。如果有任何问题,请寄给我 一封在crito[at]30loops.net的邮件。
python-docar受roar影响。
示例
下面的代码是一个如何使用python-docar的简化示例。我们 开始使用django后端:
# First declare your document from webapp.models import EntryModel, AuthorModel from docar import Document, Collection, fields class Author(Document): name = fields.StringField() class Meta: backend_type = 'django' model = AuthorModel identifier = 'name' class BlogEntry(Document): id = fields.NumberField() title = fields.StringField() body = fields.StringField() published = fields.BooleanField(default=False) author = fields.ForeignDocument(Author) class Meta: backend_type = 'django' model = EntryModel #identifier = 'id' # The identifier defaults to 'id' # You can also declare collections of documents class Blog(Collection): document = BlogEntry
您可以在应用程序视图中使用这些文档:
from webapp.documents import Author, BlogEntry, Blog entry = BlogEntry({'id': 1}) # Bind the document to a model entry.fetch()
我还可以更改文档的属性并将其保存回模型:
entry.title = "20000 Leagues under the sea" entry.save() # --> saves to the django model backend
render方法将文档呈现为规范化字典。 序列化程序获取规范化输出,并将其转换为消息 所需的格式。
# render the document as a json string entry.render() { "id": 1, "title": "20000 Leagues under the sea", "body": "The whole story about captain Nemo ...", "published": True, "author": { "name": "Jules Vernes", } }
安装
从github克隆最新的源代码。发展很快,所以 最好总是使用最新的提交:
$ git clone git://github.com/30loops/python-docar.git $ cd python-docar $ sudo python setup.py install
开发
这个模块附带了一个完整的测试套件。按以下方式运行:
$ python setup.py nosetests
文档
要生成需要安装Sphinx的文档:
$ cd docs $ make html
您可以在docs/_build/html/中找到文档。
免责声明
使用本软件不作任何保证。它是根据BSD发布的 执照。有关详细信息,请参见LICENSE。