# HG changeset patch # User Nicolas DURAND # Date 1417624819 -3600 # Node ID d02faa4a2eb2067cdada2a51f0e3e77d08a36db8 # Parent b3c03d54e144136e159668d99af19757dbd59a21 pep8-fication + cleaning comments so the only comments lefts are to be replaced by logs into a file diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/api.py --- a/src/catedit/api.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/api.py Wed Dec 03 17:40:19 2014 +0100 @@ -10,33 +10,28 @@ api = Api(app) -cat_parser=reqparse.RequestParser() +cat_parser = reqparse.RequestParser() cat_parser.add_argument('label', type=str) cat_parser.add_argument('description', type=str) cat_parser.add_argument('commit_message', type=str) cat_parser.add_argument('property_predicate', type=str, action="append") cat_parser.add_argument('property_object', type=str, action="append") -#cat_parser.add_argument('delete_message', type=str) +# cat_parser.add_argument('delete_message', type=str) class CategoryAPI(Resource): - # returns category cat_id + # returns category cat_id or all if cat_id is None @classmethod @cache.memoize(timeout=3600) def get(self, cat_id=None): - # load file cat_id - # TODO: load list if cat_id=None cat_manager_instance = CategoryManager() if cat_id is not None: c = cat_manager_instance.load_cat(cat_id) - # print "get id: "+c.cat_id return c.cat_graph.serialize(format='turtle') else: - response=[] + response = [] for c in cat_manager_instance.list_cat(): response.append(c.cat_graph.serialize(format='turtle')) - # print response - # print "get id: None" return response # update category cat_id @@ -45,7 +40,7 @@ args = cat_parser.parse_args() cat_manager_instance = CategoryManager() - new_property_list=[] + new_property_list = [] # print args["property_predicate"] # print args["property_object"] for property_predicate, property_object in zip( @@ -58,7 +53,7 @@ [property_predicate] \ ["object_type"] == "uriref-category" : property_object_to_append = app.config["ONTOLOGY_NAMESPACE"] \ - +property_object + + property_object # print property_object_to_append new_property_list.append((property_predicate, property_object_to_append)) # print new_property_list @@ -76,8 +71,8 @@ def post(self): args = cat_parser.parse_args() property_list = [] - #print args["property_predicate"] - #print args["property_object"] + # print args["property_predicate"] + # print args["property_object"] for property_predicate, property_object in zip( request.form.getlist('property_predicate'), request.form.getlist('property_object')): diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/app.py --- a/src/catedit/app.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/app.py Wed Dec 03 17:40:19 2014 +0100 @@ -14,7 +14,7 @@ github = GitHub(app) # set up logging -file_handler=FileHandler(filename=app.config["LOG_FILE_PATH"]) +file_handler = FileHandler(filename=app.config["LOG_FILE_PATH"]) file_handler.setFormatter(Formatter(''' Message type: %(levelname)s Location: %(pathname)s:%(lineno)d diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/models.py --- a/src/catedit/models.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/models.py Wed Dec 03 17:40:19 2014 +0100 @@ -9,12 +9,13 @@ """ Namespace: ld.iri-research.org/ontology/categorisation/# -Category URI: ld.iri-research.org/categorisation/#cat_id +Category URI: ld.iri-research.org/ontology/categorisation/#cat_id Category Class: label is the rdf label of the category description is the description of the category -other_properties is a dictionnary containing every other RDF(S) property +other_properties is a dictionnary containing every other supported property +as defined in the PROPERTY_LIST dict in settings.py """ @@ -22,14 +23,12 @@ def __init__(self, label=None, description=None, other_properties=None, graph=None): if not(graph): - #cat_id = uuid4().hex - cat_id="category_id_"+slugify(label) + # cat_id = uuid4().hex - Alternate method of generating ids + cat_id = "category_id_"+slugify(label) self.cat_graph = Graph() self.this_category = URIRef(app.config["ONTOLOGY_NAMESPACE"]+cat_id) self.cat_graph.add((self.this_category, RDF.ID, Literal(cat_id))) - # parsing label and description - if label: self.cat_graph.add((self.this_category, RDFS.label, @@ -39,7 +38,6 @@ RDF.Description, Literal(description))) - # parsing other properties if other_properties: for (predicate, obj) in other_properties: self.cat_graph.add((self.this_category, @@ -78,7 +76,6 @@ @property def properties(self): - # list of properties from the PROPERTY_LIST defined in settings.py property_list = [] for key in app.config["PROPERTY_LIST"]: for obj in self.cat_graph \ @@ -89,47 +86,53 @@ property_list.append((key, obj.toPython())) return property_list - def edit_category(self, new_label=None, - new_description=None, - new_other_properties=None): - # Did the label change? - if new_label is not None and new_label != self.label: - # Remove the old triple + def edit_category(self, new_label=False, + new_description=False, + new_other_properties=False): + """ + Checks if there is a new label and if so apply changes + """ + if new_label is not False and new_label != self.label: self.cat_graph.remove((self.this_category, RDFS.label, self.cat_graph.label(self.this_category))) - # Make a new one self.cat_graph.add((self.this_category, RDFS.label, Literal(new_label))) - # Did the description change? - if new_description is not None and new_description != self.description: - # Remove the old triple + + """ + Checks if there is a new description and if so apply changes + """ + if new_description is not False and new_description != self.description: self.cat_graph.remove((self.this_category, RDF.Description, self.cat_graph.value(self.this_category, RDF.Description))) - # Make a new one self.cat_graph.add((self.this_category, RDF.Description, Literal(new_description))) - # Did the properties change? - if new_other_properties is not None or self.properties is not None: - print "before suppressing properties: " - print self.properties - print "will replace with: " - print new_other_properties + """ + Checks if there is a new property list and if so apply changes + (can be [] so it deletes everything) + """ + if new_other_properties is not False or \ + (new_other_properties is None and \ + self.properties is not None): + # print "before suppressing properties: " + # print self.properties + # print "will replace with: " + # print new_other_properties for key in app.config["PROPERTY_LIST"]: self.cat_graph.remove((self.this_category, app.config["PROPERTY_LIST"] [key] ["rdflib_class"], None)) - print "now properties are: " - print self.properties - print "making new properties: " - print new_other_properties + # print "now properties are: " + # print self.properties + # print "making new properties: " + # print new_other_properties for (predicate, obj) in new_other_properties: self.cat_graph.add((self.this_category, app.config["PROPERTY_LIST"] @@ -151,7 +154,7 @@ class CategoryManager(object): def load_cat(self, cat_id): - p = getattr(persistence,app.config["PERSISTENCE_METHOD"])() + p = getattr(persistence, app.config["PERSISTENCE_METHOD"])() cat_serial = p.load(name=cat_id) loaded_cat_graph = Graph() loaded_cat_graph.parse(source=StringIO(cat_serial), format='turtle') @@ -159,15 +162,15 @@ return cat def save_cat(self, cat, message=None): - p = getattr(persistence,app.config["PERSISTENCE_METHOD"])() + p = getattr(persistence, app.config["PERSISTENCE_METHOD"])() p.save(content=cat.cat_graph.serialize(format='turtle'), name=cat.cat_id, message=message) def delete_cat(self, deleted_cat_id, message=None): - cat_list=self.list_cat() + cat_list = self.list_cat() for cat in cat_list: if cat.cat_id != app.config["ONTOLOGY_NAMESPACE"]+deleted_cat_id: - new_property_list_for_cat=[] + new_property_list_for_cat = [] for (predicate, obj) in cat.properties: if not ((app.config["PROPERTY_LIST"] [predicate] @@ -176,14 +179,14 @@ new_property_list_for_cat.append((predicate, obj)) cat.edit_category(new_other_properties=new_property_list_for_cat) self.save_cat(cat, message=message+", cleaning up other properties") - p = getattr(persistence,app.config["PERSISTENCE_METHOD"])() + p = getattr(persistence, app.config["PERSISTENCE_METHOD"])() p.delete(name=deleted_cat_id, message=message) def list_cat(self): - p = getattr(persistence,app.config["PERSISTENCE_METHOD"])() + p = getattr(persistence, app.config["PERSISTENCE_METHOD"])() cat_serial_list = p.list() # print cat_serial_list - cat_list=[] + cat_list = [] for cat_serial in cat_serial_list: loaded_cat_graph = Graph() loaded_cat_graph.parse(source=StringIO(cat_serial), format='turtle') diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/persistence.py --- a/src/catedit/persistence.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/persistence.py Wed Dec 03 17:40:19 2014 +0100 @@ -5,6 +5,7 @@ import os import json + class Persistence: __metaclass__ = ABCMeta @@ -50,32 +51,33 @@ return file_content def delete(self, **kwargs): - path_to_delete= app.config["FILE_SAVE_DIRECTORY"]+kwargs["name"] + path_to_delete = app.config["FILE_SAVE_DIRECTORY"]+kwargs["name"] os.remove(path_to_delete) # IDEA: return { file_name: file_content } type dict def list(self, **kwargs): - file_content_list=[] + file_content_list = [] for file_name in os.listdir(app.config["FILE_SAVE_DIRECTORY"]): - file=open(app.config["FILE_SAVE_DIRECTORY"]+file_name) + file = open(app.config["FILE_SAVE_DIRECTORY"]+file_name) file_content = file.read() file.close file_content_list.append(file_content) # print file_content_list return file_content_list + class PersistenceToGithub(Persistence): def save(self, **kwargs): # print kwargs["content"] - request_data = {"content": b64encode(kwargs["content"]), \ + request_data = {"content": b64encode(kwargs["content"]), "message": kwargs["message"]} try: filedict = github.get("repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"] - +kwargs["name"]) + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"] + + kwargs["name"]) request_data["sha"] = filedict["sha"] except GitHubError: pass @@ -83,11 +85,11 @@ try: github.request('PUT', "repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"] - +kwargs["name"], + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"] + + kwargs["name"], data=json.dumps(request_data)) except GitHubError: pass @@ -95,60 +97,60 @@ def load(self, **kwargs): try: filedict = github.get("repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"] - +kwargs["name"]) - file_content=b64decode(filedict["content"]) + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"] + + kwargs["name"]) + file_content = b64decode(filedict["content"]) except GitHubError: pass return file_content def delete(self, **kwargs): - request_data = { "message": kwargs["message"] } + request_data = {"message": kwargs["message"]} try: filedict = github.get("repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"] - +kwargs["name"]) + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"] + + kwargs["name"]) request_data["sha"] = filedict["sha"] except GitHubError: pass try: github.request('DELETE', - "repos/catedit-system/" - +app.config["REPOSITORY_NAME"] - +"/contents/categories/" - +kwargs["name"], - data=json.dumps(request_data)) + "repos/catedit-system/" + + app.config["REPOSITORY_NAME"] + + "/contents/categories/" + + kwargs["name"], + data=json.dumps(request_data)) except GitHubError: pass def list(self, **kwargs): - filenames_list=[] + filenames_list = [] try: files_in_repo = github.get("repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"]) - filenames_list = [ file["name"] for file in files_in_repo ] + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"]) + filenames_list = [file["name"] for file in files_in_repo] # print filenames_list except GitHubError: pass - file_content_list=[] + file_content_list = [] for filename in filenames_list: try: filedict = github.get("repos/" - +app.config["REPOSITORY_OWNER"]+"/" - +app.config["REPOSITORY_NAME"] - +"/contents/" - +app.config["CATEGORIES_PATH"] - +filename) + + app.config["REPOSITORY_OWNER"]+"/" + + app.config["REPOSITORY_NAME"] + + "/contents/" + + app.config["CATEGORIES_PATH"] + + filename) file_content_list.append(b64decode(filedict["content"])) except GitHubError: pass diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/settings.py --- a/src/catedit/settings.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/settings.py Wed Dec 03 17:40:19 2014 +0100 @@ -1,6 +1,5 @@ from rdflib import * -# Category persistence parameters class appSettings(object): @@ -8,7 +7,7 @@ HOST = "0.0.0.0" DEBUG = True - LOGGING = True + LOGGING = False # WTForms settings @@ -38,39 +37,39 @@ GITHUB_CLIENT_ID = "3e31f3b000a4914f75ef" GITHUB_CLIENT_SECRET = "bc17eb0ec11385628c2e75aacb5ff8ef5f29e490" - # Property List + # Property List - DO NOT OVERWRITE IN config.py EVER PROPERTY_LIST = { "subClassOf": { - "descriptive_label_fr" : "Sous-classe de", - "descriptive_label_en" : "Subclass of", - "object_type" : "uriref-category", - "rdflib_class" : RDFS.subClassOf, - "object_rdflib_class" : URIRef, + "descriptive_label_fr": "Sous-classe de", + "descriptive_label_en": "Subclass of", + "object_type": "uriref-category", + "rdflib_class": RDFS.subClassOf, + "object_rdflib_class": URIRef, }, "value": { - "descriptive_label_fr" : "Valeur", - "descriptive_label_en" : "Value", - "object_type" : "literal", - "rdflib_class" : RDF.value, - "object_rdflib_class" : Literal, + "descriptive_label_fr": "Valeur", + "descriptive_label_en": "Value", + "object_type": "literal", + "rdflib_class": RDF.value, + "object_rdflib_class": Literal, }, "type": { - "descriptive_label_fr" : "Type", - "descriptive_label_en" : "Type", - "object_type" : "uriref-category", - "rdflib_class" : RDF.type, - "object_rdflib_class" : URIRef, + "descriptive_label_fr": "Type", + "descriptive_label_en": "Type", + "object_type": "uriref-category", + "rdflib_class": RDF.type, + "object_rdflib_class": URIRef, }, "resource": { - "descriptive_label_fr" : "Ressource", - "descriptive_label_en" : "Resource", - "object_type" : "uriref-link", - "rdflib_class" : RDFS.Resource, - "object_rdflib_class" : URIRef, + "descriptive_label_fr": "Ressource", + "descriptive_label_en": "Resource", + "object_type": "uriref-link", + "rdflib_class": RDFS.Resource, + "object_rdflib_class": URIRef, }, } - # PersistenceMethod + # Category persistence parameters - PERSISTENCE_METHOD="PersistenceToGithub" + PERSISTENCE_METHOD = "PersistenceToGithub" diff -r b3c03d54e144 -r d02faa4a2eb2 src/catedit/views.py --- a/src/catedit/views.py Fri Nov 28 17:32:22 2014 +0100 +++ b/src/catedit/views.py Wed Dec 03 17:40:19 2014 +0100 @@ -18,7 +18,7 @@ @app.route('/', methods=['GET']) @app.route('/catrecap', methods=['GET']) -@app.route('/catrecap/delete/', methods=['GET','POST']) +@app.route('/catrecap/delete/', methods=['POST']) def cat_recap(delete_cat_id=None): cat_api_instance = CategoryAPI() # list categories @@ -29,7 +29,8 @@ # print cat_list for serialized_cat in serialized_cat_list: cat_rdf_graph = Graph() - cat_rdf_graph.parse(source=StringIO(serialized_cat), format='turtle') + cat_rdf_graph.parse(source=StringIO(serialized_cat), + format='turtle') c = Category(graph=cat_rdf_graph) cat_list.append({"cat_label": c.label, @@ -53,7 +54,8 @@ cat_list = [] for serialized_cat in serialized_cat_list: cat_rdf_graph = Graph() - cat_rdf_graph.parse(source=StringIO(serialized_cat), format='turtle') + cat_rdf_graph.parse(source=StringIO(serialized_cat), + format='turtle') c = Category(graph=cat_rdf_graph) cat_list.append({"cat_label": c.label, @@ -65,7 +67,8 @@ catSerial = cat_api_instance.get(cat_id) cat_rdf_graph = Graph() - cat_rdf_graph.parse(source=StringIO(catSerial), format='turtle') + cat_rdf_graph.parse(source=StringIO(catSerial), + format='turtle') c = Category(graph=cat_rdf_graph) setattr(NewCategoryMinimalForm, @@ -94,7 +97,7 @@ if cat_form.validate_on_submit(): cat_api_instance.put(cat_id) return redirect(url_for('cat_recap')) - else : + else: return render_template('cateditor.html', cat_id=cat_id, cat_properties=c.properties, @@ -123,15 +126,17 @@ if cat_form.validate_on_submit(): cat_api_instance.post() return redirect(url_for('cat_recap')) - else : + else: return render_template('cateditor.html', form=cat_form, cat_list=cat_list) + @app.route('/catedit-github-login') def github_login(): return github.authorize(scope="repo") + @app.route('/catedit-github-callback') @github.authorized_handler def github_callback(oauth_code): @@ -140,12 +145,13 @@ session["user_logged"] = True session["user_login"] = github.get("user")["login"] try: - repoList=[] - repoList=github.get("user/repos") + repoList = [] + repoList = github.get("user/repos") # for repo in repoList: # print repo["name"] session["user_can_edit"] = True - if not any (repo["name"] == app.config["REPOSITORY_NAME"] for repo in repoList): + if not any (repo["name"] == app.config["REPOSITORY_NAME"] + for repo in repoList): session["user_can_edit"] = False # print session["user_can_edit"] except GitHubError: @@ -155,12 +161,14 @@ # print session["user_login"] return redirect(url_for('cat_recap')) + @github.access_token_getter def token_getter(): - if session.get("user_logged",None): + if session.get("user_logged", None): print "I made an authentified request" return session["user_code"] + @app.route('/catedit-logout') def logout(): session["user_code"] = None