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

Source Code for Module omero.plugins.basics

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  """ 
  4     load, quit, version, help plugins 
  5   
  6     Plugin read by omero.cli.Cli during initialization. The method(s) 
  7     defined here will be added to the Cli class for later use. 
  8   
  9     The load plugin is used to read in files with omero cli commands 
 10     (omitting the omero). For example, 
 11   
 12     ./omero load some/file.osh 
 13   
 14     The help, quit, and version plugins are self-explanatory. 
 15   
 16     Copyright 2008 Glencoe Software, Inc. All rights reserved. 
 17     Use is subject to license terms supplied in LICENSE.txt 
 18   
 19  """ 
 20   
 21  import sys 
 22   
 23  from omero_ext.argparse import FileType 
 24   
 25  from omero.cli import BaseControl 
 26  from omero.cli import CLI 
 27  from omero.cli import VERSION 
 28   
 29   
30 -class QuitControl(BaseControl):
31
32 - def __call__(self, args):
33 self.ctx.exit("", newline=False)
34 35
36 -class VersionControl(BaseControl):
37
38 - def __call__(self, args):
39 self.ctx.out(VERSION)
40 41
42 -class LoadControl(BaseControl):
43
44 - def _configure(self, parser):
45 parser.add_argument("infile", nargs="*", type=FileType("r"), 46 default=[sys.stdin]) 47 parser.set_defaults(func=self.__call__)
48
49 - def __call__(self, args):
50 for file in args.infile: 51 self.ctx.dbg("Loading file %s" % file) 52 for line in file: 53 self.ctx.invoke(line)
54 55
56 -class ShellControl(BaseControl):
57
58 - def _configure(self, parser):
59 parser.add_argument( 60 "--login", action="store_true", 61 help="Logins in and sets the 'client' variable") 62 parser.add_argument("arg", nargs="*", help="Arguments for IPython.") 63 parser.set_defaults(func=self.__call__)
64
65 - def __call__(self, args):
66 """ 67 Copied from IPython embed-short example 68 """ 69 import logging 70 logging.basicConfig() 71 from omero.util.upgrade_check import UpgradeCheck 72 check = UpgradeCheck("shell") 73 check.run() 74 if check.isUpgradeNeeded(): 75 self.ctx.out("") 76 77 ns = {} 78 if args.login: 79 import omero 80 client = self.ctx.conn(args) 81 ns = {"client": client, "omero": omero} 82 83 try: 84 # IPython 0.11 (see #7112) 85 from IPython import embed 86 embed(user_ns=ns) 87 except ImportError: 88 from IPython.Shell import IPShellEmbed 89 ipshell = IPShellEmbed(args.arg) 90 ipshell(local_ns=ns)
91 92
93 -class HelpControl(BaseControl):
94 """ 95 Defined here since the background loading might be too 96 slow to have all help available 97 """ 98
99 - def _configure(self, parser):
100 self.__parser__ = parser # For formatting later 101 parser.set_defaults(func=self.__call__) 102 parser.add_argument( 103 "--all", action="store_true", help="Print help for all topics") 104 parser.add_argument( 105 "topic", nargs="?", help="Topic for more information")
106
107 - def _complete(self, text, line, begidx, endidx):
108 """ 109 This is something of a hack. This should either be a part 110 of the context interface, or we should put it somewhere 111 in a utility. FIXME. 112 """ 113 return self.ctx.completenames(text, line, begidx, endidx)
114
115 - def __call__(self, args):
116 117 self.ctx.waitForPlugins() 118 # commands = "\n".join([" %s" % name for name in 119 # sorted(self.ctx.controls)]) 120 # topics = "\n".join([" %s" % topic for topic in self.ctx.topics]) 121 commands, topics = [ 122 self.__parser__._format_list(x) for x in 123 [sorted(self.ctx.controls), sorted(self.ctx.topics)]] 124 125 if args.all: 126 for control in sorted(self.ctx.controls): 127 self.ctx.out("*" * 80) 128 self.ctx.out(control) 129 self.ctx.out("*" * 80) 130 self.ctx.invoke([control, "-h"]) 131 self.ctx.out("\n") 132 for topic in sorted(self.ctx.topics): 133 self.ctx.out("*" * 80) 134 self.ctx.out(topic) 135 self.ctx.out("*" * 80) 136 self.ctx.out(self.ctx.topics[topic]) 137 self.ctx.out("\n") 138 elif not args.topic: 139 # self.ctx.invoke("-h") 140 key_list = {"program_name": sys.argv[0], "version": VERSION, 141 "commands": commands, "topics": topics} 142 print """usage: %(program_name)s <command> [options] args 143 See 'help <command>' or '<command> -h' for more information on syntax 144 Type 'quit' to exit 145 146 Available commands: 147 %(commands)s 148 149 Other help topics: 150 %(topics)s 151 152 For additional information, see: 153 http://www.openmicroscopy.org/site/support/omero4/users/\ 154 command-line-interface.html 155 Report bugs to <ome-users@lists.openmicroscopy.org.uk> 156 """ % key_list 157 158 else: 159 try: 160 print 1 161 self.ctx.controls[args.topic] 162 self.ctx.invoke("%s -h" % args.topic) 163 except KeyError: 164 try: 165 self.ctx.out(self.ctx.topics[args.topic]) 166 except KeyError: 167 self.ctx.err("Unknown help topic: %s" % args.topic)
168 169 controls = { 170 "help": (HelpControl, "Syntax help for all commands"), 171 "quit": (QuitControl, "Quit application"), 172 "shell": (ShellControl, """Starts an IPython interpreter session 173 174 All arguments not understood vi %(prog)s will be passed to the shell. 175 Use "--" to end parsing, e.g. '%(prog)s -- --help' for IPython help"""), 176 "version": (VersionControl, "Version number"), 177 "load": (LoadControl, """Load file as if it were sent on standard in. 178 179 This can be used as the #! header of a file to make standand-alone script. 180 181 Examples: 182 #!/usr/bin/env omero load 183 login -C root@localhost 184 group add new_group 185 user add foo some user new_group 186 187 or 188 189 $ bin/omero login # login can't take place in HERE-document 190 $ bin/omero load <<EOF 191 user list 192 group list 193 EOF 194 195 """) 196 } 197 198 try: 199 for k, v in controls.items(): 200 register(k, v[0], v[1]) 201 except NameError: 202 if __name__ == "__main__": 203 cli = CLI() 204 for k, v in controls.items(): 205 cli.register(k, v[0], v[1]) 206 cli.invoke(sys.argv[1:]) 207