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

Source Code for Module omero.plugins.import

  1  #!/usr/bin/env python 
  2  """ 
  3     Startup plugin for command-line importer. 
  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 subprocess, optparse, os, sys, signal, time 
 11  from omero.cli import BaseControl, CLI, OMERODIR 
 12  import omero.java 
 13   
 14  START_CLASS="ome.formats.importer.cli.CommandLineImporter" 
 15  TEST_CLASS="ome.formats.test.util.TestEngine" 
 16   
 17  HELP = """Run the Java-based command-line importer 
 18   
 19  This is a Python wrapper around the Java importer. Login is handled 
 20  by Python OmeroCli. To see more options, use "--javahelp". 
 21   
 22  Options marked with "**" are passed strictly to Java. If they interfere 
 23  with any of the Python arguments, you may need to end precede your arguments 
 24  with a "--". 
 25   
 26  Examples: 
 27   
 28    bin/omero import ~/Data/my_file.dv                    # Use current login 
 29    bin/omero import -- --debug=ALL ~/Data/my_file2.png   # Set Java debug 
 30   
 31  """ 
 32  TESTHELP = """Run the Importer TestEngine suite (devs-only)""" 
 33   
34 -class ImportControl(BaseControl):
35 36 COMMAND = [START_CLASS] 37
38 - def _configure(self, parser):
39 parser.add_argument("--javahelp", action="store_true", help="Show the Java help text") 40 parser.add_argument("---file", nargs="?", help="File for storing the standard out of the Java process") 41 parser.add_argument("---errs", nargs="?", help="File for storing the standard err of the Java process") 42 # The following arguments are strictly passed to Java 43 parser.add_argument("-a", dest="java_a", action="store_true", help="Archive original files (**)") 44 parser.add_argument("-f", dest="java_f", action="store_true", help="Display used files (**)") 45 parser.add_argument("-c", dest="java_c", action="store_true", help="Continue importing after errors (**)") 46 parser.add_argument("-l", dest="java_l", help="Use the list of readers rather than the default (**)", metavar="READER_FILE") 47 parser.add_argument("-d", dest="java_d", help="OMERO dataset Id to import image into (**)", metavar="DATASET_ID") 48 parser.add_argument("-r", dest="java_r", help="OMERO screen Id to import plate into (**)", metavar="SCREEN_ID") 49 parser.add_argument("-n", dest="java_n", help="Image name to use (**)", metavar="NAME") 50 parser.add_argument("-x", dest="java_x", help="Image description to use (**)", metavar="DESCRIPTION") 51 parser.add_argument("--report", action="store_true", dest="java_report", help="Report errors to the OME team (**)") 52 parser.add_argument("--upload", action="store_true", dest="java_upload", help="Upload broken files with report (**)") 53 parser.add_argument("--logs", action="store_true", dest="java_logs", help="Upload log file with report (**)") 54 parser.add_argument("--email", dest="java_email", help="Email for reported errors (**)", metavar="EMAIL") 55 parser.add_argument("--debug", dest="java_debug", help="Turn debug logging on (**; must be preceded by '--')",\ 56 choices=["ALL","DEBUG","ERROR","FATAL","INFO","TRACE","WARN"], metavar="LEVEL") 57 parser.add_argument("arg", nargs="*", help="Arguments to be passed to the Java process") 58 parser.set_defaults(func=self.importer)
59 60
61 - def importer(self, args):
62 63 client_dir = self.ctx.dir / "lib" / "client" 64 log4j = "-Dlog4j.configuration=log4j-cli.properties" 65 classpath = [ file.abspath() for file in client_dir.files("*.jar") ] 66 xargs = [ log4j, "-Xmx1024M", "-cp", os.pathsep.join(classpath) ] 67 68 # Here we permit passing ---file=some_output_file in order to 69 # facilitate the omero.util.import_candidates.as_dictionary 70 # call. This may not always be necessary. 71 out = args.file 72 err = args.errs 73 74 if out: 75 out = open(out, "w") 76 if err: 77 err = open(err, "w") 78 79 login_args = [] 80 if args.javahelp: 81 login_args.append("-h") 82 83 if "-h" not in login_args and "-f" not in login_args and not args.java_f: 84 client = self.ctx.conn(args) 85 srv = client.getProperty("omero.host") 86 prt = client.getProperty("omero.port") 87 login_args.extend(["-s", srv]) 88 login_args.extend(["-p", prt]) 89 login_args.extend(["-k", client.getSessionId()]) 90 91 # Due to the use of "--" some of these like debug 92 # will never be filled out. But for completeness 93 # sake, we include them here. 94 java_args = { 95 "java_a": "-a", 96 "java_f": "-f", 97 "java_c": "-c", 98 "java_l": "-l", 99 "java_d": "-d", 100 "java_r": "-r", 101 "java_r": "-r", 102 "java_n": "-n", 103 "java_x": "-x", 104 "java_report": "--report", 105 "java_upload": "--upload", 106 "java_logs": "--logs", 107 "java_email": "--email", 108 "java_debug": "--debug" } 109 110 for attr_name, arg_name in java_args.items(): 111 arg_value = getattr(args, attr_name) 112 if arg_value: 113 login_args.append(arg_name) 114 if isinstance(arg_value, (str, unicode)): 115 login_args.append(arg_value) 116 117 a = self.COMMAND + login_args + args.arg 118 p = omero.java.popen(a, debug=False, xargs = xargs, stdout=out, stderr=err) 119 self.ctx.rv = p.wait()
120
121 -class TestEngine(ImportControl):
122 COMMAND = [ TEST_CLASS ]
123 124 try: 125 register("import", ImportControl, HELP) 126 register("testengine", TestEngine, TESTHELP) 127 except NameError: 128 if __name__ == "__main__": 129 cli = CLI() 130 cli.register("import", ImportControl, HELP) 131 cli.register("testengine", TestEngine, TESTHELP) 132 cli.invoke(sys.argv[1:]) 133