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

Source Code for Module omero.plugins.export

  1  #!/usr/bin/env python 
  2  """ 
  3     Startup plugin for command-line exporter 
  4   
  5     Copyright 2009 Glencoe Software, Inc. All rights reserved. 
  6     Use is subject to license terms supplied in LICENSE.txt 
  7   
  8  """ 
  9   
 10  import os 
 11  import sys 
 12  import array 
 13  from getopt import getopt, GetoptError 
 14   
 15  from omero.cli import Arguments, BaseControl, VERSION, OMERODIR 
 16  import omero.java 
 17   
18 -class ExportControl(BaseControl):
19
20 - def _run(self, args = []):
21 args = Arguments(args) 22 23 try: 24 options, args = getopt(args.args, "s:u:k:w:p:f:t:h") 25 if len(options) == 0 and len(args) == 0: 26 raise GetoptError("No arguments") 27 except GetoptError, (msg, opt): 28 self.help("Bad arguments") 29 self.ctx.die(0, "") 30 31 server = None 32 user = None 33 port = 4063 34 pasw = None 35 file = None 36 tiff = "TIFF" 37 key = None 38 for option, argument in options: 39 if option == "-u": 40 user = argument 41 elif option == "-w": 42 pasw = argument 43 elif option == "-s": 44 server = argument 45 elif option == "-p": 46 port = int(argument) 47 elif option == "-f": 48 file = argument 49 elif option == "-k": 50 key = argument 51 elif option == "-t": 52 tiff = argument 53 else: 54 self.ctx.out("Ignoring option: %s" % option) 55 56 if server is None: 57 server = self.ctx.input("Server:") 58 if key: 59 user = key 60 pasw = key 61 else: 62 if file is None and pasw is None: 63 self.ctx.die(3, "Password or key must be provided to send to stdout") 64 if user is None and key is None: 65 user = self.ctx.input("Username:") 66 if pasw is None and key is None: 67 pasw = self.ctx.input("Password:", hidden = True) 68 if tiff not in ["TIFF","XML"]: 69 self.ctx.die(1, "Only TIFF and XML supported") 70 if file is None: 71 self.ctx.die(7, "No file specified") 72 if os.path.exists(str(file)): 73 self.ctx.die(2, "%s already exists" % file) 74 75 if len(args) > 1: 76 self.ctx.die(4, "Currently only a single image supported") 77 78 klass, id = args[0].split(":") 79 images = [] 80 if klass == "Image": 81 images.append(id) 82 else: 83 self.ctx.die(5, "Can't add type: %s" % klass) 84 85 c = self.ctx.conn({"omero.host":server, "omero.user":user,"omero.pass":pasw}) 86 e = None 87 88 if file: 89 f = open(file, "wb") 90 91 try: 92 e = c.getSession().createExporter() 93 try: 94 for img in images: 95 e.addImage(long(img)) 96 if tiff == "TIFF": 97 l = e.generateTiff() 98 else: 99 l = e.generateXml() 100 101 offset = 0 102 while True: 103 rv = e.read(offset, 1000*1000) 104 if not rv: 105 break 106 offset += len(rv) 107 if file: 108 f.write(rv) 109 else: 110 sys.stdout.buffer.write(rv) 111 112 finally: 113 if file: 114 f.close() 115 e.close() 116 finally: 117 c.closeSession() # FIXME
118 119
120 - def help(self, args = None):
121 self.ctx.out(""" 122 Usage: %s export [OPTION]... Image:<id> 123 Export OMERO image data. 124 125 Mandatory arguments: 126 -f Output file 127 128 Queried arguments: (if not provided, requires user input) 129 -s OMERO server hostname 130 -u OMERO experimenter name (username) 131 -k OMERO session key (can be used in place of -u and -w) 132 -w OMERO experimenter password (Requested if not provided) 133 134 Optional arguments: 135 -p OMERO server port [defaults to 4063] 136 -t Format: XML or TIFF Default: TIFF 137 -h Display this help and exit 138 139 ex. %s export -s localhost -u bart -w simpson -f image_50.ome.tiff Image:50 140 141 Report bugs to <ome-users@openmicroscopy.org.uk>""" % (sys.argv[0], sys.argv[0]))
142
143 - def __call__(self, *args):
144 args = Arguments(*args) 145 self._run(args)
146 147 try: 148 register("export", ExportControl) 149 except NameError: 150 ExportControl()._main() 151