home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / store / REGEXMatching.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.9 KB  |  175 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """
  5. This wrapper intercepts calls through the store interface which  make use of
  6. The REGEXTerm class to represent matches by REGEX instead of literal comparison
  7. Implemented for stores that don't support this and essentially provides the support
  8. by replacing the REGEXTerms by wildcards (None) and matching against the results
  9. from the store it's wrapping
  10. """
  11. from rdflib.store import Store
  12. from pprint import pprint
  13. from rdflib.Graph import Graph, QuotedGraph, ConjunctiveGraph, BackwardCompatGraph
  14. import re
  15. NATIVE_REGEX = 0
  16. PYTHON_REGEX = 1
  17.  
  18. class REGEXTerm(unicode):
  19.     
  20.     def __init__(self, expr):
  21.         self.compiledExpr = re.compile(expr)
  22.  
  23.     
  24.     def __reduce__(self):
  25.         return (REGEXTerm, (unicode(''),))
  26.  
  27.  
  28.  
  29. def regexCompareQuad(quad, regexQuad):
  30.     for index in range(4):
  31.         if isinstance(regexQuad[index], REGEXTerm) and not regexQuad[index].compiledExpr.match(quad[index]):
  32.             return False
  33.     
  34.     return True
  35.  
  36.  
  37. class REGEXMatching(Store):
  38.     
  39.     def __init__(self, storage):
  40.         self.storage = storage
  41.         self.context_aware = storage.context_aware
  42.         self.formula_aware = storage.formula_aware
  43.         self.transaction_aware = storage.transaction_aware
  44.  
  45.     
  46.     def open(self, configuration, create = True):
  47.         return self.storage.open(configuration, create)
  48.  
  49.     
  50.     def close(self, commit_pending_transaction = False):
  51.         self.storage.close()
  52.  
  53.     
  54.     def destroy(self, configuration):
  55.         self.storage.destroy(configuration)
  56.  
  57.     
  58.     def add(self, .1, context, quoted = False):
  59.         (subject, predicate, object_) = .1
  60.         self.storage.add((subject, predicate, object_), context, quoted)
  61.  
  62.     
  63.     def remove(self, .1, context = None):
  64.         (subject, predicate, object_) = .1
  65.         if (isinstance(subject, REGEXTerm) and isinstance(predicate, REGEXTerm) and isinstance(object_, REGEXTerm) or context is not None) and isinstance(context.identifier, REGEXTerm):
  66.             if not not isinstance(subject, REGEXTerm) or subject:
  67.                 pass
  68.             s = None
  69.             if not not isinstance(predicate, REGEXTerm) or predicate:
  70.                 pass
  71.             p = None
  72.             if not not isinstance(object_, REGEXTerm) or object_:
  73.                 pass
  74.             o = None
  75.             if not context is not None or not isinstance(context.identifier, REGEXTerm) or context:
  76.                 pass
  77.             c = None
  78.             removeQuadList = []
  79.             for s1, p1, o1 in self.storage.triples((s, p, o), c):
  80.                 cg = None
  81.                 for ctx in cg:
  82.                     ctx = ctx.identifier
  83.                     if not context is not None or context.identifier:
  84.                         pass
  85.                     if regexCompareQuad((s1, p1, o1, ctx), (subject, predicate, object_, context)):
  86.                         removeQuadList.append((s1, p1, o1, ctx))
  87.                         continue
  88.                 
  89.             
  90.             for s, p, o, c in removeQuadList:
  91.                 if not c or Graph(self, c):
  92.                     pass
  93.                 self.storage.remove((s, p, o), c)
  94.             
  95.         else:
  96.             self.storage.remove((subject, predicate, object_), context)
  97.  
  98.     
  99.     def triples(self, .1, context = None):
  100.         (subject, predicate, object_) = .1
  101.         if (isinstance(subject, REGEXTerm) and isinstance(predicate, REGEXTerm) and isinstance(object_, REGEXTerm) or context is not None) and isinstance(context.identifier, REGEXTerm):
  102.             if not not isinstance(subject, REGEXTerm) or subject:
  103.                 pass
  104.             s = None
  105.             if not not isinstance(predicate, REGEXTerm) or predicate:
  106.                 pass
  107.             p = None
  108.             if not not isinstance(object_, REGEXTerm) or object_:
  109.                 pass
  110.             o = None
  111.             if not context is not None or not isinstance(context.identifier, REGEXTerm) or context:
  112.                 pass
  113.             c = None
  114.             for s1, p1, o1 in self.storage.triples((s, p, o), c):
  115.                 cg = None
  116.                 matchingCtxs = []
  117.                 for ctx in cg:
  118.                     if c is None:
  119.                         if context is None or context.identifier.compiledExpr.match(ctx.identifier):
  120.                             matchingCtxs.append(ctx)
  121.                         
  122.                     context.identifier.compiledExpr.match(ctx.identifier)
  123.                     matchingCtxs.append(ctx)
  124.                 
  125.                 if matchingCtxs and regexCompareQuad((s1, p1, o1, None), (subject, predicate, object_, None)):
  126.                     yield ((s1, p1, o1), (lambda .0: for c in .0:
  127. c)(matchingCtxs))
  128.                     continue
  129.             
  130.         else:
  131.             for s1, p1, o1 in self.storage.triples((subject, predicate, object_), context):
  132.                 cg = None
  133.                 yield ((s1, p1, o1), cg)
  134.             
  135.  
  136.     
  137.     def __len__(self, context = None):
  138.         return self.storage.__len__(context)
  139.  
  140.     
  141.     def contexts(self, triple = None):
  142.         for ctx in self.storage.contexts(triple):
  143.             yield ctx
  144.         
  145.  
  146.     
  147.     def remove_context(self, identifier):
  148.         self.storage.remove((None, None, None), identifier)
  149.  
  150.     
  151.     def bind(self, prefix, namespace):
  152.         self.storage.bind(prefix, namespace)
  153.  
  154.     
  155.     def prefix(self, namespace):
  156.         return self.storage.prefix(namespace)
  157.  
  158.     
  159.     def namespace(self, prefix):
  160.         return self.storage.namespace(prefix)
  161.  
  162.     
  163.     def namespaces(self):
  164.         return self.storage.namespaces()
  165.  
  166.     
  167.     def commit(self):
  168.         self.storage.commit()
  169.  
  170.     
  171.     def rollback(self):
  172.         self.storage.rollback()
  173.  
  174.  
  175.