start of small refactorisation: python 3.4, remove imports *, logger, apply pylint, remove app.py
--- a/.hgignore Fri Dec 12 16:45:47 2014 +0100
+++ b/.hgignore Tue Dec 16 11:14:55 2014 +0100
@@ -2,5 +2,5 @@
dev/.vagrant/
files/
*.pyc
-src\catedit\config.py
-src/catedit/log/
\ No newline at end of file
+src/catedit/config.py
+src/catedit/log/
--- a/src/catedit/__init__.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/__init__.py Tue Dec 16 11:14:55 2014 +0100
@@ -0,0 +1,35 @@
+from flask import Flask, session
+from flask.ext.github import GitHub
+from flask.ext.cache import Cache
+from settings import *
+from config import *
+from logging import *
+
+# set up app and database
+app = Flask(__name__)
+app.config.from_object(appSettings)
+cache = Cache(app, config={"CACHE_TYPE": "simple"})
+app.config.from_object(appConfig)
+
+github = GitHub(app)
+
+# set up logging
+if app.config["LOGGING"]:
+ file_handler = FileHandler(filename=app.config["LOG_FILE_PATH"])
+ file_handler.setFormatter(Formatter('''
+ Message type: %(levelname)s
+ Location: %(pathname)s:%(lineno)d
+ Module: %(module)s
+ Function: %(funcName)s
+ Time: %(asctime)s
+
+ Message:
+
+ %(message)s
+ '''))
+ app.logger.addHandler(file_handler)
+ app.logger.setLevel(app.config["LOGGING_LEVEL"])
+
+# session management
+
+app.secret_key = "extremely_secure_and_very_secret_key"
--- a/src/catedit/api.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/api.py Tue Dec 16 11:14:55 2014 +0100
@@ -1,6 +1,6 @@
from flask.ext.restful import Resource, fields, Api, reqparse
from flask import request
-from app import app, cache
+from catedit import app, cache
from models import Category, CategoryManager
from rdflib import Graph, RDF
from utils import *
@@ -8,7 +8,9 @@
from config import *
import os
+
api = Api(app)
+logger = app.logger
cat_parser = reqparse.RequestParser()
cat_parser.add_argument('label', type=str)
@@ -41,29 +43,30 @@
cat_manager_instance = CategoryManager()
new_property_list = []
- # print args["property_predicate"]
- # print args["property_object"]
+
+ logger.debug(args["property_predicate"])
+ logger.debug(args["property_object"])
for property_predicate, property_object in zip(
args["property_predicate"],
args["property_object"]):
if property_object:
property_object_to_append = property_object
# if URIRef category, we must prefix id with namespace
- if app.config["PROPERTY_LIST"] \
- [property_predicate] \
- ["object_type"] == "uriref-category" :
- property_object_to_append = app.config["ONTOLOGY_NAMESPACE"] \
- + property_object
- # print property_object_to_append
+ if (app.config["PROPERTY_LIST"]
+ [property_predicate]
+ ["object_type"]) == "uriref-category":
+ property_object_to_append = \
+ app.config["ONTOLOGY_NAMESPACE"] + property_object
+ logger.debug(property_object_to_append)
new_property_list.append((property_predicate,
property_object_to_append))
- # print new_property_list
+ logger.debug(new_property_list)
c = cat_manager_instance.load_cat(cat_id)
c.edit_category(new_description=args["description"],
new_label=args["label"],
new_other_properties=new_property_list)
cat_manager_instance.save_cat(c, message=args["commit_message"])
- # print "put id: "+c.cat_id
+ logger.debug("put id: "+c.cat_id)
cache.clear()
return c.cat_graph.serialize(format='turtle'), 200
# Maybe not send the whole cat back, see if it's worth it
@@ -72,28 +75,28 @@
def post(self):
args = cat_parser.parse_args()
property_list = []
- # print args["property_predicate"]
- # print args["property_object"]
+ logger.debug(args["property_predicate"])
+ logger.debug(args["property_object"])
for property_predicate, property_object in zip(
request.form.getlist('property_predicate'),
request.form.getlist('property_object')):
if property_object:
- if app.config["PROPERTY_LIST"] \
- [property_predicate] \
- ["object_type"] == "uriref-category":
+ if (app.config["PROPERTY_LIST"]
+ [property_predicate]
+ ["object_type"]) == "uriref-category":
property_list.append((property_predicate,
app.config["ONTOLOGY_NAMESPACE"]
- +property_object))
+ + property_object))
else:
property_list.append((property_predicate,
property_object))
- # print property_list
+ logger.debug(property_list)
c = Category(label=args["label"],
description=args["description"],
other_properties=property_list)
cat_manager_instance = CategoryManager()
cat_manager_instance.save_cat(c, message=args["commit_message"])
- # print "post id: "+c.cat_id
+ logger.debug("post id: "+c.cat_id)
cache.clear()
return c.cat_graph.serialize(format='turtle'), 201
@@ -105,7 +108,7 @@
else:
message = request.form["delete_message"]
cat_manager_instance.delete_cat(cat_id, message=message)
- # print "delete id: "+cat_id
+ logger.debug("delete id: "+cat_id)
cache.clear()
return 204
--- a/src/catedit/app.py Fri Dec 12 16:45:47 2014 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-from flask import Flask, session
-from flask.ext.github import GitHub
-from flask.ext.cache import Cache
-from settings import *
-from config import *
-from logging import *
-
-# set up app and database
-app = Flask(__name__)
-app.config.from_object(appSettings)
-cache = Cache(app, config={"CACHE_TYPE": "simple"})
-app.config.from_object(appConfig)
-
-github = GitHub(app)
-
-# set up logging
-if app.config["LOGGING"]:
- file_handler = FileHandler(filename=app.config["LOG_FILE_PATH"])
- file_handler.setFormatter(Formatter('''
- Message type: %(levelname)s
- Location: %(pathname)s:%(lineno)d
- Module: %(module)s
- Function: %(funcName)s
- Time: %(asctime)s
-
- Message:
-
- %(message)s
- '''))
- app.logger.addHandler(file_handler)
- app.logger.setLevel(app.config["LOGGING_LEVEL"])
-
-# session management
-
-app.secret_key = "extremely_secure_and_very_secret_key"
--- a/src/catedit/main.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/main.py Tue Dec 16 11:14:55 2014 +0100
@@ -1,4 +1,4 @@
-from app import app
+from catedit import app
from api import api
from models import *
--- a/src/catedit/models.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/models.py Tue Dec 16 11:14:55 2014 +0100
@@ -2,10 +2,12 @@
from flask.ext.restful import fields
from rdflib import Graph, RDF, RDFS, BNode, Literal, URIRef
from uuid import uuid4
-from StringIO import StringIO
-from app import *
+from io import StringIO
from slugify import slugify
import persistence
+from catedit import app
+
+logger = app.logger
"""
Namespace: ld.iri-research.org/ontology/categorisation/#
@@ -24,9 +26,10 @@
other_properties=None, graph=None):
if not(graph):
# cat_id = uuid4().hex - Alternate method of generating ids
- cat_id = "category_id_"+slugify(label)
+ cat_id = "category_id_"+slugify(bytes(label))
self.cat_graph = Graph()
- self.this_category = URIRef(app.config["ONTOLOGY_NAMESPACE"]+cat_id)
+ self.this_category = URIRef(app.config["ONTOLOGY_NAMESPACE"] +
+ cat_id)
self.cat_graph.add((self.this_category, RDF.ID, Literal(cat_id)))
if label:
@@ -64,7 +67,8 @@
@property
def description(self):
- return_value = self.cat_graph.value(self.this_category, RDF.Description)
+ return_value = \
+ self.cat_graph.value(self.this_category, RDF.Description)
if return_value is None:
return None
else:
@@ -119,22 +123,22 @@
(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
+ (new_other_properties is None and
+ self.properties is not None):
+ logger.debug("before suppressing properties: ")
+ logger.debug(self.properties)
+ logger.debug("will replace with: ")
+ logger.debug(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
+ logger.debug("now properties are: ")
+ logger.debug(self.properties)
+ logger.debug("making new properties: ")
+ logger.debug(new_other_properties)
for (predicate, obj) in new_other_properties:
self.cat_graph.add((self.this_category,
app.config["PROPERTY_LIST"]
@@ -174,24 +178,35 @@
if cat.cat_id != app.config["ONTOLOGY_NAMESPACE"]+deleted_cat_id:
new_property_list_for_cat = []
for (predicate, obj) in cat.properties:
- if not ((app.config["PROPERTY_LIST"]
- [predicate]
- ["object_type"] == "uriref-category") and
- (obj == app.config["ONTOLOGY_NAMESPACE"]+deleted_cat_id)):
+ if not (
+ (app.config["PROPERTY_LIST"]
+ [predicate]
+ ["object_type"] == "uriref-category") and
+ (obj == (app.config["ONTOLOGY_NAMESPACE"] +
+ deleted_cat_id))
+ ):
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")
+ 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.delete(name=deleted_cat_id, message=message)
def list_cat(self):
p = getattr(persistence, app.config["PERSISTENCE_METHOD"])()
cat_serial_list = p.list()
- # print cat_serial_list
+ logger.debug(cat_serial_list)
cat_list = []
for cat_serial in cat_serial_list:
loaded_cat_graph = Graph()
- loaded_cat_graph.parse(source=StringIO(cat_serial), format='turtle')
+ loaded_cat_graph.parse(
+ source=StringIO(cat_serial),
+ format='turtle'
+ )
c = Category(graph=loaded_cat_graph)
cat_list.append(c)
return cat_list
--- a/src/catedit/persistence.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/persistence.py Tue Dec 16 11:14:55 2014 +0100
@@ -1,6 +1,5 @@
-from flask.ext.github import GitHubError
from abc import ABCMeta, abstractmethod
-from app import app, github, session
+from catedit import app
from base64 import b64encode, b64decode
import os
import json
@@ -39,13 +38,13 @@
class PersistenceToFile(Persistence):
def save(self, **kwargs):
path_to_save = app.config["FILE_SAVE_DIRECTORY"]+kwargs["name"]
- file = open(path_to_save, 'w+')
+ file = open(path_to_save, 'wb')
file.write(kwargs["content"])
file.close()
def load(self, **kwargs):
path_to_load = app.config["FILE_SAVE_DIRECTORY"]+kwargs["name"]
- file = open(path_to_load, 'r')
+ file = open(path_to_load, 'rb')
file_content = file.read()
file.close()
return file_content
@@ -58,17 +57,19 @@
def list(self, **kwargs):
file_content_list = []
for file_name in os.listdir(app.config["FILE_SAVE_DIRECTORY"]):
+ if not file_name or file_name[0] == ".":
+ continue
file = open(app.config["FILE_SAVE_DIRECTORY"]+file_name)
file_content = file.read()
- file.close
+ file.close()
file_content_list.append(file_content)
- # print file_content_list
+ # logger.debug(file_content_list)
return file_content_list
class PersistenceToGithub(Persistence):
def save(self, **kwargs):
- # print kwargs["content"]
+ # logger.debug(kwargs["content"])
request_data = {"content": b64encode(kwargs["content"]),
"message": kwargs["message"]}
try:
@@ -81,7 +82,7 @@
request_data["sha"] = filedict["sha"]
except GitHubError:
pass
- # print json.dumps(request_data)
+ # logger.debug(json.dumps(request_data))
try:
github.request('PUT',
"repos/"
@@ -139,7 +140,7 @@
+ "/contents/"
+ app.config["CATEGORIES_PATH"])
filenames_list = [file["name"] for file in files_in_repo]
- # print filenames_list
+ # logger.debug(filenames_list)
except GitHubError:
pass
file_content_list = []
@@ -154,5 +155,5 @@
file_content_list.append(b64decode(filedict["content"]))
except GitHubError:
pass
- # print file_content_list
+ # logger.debug(file_content_list)
return file_content_list
--- a/src/catedit/settings.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/settings.py Tue Dec 16 11:14:55 2014 +0100
@@ -1,4 +1,4 @@
-from rdflib import *
+from rdflib import URIRef, RDF, RDFS, Literal
from rdflib.namespace import SKOS
@@ -21,10 +21,10 @@
CATEGORY_NAMESPACE = "http://ld.iri-research.org/categorisation/category/"
# Saving file for local persistence
- FILE_SAVE_DIRECTORY = "../../files/"
+ FILE_SAVE_DIRECTORY = "../../run/files/"
# Logging config
- LOG_FILE_PATH = "log/log.txt"
+ LOG_FILE_PATH = "../../run/log/log.txt"
LOGGING = False
# Github repository config
@@ -80,4 +80,4 @@
# Category persistence parameters
- PERSISTENCE_METHOD = "PersistenceToGithub"
+ PERSISTENCE_METHOD = "PersistenceToFile"
--- a/src/catedit/utils.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/utils.py Tue Dec 16 11:14:55 2014 +0100
@@ -3,15 +3,19 @@
from rdflib.compare import *
'''
- This function will compare two categories from their Graph object serializations
+ This function will compare two categories from their Graph object
+ serializations.
Exact return values are to be defined, some ideas
* check if the 2 categories are the same? (same ID, isomorphic)
- * check if the 2 categories are the same but different version? (same ID, not isomorphic)
- * check if unique, non empty parameters changed and list them (label and description)
+ * check if the 2 categories are the same but different version?
+ (same ID, not isomorphic)
+ * check if unique, non empty parameters changed and list them
+ (label and description)
* returns 2 lists of properties: only_in_first, only_in_second
* more?
'''
+
def compare_two_cat(cat_serial_1, cat_serial_2):
cat1 = Category(cat_serial_1)
cat2 = Category(cat_serial_2)
--- a/src/catedit/views.py Fri Dec 12 16:45:47 2014 +0100
+++ b/src/catedit/views.py Tue Dec 16 11:14:55 2014 +0100
@@ -1,4 +1,4 @@
-from app import app, github
+from catedit import app, github
from models import Category, CategoryManager
from flask import render_template, request, redirect, url_for, session
from flask.ext.github import GitHubError
@@ -7,13 +7,23 @@
from wtforms import StringField, TextAreaField
from wtforms.validators import DataRequired
from rdflib import Graph
-from StringIO import StringIO
+from io import StringIO
+logger = app.logger
class NewCategoryMinimalForm(Form):
- label = StringField("Nom de la categorie (obligatoire)", validators=[DataRequired()])
- description = TextAreaField("Description de la categorie (obligatoire)", validators=[DataRequired()])
- commit_message = StringField("Message de soumission (Obligatoire)", validators=[DataRequired()])
+ label = StringField(
+ "Nom de la categorie (obligatoire)",
+ validators=[DataRequired()]
+ )
+ description = TextAreaField(
+ "Description de la categorie (obligatoire)",
+ validators=[DataRequired()]
+ )
+ commit_message = StringField(
+ "Message de soumission (Obligatoire)",
+ validators=[DataRequired()]
+ )
@app.route('/', methods=['GET'])
@@ -24,9 +34,9 @@
# list categories
if delete_cat_id is None:
serialized_cat_list = cat_api_instance.get()
- # print serialized_cat_list
+ logger.debug(serialized_cat_list)
cat_list = []
- # print cat_list
+ logger.debug(cat_list)
for serialized_cat in serialized_cat_list:
cat_rdf_graph = Graph()
cat_rdf_graph.parse(source=StringIO(serialized_cat),
@@ -37,7 +47,7 @@
"cat_description": c.description,
"cat_id": c.cat_id,
"cat_properties": c.properties})
- # print c.properties
+ logger.debug(c.properties)
return render_template('catrecap.html',
cat_list=cat_list)
else:
@@ -81,7 +91,7 @@
TextAreaField("Description de la categorie",
validators=[DataRequired()],
default=c.description))
- # print "CatForm fields preset to "+c.label+" and "+c.description
+ logger.debug("CatForm fields preset to "+c.label+" and "+c.description)
cat_form = NewCategoryMinimalForm(request.form)
@@ -147,25 +157,25 @@
try:
repoList = []
repoList = github.get("user/repos")
- # for repo in repoList:
- # print repo["name"]
+ for repo in repoList:
+ logger.debug(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"]
+ logger.debug(session["user_can_edit"])
except GitHubError:
- # print "error getting repos!"
+ logger.debug("error getting repos!")
pass
- # print session["user_login"]
+ logger.debug(session["user_login"])
return redirect(url_for('cat_recap'))
@github.access_token_getter
def token_getter():
if session.get("user_logged", None):
- print "I made an authentified request"
+ logger.debug("I made an authentified request")
return session["user_code"]