commit/galaxy-central: jmchilton: Refactor duplicated get_display_name code out of galaxy.model.
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/6b627e15f18b/ Changeset: 6b627e15f18b User: jmchilton Date: 2014-02-02 19:47:15 Summary: Refactor duplicated get_display_name code out of galaxy.model. Also added test cases. Need to use this downstream in for dataset collections. Affected #: 2 files diff -r 89d7cf67241f36e5f2c4de0aa31328fb7823e7d5 -r 6b627e15f18b13a489ca4e4343703931d9333021 lib/galaxy/model/__init__.py --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -71,6 +71,19 @@ datatypes_registry = d_registry +class HasName: + + def get_display_name( self ): + """ + These objects have a name attribute can be either a string or a unicode + object. If string, convert to unicode object assuming 'utf-8' format. + """ + name = self.name + if isinstance(name, str): + name = unicode(name, 'utf-8') + return name + + class User( object, Dictifiable ): use_pbkdf2 = True """ @@ -716,7 +729,7 @@ self.user = user self.group = group -class History( object, Dictifiable, UsesAnnotations ): +class History( object, Dictifiable, UsesAnnotations, HasName ): dict_collection_visible_keys = ( 'id', 'name', 'published', 'deleted' ) dict_element_visible_keys = ( 'id', 'name', 'published', 'deleted', 'genome_build', 'purged' ) @@ -823,16 +836,6 @@ # This needs to be a list return [ hda for hda in self.datasets if not hda.dataset.deleted ] - def get_display_name( self ): - """ - History name can be either a string or a unicode object. - If string, convert to unicode object assuming 'utf-8' format. - """ - history_name = self.name - if isinstance(history_name, str): - history_name = unicode(history_name, 'utf-8') - return history_name - def to_dict( self, view='collection', value_mapper = None ): # Get basic value. @@ -1604,7 +1607,7 @@ return msg -class HistoryDatasetAssociation( DatasetInstance, Dictifiable, UsesAnnotations ): +class HistoryDatasetAssociation( DatasetInstance, Dictifiable, UsesAnnotations, HasName ): """ Resource class that creates a relation between a dataset and a user history. """ @@ -1734,17 +1737,6 @@ for assoc in self.implicitly_converted_parent_datasets: assoc.clear( purge = purge, delete_dataset = False ) - def get_display_name( self ): - """ - Return the name of this HDA in either ascii or utf-8 encoding. - """ - # Name can be either a string or a unicode object. - # If string, convert to unicode object assuming 'utf-8' format. - hda_name = self.name - if isinstance(hda_name, str): - hda_name = unicode(hda_name, 'utf-8') - return hda_name - def get_access_roles( self, trans ): """ Return The access roles associated with this HDA's dataset. @@ -1878,7 +1870,7 @@ self.subset = subset self.location = location -class Library( object, Dictifiable ): +class Library( object, Dictifiable, HasName ): permitted_actions = get_permitted_actions( filter='LIBRARY' ) dict_collection_visible_keys = ( 'id', 'name' ) dict_element_visible_keys = ( 'id', 'deleted', 'name', 'description', 'synopsis', 'root_folder_id' ) @@ -1939,15 +1931,8 @@ if lp.action == trans.app.security_agent.permitted_actions.LIBRARY_ACCESS.action: roles.append( lp.role ) return roles - def get_display_name( self ): - # Library name can be either a string or a unicode object. If string, - # convert to unicode object assuming 'utf-8' format. - name = self.name - if isinstance( name, str ): - name = unicode( name, 'utf-8' ) - return name -class LibraryFolder( object, Dictifiable ): +class LibraryFolder( object, Dictifiable, HasName ): dict_element_visible_keys = ( 'id', 'parent_id', 'name', 'description', 'item_count', 'genome_build', 'update_time' ) def __init__( self, name=None, description=None, item_count=0, order_id=None ): self.name = name or "Unnamed folder" @@ -2012,13 +1997,7 @@ def activatable_library_datasets( self ): # This needs to be a list return [ ld for ld in self.datasets if ld.library_dataset_dataset_association and not ld.library_dataset_dataset_association.dataset.deleted ] - def get_display_name( self ): - # Library folder name can be either a string or a unicode object. If string, - # convert to unicode object assuming 'utf-8' format. - name = self.name - if isinstance( name, str ): - name = unicode( name, 'utf-8' ) - return name + def to_dict( self, view='collection' ): rval = super( LibraryFolder, self ).to_dict( view=view ) info_association, inherited = self.get_info_association() @@ -2129,7 +2108,7 @@ rval['metadata_' + name] = val return rval -class LibraryDatasetDatasetAssociation( DatasetInstance ): +class LibraryDatasetDatasetAssociation( DatasetInstance, HasName ): def __init__( self, copied_from_history_dataset_association=None, copied_from_library_dataset_dataset_association=None, @@ -2304,15 +2283,6 @@ def templates_json( self, use_name=False ): return json.dumps( self.templates_dict( use_name=use_name ) ) - def get_display_name( self ): - """ - LibraryDatasetDatasetAssociation name can be either a string or a unicode object. - If string, convert to unicode object assuming 'utf-8' format. - """ - ldda_name = self.name - if isinstance( ldda_name, str ): - ldda_name = unicode( ldda_name, 'utf-8' ) - return ldda_name class ExtendedMetadata( object ): def __init__(self, data): diff -r 89d7cf67241f36e5f2c4de0aa31328fb7823e7d5 -r 6b627e15f18b13a489ca4e4343703931d9333021 test/unit/test_galaxy_mapping.py --- a/test/unit/test_galaxy_mapping.py +++ b/test/unit/test_galaxy_mapping.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import unittest import galaxy.model.mapping as mapping @@ -95,6 +96,35 @@ self.persist( visualization ) persist_and_check_rating( model.VisualizationRatingAssociation, visualization=visualization ) + def test_display_name( self ): + + def assert_display_name_converts_to_unicode( item, name ): + assert not isinstance( item.name, unicode ) + assert isinstance( item.get_display_name(), unicode ) + assert item.get_display_name() == name + + ldda = self.model.LibraryDatasetDatasetAssociation( name='ldda_name' ) + assert_display_name_converts_to_unicode( ldda, 'ldda_name' ) + + hda = self.model.HistoryDatasetAssociation( name='hda_name' ) + assert_display_name_converts_to_unicode( hda, 'hda_name' ) + + history = self.model.History( name='history_name' ) + assert_display_name_converts_to_unicode( history, 'history_name' ) + + library = self.model.Library( name='library_name' ) + assert_display_name_converts_to_unicode( library, 'library_name' ) + + library_folder = self.model.LibraryFolder( name='library_folder' ) + assert_display_name_converts_to_unicode( library_folder, 'library_folder' ) + + history = self.model.History( + name=u'Hello₩◎ґʟⅾ' + ) + assert isinstance( history.name, unicode ) + assert isinstance( history.get_display_name(), unicode ) + assert history.get_display_name() == u'Hello₩◎ґʟⅾ' + def test_tags( self ): model = self.model Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.
participants (1)
-
commits-noreply@bitbucket.org