Package omero :: Package plugins :: Module upload
[hide private]
[frames] | no frames]

Source Code for Module omero.plugins.upload

  1  #!/usr/bin/env python 
  2  """ 
  3     upoad plugin 
  4   
  5     Plugin read by omero.cli.Cli during initialization. The method(s) 
  6     defined here will be added to the Cli class for later use. 
  7   
  8     Copyright 2007 Glencoe Software, Inc. All rights reserved. 
  9     Use is subject to license terms supplied in LICENSE.txt 
 10   
 11  """ 
 12   
 13  from omero.cli import BaseControl 
 14  import omero.util.originalfileutils; 
 15  import omero; 
 16  import omero.rtypes 
 17  from omero.rtypes import rlong 
 18  from omero.rtypes import rint 
 19  from omero.rtypes import rstring 
 20  from omero.rtypes import rdouble 
 21  from omero.rtypes import rfloat 
 22   
 23   
 24  try:  
 25          import hashlib  
 26          hash_sha1 = hashlib.sha1  
 27  except:  
 28          import sha  
 29          hash_sha1 = sha.new  
 30   
31 -class UploadControl(BaseControl):
32
33 - def help(self, args = None):
34 return \ 35 """ 36 Syntax: %(program_name)s upload <filename> [1..n] 37 Upload the given files to omero. 38 39 Syntax: %(program_name)s upload script 40 Upload default scripts defined in default scripts to the server. 41 42 Syntax: %(program_name)s upload pytable <filename> [1..n] 43 Upload the given files to pytables in omero. 44 45 """
46 SCRIPT_ARG='scripts'; 47 PYTABLE_ARG='pytable'; 48 FILE_ARG='files'; 49
50 - def calcSha1(self, filename):
51 fileHandle = open(filename) 52 h = hash_sha1() 53 h.update(fileHandle.read()) 54 hash = h.hexdigest() 55 fileHandle.close() 56 return hash;
57
58 - def createOriginalFile(self, id, name, filename):
59 file = open(filename, 'rb') 60 if(id != None): 61 ofile = omero.model.OriginalFileI(rlong(id)) 62 else: 63 ofile = omero.model.OriginalFileI(); 64 try: 65 size = os.path.getsize(file.name) 66 ofile.size = rlong(size) 67 ofile.sha1 = rstring(self.calcSha1(file.name)) 68 ofile.name = rstring(name) 69 ofile.path = rstring(os.path.abspath(file.name)); 70 fmt = omero.util.originalfileutils.getFormat(filename); 71 ofile.format = omero.model.FormatI(); 72 ofile.format.value = rstring(fmt[1]); 73 up = self.client.getSession().getUpdateService() 74 ofile = up.saveAndReturnObject(ofile) 75 finally: 76 file.close(); 77 return ofile;
78
79 - def uploadFile(self, filename, originalFile = None):
80 format = omero.util.originalfileutils.getFormat(filename); 81 omeroFormat = format[1]; 82 if(format[0]==omero.util.originalfileutils.IMPORTER): 83 self.ctx.out("This file should be imported using omero import"); 84 return self.client.upload(filename, filename, filename, omeroFormat, originalFile)
85
86 - def uploadFromString(self, string, originalFile):
87 prx = self.client.getSession().createRawFileStore() 88 prx.setFileId(originalFile.id.val) 89 strlen = len(string); 90 prx.write(string, 0, strlen) 91 prx.close()
92
93 - def readCommandArgs(self, commandline):
94 script = False; 95 pytable = False; 96 files = list(); 97 for arg in commandline: 98 if arg in (self.SCRIPT_ARG): 99 script = True; 100 elif arg in (self.PYTABLE_ARG): 101 pytable = True; 102 else: 103 files.append(arg); 104 return {self.SCRIPT_ARG:script, self.PYTABLE_ARG:pytable, self.FILE_ARG:files}
105
106 - def returnSource(self, filename):
107 if(filename[len(filename)-3:] == 'pyc'): 108 return filename[:len(filename)-1] 109 return filename;
110
111 - def uploadDefaultScripts(self, args):
112 import defaultscripts; 113 scripts = defaultscripts.defaultscripts; 114 for id in scripts: 115 script = scripts[id]; 116 filename = ""; 117 try: 118 importedScript = __import__(script); 119 filename = self.returnSource(importedScript.__file__); 120 except: 121 raise Exception("Script: " + script + " does not exist"); 122 if not filename: 123 raise Exception("Non-null filename must be provided") 124 125 if not os.path.exists(filename): 126 raise Exception("File does not exist: " + filename) 127 128 originalFile = self.createOriginalFile(None, script, filename); 129 self.uploadFile(filename, originalFile);
130
131 - def uploadFromCommandline(self, commandline):
132 fileList = commandline[self.FILE_ARG]; 133 for file in fileList: 134 obj = self.uploadFile(file); 135 self.ctx.out("Uploaded %s as " % file + str(obj.id.val))
136 137
138 - def __call__(self, *args):
139 import omero 140 args = Arguments(*args) 141 self.client = self.ctx.conn() 142 argMap = self.readCommandArgs(args); 143 # if(argMap[self.SCRIPT_ARG]==True): # and argMap[self.PYTABLE_ARG] == True): 144 # raise ClientError("upload can only be used with %s or %s arguments" % (self.SCRIPT_ARG, self.PYTABLE_ARG)); 145 146 if(argMap[self.SCRIPT_ARG]): 147 self.uploadDefaultScripts(argMap); 148 else: 149 self.uploadFromCommandline(argMap);
150 151 try: 152 register("upload", UploadControl) 153 except NameError: 154 UploadControl()._main() 155