Browse Source

convert more getter to properties

Convert MetadataRootRecord.get_file_tree() to property
MetadataRootRecord.file_tree

Convert VersionList.get_versioned_elements() to property
VersionList.versioned_elements
Christian Mönch 2 years ago
parent
commit
f4ba2584df

+ 3 - 3
dataladmetadatamodel/mapper/gitmapper/metadatamapper.py

@@ -19,17 +19,17 @@ class MetadataGitMapper(Mapper):
     metadata_caches: Dict[str, GitBlobCache] = dict()
 
     @classmethod
-    def get_cache(cls, realm):
+    def get_cache(cls, realm: str):
         return cls.metadata_caches.get(realm, None)
 
     @classmethod
-    def cache_realm(cls, realm):
+    def cache_realm(cls, realm: str):
         if cls.get_cache(realm) is not None:
             raise RuntimeError(f"already caching realm: {realm}")
         cls.metadata_caches[realm] = GitBlobCache(realm)
 
     @classmethod
-    def flush_realm(cls, realm):
+    def flush_realm(cls, realm: str):
         cache = cls.get_cache(realm)
         if cache is None:
             raise RuntimeError(f"realm is not cached: {realm}")

+ 2 - 2
dataladmetadatamodel/mapper/gitmapper/metadatarootrecordmapper.py

@@ -64,10 +64,10 @@ class MetadataRootRecordGitMapper(Mapper):
 
         assert isinstance(mrr, MetadataRootRecord)
 
-        if mrr.file_tree is None:
+        if mrr._file_tree is None:
             file_tree_reference = Reference.get_none_reference("FileTree")
         else:
-            file_tree_reference = mrr.file_tree.write_out(
+            file_tree_reference = mrr._file_tree.write_out(
                 realm,
                 "git",
                 force_write)

+ 12 - 10
dataladmetadatamodel/metadatarootrecord.py

@@ -30,7 +30,7 @@ class MetadataRootRecord(MappableObject):
         self.dataset_identifier = dataset_identifier
         self.dataset_version = dataset_version
         self.dataset_level_metadata = dataset_level_metadata
-        self.file_tree = file_tree
+        self._file_tree = file_tree
 
     @staticmethod
     def get_empty_instance(realm: Optional[str] = None,
@@ -40,27 +40,29 @@ class MetadataRootRecord(MappableObject):
     def modifiable_sub_objects_impl(self) -> Iterable[MappableObject]:
         return [
             child
-            for child in [self.dataset_level_metadata, self.file_tree]
+            for child in [self.dataset_level_metadata, self._file_tree]
             if child is not None
         ]
 
     def purge_impl(self):
         self.dataset_level_metadata.purge()
-        if self.file_tree is not None:
-            self.file_tree.purge()
+        if self._file_tree is not None:
+            self._file_tree.purge()
         if self.dataset_level_metadata is not None:
             self.dataset_level_metadata = None
-        self.file_tree = None
+        self._file_tree = None
 
     def set_file_tree(self, file_tree: FileTree):
         self.touch()
-        self.file_tree = file_tree
+        self._file_tree = file_tree
 
     def get_file_tree(self):
         self.ensure_mapped()
-        if self.file_tree is None:
+        if self._file_tree is None:
             return None
-        return self.file_tree.read_in()
+        return self._file_tree.read_in()
+
+    file_tree = property(fget=get_file_tree, fset=set_file_tree)
 
     def set_dataset_level_metadata(self, dataset_level_metadata: Metadata):
         self.touch()
@@ -86,10 +88,10 @@ class MetadataRootRecord(MappableObject):
                 else None
             ),
             (
-                self.file_tree.deepcopy(
+                self._file_tree.deepcopy(
                     new_mapper_family,
                     new_destination)
-                if self.file_tree is not None
+                if self._file_tree is not None
                 else None
             ),
             None,

+ 2 - 2
dataladmetadatamodel/tests/test_remote.py

@@ -17,7 +17,7 @@ class TestRemote(unittest.TestCase):
         self.assertEqual(len(tree_version_list.version_set), 1)
         self.assertEqual(len(uuid_set.uuid_set), 1)
 
-        for version, element_info in tree_version_list.get_versioned_elements():
+        for version, element_info in tree_version_list.versioned_elements:
             time_stamp, dataset_path, dataset_tree = element_info
             dataset_tree = cast(DatasetTree, dataset_tree)
             dataset_tree.read_in()
@@ -25,7 +25,7 @@ class TestRemote(unittest.TestCase):
             self.assertEqual(27, len(dataset_paths))
 
             mrr = dataset_tree.get_metadata_root_record(MetadataPath("study-104"))
-            file_tree = mrr.get_file_tree()
+            file_tree = mrr.file_tree
             file_paths = list(file_tree.get_paths_recursive())
             self.assertEqual(7, len(file_paths))
 

+ 2 - 2
dataladmetadatamodel/tests/test_versionlist.py

@@ -96,11 +96,11 @@ class TestVersionList(unittest.TestCase):
             copied_version_list.read_in()
 
             # Check that the copied elements are unmapped
-            for _, (_, _, copied_dataset_tree) in copied_version_list.get_versioned_elements():
+            for _, (_, _, copied_dataset_tree) in copied_version_list.versioned_elements:
                 self.assertFalse(copied_dataset_tree.mtree.mapped)
 
             # Compare the version lists, taking modified dataset tree paths into account
-            for primary_version, (_, path, dataset_tree) in version_list.get_versioned_elements():
+            for primary_version, (_, path, dataset_tree) in version_list.versioned_elements:
                 expected_path = path_prefix / path
                 _, copy_path, copied_dataset_tree = copied_version_list.get_versioned_element(primary_version)
                 self.assertEqual(expected_path, copy_path)

+ 4 - 4
dataladmetadatamodel/tests/utils.py

@@ -86,8 +86,8 @@ def assert_mrr_equal(test_case: unittest.TestCase,
 
     assert_mappable_objects_equal(
         test_case,
-        a_mrr.file_tree,
-        b_mrr.file_tree,
+        a_mrr._file_tree,
+        b_mrr._file_tree,
         a_purge_unsafe,
         assert_file_trees_equal)
 
@@ -179,8 +179,8 @@ def assert_mrrs_equal(test_case: unittest.TestCase,
     # Compare file trees
     assert_mappable_objects_equal(
         test_case,
-        a.file_tree,
-        b.file_tree,
+        a._file_tree,
+        b._file_tree,
         unsafe,
         assert_file_trees_equal
     )

+ 1 - 1
dataladmetadatamodel/uuidset.py

@@ -46,7 +46,7 @@ class UUIDSet(MappableObject):
         self.touch()
         self.uuid_set[uuid] = version_list
 
-    def get_version_list(self, uuid) -> VersionList:
+    def get_version_list(self, uuid: UUID) -> VersionList:
         """
         Get the version list for uuid. If it is not mapped yet,
         it will be mapped.

+ 3 - 2
dataladmetadatamodel/versionlist.py

@@ -85,8 +85,9 @@ class VersionList(MappableObject):
             version_record.path,
             version_record.element)
 
-    def get_versioned_elements(self
-                               ) -> Iterable[Tuple[str, Tuple[str, MetadataPath, MappableObject]]]:
+    @property
+    def versioned_elements(self
+                           ) -> Iterable[Tuple[str, Tuple[str, MetadataPath, MappableObject]]]:
         """
         Get an iterable of all versions and their records
         """

+ 1 - 1
tools/metadata_creator/mrrcreator.py

@@ -59,7 +59,7 @@ def create_metadata_root_record(mapper_family,
 
     mrr.save()
     mrr.dataset_level_metadata.purge()
-    mrr.file_tree.purge()
+    mrr._file_tree.purge()
     return mrr