#!/usr/bin/python """List Built-ins for CWM""" import string, re import thing import notation3 # N3 parsers and generators, and RDF generator import urllib # for hasContent import md5, binascii # for building md5 URIs from thing import * # Magic resources we know about LITERAL_URI_prefix = "data:application/n3;" DAML_LISTS = notation3.DAML_LISTS # If not, do the funny compact ones RDF_type_URI = notation3.RDF_type_URI DAML_equivalentTo_URI = notation3.DAML_equivalentTo_URI LEXP_NS_URI = "http://infomesh.net/info/lexp#" ################################################################################ # # L I S T B U I L T - I N s # # This should be in a separate module, imported and called once by the user # to register the code with the store # # Light Built-in classes class BI_cons(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): if 0: print "Called cons with", subj_py list, item = subj_py res = [item] res.extend(list) return store._fromPython(context, res) class BI_append(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): list, item = subj_py list.append(item) return store._fromPython(context, list) class BI_slice(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): list, fro, to = subj_py result = list[int(fro):int(to)] return store._fromPython(context, result) class BI_extend(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): list, newlist = subj_py list.extend(newlist) return store._fromPython(context, list) class BI_pop(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): list = subj_py result = list.pop() return store._fromPython(context, result) class BI_sort(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): result = subj_py[:] result.sort() return store._fromPython(context, result) class BI_get(LightBuiltIn, Function): def evaluateObject(self, store, context, subj, subj_py): list, i = subj_py return store._fromPython(context, list[int(i)]) # Register the string built-ins with the store def isString(x): return type(x) is type('') or type(x) is type(u'') def register(store): str = store.internURI(LEXP_NS_URI[:-1]) str.internFrag("cons", BI_cons) str.internFrag("append", BI_append) str.internFrag("slice", BI_slice) str.internFrag("extend", BI_extend) str.internFrag("pop", BI_pop) str.internFrag("sort", BI_sort) str.internFrag("get", BI_get)