src/p4l/mapping/parsers.py
changeset 137 bb8bf2688d7e
parent 131 f1854630734f
equal deleted inserted replaced
136:2afc9b5aab17 137:bb8bf2688d7e
    66     def __init__(self, query_cache = None):
    66     def __init__(self, query_cache = None):
    67         self.query_cache = None
    67         self.query_cache = None
    68         if self.query_cache is None:
    68         if self.query_cache is None:
    69             self.query_cache = QueryCache()        
    69             self.query_cache = QueryCache()        
    70     
    70     
       
    71         
    71     def extract_single_value_form_graph(self, graph, q, bindings={}, index=0, convert=lambda v:unicode(v) if v is not None else None, default=None):
    72     def extract_single_value_form_graph(self, graph, q, bindings={}, index=0, convert=lambda v:unicode(v) if v is not None else None, default=None):
       
    73         '''
       
    74         Extract a single value form an rdf graph.
       
    75         
       
    76         :param graph: the rdflib.Graph object to parse
       
    77         :param q: the SPARQL query used to extact the data from the graph
       
    78         :param bindings: Optional binding values for the query
       
    79         :param index: zero based index of the result to extract
       
    80         :param convert: Either a single method of a map of method to apply to the data to extract 
       
    81         :param default: The default value to return if no result is returned by the query
       
    82         
       
    83         @return: None or a single value        
       
    84         '''
    72         return next(self.extract_multiple_values_from_graph(graph, q, bindings, index, convert), default)
    85         return next(self.extract_multiple_values_from_graph(graph, q, bindings, index, convert), default)
    73 
    86 
       
    87 
    74     def extract_multiple_values_from_graph(self, graph, q, bindings={}, index=0, convert=lambda v:unicode(v) if v is not None else None):
    88     def extract_multiple_values_from_graph(self, graph, q, bindings={}, index=0, convert=lambda v:unicode(v) if v is not None else None):
    75 
    89         '''
       
    90         Extract multiple values from a rdf graph.
       
    91         
       
    92         :param graph: the rdflib.Graph object to parse
       
    93         :param q: the SPARQL query used to extact the data from the graph
       
    94         :param bindings: Optional binding values for the query
       
    95         :param index:  zero based index of the result to extract
       
    96         :param convert: The default value to return if no result is returned by the query
       
    97         
       
    98         @return: an iterator on the extracted values
       
    99         '''
    76         index_list = index
   100         index_list = index
    77         if isinstance(index, int):
   101         if isinstance(index, int):
    78             index_list = range(index+1)
   102             index_list = range(index+1)
    79 
   103 
    80         if hasattr(convert, '__call__'):
   104         if hasattr(convert, '__call__'):
   109                 return False        
   133                 return False        
   110         return bool(val)
   134         return bool(val)
   111 
   135 
   112 
   136 
   113     def add_to_related_collection(self, coll, graph, fields, q, bindings={},  convert=lambda v: unicode(v) if v is not None else None, through_fields=None):
   137     def add_to_related_collection(self, coll, graph, fields, q, bindings={},  convert=lambda v: unicode(v) if v is not None else None, through_fields=None):
       
   138         '''
       
   139         This method add new object to a related object collection by extracting data from an rdflib.Graph.
       
   140         
       
   141         
       
   142         :param coll: The collection to add the new objects into. This must be a related collection (cf. django)
       
   143         :param graph: The graph from wich data is extracted
       
   144         :param fields: The list of fields to extract
       
   145         :param q: The SPAQL query. The order and number of the binding parameters of the query must be equals to the one descibed in the ``fields`` parameter.
       
   146         :param bindings: Binding for the SPARQL qery
       
   147         :param convert: map of methods or simple method  to convert data extracted form the graph.
       
   148         :param through_fields: the list (in order) of the througt table used to make a relation between the main object and an "external" object.
       
   149         '''
   114         
   150         
   115         for val in self.extract_multiple_values_from_graph(graph, q, bindings=bindings, index=fields, convert=convert):
   151         for val in self.extract_multiple_values_from_graph(graph, q, bindings=bindings, index=fields, convert=convert):
   116 
   152 
   117             if through_fields:                
   153             if through_fields:                
   118                 new_obj_val = dict([(k,v) for k,v in val.iteritems() if k not in through_fields])
   154                 new_obj_val = dict([(k,v) for k,v in val.iteritems() if k not in through_fields])