src/catedit/utils.py
changeset 95 0a131f9b92a5
parent 86 1c84b37adaf4
child 134 fc3b0b59cf0f
equal deleted inserted replaced
94:71a4a7300c35 95:0a131f9b92a5
     1 """
     1 """
     2     utils.py:
     2     utils.py:
     3     Module that lists utility functions used through the app
     3     Module that lists utility functions used through the app
     4 """
     4 """
     5 from catedit import app
     5 from base64 import b64decode
       
     6 from catedit import app, cache, github, log_api_rate
       
     7 import json
     6 import logging
     8 import logging
     7 
     9 
     8 from flask import session
    10 from flask import session
     9 from rdflib import RDF, RDFS, URIRef
    11 from rdflib import RDF, RDFS, URIRef
    10 from rdflib.compare import to_isomorphic, graph_diff
    12 from rdflib.compare import to_isomorphic, graph_diff
       
    13 
       
    14 from flask.ext.github import GitHubError
    11 
    15 
    12 
    16 
    13 logger = logging.getLogger(__name__)
    17 logger = logging.getLogger(__name__)
    14 
    18 
    15 def compare_categories(first_category, second_category, repository, with_details=True):
    19 def compare_categories(first_category, second_category, repository, with_details=True):
    54             second_category.cat_graph
    58             second_category.cat_graph
    55         )
    59         )
    56         in_first = []
    60         in_first = []
    57         in_both = []
    61         in_both = []
    58         in_second = []
    62         in_second = []
    59         logger.debug(rdf_in_both)
    63         #TODO: vérifier encoding - logger.debug(rdf_in_both)
    60         logger.debug(rdf_only_in_first)
    64         #TODO: vérifier encoding - logger.debug(rdf_only_in_first)
    61         logger.debug(rdf_only_in_second)
    65         #TODO: vérifier encoding - logger.debug(rdf_only_in_second)
    62         for (final_list, diff_list) in [
    66         for (final_list, diff_list) in [
    63                 (in_both, rdf_in_both),
    67                 (in_both, rdf_in_both),
    64                 (in_first, rdf_only_in_first),
    68                 (in_first, rdf_only_in_first),
    65                 (in_second, rdf_only_in_second)
    69                 (in_second, rdf_only_in_second)
    66         ]:
    70         ]:
    68                 if triple[1] == RDFS.label:
    72                 if triple[1] == RDFS.label:
    69                     final_list.append(("label", triple[2].toPython()))
    73                     final_list.append(("label", triple[2].toPython()))
    70                 elif triple[1] == RDF.Description:
    74                 elif triple[1] == RDF.Description:
    71                     final_list.append(("description", triple[2].toPython()))
    75                     final_list.append(("description", triple[2].toPython()))
    72                 else:
    76                 else:
    73                     for predicate in session["properties"][repository].keys():
    77                     for predicate in get_property_list()[repository].keys():
    74                         if triple[1] == \
    78                         if triple[1] == \
    75                         URIRef(session["properties"][repository][predicate]["rdflib_class"]):
    79                         URIRef(get_property_list()[repository][predicate]["rdflib_class"]):
    76                             if (session["properties"]
    80                             if (get_property_list()[repository]
    77                                        [repository]
    81                                                    [predicate]
    78                                        [predicate]
    82                                                    ["object_type"] == "uriref-link"
    79                                        ["object_type"] == "uriref-link"
    83                             or get_property_list()[repository]
    80                             or session["properties"]
    84                                                   [predicate]
    81                                       [repository]
    85                                                   ["object_type"] == "literal"):
    82                                       [predicate]
       
    83                                       ["object_type"] == "literal"):
       
    84                                 final_list.append(
    86                                 final_list.append(
    85                                     (
    87                                     (
    86                                         predicate,
    88                                         predicate,
    87                                         triple[2].toPython()
    89                                         triple[2].toPython()
    88                                     )
    90                                     )
   139     return {
   141     return {
   140         "additions": created_categories,
   142         "additions": created_categories,
   141         "modifications": modified_categories,
   143         "modifications": modified_categories,
   142         "deletions": deleted_categories
   144         "deletions": deleted_categories
   143     }
   145     }
       
   146 
       
   147 def get_property_list():
       
   148     """
       
   149         Function to get the property list, also registered in templates
       
   150     """
       
   151     cache_key = "property_lists_dict"
       
   152     if cache.get(cache_key) is None:
       
   153         property_list = {
       
   154             repo: {} for repo
       
   155             in app.config["PERSISTENCE_CONFIG"]["REPOSITORY_LIST"]
       
   156         }
       
   157         if app.config["PERSISTENCE_CONFIG"]["METHOD"] == "PersistenceToGithub":
       
   158             for repo in app.config["PERSISTENCE_CONFIG"]["REPOSITORY_LIST"]:
       
   159                 try:
       
   160                     json_file=github.get(
       
   161                         "repos/"
       
   162                         + app.config["PERSISTENCE_CONFIG"]["REPOSITORY_OWNER"] + "/"
       
   163                         + repo + "/contents/properties/properties.json",
       
   164                         hooks=dict(response=log_api_rate)
       
   165                     )
       
   166                     #TODO: vérifier encoding - logger.debug("repo: "+repo+" - properties: "+str(json.loads(str(b64decode(json_file["content"]), "utf-8")))) #wat
       
   167                     property_list[repo] = json.loads(str(b64decode(json_file["content"]), "utf-8"))["property_list"]
       
   168                 except GitHubError as ghe:
       
   169                     logger.debug(
       
   170                         "GitHub Error trying to get the property list. We'll assume " +
       
   171                         "there is none and use default list as defined in config.py"
       
   172                     )
       
   173                     property_list[repo] = app.config["PROPERTY_LIST"]
       
   174         else:
       
   175             for repo in app.config["PERSISTENCE_CONFIG"]["REPOSITORY_LIST"]:
       
   176                 property_list[repo] = app.config["PROPERTY_LIST"]
       
   177         rv = property_list
       
   178         cache.set(cache_key, rv, timeout=3600)
       
   179         return rv
       
   180     else: 
       
   181         return cache.get(cache_key)
       
   182 app.jinja_env.globals['get_property_list'] = get_property_list  
       
   183 
       
   184