From c8469b7d0e788eb0a1b4e80b686eebfd5a3e6d11 Mon Sep 17 00:00:00 2001 From: Remi Marenco Date: Thu, 18 Feb 2016 13:01:07 -0500 Subject: [PATCH] Refactor the Html Datatype Class into text.py instead of images.py --- config/datatypes_conf.xml.sample | 4 +- lib/galaxy/datatypes/assembly.py | 2 +- lib/galaxy/datatypes/data.py | 2 +- lib/galaxy/datatypes/genetics.py | 2 +- lib/galaxy/datatypes/images.py | 41 ++----------------- lib/galaxy/datatypes/ngsindex.py | 2 +- lib/galaxy/datatypes/registry.py | 3 +- lib/galaxy/datatypes/text.py | 40 ++++++++++++++++++ .../webapps/galaxy/controllers/dataset.py | 2 +- 9 files changed, 53 insertions(+), 45 deletions(-) diff --git a/config/datatypes_conf.xml.sample b/config/datatypes_conf.xml.sample index 9495f1637fd..908575724fc 100644 --- a/config/datatypes_conf.xml.sample +++ b/config/datatypes_conf.xml.sample @@ -108,7 +108,7 @@ - + @@ -494,7 +494,7 @@ - + diff --git a/lib/galaxy/datatypes/assembly.py b/lib/galaxy/datatypes/assembly.py index e8ec95525cf..b93611eab82 100644 --- a/lib/galaxy/datatypes/assembly.py +++ b/lib/galaxy/datatypes/assembly.py @@ -11,7 +11,7 @@ import os import re import sys from galaxy.datatypes import sequence -from galaxy.datatypes.images import Html +from galaxy.datatypes.text import Html from galaxy.datatypes.metadata import MetadataElement log = logging.getLogger(__name__) diff --git a/lib/galaxy/datatypes/data.py b/lib/galaxy/datatypes/data.py index af276616b32..5036448cf2c 100644 --- a/lib/galaxy/datatypes/data.py +++ b/lib/galaxy/datatypes/data.py @@ -364,7 +364,7 @@ class Data( object ): if not os.path.exists( data.file_name ): raise paste.httpexceptions.HTTPNotFound( "File Not Found (%s)." % data.file_name ) max_peek_size = 1000000 # 1 MB - if isinstance(data.datatype, datatypes.images.Html): + if isinstance(data.datatype, datatypes.text.Html): max_peek_size = 10000000 # 10 MB for html preview = util.string_as_bool( preview ) if not preview or isinstance(data.datatype, datatypes.images.Image) or os.stat( data.file_name ).st_size < max_peek_size: diff --git a/lib/galaxy/datatypes/genetics.py b/lib/galaxy/datatypes/genetics.py index 161849f2591..71f6cad7341 100644 --- a/lib/galaxy/datatypes/genetics.py +++ b/lib/galaxy/datatypes/genetics.py @@ -19,7 +19,7 @@ import urllib from cgi import escape from galaxy.datatypes import metadata -from galaxy.datatypes.images import Html +from galaxy.datatypes.text import Html from galaxy.datatypes.metadata import MetadataElement from galaxy.datatypes.tabular import Tabular from galaxy.util import nice_size diff --git a/lib/galaxy/datatypes/images.py b/lib/galaxy/datatypes/images.py index 165f4dcd4fb..ea2f78197da 100644 --- a/lib/galaxy/datatypes/images.py +++ b/lib/galaxy/datatypes/images.py @@ -8,6 +8,7 @@ import zipfile from urllib import quote_plus from galaxy.datatypes.binary import Binary +from galaxy.datatypes.text import Html as HtmlFromText from galaxy.datatypes.sniff import get_headers from galaxy.datatypes.util.image_util import check_image_type from galaxy.util import nice_size @@ -297,43 +298,9 @@ class Gmaj( data.Data ): return True -class Html( data.Text ): - """Class describing an html file""" - edam_format = "format_2331" - file_ext = "html" - - def set_peek( self, dataset, is_multi_byte=False ): - if not dataset.dataset.purged: - dataset.peek = "HTML file" - dataset.blurb = nice_size( dataset.get_size() ) - else: - dataset.peek = 'file does not exist' - dataset.blurb = 'file purged from disk' - - def get_mime(self): - """Returns the mime type of the datatype""" - return 'text/html' - - def sniff( self, filename ): - """ - Determines whether the file is in html format - - >>> from galaxy.datatypes.sniff import get_test_fname - >>> fname = get_test_fname( 'complete.bed' ) - >>> Html().sniff( fname ) - False - >>> fname = get_test_fname( 'file.html' ) - >>> Html().sniff( fname ) - True - """ - headers = get_headers( filename, None ) - try: - for i, hdr in enumerate(headers): - if hdr and hdr[0].lower().find( '' ) >= 0: - return True - return False - except: - return True +class Html( HtmlFromText ): + """Deprecated class. This class should not be used anymore, but the galaxy.datatypes.text:Html one. + This is for backwards compatibilities only.""" class Laj( data.Text ): diff --git a/lib/galaxy/datatypes/ngsindex.py b/lib/galaxy/datatypes/ngsindex.py index 3866365b7fe..1239dcb2da7 100644 --- a/lib/galaxy/datatypes/ngsindex.py +++ b/lib/galaxy/datatypes/ngsindex.py @@ -5,7 +5,7 @@ import os import logging from metadata import MetadataElement -from images import Html +from text import Html log = logging.getLogger(__name__) diff --git a/lib/galaxy/datatypes/registry.py b/lib/galaxy/datatypes/registry.py index e16a02637bc..ade65e8655e 100644 --- a/lib/galaxy/datatypes/registry.py +++ b/lib/galaxy/datatypes/registry.py @@ -17,6 +17,7 @@ from . import xml from . import coverage from . import tracks from . import binary +from . import text import galaxy.util from galaxy.util.odict import odict from .display_applications.application import DisplayApplication @@ -746,7 +747,7 @@ class Registry( object ): sequence.Fasta(), sequence.Fastq(), interval.Wiggle(), - images.Html(), + text.Html(), sequence.Axt(), interval.Bed(), interval.CustomTrack(), diff --git a/lib/galaxy/datatypes/text.py b/lib/galaxy/datatypes/text.py index b8688a79f9e..0c4b4697e98 100644 --- a/lib/galaxy/datatypes/text.py +++ b/lib/galaxy/datatypes/text.py @@ -12,11 +12,51 @@ import tempfile from galaxy.datatypes.data import get_file_peek, Text from galaxy.datatypes.metadata import MetadataElement, MetadataParameter +from galaxy.datatypes.sniff import get_headers from galaxy.util import nice_size, string_as_bool log = logging.getLogger(__name__) +class Html( Text ): + """Class describing an html file""" + edam_format = "format_2331" + file_ext = "html" + + def set_peek( self, dataset, is_multi_byte=False ): + if not dataset.dataset.purged: + dataset.peek = "HTML file" + dataset.blurb = nice_size( dataset.get_size() ) + else: + dataset.peek = 'file does not exist' + dataset.blurb = 'file purged from disk' + + def get_mime(self): + """Returns the mime type of the datatype""" + return 'text/html' + + def sniff( self, filename ): + """ + Determines whether the file is in html format + + >>> from galaxy.datatypes.sniff import get_test_fname + >>> fname = get_test_fname( 'complete.bed' ) + >>> Html().sniff( fname ) + False + >>> fname = get_test_fname( 'file.html' ) + >>> Html().sniff( fname ) + True + """ + headers = get_headers( filename, None ) + try: + for i, hdr in enumerate(headers): + if hdr and hdr[0].lower().find( '' ) >= 0: + return True + return False + except: + return True + + class Json( Text ): edam_format = "format_3464" file_ext = "json" diff --git a/lib/galaxy/webapps/galaxy/controllers/dataset.py b/lib/galaxy/webapps/galaxy/controllers/dataset.py index ba531999960..dcae7335611 100644 --- a/lib/galaxy/webapps/galaxy/controllers/dataset.py +++ b/lib/galaxy/webapps/galaxy/controllers/dataset.py @@ -593,7 +593,7 @@ class DatasetInterface( BaseUIController, UsesAnnotations, UsesItemRatings, Uses # If data is binary or an image, stream without template; otherwise, use display template. # TODO: figure out a way to display images in display template. - if isinstance(dataset.datatype, datatypes.binary.Binary) or isinstance(dataset.datatype, datatypes.images.Image) or isinstance(dataset.datatype, datatypes.images.Html): + if isinstance(dataset.datatype, datatypes.binary.Binary) or isinstance(dataset.datatype, datatypes.images.Image) or isinstance(dataset.datatype, datatypes.text.Html): trans.response.set_content_type( dataset.get_mime() ) return open( dataset.file_name ) else: