src/catedit/persistence.py
changeset 40 8c32ea41b391
parent 39 67d2ddbebf7e
child 42 dac9b1248e0f
--- a/src/catedit/persistence.py	Tue Jan 13 10:43:26 2015 +0100
+++ b/src/catedit/persistence.py	Fri Jan 30 12:38:15 2015 +0100
@@ -69,22 +69,41 @@
         * name : name of the file to write in/read from
         * content : desired content of the file when writing
     """
+    def __init__(self, **kwargs):
+        pass
+
     @property
     def session_compliant(self):
         """
             Not session compliant: each modification is submitted
         """
-        return False
+        return True
 
     def save(self, **kwargs):
         """
             Saves to a file
+
+            Expected args:
+            * deletion_dict
+            * modification_dict
         """
-        path_to_save = app.config["PERSISTENCE_CONFIG"]["FILE_SAVE_DIRECTORY"] \
-                       + kwargs["name"]
-        file_to_save = open(path_to_save, 'wb')
-        file_to_save.write(kwargs["content"])
-        file_to_save.close()
+        modification_dict = kwargs["modification_dict"]
+        deletion_dict = kwargs["deletion_dict"]
+
+        for (file_name, file_content) in modification_dict.items():
+            path_to_save = app.config["PERSISTENCE_CONFIG"] \
+                                     ["FILE_SAVE_DIRECTORY"] \
+                           + file_name
+            print(path_to_save)
+            file_to_save = open(path_to_save, 'wb')
+            file_to_save.write(bytes(file_content, "utf-8"))
+            file_to_save.close()
+
+        for file_name in deletion_dict.keys():
+            path_to_delete = app.config["PERSISTENCE_CONFIG"] \
+                                       ["FILE_SAVE_DIRECTORY"] \
+                             + file_name
+            os.remove(path_to_delete)
 
     def load(self, **kwargs):
         """
@@ -92,9 +111,13 @@
         """
         path_to_load = app.config["PERSISTENCE_CONFIG"]["FILE_SAVE_DIRECTORY"] \
                        + kwargs["name"]
-        file_to_load = open(path_to_load, 'rb')
-        file_content = file_to_load.read()
-        file_to_load.close()
+        try:
+            file_to_load = open(path_to_load, 'rb')
+            file_content = str(file_to_load.read(), "utf-8")
+            print(file_content)
+            file_to_load.close()
+        except FileNotFoundError:
+            file_content=""
         return file_content
 
     def delete(self, **kwargs):
@@ -115,10 +138,11 @@
                                               ["FILE_SAVE_DIRECTORY"]):
             if not file_name or file_name[0] == ".":
                 continue
-            path_to_load = open(app.config["PERSISTENCE_CONFIG"]
-                                          ["FILE_SAVE_DIRECTORY"] + file_name)
-            file_content = path_to_load.read()
-            path_to_load.close()
+            path_to_load = app.config["PERSISTENCE_CONFIG"] \
+                                     ["FILE_SAVE_DIRECTORY"] + file_name
+            file_to_load = open(path_to_load, 'rb')
+            file_content = str(file_to_load.read(), "utf-8")
+            file_to_load.close()
             file_content_list.append(file_content)
         # logger.debug(file_content_list)
         return file_content_list
@@ -200,16 +224,16 @@
             )
             logger.debug(str(ref_master))
         except GitHubError as ghe:
-            logger.debug("GitHubError trying to get the reference "
+            logger.error("GitHubError trying to get the reference "
                          + "to the master branch")
-            logger.debug(
+            logger.error(
                 "Endpoint: "
                 + "repos/"
                 + app.config["PERSISTENCE_CONFIG"]["REPOSITORY_OWNER"] + "/"
                 + self.repository
                 + "/git/refs/heads/master"
             )
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
         # point 2
@@ -223,9 +247,9 @@
             )
             logger.debug(str(last_commit_master))
         except GitHubError as ghe:
-            logger.debug("GitHubError trying to get the commit associated "
+            logger.error("GitHubError trying to get the commit associated "
                          + "to the latest reference to the master branch")
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
         # Point 3
@@ -240,10 +264,10 @@
             )
             logger.debug(str(last_commit_tree))
         except GitHubError as ghe:
-            logger.debug("GitHubError trying to get the tree from the commit "
+            logger.error("GitHubError trying to get the tree from the commit "
                          + "associated to the latest reference to the master "
                          + "branch")
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
         # Point 4
@@ -300,12 +324,12 @@
                                     blob_sha = new_blob["sha"]
                                     break
                                 except GitHubError as ghe:
-                                    logger.debug(
+                                    logger.error(
                                         "GitHubError trying to post a new"
                                         + "blob with following data: "
                                         + str(new_blob_data)
                                     )
-                                    logger.debug(ghe.response.text)
+                                    logger.error(ghe.response.text)
                                 logger.debug(str(
                                     github.get("rate_limit")["resources"]
                                 ))
@@ -344,12 +368,12 @@
                         data=new_blob_data
                     )
                 except GitHubError as ghe:
-                    logger.debug(
+                    logger.error(
                         "GitHubError trying to post a new blob with following"
                         + "data: "
                         + str(new_blob_data)
                     )
-                    logger.debug(ghe.response.text)
+                    logger.error(ghe.response.text)
                 logger.debug(str(github.get("rate_limit")["resources"]))
                 new_tree_data["tree"].append({
                     "path": app.config["PERSISTENCE_CONFIG"]
@@ -370,11 +394,11 @@
                 data=new_tree_data
             )
         except GitHubError as ghe:
-            logger.debug(
+            logger.error(
                 "GitHubError trying to post a new tree with following data: "
                 + str(new_tree_data)
             )
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
 
         # Point 5
         new_commit_data = {"message": kwargs["message"],
@@ -391,11 +415,11 @@
             )
             logger.debug(str(new_commit))
         except GitHubError as ghe:
-            logger.debug(
+            logger.error(
                 "GitHubError trying to post a new commit with following data: "
                 + str(new_commit_data)
             )
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
         # Point 6
@@ -411,12 +435,12 @@
             )
             logger.debug(str(new_head))
         except GitHubError as ghe:
-            logger.debug(
+            logger.error(
                 "GitHubError trying to edit the head to the master branch"
                 + "with the following data: "
                 + str(new_head_data)
             )
-            logger.debug(ghe.response.text)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
 
@@ -424,6 +448,7 @@
         """
             Loads from a Github repository
         """
+        file_content=""
         try:
             filedict = github.get("repos/"
                                   + app.config["PERSISTENCE_CONFIG"]
@@ -435,10 +460,11 @@
                                   + kwargs["name"])
             file_content = str(b64decode(filedict["content"]), "utf-8")
         except GitHubError as ghe:
-            logger.debug("Github Error trying to get file: "+kwargs["name"])
-            logger.debug("Github sent an error, if 404, either you may not "
-                         + "have access to the repository or it doesn't exist ")
-            logger.debug(ghe.response.text)
+            logger.error("Github Error trying to get file: "+kwargs["name"])
+            logger.error("Github sent an error, if 404, either you may not "
+                         + "have access to the repository or the file doesn't "
+                         + "exist ")
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
         return file_content
 
@@ -449,7 +475,6 @@
             Expected kwargs are:
             * name : the name of the file to delete
             * message : the commit message for the deletion
-
         """
         pass
 
@@ -470,12 +495,11 @@
                               for github_file in files_in_repo]
             # logger.debug(filenames_list)
         except GitHubError as ghe:
-            logger.debug("Github Error trying to get the file list in the "
+            logger.error("Github Error trying to get the file list in the "
                          + "category repository")
-            logger.debug("NOTE: Github sent an error, if 404 either you "
-                         + "may not have access to the repository or it "
-                         + "doesn't exist or there isn't any files in it")
-            logger.debug(ghe.response.text)
+            logger.error("IMPORTANT: This message can mean there is no "
+                         + "category in the repository " + self.repository)
+            logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
 
         file_content_list = []
@@ -492,8 +516,8 @@
                 file_content_list.append(str(b64decode(filedict["content"]),
                                          "utf-8"))
             except GitHubError as ghe:
-                logger.debug("Github Error trying to get file: "+filename)
-                logger.debug(ghe.response.text)
+                logger.error("Github Error trying to get file: "+filename)
+                logger.error(ghe.response.text)
         logger.debug(str(github.get("rate_limit")["resources"]))
         # logger.debug(file_content_list)
         return file_content_list