Commits

Anonymous committed b794040

added removing a site from the database.

  • Participants
  • Parent commits fdcbd5d

Comments (0)

Files changed (1)

 
     >>> io._test_clean_dirs()
     >>> gpl = {'path': 'KSK@gpl.txt', 'description': 'The GNU General Public License. The most widespread license for free software.'}
-    >>> put_data_for_key("KSK@gpl.txt", site=gpl, sites_dir="test_sites", states_dir="test_states")
+    >>> edit_data_for_key("KSK@gpl.txt", site=gpl, sites_dir="test_sites", states_dir="test_states")
     >>> gpl == get_data_for_key("KSK@gpl.txt", sites_dir="test_sites", states_dir="test_states")[0]
     True
     """
     return site, state
 
 
-def put_data_for_key(key, site=None, state=None, 
+def _put_data_for_key(key, subkey, site=None, state=None, 
+                     sites_dir="sites",
+                     states_dir="states"):
+    """Put a site or state or both into the site. This HAS TO be called in the CRITICAL SECTION of edit_data_for_key() to make it multiprocess-safe."""
+    sites_new = []
+    states_new = []
+    sites, states = io.get_dataset_for_key(key, sites_dir, states_dir)
+    if site is not None:
+        added = False
+        for s in sites:
+            if not added and s.get("path", None) == subkey:
+                # update the site
+                sites_new.append(site)
+                added = True
+            else:
+                sites_new.append(s)
+        if not added:
+            sites_new.append(site)
+        sites = sites_new
+        
+    if state is not None:
+        for s in states:
+            added = False
+            if not added and s.get("path", None) == subkey:
+                # update the site
+                states_new.append(site)
+                added = True
+            else:
+                states_new.append(s)
+        if not added:
+            states_new.append(site)
+        states = states_new
+
+    io.put_dataset_for_key(key, sites, states, sites_dir, states_dir)
+    
+
+def _remove_data_for_key(key, subkey, site=None, state=None, 
+                     sites_dir="sites",
+                     states_dir="states"):
+    """Remove a site or state or both from the site. This HAS TO be called in the CRITICAL SECTION of edit_data_for_key() to make it multiprocess-safe."""
+    sites_new = []
+    states_new = []
+    sites, states = io.get_dataset_for_key(key, sites_dir, states_dir)
+    if site is not None:
+        for s in sites:
+            if not s.get("path", None) == subkey:
+                sites_new.append(s)
+        sites = sites_new
+        
+    if state is not None:
+        for s in states:
+            if not s.get("path", None) == subkey:
+                states_new.append(s)
+        states = states_new
+
+    io.put_dataset_for_key(key, sites, states, sites_dir, states_dir)
+    
+
+def edit_data_for_key(key, site=None, state=None, action="put", 
                      sites_dir="sites",
                      states_dir="states"):
     """Put a site or state or both into the site.
 
     >>> io._test_clean_dirs()
     >>> gpl = {'path': 'KSK@gpl.txt', 'description': 'The GNU General Public License. The most widespread license for free software.'}
-    >>> put_data_for_key("KSK@gpl.txt", site=gpl, sites_dir="test_sites", states_dir="test_states")
+    >>> edit_data_for_key("KSK@gpl.txt", site=gpl, sites_dir="test_sites", states_dir="test_states")
     >>> gpl == get_data_for_key("KSK@gpl.txt", sites_dir="test_sites", states_dir="test_states")[0]
     True
     >>> rogar = {'path': 'KSK@rogar.txt', 'description': 'Rogar, the barbarian.'}
-    >>> put_data_for_key(rogar["path"], site=rogar, sites_dir="test_sites", states_dir="test_states")
+    >>> edit_data_for_key(rogar["path"], site=rogar, sites_dir="test_sites", states_dir="test_states")
     >>> sites, states = io.get_dataset_for_key("KSK@gpl.txt", sites_dir="test_sites", states_dir="test_states")
     >>> list(sites)
     [{'path': 'KSK@gpl.txt', 'description': 'The GNU General Public License. The most widespread license for free software.'}, {'path': 'KSK@rogar.txt', 'description': 'Rogar, the barbarian.'}]
     >>> list(states)
     []
     >>> infinite_hands = {'path': '/Infinite_Hands/5/', 'description': 'singing a part of the history of free software.', 'randomint': 5}
-    >>> put_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands["path"], site=infinite_hands, sites_dir="test_sites", states_dir="test_states")
+    >>> edit_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands["path"], site=infinite_hands, sites_dir="test_sites", states_dir="test_states")
     >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands["path"], sites_dir="test_sites", states_dir="test_states")
     ({'path': '/Infinite_Hands/5/', 'description': 'singing a part of the history of free software.', 'randomint': 5}, {})
+    >>> edit_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands["path"], site=infinite_hands, action="remove", sites_dir="test_sites", states_dir="test_states")
+    >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands["path"], sites_dir="test_sites", states_dir="test_states")
+    ({}, {})
     """
-
     # make sure the data folders exist.
     if not isdir(sites_dir):
         makedirs(sites_dir)
 
     # prepare local stuff for the critical section.
     subkey = io.key_to_subkey(key)
-    sites_new = []
-    states_new = []
 
     # to make sure that we can work in multiple processes
     # we use a lock directory: sites_dir/.lock/
             sleep(0.01)
     #### critical section: insert the data and writeout the dataset ####
     # nothing in this section may break the script, else the lock stays put.
-    try: 
-        sites, states = io.get_dataset_for_key(key, sites_dir, states_dir)
-        if site is not None:
-            added = False
-            for s in sites:
-                if not added and s.get("path", None) == subkey:
-                    # update the site
-                    sites_new.append(site)
-                    added = True
-                else:
-                    sites_new.append(s)
-            if not added:
-                sites_new.append(site)
-            sites = sites_new
-
-        if state is not None:
-            for s in states:
-                added = False
-                if not added and s.get("path", None) == subkey:
-                    # update the site
-                    states_new.append(site)
-                    added = True
-                else:
-                    states_new.append(s)
-            if not added:
-                states_new.append(site)
-            states = states_new
-
-        io.put_dataset_for_key(key, sites, states, sites_dir, states_dir)
+    try:
+        if action == "put": 
+            _put_data_for_key(key, subkey, site, state, sites_dir=sites_dir, states_dir=states_dir)
+        if action == "remove":
+            _remove_data_for_key(key, subkey, site, state, sites_dir=sites_dir, states_dir=states_dir)
     except IOError:
         print("Couldn’t store the updated data. Dataset discarded.")
     except: