--- a/src/catedit/persistence.py Tue Dec 30 16:55:56 2014 +0100
+++ b/src/catedit/persistence.py Wed Dec 31 15:28:40 2014 +0100
@@ -151,13 +151,19 @@
"""
return True
- def submit_changes(self, **kwargs):
+ def save(self, **kwargs):
"""
Saves all the recorded files in the session dict to a Github
repository
Expected kwargs is:
* message: the commit message to document the changes
+ * deletion_list : the list of files to delete, list of dicts, each
+ one of the format:
+ {"name": name_of_the_file}
+ * modification_list: the list of the files to change, list of
+ dicts, each one of the format:
+ {"name": name_of_the_file, "content": content_of_the_file}
IMPORTANT: To save to a file Github, we have to use Git
internal mechanics:
@@ -185,31 +191,38 @@
tree
"""
+ deletion_list = kwargs["deletion_list"]
+ modification_list = kwargs["modification_list"]
+
# point 1
ref_master = github.get("repos/"
+ app.config["REPOSITORY_OWNER"] + "/"
+ app.config["REPOSITORY_NAME"]
+ "/git/refs/heads/master")
logger.debug(str(ref_master))
+
# point 2
last_commit_master = github.get("repos/"
+ app.config["REPOSITORY_OWNER"] + "/"
+ app.config["REPOSITORY_NAME"]
+ "/git/commits/"
+ ref_master["object"]["sha"])
+ logger.debug(str(last_commit_master))
- logger.debug(str(last_commit_master))
- # point 3
+ # Point 3
last_commit_tree = github.get("repos/"
+ app.config["REPOSITORY_OWNER"] + "/"
+ app.config["REPOSITORY_NAME"]
+ "/git/trees/"
+ last_commit_master["tree"]["sha"]
+ "?recursive=1")
+ logger.debug(str(last_commit_tree))
- logger.debug(str(last_commit_tree))
- # point 4
+ # Point 4
new_tree_data = {"tree": []}
+
+ # First we loop over the existing elements to spot which are modified
+ # and which are untouched and create new blobs when needed
for element in last_commit_tree["tree"]:
logger.debug(element)
if element["type"] == "blob":
@@ -218,7 +231,7 @@
if not(
element["path"] in [
(app.config["CATEGORIES_PATH"] + category["name"])
- for category in session.get("deleted_categories", [])
+ for category in deletion_list
]
):
@@ -230,16 +243,15 @@
if (
element["path"] in [
(app.config["CATEGORIES_PATH"] + category["name"])
- for category in
- session.get("modified_categories", [])
+ for category in modification_list
]
):
# find element in modified categories
- for category in session["modified_categories"]:
+ for category in modification_list:
if element["path"] == (
app.config["CATEGORIES_PATH"] + category["name"]
):
- # 4-1 for edited files
+ # 4-1 for modified files, creating a new blob
new_blob_data = {
"content": category["content"],
"encoding": "utf-8"
@@ -254,7 +266,8 @@
blob_sha = new_blob["sha"]
break
- # this means element is an untouched file
+ # this means element is an untouched file so we just get
+ # its sha
else:
blob_sha = element["sha"]
new_tree_data["tree"].append({"path": element["path"],
@@ -262,16 +275,17 @@
"type": "blob",
"sha": blob_sha})
logger.debug(str(new_tree_data["tree"]))
- # Now we'll add new files in the tree
- for category in session.get("modified_categories", []):
+ # Now we loop over modified categories to find the ones that don't
+ # exist yet in the last commit tree in order to create blobs for them
+ for category in modification_list:
logger.debug(app.config["CATEGORIES_PATH"]+category["name"]
+ " should not be in "
+ str([elt["path"] for
elt in last_commit_tree["tree"]]))
- if (app.config["CATEGORIES_PATH"]+category["name"] not in
+ if (app.config["CATEGORIES_PATH"] + category["name"] not in
[file["path"] for file in last_commit_tree["tree"]]):
- # 4-1 for added files
+ # 4-1 for added files, creating a new blob
new_blob_data = {"content": category["content"],
"encoding": "utf-8"}
new_blob = github.post("repos/"
@@ -294,7 +308,7 @@
+ "/git/trees",
data=new_tree_data)
- # point 5
+ # Point 5
new_commit_data = {"message": kwargs["message"],
"parents": [last_commit_master["sha"]],
"tree": new_tree_response["sha"]}
@@ -306,7 +320,7 @@
data=new_commit_data)
logger.debug(str(new_commit))
- # point 6
+ # Point 6
new_head_data = {"sha": new_commit["sha"], "force": "true"}
logger.debug(str(new_head_data))
new_head = github.patch("repos/"
@@ -315,31 +329,6 @@
+ "/git/refs/heads/master",
data=json.dumps(new_head_data))
logger.debug(str(new_head))
- session["deleted_categories"] = []
- session["modified_categories"] = []
-
- def save(self, **kwargs):
- """
- Saves given file to the session dict
-
- Expected kwargs should be:
- * name: the name of the file to save
- * content: the content of the file to save
- """
- session["modified_categories"][:] = [
- elt for elt in session.get("modified_categories", [])
- if elt["name"] != kwargs["name"]
- ]
- session["modified_categories"].append(
- {"name": kwargs["name"],
- "content": str(kwargs["content"], "utf-8")}
- )
- # Now we must clean the deleted categories list in case the modified
- # category was deleted before being edited
-
- for element in session.get("deleted_categories", []):
- if element["name"] == kwargs["name"]:
- session["deleted_categories"].remove(element)
def load(self, **kwargs):
"""
@@ -363,22 +352,13 @@
def delete(self, **kwargs):
"""
Deletes from a Github repository
- Calling delete for a file already deleted will restore it
- (by deleting it from the delete files list)
+
+ Expected kwargs are:
+ * name : the name of the file to delete
+ * message : the commit message for the deletion
+
"""
- if (kwargs["name"] in [
- element["name"] for element in session.get("deleted_categories", [])
- ]):
- session["deleted_categories"].remove({"name": kwargs["name"]})
- # warning, not safe if 2 files share the same name (or category id)
- # but that shouldn't happen
- else:
- session["deleted_categories"].append({"name": kwargs["name"]})
- # now we must clean the modified categories list in case the
- # deleted category was modified before
- for element in session.get("modified_categories", []):
- if element["name"] == kwargs["name"]:
- session["modified_categories"].remove(element)
+ pass
def list(self, **kwargs):
"""