Commits

Anonymous committed 475ed44

Getting, putting and removing the latest revision works.

  • Participants
  • Parent commits 5eb49e1

Comments (0)

Files changed (1)

 from os import mkdir, rmdir # for locking
 from time import sleep
 from os.path import isdir, join
+from copy import deepcopy
 
 def get_data_for_key(key,
                      sites_dir="sites",
-                     states_dir="states"):
+                     states_dir="states",
+                     latest_revision=False):
     """Get the data for one site.
 
     >>> io._test_clean_dirs()
         makedirs(states_dir)
     
     sites, states = io.get_dataset_for_key(key, sites_dir, states_dir)
-    subkey = io.key_to_subkey(key)
+    subkey = io.key_to_subkey(key, latest_revision=latest_revision)
     site = {}
     for s in sites:
         if s.get("path", None) == subkey:
     
 
 def edit_data_for_key(key, site=None, state=None, action="put", 
-                     sites_dir="sites",
-                     states_dir="states"):
+                      sites_dir="sites",
+                      states_dir="states",
+                      latest_revision=False):
     """Put a site or state or both into the site.
 
     TODO: Refactor: the site doesn’t need to contain the path. The path should come from the key alone.
 
     >>> list(sites)
     [{'path': '/Infinite_Hands/2/', 'description': 'singing a part of the history of free software.', 'randomint': 5}, {'path': '/Infinite_Hands/-1/', '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")
+    >>> edit_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + inf_subkey, site=infinite_hands, action="remove", sites_dir="test_sites", states_dir="test_states")
+    Warning: the path in the site and the key don’t match. Adjusting the path.
+    >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + inf_subkey, sites_dir="test_sites", states_dir="test_states")
     ({}, {})
+    >>> edit_data_for_key(usk + infinite_hands['path'], site=infinite_hands, sites_dir="test_sites", states_dir="test_states", latest_revision=True)
+    Warning: the path in the site and the key don’t match. Adjusting the path.
+    >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + inf_subkey, sites_dir="test_sites", states_dir="test_states")
+    ({'path': '/Infinite_Hands/-1/', 'description': 'singing a part of the history of free software.', 'randomint': 5}, {})
+    >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + infinite_hands['path'], sites_dir="test_sites", states_dir="test_states", latest_revision=True)
+    ({'path': '/Infinite_Hands/-1/', '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", latest_revision=True)
+    Warning: the path in the site and the key don’t match. Adjusting the path.
+    >>> get_data_for_key("USK@N7dmVKbxm5Q9YIzg74T1gUJd96eyAa2VLoMlPp0CQQs,AfZRxhqkvql5E~hDcW1s0mtHTAKrkhWpfcRm2kqkUjE,AQACAAE" + inf_subkey, sites_dir="test_sites", states_dir="test_states")
+    ({}, {})
+    
     """
     # make sure the data folders exist.
     if not isdir(sites_dir):
         makedirs(states_dir)
 
     # prepare local stuff for the critical section.
-    subkey = io.key_to_subkey(key)
+    subkey = io.key_to_subkey(key, latest_revision=latest_revision)
     if site is not None and 'path' in site and site['path'] != subkey:
         print("Warning: the path in the site and the key don’t match. Adjusting the path.")
+        # TODO: use deepcopy
+        site = deepcopy(site)
         site['path'] = subkey
     if state is not None and 'path' in state and state['path'] != subkey:
         print("Warning: the path in the state and the key don’t match. Adjusting the path.")
+        site = deepcopy(site)
         state['path'] = subkey
 
     # to make sure that we can work in multiple processes