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()
def create_dir_if_missing(path): """ Create the directory structure represented by a `path`. Path can represent a file or a directory depending if it ends with a path separator. If the path represents a file, the parent directory is created, otherwise the directory itself is created. """ file_dir = os.dirname(path) if not os.exists(file_dir): os.makedirs(file_dir)
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")
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()
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')