root/galaxy-central/lib/galaxy/datatypes/images.py

リビジョン 2, 6.7 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1"""
2Image classes
3"""
4
5import data
6import logging
7from galaxy.datatypes.metadata import MetadataElement
8from galaxy.datatypes import metadata
9from galaxy.datatypes.sniff import *
10from urllib import urlencode, quote_plus
11import zipfile
12import os, subprocess, tempfile
13
14log = logging.getLogger(__name__)
15
16class Image( data.Data ):
17    """Class describing an image"""
18    def set_peek( self, dataset, is_multi_byte=False ):
19        if not dataset.dataset.purged:
20            dataset.peek = 'Image in %s format' % dataset.extension
21            dataset.blurb = data.nice_size( dataset.get_size() )
22        else:
23            dataset.peek = 'file does not exist'
24            dataset.blurb = 'file purged from disk'
25
26class Pdf( Image ):
27    def sniff(self, filename):
28        """Determine if the file is in pdf format.
29        """
30        headers = get_headers(filename, None, 1)
31        try:
32            if headers[0][0].startswith("%PDF"):
33                return True
34            else:
35                return False
36        except IndexError:
37            return False
38
39def create_applet_tag_peek( class_name, archive, params ):
40    text = """
41<!--[if !IE]>-->
42<object classid="java:%s"
43      type="application/x-java-applet"
44      height="30" width="200" align="center" >
45      <param name="archive" value="%s"/>""" % ( class_name, archive )
46    for name, value in params.iteritems():
47        text += """<param name="%s" value="%s"/>""" % ( name, value )
48    text += """
49<!--<![endif]-->
50<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
51        height="30" width="200" >
52        <param name="code" value="%s" />
53        <param name="archive" value="%s"/>""" % ( class_name, archive )
54    for name, value in params.iteritems():
55        text += """<param name="%s" value="%s"/>""" % ( name, value )
56    text += """</object>
57<!--[if !IE]>-->
58</object>
59<!--<![endif]-->
60"""
61    return """<div><p align="center">%s</p></div>""" % text
62
63class Gmaj( data.Data ):
64    """Class describing a GMAJ Applet"""
65    file_ext = "gmaj.zip"
66    copy_safe_peek = False
67    def set_peek( self, dataset, is_multi_byte=False ):
68        if not dataset.dataset.purged:
69            if hasattr( dataset, 'history_id' ):
70                params = {
71                "bundle":"display?id=%s&tofile=yes&toext=.zip" % dataset.id,
72                "buttonlabel": "Launch GMAJ",
73                "nobutton": "false",
74                "urlpause" :"100",
75                "debug": "false",
76                "posturl": quote_plus( "history_add_to?%s" % "&".join( [ "%s=%s" % ( key, value ) for key, value in { 'history_id': dataset.history_id, 'ext': 'maf', 'name': 'GMAJ Output on data %s' % dataset.hid, 'info': 'Added by GMAJ', 'dbkey': dataset.dbkey, 'copy_access_from': dataset.id }.items() ] ) )
77                }
78                class_name = "edu.psu.bx.gmaj.MajApplet.class"
79                archive = "/static/gmaj/gmaj.jar"
80                dataset.peek = create_applet_tag_peek( class_name, archive, params )
81                dataset.blurb = 'GMAJ Multiple Alignment Viewer'
82            else:
83                dataset.peek = "After you add this item to your history, you will be able to launch the GMAJ applet."
84                dataset.blurb = 'GMAJ Multiple Alignment Viewer'
85        else:
86            dataset.peek = 'file does not exist'
87            dataset.blurb = 'file purged from disk'
88    def display_peek(self, dataset):
89        try:
90            return dataset.peek
91        except:
92            return "peek unavailable"
93    def get_mime(self):
94        """Returns the mime type of the datatype"""
95        return 'application/zip'
96    def sniff(self, filename):
97        """
98        NOTE: the sniff.convert_newlines() call in the upload utility will keep Gmaj data types from being
99        correctly sniffed, but the files can be uploaded (they'll be sniffed as 'txt').  This sniff function
100        is here to provide an example of a sniffer for a zip file.
101        """
102        if not zipfile.is_zipfile( filename ):
103            return False
104        contains_gmaj_file = False
105        zip_file = zipfile.ZipFile(filename, "r")
106        for name in zip_file.namelist():
107            if name.split(".")[1].strip().lower() == 'gmaj':
108                contains_gmaj_file = True
109                break
110        zip_file.close()
111        if not contains_gmaj_file:
112            return False
113        return True
114           
115class Html( data.Text ):
116    """Class describing an html file"""
117    file_ext = "html"
118
119    def set_peek( self, dataset, is_multi_byte=False ):
120        if not dataset.dataset.purged:
121            dataset.peek = "HTML file"
122            dataset.blurb = data.nice_size( dataset.get_size() )
123        else:
124            dataset.peek = 'file does not exist'
125            dataset.blurb = 'file purged from disk'
126    def get_mime(self):
127        """Returns the mime type of the datatype"""
128        return 'text/html'
129    def sniff( self, filename ):
130        """
131        Determines whether the file is in html format
132
133        >>> fname = get_test_fname( 'complete.bed' )
134        >>> Html().sniff( fname )
135        False
136        >>> fname = get_test_fname( 'file.html' )
137        >>> Html().sniff( fname )
138        True
139        """
140        headers = get_headers( filename, None )
141        try:
142            for i, hdr in enumerate(headers):
143                if hdr and hdr[0].lower().find( '<html>' ) >=0:
144                    return True
145            return False
146        except:
147            return True
148
149class Laj( data.Text ):
150    """Class describing a LAJ Applet"""
151    file_ext = "laj"
152    copy_safe_peek = False
153
154    def set_peek( self, dataset, is_multi_byte=False ):
155        if not dataset.dataset.purged:
156            if hasattr( dataset, 'history_id' ):
157                params = {
158                "alignfile1": "display?id=%s" % dataset.id,
159                "buttonlabel": "Launch LAJ",
160                "title": "LAJ in Galaxy",
161                "posturl": quote_plus( "history_add_to?%s" % "&".join( [ "%s=%s" % ( key, value ) for key, value in { 'history_id': dataset.history_id, 'ext': 'lav', 'name': 'LAJ Output', 'info': 'Added by LAJ', 'dbkey': dataset.dbkey, 'copy_access_from': dataset.id }.items() ] ) ),
162                "noseq": "true"
163                }
164                class_name = "edu.psu.cse.bio.laj.LajApplet.class"
165                archive = "/static/laj/laj.jar"
166                dataset.peek = create_applet_tag_peek( class_name, archive, params )
167            else:
168                dataset.peek = "After you add this item to your history, you will be able to launch the LAJ applet."
169                dataset.blurb = 'LAJ Multiple Alignment Viewer'
170        else:
171            dataset.peek = 'file does not exist'
172            dataset.blurb = 'file purged from disk'
173    def display_peek(self, dataset):
174        try:
175            return dataset.peek
176        except:
177            return "peek unavailable"
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。