def __init__(self,
              kernel=None,
              initfile=None,
              consumer=None,
              kernel_loglevel=logging.NOTSET,
              stdin=PIPE,
              stdout=PIPE,
              stderr=PIPE,
              **kwargs):
     self.id = _thread_counter()
     super().__init__(name='wolfram-kernel-%i' % self.id)
     if kernel is None:
         kernel = self.default_kernel_path()
     if isinstance(kernel, six.string_types):
         if not os.isfile(kernel):
             raise WolframKernelException('Kernel not found at %s.' %
                                          kernel)
         elif not os.access(kernel, os.X_OK):
             raise WolframKernelException('Cannot execute kernel %s.' %
                                          kernel)
         else:
             self.kernel = kernel
     else:
         raise ValueError(
             'Invalid kernel value. Expecting a filepath as a string.')
     if initfile is None:
         self.initfile = os.path_join(os.dirname(__file__), 'initkernel.m')
     else:
         self.initfile = initfile
     if not os.isfile(self.initfile):
         raise FileNotFoundError(
             'Kernel initialization file %s not found.' % self.initfile)
     if logger.isEnabledFor(logging.DEBUG):
         logger.debug('Initializing kernel %s using script: %s' %
                      (self.kernel, self.initfile))
     self.tasks_queue = Queue()
     self.kernel_socket_in = None
     self.kernel_socket_out = None
     self.kernel_proc = None
     self.consumer = consumer
     self.loglevel = kernel_loglevel
     self.kernel_logger = None
     self.evaluation_count = 0
     self._stdin = stdin
     self._stdout = stdout
     self._stderr = stderr
     # some parameters may be passed as kwargs
     self.parameters = {}
     for k, v in kwargs.items():
         try:
             self.set_parameter(k, v)
         # ignore kwargs unknowns key
         except KeyError:
             pass
     # this is a state: this event is set when the kernel will not serve any more evaluation.
     self._state_terminated = False
     # lock controlling concurrent access to the state above.
     self._state_lock = RLock()
     # this is a trigger that will abort most blocking operations.
     self.trigger_termination_requested = Event()
Esempio n. 2
0
def find_default_kernel_path():
    """ Look for the most recent installed kernel. """
    rel = exe_path()
    for path in installation_directories():
        if rel:
            path = os.path_join(path, rel)
        if os.isfile(path):
            return path
Esempio n. 3
0
def explore_paths(*paths):
    highest_version = -1
    best_path = None
    for root in paths:
        if os.isdir(root):
            for version in os.listdir(root):
                full_path = os.path_join(root, version)
                if os.isdir(full_path):
                    try:
                        v_num = float(version)
                    except ValueError:
                        v_num = -2
                    if v_num > highest_version and v_num > 0:
                        highest_version = v_num
                        best_path = full_path
    if best_path:
        yield best_path
Esempio n. 4
0
def dir_test_data():
    """ Return path to the data directory in tests"""
    current_file_dir = os.dirname(__file__)
    return os.path_join(current_file_dir, "data")
Esempio n. 5
0
    def __init__(self,
                 kernel=None,
                 initfile=None,
                 consumer=None,
                 kernel_loglevel=logging.NOTSET,
                 stdin=PIPE,
                 stdout=PIPE,
                 stderr=PIPE,
                 **kwargs):
        self.id = _thread_counter()
        super().__init__(name="wolfram-kernel-%i" % self.id)

        self.kernel = kernel or self.default_kernel_path()

        if self.kernel:

            if not os.isfile(self.kernel):
                raise WolframKernelException("Kernel not found at %s." %
                                             self.kernel)

            if not os.access(self.kernel, os.X_OK):
                raise WolframKernelException("Cannot execute kernel %s." %
                                             self.kernel)

        else:
            raise WolframKernelException(
                "Cannot locate a kernel automatically. Please provide an explicit kernel path."
            )

        if initfile is None:
            self.initfile = os.path_join(os.dirname(__file__), "initkernel.m")
        else:
            self.initfile = initfile
        if not os.isfile(self.initfile):
            raise FileNotFoundError(
                "Kernel initialization file %s not found." % self.initfile)
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("Initializing kernel %s using script: %s" %
                         (self.kernel, self.initfile))

        self.tasks_queue = Queue()
        self.kernel_socket_in = None
        self.kernel_socket_out = None
        self.kernel_proc = None
        self.consumer = consumer
        self.loglevel = kernel_loglevel
        self.kernel_logger = None
        self.evaluation_count = 0
        self._stdin = stdin
        self._stdout = stdout
        self._stderr = stderr
        # some parameters may be passed as kwargs
        self.parameters = {}
        for k, v in kwargs.items():
            try:
                self.set_parameter(k, v)
            # ignore kwargs unknowns key
            except KeyError:
                pass
        # this is a state: this event is set when the kernel will not serve any more evaluation.
        self._state_terminated = False
        # lock controlling concurrent access to the state above.
        self._state_lock = RLock()
        # this is a trigger that will abort most blocking operations.
        self.trigger_termination_requested = Event()
Esempio n. 6
0
def dir_test_data():
    ''' Return path to the data directory in tests'''
    current_file_dir = os.dirname(__file__)
    return os.path_join(current_file_dir, 'data')