Package omero :: Package util :: Module import_candidates
[hide private]
[frames] | no frames]

Source Code for Module omero.util.import_candidates

 1  #!/usr/bin/env python 
 2  """ 
 3     Utility method for calling the equivalent of "bin/omero import -f". 
 4     Results are parsed when using as_dictionary. 
 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 sys, os 
12  import omero 
13  import omero_ServerErrors_ice 
14   
15  from omero.util.temp_files import create_path, remove_path 
16  from omero.cli import CLI 
17   
18 -def _to_list(path):
19 """ 20 Guarantees that a list of strings will be returned. 21 Handles unicode caused by "%s" % path.path. 22 """ 23 if isinstance(path,str) or isinstance(path,unicode): 24 path = [str(path)] 25 else: 26 path = [str(x) for x in path] 27 return path
28
29 -def as_stdout(path, readers=""):
30 path = _to_list(path) 31 readers = str(readers) 32 cli = CLI() 33 cli.loadplugins() 34 if readers: 35 cli.invoke(["import", "-l", readers, "-f"]+path) 36 else: 37 cli.invoke(["import", "-f"]+path) 38 if cli.rv != 0: 39 raise omero.InternalException(None, None, "'import -f' exited with a rc=%s. See console for more information" % cli.rv)
40
41 -def as_dictionary(path, readers=""):
42 """ 43 Run as_stdout, parses the output and returns a dictionary of the form:: 44 { 45 some_file_in_group : \ 46 [ 47 some_file_in_group 48 some_other_file_in_group 49 ... 50 last_file_in_group 51 ], 52 some_file_in_second_group : ... 53 } 54 """ 55 56 t = create_path("candidates", "err") 57 58 path = _to_list(path) 59 path.insert(0, "---file=%s" % t) 60 try: 61 as_stdout(path, readers=readers) 62 f = open(str(t),"r") 63 output = f.readlines() 64 f.close() 65 finally: 66 remove_path(t) 67 68 gline = -1 69 key = None 70 groups = {} 71 for line in output: 72 line = line.strip() 73 if len(line) == 0: 74 continue 75 if line.startswith("#"): 76 gline = -1 77 else: 78 if gline == -1: 79 gline = 1 80 key = line 81 groups[key] = [line] 82 else: 83 groups[key].append(line) 84 85 return groups
86 87 88 if __name__ == "__main__": 89 import sys 90 as_stdout(sys.argv[1:]) 91