コード例 #1
0
class ForkingTCPRequestHandler(socketserver.StreamRequestHandler):
    #
    def __init__(self, *args, **kargs):
        name = '/tmp/mdsplus_gateway_' + str(os.getpid()) + '.log'

        self.use_mdsconnect = False
        self.connection_str = ''
        self.connection = None
        if write_handler_log:
            self.fid = open(name, 'w')
        else:
            self.fid = None
        socketserver.StreamRequestHandler.__init__(self, *args, **kargs)

    def write_log(self, txt):
        if self.fid is not None:
            self.fid.write(txt + '\n')

    def handle(self):
        try:
            while True:
                self.loop_handler()
        except:
            pass

    def loop_handler(self):
        message = self.rfile.readline().strip()
        if self.fid is not None:
            self.fid.write('handling input \n' + message + '\n')
        asmessage = binascii.a2b_hex(message)
        #        logging.basicConfig(level = logging.DEBUG)
        try:
            data = pickle.loads(asmessage)
        except ValueError as EOFError:
            if self.fid is not None:
                self.fid.write('picke error \n')
                self.fid.flush()


#           logging.exception('picke load error', len(message))
#           response = None
#           return
#cur_thread = threading.current_thread()
        if self.fid is not None:
            self.fid.write('handling request  \n' + str(data) + '\n')
            self.fid.flush()
        r = {}
        for name, commands in data:
            for command in commands:
                com = command[0]
                param = command[1:]
                #cur_thread = threading.current_thread()
                if self.fid is not None:
                    self.fid.write(com + ':' + param + '\n')
                    self.fid.flush()
                if com == 's':

                    server, port, tree = parse_server_string(param)
                    self.use_mdsconnect = (False if server.upper() == 'DIRECT'
                                           else True)

                    if self.use_mdsconnect:
                        try:
                            if self.connection_str != param:
                                del self.connection
                                if port != '':
                                    server = server + ':' + port
                                self.connection = Connection(server)
                                self.connection_str = param
                                r[name] = 'ok'
                        except:
                            r[name] = 'connection error'
                            self.error = [
                                'connection error', self.connection_str
                            ]
                    else:
                        if self.connection is not None:
                            del self.connection
                            self.connection = None
                        self.connection_str = ''
                        r[name] = 'ok'
                else:
                    self.error = []
                    if self.use_mdsconnect:
                        r[name] = self._handle_mdsconnect(com, param)
                    else:
                        r[name] = self._handle(com, param)
                    if len(self.error) != 0:
                        if self.fid is not None:
                            self.fid.write(str(self.error))
                            self.fid.flush()
        if self.fid is not None:
            self.fid.write('return variables  \n' + str(list(r.keys())) + '\n')
            self.fid.flush()

        sr = pickle.dumps(r)
        response = binascii.b2a_hex(sr)
        if self.fid is not None:
            self.fid.write('sending data (length) \n' + str(len(response)) +
                           '\n')
            self.fid.flush()
        self.request.sendall(response + '\n')
        if self.fid is not None:
            self.fid.flush()

    def _handle_mdsconnect(self, com, param):
        response = ''

        self.write_log(str(com) + ':' + str(param))
        if com == 'c':  # connection request
            arr = param.split(',')
            tree = ','.join(arr[:-1])
            shot = arr[-1]
            try:
                if tree != '':
                    self.write_log('opening tree :' + str(tree) + ':' +
                                   str(shot))
                    self.connection.openTree(tree, int(shot))
                response = 'ok'
            except Exception:
                self.error = ['run error', traceback.format_exc()]
                response = None
        elif com == 'd':
            node = param
            self.write_log('setting def node ' + str(node))
            try:
                if node.strip() != '':
                    self.connection.setDefault(node)
                response = 'ok'
            except Exception:
                self.error = ['run error', traceback.format_exc()]
                response = None
        elif com == 'v':  # mdsvalue
            self.write_log('mdsvalue ' + str(param))
            try:
                response = self.connection.get(param).data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        elif com == 'u':  # mdsvalue
            self.write_log('mdsvalue ' + str(param))
            try:
                response = self.connection.get('_piscopevar=execute($)',
                                               param).data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        elif com == 'w':  # dim_of (this is not used anymore)
            self.write_log('dim_of ' + str(param))
            expr = param[1:]
            dim = int(param[0])
            try:
                #               response =MDSplus.Data.compile(param).evaluate().data()
                response = self.connection.get(expr).dim_of(dim).data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        # self.write_log(response.__repr__())
        return response

    def _handle(self, com, param):
        response = ''

        self.write_log(str(com) + ' ' + str(param))
        if com == 'c':  # connection request
            arr = param.split(',')
            tree = ','.join(arr[:-1])
            shot = arr[-1]
            try:
                if tree != '':
                    self.write_log('opening tree ' + str(tree) + ' ' +
                                   str(shot))
                    self.t = MDSplus.Tree(tree, int(shot))
                    self.write_log('opening tree ' + str(self.t))
                response = 'ok'
            except Exception:
                self.error = ['run error', traceback.format_exc()]
                response = None
        elif com == 'd':
            node = param
            self.write_log('setting def node ' + str(node))
            try:
                if node.strip() != '':
                    tn = self.t.getNode(node)
                    self.t.setDefault(tn)
                response = 'ok'
            except Exception:
                self.error = ['run error', traceback.format_exc()]
                response = None
        elif com == 'f':  # connection request
            a = param.split(',')
            tree = a[0]
            shot = a[1]
            node = a[2]
            expr = ','.join(a[3:])
            try:
                self.t = MDSplus.Tree(tree, int(shot))
                if node.strip() != '':
                    tn = self.t.getNode(node)
                    self.t.setDefault(tn)
                response = MDSplus.Data.compile(expr).evaluate().data()
            except Exception:
                self.error = ['run error', expr, traceback.format_exc()]
                response = None
        elif com == 'v':  # mdsvalue
            self.write_log('mdsvalue ' + str(param))
            try:
                response = MDSplus.Data.compile(param).evaluate().data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        elif com == 'u':  # mdsvalue
            self.write_log('mdsvalue ' + str(param))
            try:
                response = MDSplus.Data.execute('_piscopevar=execute($)',
                                                param).data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        elif com == 'w':  # dim_of (this is not used anymore)
            self.write_log('dim_of ' + str(param))
            expr = param[1:]
            dim = int(param[0])
            try:
                #               response =MDSplus.Data.compile(param).evaluate().data()
                response = MDSplus.Data.execute(expr).dim_of(dim).data()
            except Exception:
                self.error = ['run error', param, traceback.format_exc()]
                response = None
        return response
コード例 #2
0
ファイル: direct_jobrunner.py プロジェクト: piScope/piScope
class JobRunner(object):
    def __init__(self):
        self.use_mdsconnect = False
        self.connection_str = ''
        self.connection = None
        self.g = {}
        self._shot = -1
        self.gjob = None   # global tdi (executed when shot number changed)
        self._tree = ''
        self._defnode = ''
    def __del__(self):
        if self.connection is not None:       
            del self.connection
    def run(self, jobset, skip_init):
#        def _evaluate_sig(res, sig_names, dim_names):
#            for name in dim_names:
#                try:
#                   src, dim = res[name][0:3].split(',')
#                   res[name] = res[src].dim_of(long(dim)).data()
#                except:
#                   pass
#            for name in sig_names:
#                try:
#                   res[name] = res[name].data()
#                except:
#                   pass
#            return res
 

        self.error = []
        jobs, names = jobset
        self.r = {'error message': []}
        sig_names = []
        dim_names = []
        try:
           for k, job in enumerate(jobs):
              for j in job:
#                print j
                if j.command == 'connection_mode':
#                   print 'connection_mode param', self.connection_str, j.params, self.connection
#                   if self.connection is not None:
#                        del self.connection
#                        self.connection = None

                   if self.connection_str != j.params[0]:
#                   if True:
                       server, port, tree = parse_server_string(j.params[0])
                       self.use_mdsconnect = (False if server.upper() == 
                                              'DIRECT' else True)

                       if self.use_mdsconnect:
                           if self.connection is not None:
                               del self.connection
                               self.connection = None
                           try:
                               if port != '': server = server + ':' + port
                               self.connection = Connection(server)
                               self.connection_str = j.params[0]
                               res = 'ok'                      
                           except:
                               self.r['error message'] = ['connection error' + 
                                      traceback.format_exc()]
                               self.r[names[k]] = None
                               return self.r
                       else:
                           if self.connection is not None:
                               del self.connection
                               self.connection = None
                           res = 'ok'        
                   else:         
                       res = 'ok'    
                   self._connection_flag = True    
                else:
                   if j.command == 'init':
                       if skip_init:
                           continue
                       else:
                           j.command = 'value'
                   if self.use_mdsconnect:
#                       res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k])
                       #ttt = time.time()
                       res = self._run_job_mdsconnect(j)
                       #print time.time() - ttt, j
                       if len(self.error) != 0:
                           self.r['error message'] = self.error[:]
                           self.r[names[k]] = res
                           return self.r
#                           return _evaluate_sig(self.r, sig_names, dim_names)
                   else:
                       res = self._run_job(j)
                       if len(self.error) != 0:
                           self.r['error message'] = self.error[:]
                           self.r[names[k]] = res
                           return self.r
#                           return _evaluate_sig(self.r, sig_names, dim_names)
                   self._connection_flag = False    

              self.r[names[k]] = res
        except:
           self.error = ['run error', traceback.format_exc()]

#        self.r =  _evaluate_sig(self.r, sig_names, dim_names)
        self.r['error message'] = self.error
        return self.r

    def set_globaljob(self, job):
        self.gjob = job

    def terminate(self):
        if self.connection is not None:
            del self.connection
        self.connection = None


    def _run_script_txt(self, job):
         expr = job.params[0]
         try:
            code = compile(expr, '<string>', 'exec')
            g = {}
            l = {}
            exec code in self.g, self.r
            return 'ok'
         except:
            self.error = ['Scrip Error', expr, sys.exc_info()[0]]
         return None

#    def _run_job_mdsconnect(self, job, sig_names,dim_names, name):
    def _run_job_mdsconnect(self, job):
           com = job.command
           if com == 'novalue': return ''
           if debug_runner: print(job)
           #print threading.current_thread().name, com, job.params
           if com == 'open':           
               tree = job.params[0]
               shot = job.params[1]
               if self._tree != tree or self._shot != shot: 
                  try:
                     #print tree, 
                     #print long(shot)
                     if tree != '':
                         self.connection.openTree(tree, long(shot))
                  except:
                     self.error = ['run error', traceback.format_exc()]
                     return None
               try:
                  t = tree.split(',')[0].strip()
                  node = '\\'+ t +'::TOP'
                  if node != self._defnode:
                     self.connection.setDefault(node)
                     self._defnode = node
               except:
                  pass 
               try:
                  if shot != self._shot:
                      expr = 'reset_private();reset_public();1'
                      r =self.connection.get(expr).data()
                  if self._connection_flag:
                      if self.gjob is not None:
                         expr = self.gjob.params[0]
                         #print 'performing global tdi', expr
                         r =self.connection.get(expr).data()
                  self._connection_flag = False
                  self._shot = shot
                  self._tree = tree
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'defnode':
               node  = job.params[0]
               try:
                  if node.strip() != '':
                     if node != self._defnode:
                        self.connection.setDefault(node)
                        self._defnode = node
                  return 'ok'
               except:
                  print(node)
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'value':
               try:
                   expr = job.params[0]
                   r =self.connection.get(expr).data()
                   return r
               except:
                   self.error = ['run error', expr,traceback.format_exc()]
                   return None
           elif com == 'valuesig':
               try:
                   expr = job.params[0]
                   r =self.connection.get('_piscopevar=execute($)', expr).data()
                   return r
               except:
                   self.error = ['run error', expr,traceback.format_exc()]
                   return None
#           elif com == 'dim_of':# this is not used anymore
#               expr = job.params[0]
#               dim_names.append(name)
#               return expr
           elif com == 'script_txt':
               return self._run_script_txt(job)

#    def _run_job(self, job, sig_names, dim_names, name):
    def _run_job(self, job):
           import MDSplus
           from MDSplus import Connection

           com = job.command
           if com == 'novalue': return ''
           if com == 'open':           
               tree = job.params[0]
               shot = job.params[1]
               try:
                  if tree != '':
                      self.t = MDSplus.Tree(tree, long(shot))
                  if shot != self._shot:
                      expr = 'reset_private();reset_public();1'
#                      r =MDSplus.Data.compile(expr).evaluate().data()
                      r =MDSplus.Data.execute(expr).data()
                  if self._connection_flag:
                      if self.gjob is not None:
                         expr = self.gjob.params[0]
                         r =MDSplus.Data.execute(expr).data()
                  self._connection_flag = False
                  self._shot = shot
                  return 'ok'
               except:
#                  print traceback.format_exc()
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'defnode':
               node  = job.params[0]
               try:
                  if node.strip() != '':
                     tn = self.t.getNode(node)
                     self.t.setDefault(tn)
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'value':
               try:
                  expr = job.params[0]
                  r =MDSplus.Data.execute(expr).data()
#                  r =MDSplus.Data.execute(expr).data()
#                 sig_names.append(name)
                  return r
               except:
                  self.error = ['run error', expr, traceback.format_exc()]
                  return None
           elif com == 'valuesig':
               try:
                  expr = job.params[0]
                  r =MDSplus.Data.execute('_piscopevar=execute($)', expr).data()
#                  r =MDSplus.Data.execute(expr).data()
#                 sig_names.append(name)
                  return r
               except:
                  self.error = ['run error', expr, traceback.format_exc()]
                  return None

#           elif com == 'dim_of':     # this is not used anymore
#               expr = job.params[0]  ### expr should be y, 0 (= dim_of(y, 0))
#               dim_names.append(name)
#               return expr
           elif com == 'script_txt':
               return self._run_script_txt(job)
コード例 #3
0
class JobRunner(object):
    def __init__(self):
        self.use_mdsconnect = False
        self.connection_str = ''
        self.connection = None
        self.g = {}
        self._shot = -1
        self.gjob = None  # global tdi (executed when shot number changed)
        self._tree = ''
        self._defnode = ''

    def __del__(self):
        if self.connection is not None:
            del self.connection

    def run(self, jobset, skip_init):
        #        def _evaluate_sig(res, sig_names, dim_names):
        #            for name in dim_names:
        #                try:
        #                   src, dim = res[name][0:3].split(',')
        #                   res[name] = res[src].dim_of(long(dim)).data()
        #                except:
        #                   pass
        #            for name in sig_names:
        #                try:
        #                   res[name] = res[name].data()
        #                except:
        #                   pass
        #            return res

        self.error = []
        jobs, names = jobset
        self.r = {'error message': []}
        sig_names = []
        dim_names = []
        try:
            for k, job in enumerate(jobs):
                for j in job:
                    #                print j
                    if j.command == 'connection_mode':
                        #                   print 'connection_mode param', self.connection_str, j.params, self.connection
                        #                   if self.connection is not None:
                        #                        del self.connection
                        #                        self.connection = None

                        if self.connection_str != j.params[0]:
                            #                   if True:
                            server, port, tree = parse_server_string(
                                j.params[0])
                            self.use_mdsconnect = (False if server.upper()
                                                   == 'DIRECT' else True)

                            if self.use_mdsconnect:
                                if self.connection is not None:
                                    del self.connection
                                    self.connection = None
                                try:
                                    if port != '': server = server + ':' + port
                                    self.connection = Connection(server)
                                    self.connection_str = j.params[0]
                                    res = 'ok'
                                except:
                                    self.r['error message'] = [
                                        'connection error' +
                                        traceback.format_exc()
                                    ]
                                    self.r[names[k]] = None
                                    return self.r
                            else:
                                if self.connection is not None:
                                    del self.connection
                                    self.connection = None
                                res = 'ok'
                        else:
                            res = 'ok'
                        self._connection_flag = True
                    else:
                        if j.command == 'init':
                            if skip_init:
                                continue
                            else:
                                j.command = 'value'
                        if self.use_mdsconnect:
                            #                       res = self._run_job_mdsconnect(j, sig_names, dim_names, names[k])
                            #ttt = time.time()
                            res = self._run_job_mdsconnect(j)
                            #print time.time() - ttt, j
                            if len(self.error) != 0:
                                self.r['error message'] = self.error[:]
                                self.r[names[k]] = res
                                return self.r
#                           return _evaluate_sig(self.r, sig_names, dim_names)
                        else:
                            res = self._run_job(j)
                            if len(self.error) != 0:
                                self.r['error message'] = self.error[:]
                                self.r[names[k]] = res
                                return self.r
#                           return _evaluate_sig(self.r, sig_names, dim_names)
                        self._connection_flag = False

                self.r[names[k]] = res
        except:
            self.error = ['run error', traceback.format_exc()]

#        self.r =  _evaluate_sig(self.r, sig_names, dim_names)
        self.r['error message'] = self.error
        return self.r

    def set_globaljob(self, job):
        self.gjob = job

    def terminate(self):
        if self.connection is not None:
            del self.connection
        self.connection = None

    def _run_script_txt(self, job):
        expr = job.params[0]
        try:
            code = compile(expr, '<string>', 'exec')
            g = {}
            l = {}
            exec code in self.g, self.r
            return 'ok'
        except:
            self.error = ['Scrip Error', expr, sys.exc_info()[0]]
        return None

#    def _run_job_mdsconnect(self, job, sig_names,dim_names, name):

    def _run_job_mdsconnect(self, job):
        com = job.command
        if com == 'novalue': return ''
        if debug_runner: print(job)
        #print threading.current_thread().name, com, job.params
        if com == 'open':
            tree = job.params[0]
            shot = job.params[1]
            if self._tree != tree or self._shot != shot:
                try:
                    #print tree,
                    #print long(shot)
                    if tree != '':
                        self.connection.openTree(tree, long(shot))
                except:
                    self.error = ['run error', traceback.format_exc()]
                    return None
            try:
                t = tree.split(',')[0].strip()
                node = '\\' + t + '::TOP'
                if node != self._defnode:
                    self.connection.setDefault(node)
                    self._defnode = node
            except:
                pass
            try:
                if shot != self._shot:
                    expr = 'reset_private();reset_public();1'
                    r = self.connection.get(expr).data()
                if self._connection_flag:
                    if self.gjob is not None:
                        expr = self.gjob.params[0]
                        #print 'performing global tdi', expr
                        r = self.connection.get(expr).data()
                self._connection_flag = False
                self._shot = shot
                self._tree = tree
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'defnode':
            node = job.params[0]
            try:
                if node.strip() != '':
                    if node != self._defnode:
                        self.connection.setDefault(node)
                        self._defnode = node
                return 'ok'
            except:
                print(node)
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'value':
            try:
                expr = job.params[0]
                r = self.connection.get(expr).data()
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None
        elif com == 'valuesig':
            try:
                expr = job.params[0]
                r = self.connection.get('_piscopevar=execute($)', expr).data()
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None
#           elif com == 'dim_of':# this is not used anymore
#               expr = job.params[0]
#               dim_names.append(name)
#               return expr
        elif com == 'script_txt':
            return self._run_script_txt(job)

#    def _run_job(self, job, sig_names, dim_names, name):

    def _run_job(self, job):
        import MDSplus
        from MDSplus import Connection

        com = job.command
        if com == 'novalue': return ''
        if com == 'open':
            tree = job.params[0]
            shot = job.params[1]
            try:
                if tree != '':
                    self.t = MDSplus.Tree(tree, long(shot))
                if shot != self._shot:
                    expr = 'reset_private();reset_public();1'
                    #                      r =MDSplus.Data.compile(expr).evaluate().data()
                    r = MDSplus.Data.execute(expr).data()
                if self._connection_flag:
                    if self.gjob is not None:
                        expr = self.gjob.params[0]
                        r = MDSplus.Data.execute(expr).data()
                self._connection_flag = False
                self._shot = shot
                return 'ok'
            except:
                #                  print traceback.format_exc()
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'defnode':
            node = job.params[0]
            try:
                if node.strip() != '':
                    tn = self.t.getNode(node)
                    self.t.setDefault(tn)
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'value':
            try:
                expr = job.params[0]
                r = MDSplus.Data.execute(expr).data()
                #                  r =MDSplus.Data.execute(expr).data()
                #                 sig_names.append(name)
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None
        elif com == 'valuesig':
            try:
                expr = job.params[0]
                r = MDSplus.Data.execute('_piscopevar=execute($)', expr).data()
                #                  r =MDSplus.Data.execute(expr).data()
                #                 sig_names.append(name)
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None

#           elif com == 'dim_of':     # this is not used anymore
#               expr = job.params[0]  ### expr should be y, 0 (= dim_of(y, 0))
#               dim_names.append(name)
#               return expr
        elif com == 'script_txt':
            return self._run_script_txt(job)
コード例 #4
0
class JobRunner(object):
    def __init__(self):
        self.use_mdsconnect = False
        self.connection_str = ''
        self.connection = None
        self.g = {}
        self._shot = -1

    def run(self, jobset, skip_init):
        import MDSplus
        from MDSplus import Connection

        self.error = []
        jobs, names = jobset
        self.r = {}
        self.r['error message'] = ['']
        try:
           for k, job in enumerate(jobs):
              for j in job:
                if j.command == 'connection_mode':
                   if self.connection_str != j.params[0]:
                       self.connection_str = j.params[0]
                       server, port, tree = parse_server_string(self.connection_str)
                       self.use_mdsconnect = (False if server.upper() == 
                                              'DIRECT' else True)
                       if self.use_mdsconnect:
                           if self.connection is not None:
                              del self.connection
                           try:
                               if port != '': server = server + ':' + port
                               self.connection = Connection(server)
                               res = 'ok'                      
                           except:
                               res = ('connection error' + 
                                      traceback.format_exc())
                       else:
                           if self.connection is not None:
                               del self.connection
                               self.connection = None
                           res = 'ok'        
                   else:         
                       res = 'ok'        
                else:
                   if j.command == 'init':
                       if skip_init:
                           continue
                       else:
                           j.command = 'value'
                   if self.use_mdsconnect:
                       res = self._run_job_mdsconnect(j)
                       if len(self.error) != 0:
                           self.r['error message'] = self.error
                           self.r[names[k]] = res
                           return self.r
                   else:
                       res = self._run_job(j)
                       if len(self.error) != 0:
                           self.r['error message'] = self.error
                           self.r[names[k]] = res
                           return self.r
              self.r[names[k]] = res
        except:
           self.error = ['run error', traceback.format_exc()]
        self.r['error message'] = self.error
        return self.r

    def _run_script_txt(self, job):
         expr = job.params[0]
         try:
            code = compile(expr, '<string>', 'exec')
            g = {}
            l = {}
            exec code in self.g, self.r
            return 'ok'
         except:
            self.error = ['Scrip Error', expr, sys.exc_info()[0]]
         return None

    def _run_job_mdsconnect(self, job):
           com = job.command
           if com == 'open':           
               tree = job.params[0]
               shot = job.params[1]
               try:
                  self.connection.openTree(tree, long(shot))
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
               self._shot = shot
           elif com == 'defnode':
               node  = job.params[0]
               try:
                  if node.strip() != '':
                     self.connection.setDefault(node)
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'value':
               try:
                   expr = job.params[0]
                   r =self.connection.get(expr).data()
                   return r
               except:
                   self.error = ['run error', expr,traceback.format_exc()]
                   return None
           elif com == 'script_txt':
               return self._run_script_txt(job)

    def _run_job(self, job):
           import MDSplus
           from MDSplus import Connection

           com = job.command

           if com == 'open':           
               tree = job.params[0]
               shot = job.params[1]
               try:
                  self.t = MDSplus.Tree(tree, long(shot))
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
               self._shot = shot
           elif com == 'defnode':
               node  = job.params[0]
               try:
                  if node.strip() != '':
                     tn = self.t.getNode(node)
                     self.t.setDefault(tn)
                  return 'ok'
               except:
                  self.error = ['run error', traceback.format_exc()]
                  return None
           elif com == 'value':
               try:
                  expr = job.params[0]
                  r =MDSplus.Data.compile(expr).evaluate().data()
                  return r
               except:
                  self.error = ['run error', expr, traceback.format_exc()]
                  return None
           elif com == 'script_txt':
               return self._run_script_txt(job)
コード例 #5
0
class JobRunner(object):
    def __init__(self):
        self.use_mdsconnect = False
        self.connection_str = ''
        self.connection = None
        self.g = {}
        self._shot = -1

    def run(self, jobset, skip_init):
        import MDSplus
        from MDSplus import Connection

        self.error = []
        jobs, names = jobset
        self.r = {}
        self.r['error message'] = ['']
        try:
            for k, job in enumerate(jobs):
                for j in job:
                    if j.command == 'connection_mode':
                        if self.connection_str != j.params[0]:
                            self.connection_str = j.params[0]
                            server, port, tree = parse_server_string(
                                self.connection_str)
                            self.use_mdsconnect = (False if server.upper()
                                                   == 'DIRECT' else True)
                            if self.use_mdsconnect:
                                if self.connection is not None:
                                    del self.connection
                                try:
                                    if port != '':
                                        server = server + ':' + port
                                    self.connection = Connection(server)
                                    res = 'ok'
                                except:
                                    res = ('connection error' +
                                           traceback.format_exc())
                            else:
                                if self.connection is not None:
                                    del self.connection
                                    self.connection = None
                                res = 'ok'
                        else:
                            res = 'ok'
                    else:
                        if j.command == 'init':
                            if skip_init:
                                continue
                            else:
                                j.command = 'value'
                        if self.use_mdsconnect:
                            res = self._run_job_mdsconnect(j)
                            if len(self.error) != 0:
                                self.r['error message'] = self.error
                                self.r[names[k]] = res
                                return self.r
                        else:
                            res = self._run_job(j)
                            if len(self.error) != 0:
                                self.r['error message'] = self.error
                                self.r[names[k]] = res
                                return self.r
                self.r[names[k]] = res
        except:
            self.error = ['run error', traceback.format_exc()]
        self.r['error message'] = self.error
        return self.r

    def _run_script_txt(self, job):
        expr = job.params[0]
        try:
            code = compile(expr, '<string>', 'exec')
            g = {}
            l = {}
            exec(code, self.g, self.r)
            return 'ok'
        except:
            self.error = ['Scrip Error', expr, sys.exc_info()[0]]
        return None

    def _run_job_mdsconnect(self, job):
        com = job.command
        if com == 'open':
            tree = job.params[0]
            shot = job.params[1]
            try:
                self.connection.openTree(tree, int(shot))
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
            self._shot = shot
        elif com == 'defnode':
            node = job.params[0]
            try:
                if node.strip() != '':
                    self.connection.setDefault(node)
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'value':
            try:
                expr = job.params[0]
                r = self.connection.get(expr).data()
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None
        elif com == 'script_txt':
            return self._run_script_txt(job)

    def _run_job(self, job):
        import MDSplus
        from MDSplus import Connection

        com = job.command

        if com == 'open':
            tree = job.params[0]
            shot = job.params[1]
            try:
                self.t = MDSplus.Tree(tree, int(shot))
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
            self._shot = shot
        elif com == 'defnode':
            node = job.params[0]
            try:
                if node.strip() != '':
                    tn = self.t.getNode(node)
                    self.t.setDefault(tn)
                return 'ok'
            except:
                self.error = ['run error', traceback.format_exc()]
                return None
        elif com == 'value':
            try:
                expr = job.params[0]
                r = MDSplus.Data.compile(expr).evaluate().data()
                return r
            except:
                self.error = ['run error', expr, traceback.format_exc()]
                return None
        elif com == 'script_txt':
            return self._run_script_txt(job)