[hg] galaxy 1512: The MetadataCollection object is now created o...

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[hg] galaxy 1512: The MetadataCollection object is now created o...

greg
details:   http://www.bx.psu.edu/hg/galaxy/rev/1e408bab8941
changeset: 1512:1e408bab8941
user:      Dan Blankenberg <[hidden email]>
date:      Tue Sep 16 15:23:23 2008 -0400
description:
The MetadataCollection object is now created only once per dataset object instance (and when datatype is changed), instead of each time dataset.metadata is called.

The 'no_value' attribute for a metadata element's spec is returned when the metadata element's value is None.

2 file(s) affected in this change:

lib/galaxy/datatypes/metadata.py
lib/galaxy/model/__init__.py

diffs (89 lines):

diff -r c3ce08879473 -r 1e408bab8941 lib/galaxy/datatypes/metadata.py
--- a/lib/galaxy/datatypes/metadata.py Tue Sep 16 14:26:14 2008 -0400
+++ b/lib/galaxy/datatypes/metadata.py Tue Sep 16 15:23:23 2008 -0400
@@ -151,9 +151,16 @@
     """
     def __init__(self, parent, spec):
         self.parent = parent
-        self.bunch = parent._metadata or dict()
         if spec is None: self.spec = MetadataSpecCollection()
         else: self.spec = spec
+        
+        #set default metadata values
+        if not self.parent._metadata:
+            self.parent._metadata = {}
+        for name, value in self.spec.items():
+            if name not in self.bunch:
+                self.bunch[name] = value.default
+        
     def __iter__(self):
         return self.bunch.__iter__()
     def get( self, key, default=None ):
@@ -168,19 +175,21 @@
     def __nonzero__(self):
         return self.bunch.__nonzero__()
     def __getattr__(self, name):
-        if self.bunch.get( name ):
-            return self.bunch.get( name )
+        if name == "bunch":
+            return self.parent._metadata
+        rval = self.bunch.get( name )
+        if rval is None:
+            rval = self.spec.get( name, None )
+            if rval:
+                rval = rval.no_value
+        return rval
+    def __setattr__(self, name, value):
+        if name in ["parent","spec"]:
+            self.__dict__[name] = value
+        elif name == "bunch":
+            self.parent._metadata = value
         else:
-            if self.spec.get(name, None):
-                return self.spec[name].default
-            else:
-                return None
-    def __setattr__(self, name, value):
-        if name in ["parent","bunch","spec"]:
-            self.__dict__[name] = value
-        else:
-            self.__dict__["bunch"][name] = value
-            self.bunch = self.parent._metadata = dict( self.bunch )
+            self.bunch[name] = value
 
 MetadataElement = Statement(MetadataElementSpec)
 
diff -r c3ce08879473 -r 1e408bab8941 lib/galaxy/model/__init__.py
--- a/lib/galaxy/model/__init__.py Tue Sep 16 14:26:14 2008 -0400
+++ b/lib/galaxy/model/__init__.py Tue Sep 16 15:23:23 2008 -0400
@@ -113,7 +113,7 @@
         self.peek = peek
         self.extension = extension
         self.designation = designation
-        self._metadata = metadata or dict()
+        self.metadata = metadata or dict()
         self.dbkey = dbkey
         self.deleted = deleted
         self.visible = visible
@@ -159,9 +159,9 @@
         return datatypes_registry.get_datatype_by_extension( self.extension )
 
     def get_metadata( self ):
-        if not self._metadata:
-            self._metadata = dict()
-        return MetadataCollection( self, self.datatype.metadata_spec )
+        if not hasattr( self, '_metadata_collection' ):
+            self._metadata_collection = MetadataCollection( self, self.datatype.metadata_spec )
+        return self._metadata_collection
     def set_metadata( self, bunch ):
         # Needs to accept a MetadataCollection, a bunch, or a dict
         self._metadata = dict( bunch.items() )
@@ -191,6 +191,8 @@
 
     def change_datatype( self, new_ext ):
         self.clear_associated_files()
+        if hasattr( self, '_metadata_collection' ):
+            del self._metadata_collection
         datatypes_registry.change_datatype( self, new_ext )
     def get_size( self ):
         """Returns the size of the data on disk"""