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

Source Code for Module omero.plugins.basics

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