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

Source Code for Module omero.plugins.search

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # 
  5  # Copyright (C) 2012 Glencoe Software, Inc. All Rights Reserved. 
  6  # Use is subject to license terms supplied in LICENSE.txt 
  7  # 
  8   
  9  """ 
 10  Simple command-line searching. Similar to the hql plugin. 
 11  """ 
 12   
 13   
 14  import sys 
 15   
 16  from omero.cli import CLI 
 17  from omero.plugins.hql import HqlControl 
 18   
 19   
 20  HELP = """Search for object ids by string. 
 21   
 22  Examples: 
 23   
 24    bin/omero search Image "my-text" 
 25    bin/omero search Image "with wildcard*" 
 26    bin/omero search Project "with wildcard*" 
 27   
 28  Examples (admin-only): 
 29   
 30    bin/omero search --index Image:1 
 31    bin/omero search --index Well:5 
 32   
 33  See also: 
 34   
 35    https://lucene.apache.org/java/2_9_1/queryparsersyntax.html 
 36   
 37  """ 
 38   
 39   
40 -class SearchControl(HqlControl):
41
42 - def _configure(self, parser):
43 parser.add_argument( 44 "--index", action="store_true", default=False, 45 help="Index an object as a administrator") 46 parser.add_argument( 47 "type", 48 help="Object type to search for, e.g. 'Image' or 'Well'") 49 parser.add_argument( 50 "search_string", nargs="?", 51 help="Lucene search string") 52 parser.set_defaults(func=self.search) 53 parser.add_login_arguments()
54
55 - def search(self, args):
56 c = self.ctx.conn(args) 57 58 import omero 59 import omero.all 60 61 if args.index: 62 try: 63 parts = args.type.split(":") 64 kls = parts[0].strip() 65 kls = getattr(omero.model, kls) 66 kls = kls.ice_staticId() 67 of = c.getCommunicator().findObjectFactory(kls) 68 obj = of.create(kls) 69 id = long(parts[1].strip()) 70 obj.setId(omero.rtypes.rlong(id)) 71 except Exception, e: 72 self.ctx.dbg(e) 73 self.ctx.die(432, "Bad object: %s" % args.type) 74 75 c.sf.getUpdateService().indexObject(obj) 76 77 else: 78 search = c.sf.createSearchService() 79 try: 80 try: 81 search.onlyType(args.type) 82 search.byFullText(args.search_string) 83 if not search.hasNext(): 84 self.ctx.die(433, "No results found.") 85 while search.hasNext(): 86 results = search.results() 87 results = [[x] for x in results] 88 self.display(results) 89 except omero.ApiUsageException, aue: 90 self.ctx.die(434, aue.message) 91 92 finally: 93 search.close()
94 95 try: 96 register("search", SearchControl, HELP) 97 except NameError: 98 if __name__ == "__main__": 99 cli = CLI() 100 cli.register("search", SearchControl, HELP) 101 cli.invoke(sys.argv[1:]) 102