| 1 | #!/usr/bin/python -tti | 
|---|
| 2 | import rlcompleter | 
|---|
| 3 | import readline | 
|---|
| 4 | readline.parse_and_bind('tab: complete') | 
|---|
| 5 |  | 
|---|
| 6 | import sys | 
|---|
| 7 | import pydim | 
|---|
| 8 | import types | 
|---|
| 9 | import time # for sleep | 
|---|
| 10 | from pprint import pprint | 
|---|
| 11 | from keyword import iskeyword | 
|---|
| 12 | pydim.dic_set_dns_node('daq') | 
|---|
| 13 |  | 
|---|
| 14 | # making server list | 
|---|
| 15 | rawlist = pydim.dic_sync_info_service('DIS_DNS/SERVER_LIST','C') | 
|---|
| 16 | # the output needs to be treated a bit .. it is a tuple with only one long string in it | 
|---|
| 17 | # the string contains | and the strange character \x00 | 
|---|
| 18 | # I use both to cut the list apart | 
|---|
| 19 | rawlist = rawlist[0].split('\x00') | 
|---|
| 20 | servers_n_hosts = rawlist[0].split('|') | 
|---|
| 21 | server_ids = rawlist[1].split('|') | 
|---|
| 22 |  | 
|---|
| 23 | servers = {} | 
|---|
| 24 | for i,snh in enumerate(servers_n_hosts): | 
|---|
| 25 | snh = snh.split('@') | 
|---|
| 26 | s = snh[0] | 
|---|
| 27 | h = snh[1] | 
|---|
| 28 | sid = server_ids[i] | 
|---|
| 29 | servers[s] = (sid, h) | 
|---|
| 30 |  | 
|---|
| 31 | # delete unneeded vars | 
|---|
| 32 | del servers_n_hosts | 
|---|
| 33 | del rawlist | 
|---|
| 34 | del server_ids | 
|---|
| 35 |  | 
|---|
| 36 | # print servers | 
|---|
| 37 |  | 
|---|
| 38 | # make service list | 
|---|
| 39 | services = {} | 
|---|
| 40 | dd = {} | 
|---|
| 41 | for serv in servers.keys(): | 
|---|
| 42 | # print serv+'/SERVICE_LIST' | 
|---|
| 43 | sl_raw = pydim.dic_sync_info_service(serv+'/SERVICE_LIST','C')[0] | 
|---|
| 44 | if serv+'/SERVICE_DESC' in sl_raw: | 
|---|
| 45 | sd_raw = pydim.dic_sync_info_service(serv+'/SERVICE_DESC','C')[0] | 
|---|
| 46 | else: | 
|---|
| 47 | # print "Warning:", serv+'/SERVICE_DESC' , "not in SERVICE_LIST of", serv | 
|---|
| 48 | sd_raw = '' | 
|---|
| 49 | sl_raw = sl_raw.rstrip('\x00\n') | 
|---|
| 50 | sl_raw = sl_raw.replace('\x00','') | 
|---|
| 51 | sd_raw = sd_raw.rstrip('\x00\n') | 
|---|
| 52 | sl = sl_raw.split('\n') | 
|---|
| 53 | sd = sd_raw.split('\n') | 
|---|
| 54 |  | 
|---|
| 55 | # create descripton dict dd from service_desc list sd | 
|---|
| 56 | for d_str in sd: | 
|---|
| 57 | service,equalsign,desc = d_str.partition('=') | 
|---|
| 58 | #if '=' != equalsign: | 
|---|
| 59 | # print "Error: server:", serv, "desc:", d_str | 
|---|
| 60 | dd[service] = desc | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | services[serv] = {} | 
|---|
| 64 | for service in sl: | 
|---|
| 65 | service = service.split('|') | 
|---|
| 66 | if service[0] in dd: | 
|---|
| 67 | services[serv][service[0]] = ( | 
|---|
| 68 | service[1], service[2], dd[service[0]]) | 
|---|
| 69 | #      else: | 
|---|
| 70 | # print service[0], 'has no DESC in SERVICE_DESC' | 
|---|
| 71 |  | 
|---|
| 72 | # pprint( services ) | 
|---|
| 73 |  | 
|---|
| 74 | class FactDimServer( object ): | 
|---|
| 75 |  | 
|---|
| 76 | def __init__(self, name): | 
|---|
| 77 | self.name = name | 
|---|
| 78 |  | 
|---|
| 79 | def cmd(self, cmdstr, *args): | 
|---|
| 80 | #args = cmdstr.split(' ') | 
|---|
| 81 | #for i in range(len(args)): | 
|---|
| 82 | #               i = len(args)-1-i | 
|---|
| 83 | #               if not args[i]: | 
|---|
| 84 | #                       del args[i] | 
|---|
| 85 | cmdstr=self.name+'/'+cmdstr.upper() | 
|---|
| 86 | #       args = tuple(args[1:]) | 
|---|
| 87 | desc = services[self.name][cmdstr.upper()][0] | 
|---|
| 88 | # there is a bug in the pydim library ... | 
|---|
| 89 | # even if a command need no argument | 
|---|
| 90 | # one has to give one ... need to tell Niko about it. | 
|---|
| 91 | if len(desc) == 0: | 
|---|
| 92 | desc = 'I' | 
|---|
| 93 | args=(1,) | 
|---|
| 94 | # print 'cmd: cmdstr:', cmdstr, | 
|---|
| 95 | # print '-args:', args, | 
|---|
| 96 | # print '-desc:', desc | 
|---|
| 97 | pydim.dic_sync_cmnd_service(cmdstr, args, desc, timeout=None) | 
|---|
| 98 | def get(self, service): | 
|---|
| 99 | full_srv_name = self.name+'/'+service.upper() | 
|---|
| 100 | desc = services[self.name][full_srv_name][0] | 
|---|
| 101 | return pydim.dic_sync_info_service(full_srv_name, desc) | 
|---|
| 102 |  | 
|---|
| 103 | def __call__(self): | 
|---|
| 104 | """ get Server State as numeric code | 
|---|
| 105 | for convenience | 
|---|
| 106 | """ | 
|---|
| 107 | if hasattr(self, 'state'): | 
|---|
| 108 | s = self.state()[0] | 
|---|
| 109 | return int(s[ s.find('[')+1 : s.find(']') ]) | 
|---|
| 110 | else: | 
|---|
| 111 | raise TypeError(self.name+' has no CMD called STATE') | 
|---|
| 112 |  | 
|---|
| 113 | def wait(self, state_num, timeout=None): | 
|---|
| 114 | """ waits for a certain state | 
|---|
| 115 | BLOCKING | 
|---|
| 116 | returns True if state was reached | 
|---|
| 117 | returns False if timeout occured | 
|---|
| 118 | raises TypeError if Server has no method state | 
|---|
| 119 | """ | 
|---|
| 120 |  | 
|---|
| 121 | if not hasattr(self, 'state'): | 
|---|
| 122 | raise TypeError(self.name+' has no CMD called STATE') | 
|---|
| 123 | if timeout == None: | 
|---|
| 124 | timeout = float('inf') | 
|---|
| 125 | else: | 
|---|
| 126 | timeout = float(timeout) | 
|---|
| 127 | start = time.time() | 
|---|
| 128 | while not self() == state_num: | 
|---|
| 129 | time.sleep(0.1) | 
|---|
| 130 | if time.time() >= start+timeout: | 
|---|
| 131 | return False | 
|---|
| 132 | return True | 
|---|
| 133 | # create one class for each Fact Dim Server | 
|---|
| 134 | FactDimServerClasses = [] | 
|---|
| 135 | for server_name in servers: | 
|---|
| 136 | FactDimServerClasses.append( | 
|---|
| 137 | types.ClassType( server_name, (FactDimServer,), {}) ) | 
|---|
| 138 |  | 
|---|
| 139 | # create an instace of each of the classes | 
|---|
| 140 | # and make it globally known, i.e. known to the Python interpreter | 
|---|
| 141 | # all the ServerClass instances are collected in a list | 
|---|
| 142 | # so one can get a quick overview --> print dims | 
|---|
| 143 | all_dims = [] | 
|---|
| 144 | for i,server_name in enumerate(servers): | 
|---|
| 145 | new_instance = FactDimServerClasses[i](server_name) | 
|---|
| 146 | all_dims.append( new_instance ) | 
|---|
| 147 | del new_instance | 
|---|
| 148 |  | 
|---|
| 149 | dims = [] | 
|---|
| 150 | #print "connecting to Dim Servers... " | 
|---|
| 151 | for dim in all_dims: | 
|---|
| 152 | #       print dim.name.lower() | 
|---|
| 153 | if dim.name == 'DIS_DNS': | 
|---|
| 154 | continue | 
|---|
| 155 | globals()[dim.name.lower()] = dim | 
|---|
| 156 | dims.append(dim) | 
|---|
| 157 |  | 
|---|
| 158 | # utility class for dynamic addid of methods to classes | 
|---|
| 159 | def add_command(cls, name): | 
|---|
| 160 | meth_name = name.split('/')[1].lower() | 
|---|
| 161 | if iskeyword(meth_name): | 
|---|
| 162 | meth_name += '_cmd' | 
|---|
| 163 | def new_command(self, *args): | 
|---|
| 164 | self.cmd(meth_name, *args) | 
|---|
| 165 | new_command.__name__ = meth_name | 
|---|
| 166 | if name in dd: | 
|---|
| 167 | if not dd[name]: | 
|---|
| 168 | new_command.__doc__ = "DESC in SERVICE_DESC is empty ?!" | 
|---|
| 169 | else: | 
|---|
| 170 | new_command.__doc__ = dd[name] | 
|---|
| 171 | else: | 
|---|
| 172 | new_command.__doc__ = "-- no DESC found in SERVICE_DESC --" | 
|---|
| 173 | new_command.__doc__ += '\n' | 
|---|
| 174 | new_command.__doc__ += services[name.split('/')[0]][name][0] | 
|---|
| 175 | setattr( cls, new_command.__name__, new_command) | 
|---|
| 176 |  | 
|---|
| 177 | def add_getter(cls, name): | 
|---|
| 178 | meth_name = name.split('/')[1].lower() | 
|---|
| 179 | if iskeyword(meth_name): | 
|---|
| 180 | meth_name += '_cmd' | 
|---|
| 181 | def new_command(self): | 
|---|
| 182 | return self.get(meth_name) | 
|---|
| 183 | new_command.__name__ = meth_name | 
|---|
| 184 | if name in dd: | 
|---|
| 185 | if not dd[name]: | 
|---|
| 186 | new_command.__doc__ = "DESC in SERVICE_DESC is empty ?!" | 
|---|
| 187 | else: | 
|---|
| 188 | new_command.__doc__ = dd[name] | 
|---|
| 189 | else: | 
|---|
| 190 | new_command.__doc__ = "-- no DESC found in SERVICE_DESC --" | 
|---|
| 191 | new_command.__doc__ += '\n' | 
|---|
| 192 | new_command.__doc__ += services[name.split('/')[0]][name][0] | 
|---|
| 193 | setattr( cls, new_command.__name__, new_command) | 
|---|
| 194 |  | 
|---|
| 195 | for i,dim in enumerate(all_dims): | 
|---|
| 196 | for cmd in services[dim.name]: | 
|---|
| 197 | if 'CMD' in services[dim.name][cmd][1]: | 
|---|
| 198 | cmdname = cmd.split('/')[1] | 
|---|
| 199 | add_command(FactDimServerClasses[i], cmd) | 
|---|
| 200 | elif not services[dim.name][cmd][1]: | 
|---|
| 201 | cmdname = cmd.split('/')[1] | 
|---|
| 202 | add_getter(FactDimServerClasses[i], cmd) | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | class bcolors: | 
|---|
| 206 | HEADER = '\033[95m' | 
|---|
| 207 | OKBLUE = '\033[94m' | 
|---|
| 208 | OKGREEN = '\033[92m' | 
|---|
| 209 | WARNING = '\033[93m' | 
|---|
| 210 | FAIL = '\033[91m' | 
|---|
| 211 | ENDC = '\033[0m' | 
|---|
| 212 |  | 
|---|
| 213 | def disable(self): | 
|---|
| 214 | self.HEADER = '' | 
|---|
| 215 | self.OKBLUE = '' | 
|---|
| 216 | self.OKGREEN = '' | 
|---|
| 217 | self.WARNING = '' | 
|---|
| 218 | self.FAIL = '' | 
|---|
| 219 | self.ENDC = '' | 
|---|
| 220 | class MSG( bcolors): | 
|---|
| 221 | def __init__(self, verbose = True): | 
|---|
| 222 | self.output = verbose | 
|---|
| 223 |  | 
|---|
| 224 | def fail(self, text ): | 
|---|
| 225 | text = str(text) | 
|---|
| 226 | if self.output: | 
|---|
| 227 | print bcolors.FAIL + "ERROR:" + bcolors.ENDC, | 
|---|
| 228 | print bcolors.FAIL + text + bcolors.ENDC | 
|---|
| 229 |  | 
|---|
| 230 | def warn(self, text ): | 
|---|
| 231 | text = str(text) | 
|---|
| 232 | if self.output: | 
|---|
| 233 | print bcolors.WARNING + text + bcolors.ENDC | 
|---|
| 234 |  | 
|---|
| 235 | def ok(self, text ): | 
|---|
| 236 | text = str(text) | 
|---|
| 237 | if self.output: | 
|---|
| 238 | print bcolors.OKGREEN + text + bcolors.ENDC | 
|---|
| 239 |  | 
|---|
| 240 | def __call__(self, *args): | 
|---|
| 241 | if self.output: | 
|---|
| 242 | for arg in args: | 
|---|
| 243 | print arg, | 
|---|
| 244 | print | 
|---|
| 245 |  | 
|---|