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

Source Code for Module omero.plugins.import

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  """ 
  4     Startup plugin for command-line importer. 
  5   
  6     Copyright 2009 Glencoe Software, Inc. All rights reserved. 
  7     Use is subject to license terms supplied in LICENSE.txt 
  8   
  9  """ 
 10   
 11  import os 
 12  import sys 
 13  from omero.cli import BaseControl, CLI 
 14  import omero.java 
 15   
 16  START_CLASS = "ome.formats.importer.cli.CommandLineImporter" 
 17  TEST_CLASS = "ome.formats.test.util.TestEngine" 
 18   
 19  HELP = """Run the Java-based command-line importer 
 20   
 21  This is a Python wrapper around the Java importer. Login is handled by Python 
 22  OmeroCli. To see more options, use "--javahelp". 
 23   
 24  Options marked with "**" are passed strictly to Java. If they interfere with 
 25  any of the Python arguments, you may need to end precede your arguments with a 
 26  "--". 
 27   
 28  Examples: 
 29   
 30    bin/omero import ~/Data/my_file.dv                    # Use current login 
 31    bin/omero import -- --debug=ALL ~/Data/my_file2.png   # Set Java debug 
 32   
 33  """ 
 34  TESTHELP = """Run the Importer TestEngine suite (devs-only)""" 
 35   
 36   
37 -class ImportControl(BaseControl):
38 39 COMMAND = [START_CLASS] 40
41 - def _configure(self, parser):
42 parser.add_argument( 43 "--javahelp", action="store_true", help="Show the Java help text") 44 parser.add_argument( 45 "---file", nargs="?", 46 help="File for storing the standard out of the Java process") 47 parser.add_argument( 48 "---errs", nargs="?", 49 help="File for storing the standard err of the Java process") 50 # The following arguments are strictly passed to Java 51 parser.add_argument( 52 "-a", dest="java_a", action="store_true", 53 help="Archive original files (**)") 54 parser.add_argument( 55 "-f", dest="java_f", action="store_true", 56 help="Display used files (**)") 57 parser.add_argument( 58 "-c", dest="java_c", action="store_true", 59 help="Continue importing after errors (**)") 60 parser.add_argument( 61 "-l", dest="java_l", 62 help="Use the list of readers rather than the default (**)", 63 metavar="READER_FILE") 64 parser.add_argument( 65 "-d", dest="java_d", 66 help="OMERO dataset Id to import image into (**)", 67 metavar="DATASET_ID") 68 parser.add_argument( 69 "-r", dest="java_r", 70 help="OMERO screen Id to import plate into (**)", 71 metavar="SCREEN_ID") 72 parser.add_argument( 73 "-n", dest="java_n", 74 help="Image name to use (**)", 75 metavar="NAME") 76 parser.add_argument( 77 "-x", dest="java_x", 78 help="Image description to use (**)", 79 metavar="DESCRIPTION") 80 parser.add_argument( 81 "--report", action="store_true", dest="java_report", 82 help="Report errors to the OME team (**)") 83 parser.add_argument( 84 "--upload", action="store_true", dest="java_upload", 85 help="Upload broken files with report (**)") 86 parser.add_argument( 87 "--logs", action="store_true", dest="java_logs", 88 help="Upload log file with report (**)") 89 parser.add_argument( 90 "--email", dest="java_email", 91 help="Email for reported errors (**)", metavar="EMAIL") 92 parser.add_argument( 93 "--debug", dest="java_debug", 94 help="Turn debug logging on (**; must be preceded by '--')", 95 choices=["ALL", "DEBUG", "ERROR", "FATAL", "INFO", "TRACE", 96 "WARN"], 97 metavar="LEVEL") 98 99 parser.add_argument( 100 "--annotation_ns", dest="java_ns", 101 help="Namespace to use for subsequent annotation") 102 parser.add_argument( 103 "--annotation_text", dest="java_text", 104 help="Content for a text annotation (requires namespace)") 105 parser.add_argument( 106 "--annotation_link", dest="java_link", 107 help="Comment annotation ID to link all images to") 108 109 parser.add_argument( 110 "arg", nargs="*", 111 help="Arguments to be passed to the Java process") 112 parser.set_defaults(func=self.importer) 113 parser.add_login_arguments()
114
115 - def importer(self, args):
116 117 client_dir = self.ctx.dir / "lib" / "client" 118 log4j = "-Dlog4j.configuration=log4j-cli.properties" 119 classpath = [file.abspath() for file in client_dir.files("*.jar")] 120 xargs = [log4j, "-Xmx1024M", "-cp", os.pathsep.join(classpath)] 121 122 # Here we permit passing ---file=some_output_file in order to 123 # facilitate the omero.util.import_candidates.as_dictionary 124 # call. This may not always be necessary. 125 out = args.file 126 err = args.errs 127 128 if out: 129 out = open(out, "w") 130 if err: 131 err = open(err, "w") 132 133 login_args = [] 134 if args.javahelp: 135 login_args.append("-h") 136 137 if "-h" not in login_args and "-f" not in login_args \ 138 and not args.java_f: 139 client = self.ctx.conn(args) 140 srv = client.getProperty("omero.host") 141 prt = client.getProperty("omero.port") 142 login_args.extend(["-s", srv]) 143 login_args.extend(["-p", prt]) 144 login_args.extend(["-k", client.getSessionId()]) 145 146 # Due to the use of "--" some of these like debug 147 # will never be filled out. But for completeness 148 # sake, we include them here. 149 java_args = { 150 "java_a": "-a", 151 "java_f": "-f", 152 "java_c": "-c", 153 "java_l": "-l", 154 "java_d": "-d", 155 "java_r": "-r", 156 "java_r": "-r", 157 "java_n": "-n", 158 "java_x": "-x", 159 "java_report": "--report", 160 "java_upload": "--upload", 161 "java_logs": "--logs", 162 "java_email": "--email", 163 "java_debug": "--debug", 164 "java_ns": "--annotation_ns", 165 "java_text": "--annotation_text", 166 "java_link": "--annotation_link" 167 } 168 169 for attr_name, arg_name in java_args.items(): 170 arg_value = getattr(args, attr_name) 171 if arg_value: 172 login_args.append(arg_name) 173 if isinstance(arg_value, (str, unicode)): 174 login_args.append(arg_value) 175 176 a = self.COMMAND + login_args + args.arg 177 p = omero.java.popen( 178 a, debug=False, xargs=xargs, stdout=out, stderr=err) 179 self.ctx.rv = p.wait()
180 181
182 -class TestEngine(ImportControl):
183 COMMAND = [TEST_CLASS]
184 185 try: 186 register("import", ImportControl, HELP) 187 register("testengine", TestEngine, TESTHELP) 188 except NameError: 189 if __name__ == "__main__": 190 cli = CLI() 191 cli.register("import", ImportControl, HELP) 192 cli.register("testengine", TestEngine, TESTHELP) 193 cli.invoke(sys.argv[1:]) 194