| 
0
 | 
     1 import os
 | 
| 
 | 
     2 import zipfile
 | 
| 
 | 
     3 from galaxy.datatypes.binary import Binary
 | 
| 
 | 
     4 from galaxy.datatypes.data import Text
 | 
| 
 | 
     5 
 | 
| 
 | 
     6 class Refpkg(Text):
 | 
| 
 | 
     7     composite_type = "basic"
 | 
| 
 | 
     8 
 | 
| 
 | 
     9     def __init__(self, **kwd):
 | 
| 
 | 
    10         Text.__init__(self, **kwd)
 | 
| 
 | 
    11         self.add_composite_file("CONTENTS.json")
 | 
| 
 | 
    12 
 | 
| 
 | 
    13     def get_mime(self):
 | 
| 
 | 
    14         return "application/json"
 | 
| 
 | 
    15 
 | 
| 
 | 
    16 class RefpkgZip(Binary):
 | 
| 
 | 
    17     file_ext = "refpkg.zip"
 | 
| 
 | 
    18 
 | 
| 
 | 
    19     def __init__(self, **kwd):
 | 
| 
 | 
    20         Binary.__init__(self, **kwd)
 | 
| 
 | 
    21 
 | 
| 
 | 
    22     def sniff(self, filename):
 | 
| 
 | 
    23         if not zipfile.is_zipfile(filename):
 | 
| 
 | 
    24             return False
 | 
| 
 | 
    25         contains_contents_file = False
 | 
| 
 | 
    26         zip_file = zipfile.ZipFile(filename, "r")
 | 
| 
 | 
    27         for name in zip_file.namelist():
 | 
| 
 | 
    28             if os.path.basename(name) == "CONTENTS.json":
 | 
| 
 | 
    29                 contains_contents_file = True
 | 
| 
 | 
    30                 break
 | 
| 
 | 
    31         zip_file.close()
 | 
| 
 | 
    32         if not contains_contents_file:
 | 
| 
 | 
    33             return False
 | 
| 
 | 
    34         return True
 | 
| 
 | 
    35 
 | 
| 
 | 
    36     def get_mime(self):
 | 
| 
 | 
    37         return "application/zip"
 | 
| 
 | 
    38 
 | 
| 
 | 
    39 class OfficeXlsx(Binary):
 | 
| 
 | 
    40     file_ext = "xlsx"
 | 
| 
 | 
    41 
 | 
| 
 | 
    42     def __init__(self, **kwd):
 | 
| 
 | 
    43         Binary.__init__(self, **kwd)
 | 
| 
 | 
    44 
 | 
| 
 | 
    45     # TODO: this should check for an xl/ directory also
 | 
| 
 | 
    46     def sniff(self, filename):
 | 
| 
 | 
    47         if not zipfile.is_zipfile(filename):
 | 
| 
 | 
    48             return False
 | 
| 
 | 
    49         contains_contents_file = False
 | 
| 
 | 
    50         zip_file = zipfile.ZipFile(filename, "r")
 | 
| 
 | 
    51         for name in zip_file.namelist():
 | 
| 
 | 
    52             if os.path.basename(name) == "[Content_Types].xml":
 | 
| 
 | 
    53                 contains_contents_file = True
 | 
| 
 | 
    54                 break
 | 
| 
 | 
    55         zip_file.close()
 | 
| 
 | 
    56         if not contains_contents_file:
 | 
| 
 | 
    57             return False
 | 
| 
 | 
    58         return True
 | 
| 
 | 
    59 
 | 
| 
 | 
    60     def get_mime(self):
 | 
| 
 | 
    61         return "application/zip"
 | 
| 
 | 
    62 
 | 
| 
 | 
    63 Binary.register_sniffable_binary_format("refpkg.zip", "refpkg.zip", RefpkgZip)
 | 
| 
 | 
    64 Binary.register_sniffable_binary_format("xlsx", "xlsx", OfficeXlsx)
 |