Package oort :: Package util :: Module graphs
[hide private]
[frames] | no frames]

Source Code for Module oort.util.graphs

 1  #======================================================================= 
 2  import os 
 3  from os.path import splitext, expanduser 
 4  import logging 
 5  from rdflib import Namespace, Literal 
 6  #======================================================================= 
 7  # TODO: optional hook for inference -- with e.g. FuXi? 
 8   
 9  _logger = logging.getLogger(name=__name__) 
10   
11   
12  OUG_NS = Namespace('tag:oort.to,2006:system/util/graphs#') 
13  LAST_MOD = OUG_NS.lastmodified 
14   
15   
16 -def load_if_modified(graph, fpath, format):
17 dataUri = graph.absolutize(fpath) 18 modTime = os.stat(fpath).st_mtime 19 try: 20 loadedModValue = graph.value(dataUri, LAST_MOD) 21 loadedModTime = float(loadedModValue) 22 except TypeError: 23 loadedModTime = -1 24 if modTime > loadedModTime: 25 graph.remove((dataUri, LAST_MOD, loadedModValue)) 26 graph.remove_context(graph.context_id(dataUri)) 27 _logger.info("Loading: <%s>" % fpath) 28 graph.load(fpath, publicID=dataUri, format=format) 29 graph.add((dataUri, LAST_MOD, Literal(modTime)))
30 31 32 DEFAULT_FORMAT_MAP = { 33 'rdf': 'xml', 34 'n3': 'n3', 35 'ttl': 'n3', 36 'xhtml': 'rdfa', 37 'html': 'rdfa', 38 } 39
40 -def get_format(fpath, fmap=None):
41 fmap = fmap or DEFAULT_FORMAT_MAP 42 ext = splitext(fpath)[-1][1:] 43 return fmap.get(ext)
44 45
46 -def loader(graph, base=None, formatMap=None):
47 if base: base = expanduser(base) + '%s' 48 def load_data(fpath): 49 if base: 50 fpath = base % fpath 51 load_if_modified(graph, fpath, format=get_format(fpath, formatMap))
52 return load_data 53 54
55 -def load_dir(graph, basedir, formatMap=None, errorHandler=None):
56 basedir = expanduser(basedir) 57 for base, fdirs, fnames in os.walk(basedir): 58 for fname in fnames: 59 fpath = os.path.join(base, fname) 60 format = get_format(fpath, formatMap) 61 if not format: 62 continue 63 try: 64 load_if_modified(graph, fpath, format) 65 except Exception, e: 66 if errorHandler: 67 errorHandler(e, fpath) 68 else: 69 raise e
70