Paster
Finely hand-crafted python code is great but:
- I hate typing it in
- I make a lot of tpyos
Paste Script command rdfSubject allows RDFAlchemy model files to be generated from your schema file.
Commands From Paster
The RDFAlchemy group is currently local (could be global).
- Install PasteScript. If you don't have it:
easy_install PasteScript
- Ensure you have a dev version of RDFAlchemy above > rev 132. If you are updateing from a previous install, be sure to re-run the install as in:
# from the rdfalhemy dir python setup.py develop
- From that dir you should now be able to "see" the paster commands.
#> paster Usage: /usr/local/bin/paster COMMAND usage: paster [paster_options] COMMAND [command_options] options: --version show program's version number and exit --plugin=PLUGINS Add a plugin to the list of commands (plugins are Egg specs; will also require() the Egg) -h, --help Show this help message Commands: create Create the file layout for a Python distribution help Display help make-config Install a package and create a fresh config file/directory points Show information about entry points serve Serve the described application setup-app Setup an application, given a config file rdfalchemy: rdfSubject Create an rdfSubject subclass with descriptors from an RDF Schema
Those last lines mean that the rdfalchemy group is available to you. From that location you can:
- Type paster rdfSubject -s ./rdfalchemy/samples/schema/foaf.rdf...select an option (select 10 for foaf:Person)
- Type paster rdfSubject --help for ... well you know.
- Type paster rdfSubject -s /home/me/project/schemas/MyEsotericSchema.rdf to see the rdfSubject(s) you can create a .py for.
What you Get
The output is almost certainly not suitable for use without modification. Consider it a skel to save you some typing. Some considerations:
- Determining the correct descriptor is not possible from just the schema. rdfMultiple is the default. If you pull up the initial output in an editor and do a search/replace from rdfMultiple -> rdfSingle and then say yes or no, you can quickly get the descriptors you want.
- You can change the descriptor name to anything you want
py_attr_name = rdfMultiple(MYNS["schema-attr-name"])`
- There are probably more Namespace variables than you need. Delete away if you wish.
- You will probably find that the command produces more than you need or want. It's easier to delete lines than to type them :-)
Some sample output for foaf:Person:
from rdfalchemy import rdfSubject, Namespace, URIRef from rdfalchemy.rdfsSubject import rdfsSubject from rdfalchemy.orm import mapper XML = Namespace("http://www.w3.org/XML/1998/namespace") OWL = Namespace("http://www.w3.org/2002/07/owl#") RDFS = Namespace("http://www.w3.org/2000/01/rdf-schema#") DOAP = Namespace("http://usefulinc.com/ns/doap#") VS = Namespace("http://www.w3.org/2003/06/sw-vocab-status/ns#") DC = Namespace("http://purl.org/dc/elements/1.1/") RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") FOAF = Namespace("http://xmlns.com/foaf/0.1/") class Project(rdfsSubject): """Proyecto A project.""" rdf_type = DOAP["Project"] programming_language = rdfMultiple(DOAP["programming-language"]) tester = rdfMultiple(DOAP["tester"]) homepage = rdfMultiple(DOAP["homepage"]) old_homepage = rdfMultiple(DOAP["old-homepage"]) repository = rdfMultiple(DOAP["repository"], range_type = DOAP["Repository"]) documenter = rdfMultiple(DOAP["documenter"]) download_mirror = rdfMultiple(DOAP["download-mirror"]) category = rdfMultiple(DOAP["category"]) download_page = rdfMultiple(DOAP["download-page"]) os = rdfMultiple(DOAP["os"]) bug_database = rdfMultiple(DOAP["bug-database"]) maintainer = rdfMultiple(DOAP["maintainer"]) mailing_list = rdfMultiple(DOAP["mailing-list"]) translator = rdfMultiple(DOAP["translator"]) screenshots = rdfMultiple(DOAP["screenshots"]) release = rdfMultiple(DOAP["release"], range_type = DOAP["Version"]) wiki = rdfMultiple(DOAP["wiki"]) developer = rdfMultiple(DOAP["developer"]) helper = rdfMultiple(DOAP["helper"]) mapper()
Commands from other locations
To make expose the rdfalchemy paster commands from within you project or framework, in the egg dir for you project (pylons, turbogears etc)
- add rdfalchemy
- to Project_Name.egg-info/paster_plugins.txt
Future
Code generation is a difficult and neve-rending hole that I shalll not cast myself to deeply into. Feedback is good but rdfSubject is a command to assist in your project development. It won't do it for you.
