Package rdfalchemy :: Module commands
[hide private]
[frames] | no frames]

Source Code for Module rdfalchemy.commands

 1  from paste.script.command import Command, BadCommand 
 2  import sys 
 3  from rdfalchemy import  rdfSubject, RDF, RDFS, Namespace, URIRef 
 4  from rdfalchemy.rdfsSubject import rdfsSubject, rdfsClass 
 5   
 6  OWL = Namespace("http://www.w3.org/2002/07/owl#") 
 7   
 8   
9 -class rdfSubjectCommand(Command):
10 """Create an rdfSubject subclass with descriptors from an RDF Schema 11 12 will set the rdf_type 13 Descriptors will be created 14 15 1. rdfs:domain and rdfs:range are respected 16 2. rdfSingle is used for properties that are 17 * owl:InverseFunctionalProperty 18 * owl:FunctionalProperty 19 3. rdfList or rdfContainer is used if the proper range is set 20 4. rdfMultiple is used for all others 21 22 The resulting .py file is ment to be a skeleton for the developers confvience. 23 Do not expect to be able to use the raw results. 24 """ 25 summary = __doc__.splitlines()[0] 26 usage = '\npaster %s\n%s' % (__name__, __doc__) 27 28 min_args = 0 29 max_args = 1 30 group_name = 'rdfalchemy' 31 32 parser = Command.standard_parser(simulate=True) 33 parser.add_option('-s','--schema',help='file name or url of rdfSchema for this class') 34 parser.add_option('-o','--fout',help='output file name default: stdout (e.g. ../MyRdfModel.py)') 35 parser.add_option('-l','--list', action='store_true', help='list valid instances of `owl:Class` in the schema file') 36
37 - def command(self):
38 """Main command to create controller""" 39 try: 40 if self.options.schema: 41 ext = self.options.schema.split('.')[-1] 42 ext = ext in ['n3','nt'] and ext or 'xml' 43 print "rdfSubject.db.load('%s',format='%s')" % (self.options.schema, ext) 44 rdfSubject.db.load(self.options.schema,format=ext) 45 else: 46 raise NotImplemented('Need to pass in the schema No default yet') 47 48 choices = filter(lambda x: isinstance(x, URIRef), rdfSubject.db.subjects(RDF.type, RDFS.Class)) 49 choices += filter(lambda x: isinstance(x, URIRef), rdfSubject.db.subjects(RDF.type, OWL.Class)) 50 choices = filter(lambda x: not rdfSubject.db.qname(x).startswith('_'), choices) 51 choices.sort() 52 53 print "qnames that you can import from this schema:" 54 for i, n in enumerate(choices): 55 print "\t[%i] %s" % (i+1,rdfSubject.db.qname(n)) 56 57 if self.options.list: 58 return 59 60 name = self.challenge('Enter (a)ll,(q)uit or the number ot build for','q') 61 if name.startswith('q'): 62 return 63 elif name.startswith('a'): 64 raise NotImplemented("(a)ll option not implented yet") 65 else: 66 try: 67 name = choices[int(name)-1] 68 except Exception, e: 69 raise e 70 71 c = rdfsClass("<%s>"%name) 72 73 output = self.options.fout and file(self.options.fout,'w') or sys.stdout 74 print >>output, c._emit_rdfSubject() 75 76 # Setup the controller 77 except BadCommand, e: 78 raise BadCommand('An error occurred. %s' % e) 79 except: 80 msg = str(sys.exc_info()[1]) 81 raise BadCommand('An unknown error occurred. %s' % msg)
82