def loadDom(self, requestProcessor, location, defaultDOM): ''' Load the DOM located at location (a filepath). If location does not exist create a new DOM that is a copy of defaultDOM, a file-like of appropriate type (e.g. an XML or RDF NTriples file). ''' self.log = logging.getLogger("domstore." + requestProcessor.appName)
def loadDom(self, requestProcessor): self.log = logging.getLogger("domstore." + requestProcessor.appName) source = self._normalizeSource(requestProcessor, self.STORAGE_PATH) if os.path.exists(source): inputSource = InputSource.DefaultFactory.fromUri( Uri.OsPathToUri(source)) else: inputSource = InputSource.DefaultFactory.fromStream( self._defaultModel) self._location = source self.dom = Domlette.NonvalidatingReader.parse(inputSource)
See RxPathDOM.py for more notes and todos. Copyright (c) 2003 by Adam Souzis <*****@*****.**> All rights reserved, see COPYING for details. http://rx4rdf.sf.net ''' from __future__ import generators from rx import utils from rx.RxPathUtils import * import os.path, sys, traceback from rx import logging #for python 2.2 compatibility log = logging.getLogger("RxPath") class ColumnInfo(object): def __init__(self, pos, label, type=object): self.pos = pos self.label = label self.type = type def __repr__(self): return 'ColInfo' + repr((self.pos, self.label, self.type)) class Tupleset(object): ''' Interface for representing a set of tuples
def loadDom(self, requestProcessor): self.log = logging.getLogger("domstore." + requestProcessor.appName) normalizeSource = getattr(self.modelFactory, 'normalizeSource', DomStore._normalizeSource) source = normalizeSource(self, requestProcessor, self.STORAGE_PATH) modelUri = requestProcessor.MODEL_RESOURCE_URI if self.saveHistory: from rx import RxPathGraph initCtxUri = RxPathGraph.getTxnContextUri(modelUri, 0) else: initCtxUri = '' defaultStmts = RxPath.NTriples2Statements(self.defaultTripleStream, initCtxUri) if self.VERSION_STORAGE_PATH: normalizeSource = getattr(self.versionModelFactory, 'normalizeSource', DomStore._normalizeSource) versionStoreSource = normalizeSource(self, requestProcessor, self.VERSION_STORAGE_PATH) delmodel = self.versionModelFactory(source=versionStoreSource, defaultStatements=[]) else: delmodel = None #note: to override loadNtriplesIncrementally, set this attribute #on your custom modelFactory function if self.saveHistory and getattr(self.modelFactory, 'loadNtriplesIncrementally', False): if not delmodel: delmodel = RxPath.MemModel() dmc = RxPathGraph.DeletionModelCreator(delmodel) model = self.modelFactory(source=source, defaultStatements=defaultStmts, incrementHook=dmc) lastScope = dmc.lastScope else: model = self.modelFactory(source=source, defaultStatements=defaultStmts) lastScope = None if self.APPLICATION_MODEL: appTriples = StringIO.StringIO(self.APPLICATION_MODEL) stmtGen = RxPath.NTriples2Statements(appTriples, RxPathGraph.APPCTX) appmodel = RxPath.MemModel(stmtGen) model = RxPath.MultiModel(model, appmodel) if self.transactionLog: model = RxPath.MirrorModel( model, RxPath.IncrementalNTriplesFileModel(self.transactionLog, [])) if self.saveHistory: graphManager = RxPathGraph.NamedGraphManager( model, delmodel, lastScope) else: graphManager = None #reverse namespace map #todo: bug! revNsMap doesn't work with 2 prefixes one ns revNsMap = dict([(x[1], x[0]) for x in requestProcessor.nsMap.items()]) self.dom = RxPath.createDOM(model, revNsMap, modelUri=modelUri, schemaClass=self.schemaFactory, graphManager=graphManager) self.dom.addTrigger = self.addTrigger self.dom.removeTrigger = self.removeTrigger self.dom.newResourceTrigger = self.newResourceTrigger #associate the queryCache with the DOM Document self.dom.queryCache = requestProcessor.queryCache
class DomStore(transactions.TransactionParticipant): ''' Abstract interface for DomStores ''' log = logging.getLogger("domstore") #impl. must expose the DOM as a read-only attribute named "dom" dom = None addTrigger = None removeTrigger = None newResourceTrigger = None def __init__(**kw): pass def loadDom(self, requestProcessor, location, defaultDOM): ''' Load the DOM located at location (a filepath). If location does not exist create a new DOM that is a copy of defaultDOM, a file-like of appropriate type (e.g. an XML or RDF NTriples file). ''' self.log = logging.getLogger("domstore." + requestProcessor.appName) def evalXPath(self, xpath, context, expCache=None, queryCache=None): pass def applyXslt(self, xslStylesheet, topLevelParams=None, extFunctionMap=None, baseUri='file:', styleSheetCache=None): pass def commitTransaction(self, txnService): pass def abortTransaction(self, txnService): pass def getStateKey(self): ''' Returns the a hashable object that uniquely identifies the current state of DOM. Used for caching. If this is not implemented, it should raise KeyError (the default implementation). ''' raise KeyError def getTransactionContext(self): return None def _normalizeSource(self, requestProcessor, path): #if source was set on command line, override config source if requestProcessor.source: source = requestProcessor.source else: source = path if not source: self.log.warning('no model path given and STORAGE_PATH' ' is not set -- model is read-only.') elif not os.path.isabs(source): #todo its possible for source to not be file path # -- this will break that source = os.path.join(requestProcessor.baseDir, source) return source
Raccoon's caches have a mechanism for handling side effects that may occur when generating a value that will be cached. For example, the XPath processing cache and the XSLT/RxSLT processing caches keep track of calls to ^^wf:assign-metadata^^ and ^^wf:remove-metadata^^ so that the changes they make to the request metadata can be repeated when subsequent requests result in the value being retrieved from the cache. """ from rx.ExtFunctions import * from rx import MRUCache from Ft.Xml.Xslt import XSL_NAMESPACE from xml.dom import Node as _Node from rx import logging #for python 2.2 compatibility log = logging.getLogger("raccoon") class VarRefKey(tuple): ''' Sub-class of tuple used for marking that a key to a variable reference ''' existenceTest = False def __new__(cls, seq, existenceTest=False): it = tuple.__new__(cls, seq) if existenceTest: it.existenceTest = existenceTest return it
def configHook(self, kw): self.log = logging.getLogger("rhizome." + self.server.appName) self.spamLog = logging.getLogger("rhizome.spam." + self.server.appName) def initConstants(varlist, default): return raccoon.assignVars(self, kw, varlist, default) initConstants(['MAX_MODEL_LITERAL'], -1) self.SAVE_DIR = kw.get('SAVE_DIR', 'content/.rzvs') if not os.path.isabs(self.SAVE_DIR): self.SAVE_DIR = os.path.join(self.server.baseDir, self.SAVE_DIR) altsaveSetting = kw.get('ALTSAVE_DIR', 'content') if altsaveSetting: self.ALTSAVE_DIR = os.path.join(self.server.baseDir, altsaveSetting) else: self.ALTSAVE_DIR = '' self.THEME_DIR = kw.get('THEME_DIR', 'themes/default') if not kw.has_key('PATH'): #if PATH hasn't been set in the config, set the path to be: #ALT_SAVE_DIR: THEME_DIR : RhizomeDir #where RhizomeDir is directory that rhizome-config.py lives in #(i.e. probably 'rhizome') #we check that SAVE_DIR is a subdirectory of one of these directories #if not, you'll need to set the PATH manually rhizomeDir = os.path.split( kw['_rhizomeConfigPath'])[0] #set in rhizome-config.py if self.THEME_DIR: if not os.path.isabs(self.THEME_DIR): themeDir = os.path.join(rhizomeDir, self.THEME_DIR) else: themeDir = self.THEME_DIR self.server.PATH = themeDir + os.pathsep + rhizomeDir else: self.server.PATH = rhizomeDir if self.ALTSAVE_DIR: self.server.PATH = self.ALTSAVE_DIR + os.pathsep + self.server.PATH self.log.debug('path is %s' % self.server.PATH) if self.MAX_MODEL_LITERAL > -1: if self.ALTSAVE_DIR: assert [prefix for prefix in self.server.PATH if self.ALTSAVE_DIR.startswith(os.path.abspath(prefix)) ],\ 'ALT_SAVE_DIR must be on the PATH' #SAVE_DIR should be a sub-dir of one of the PATH #directories so that 'path:' URLs to files there include #the subfolder to make them distinctive. #(Because you don't want to be able override them) saveDirPrefix = [ prefix for prefix in self.server.PATH if self.SAVE_DIR.startswith(os.path.abspath(prefix)) ] assert saveDirPrefix and self.SAVE_DIR[len(saveDirPrefix[0]):],\ 'SAVE_DIR must be a distinct sub-directory of a directory on the PATH' self.interWikiMapURL = kw.get('interWikiMapURL', 'site:///intermap.txt') self.namespaceMapURL = kw.get('namespaceMapURL', 'site:///namespaces.txt') initConstants(['useIndex'], 1) initConstants(['RHIZOME_APP_ID'], '') initConstants(['akismetKey', 'akismetUrl'], '') self.passwordHashProperty = kw.get( 'passwordHashProperty', self.BASE_MODEL_URI + 'password-hash') self.secureHashSeed = kw.get('SECURE_HASH_SEED', self.defaultSecureHashSeed) if self.secureHashSeed == self.defaultSecureHashSeed: self.log.warning("SECURE_HASH_SEED using default seed" " -- set your own private value.") self.secureHashMap = kw.get( 'secureHashMap', {self.passwordHashProperty: self.secureHashSeed}) #make this available as an XPath variable self.resourceAuthorizationAction.assign( "__passwordHashProperty", "'" + self.passwordHashProperty + "'") if not kw.get('ADMIN_PASSWORD_HASH') and not kw.get('ADMIN_PASSWORD'): self.log.warning("neither ADMIN_PASSWORD nor ADMIN_PASSWORD_HASH " "was set; using default admin password.") elif kw.get('ADMIN_PASSWORD') == self.defaultPassword: self.log.warning( "ADMIN_PASSWORD set is to the default admin password.") self.authorizedExtFunctions = kw.get('authorizedExtFunctions', {}) #this is just like findResourceAction except we don't assign the 'not found' resource #used by hasPage self.checkForResourceAction = copy.deepcopy(self.findResourceAction) self.checkForResourceAction.queries.pop() self.indexDir = kw.get( 'INDEX_DIR', os.path.join(self.server.baseDir, 'contentindex')) self.indexableFormats = kw.get('indexableFormats', self.defaultIndexableFormats) xmlContentProcessor = self.server.contentProcessors[ 'http://rx4rdf.sf.net/ns/wiki#item-format-xml'] xmlContentProcessor.blacklistedElements = kw.get( 'blacklistedElements', utils.BlackListHTMLSanitizer.blacklistedElements) for name in ['blacklistedContent', 'blacklistedAttributes']: setting = kw.get(name) if setting is not None: value = dict([(re.compile(x), re.compile(y)) for x, y in setting.items()]) setattr(xmlContentProcessor, name, value) #apply settings to the zmlContentProcessor zmlContentProcessor = self.server.contentProcessors[ 'http://rx4rdf.sf.net/ns/wiki#item-format-zml'] raccoon.assignVars(zmlContentProcessor, kw, [ 'undefinedPageIndicator', 'externalLinkIndicator', 'interWikiLinkIndicator' ], 1) raccoon.assignVars(zmlContentProcessor, kw, ['ZMLDefaultVersion'], zml.defaultZMLVersion) self.shredders = dict([(x.uri, x) for x in kw.get('shredders', [])]) self.uninitialized = False
http://rx4rdf.sf.net """ from rx import zml, rxml, raccoon, utils, RxPath from rx.transactions import TxnFileFactory import Ft from Ft.Lib import Uri from RxPath import RDF_MS_BASE, OBJECT_TYPE_RESOURCE, RDF_SCHEMA_BASE from Ft.Xml import InputSource import os, os.path, sys, types, base64, traceback, re, copy try: import cPickle pickle = cPickle except ImportError: import pickle from rx import logging #for python 2.2 compatibility log = logging.getLogger("rhizome") def kw2vars(**kw): return dict([((None, x[0]), x[1]) for x in kw.items()]) class RhizomeBase(object): exts = { 'http://rx4rdf.sf.net/ns/wiki#item-format-rxupdate': 'xml', 'http://rx4rdf.sf.net/ns/wiki#item-format-python': 'py', 'http://www.w3.org/1999/XSL/Transform': 'xsl', 'http://rx4rdf.sf.net/ns/wiki#item-format-rxslt': 'rxsl', 'http://rx4rdf.sf.net/ns/wiki#item-format-zml': 'zml', 'http://rx4rdf.sf.net/ns/wiki#item-format-text': 'txt', 'http://rx4rdf.sf.net/ns/wiki#item-format-xml': 'xml',
def __init__(self, loggerName='transactions'): self.state = self.stateFactory() self.log = logging.getLogger(loggerName)