abo-abo/lpy

middleware sometimes not loaded

QiangF opened this issue · 0 comments

QiangF commented

Borrowed from :

  (defun elpy-shell--ensure-shell-running ()
    "Ensure that the Python shell for the current buffer is running.
  
  If the shell is not running, waits until the first prompt is visible and
  commands can be sent to the shell."
    (with-current-buffer (process-buffer (elpy-shell-get-or-create-process))
      (let ((cumtime 0))
        (while (and (when (boundp 'python-shell--first-prompt-received)
                      (not python-shell--first-prompt-received))
                    (< cumtime 3))
          (sleep-for 0.1)
          (setq cumtime (+ cumtime 0.1)))))
    (elpy-shell-get-or-create-process))

Finally I have found the solution.

  (defun lispy--python-poetry-name ()
    (let* ((venv-name (if conda-project-env-path
                          (car (last (file-name-split conda-project-env-path)))
                        "default"))
           (project (project-current)))
      (if project
          (concat venv-name "-" (car (last (file-name-split (project-root project)))))
        venv-name)))
  
  (defun lispy--python-proc (&optional name)
    (let* ((proc-name (or name (lispy--python-proc-name)))
           (project (project-current))
           (process (get-process proc-name)))
      (if (process-live-p process)
          process
        (let* ((python-shell-font-lock-enable nil)
               (inferior-python-mode-hook nil)
               (buffer
                (let ((python-shell-completion-native-enable nil)
                      (default-directory (or (and project (project-root project))
                                             default-directory)))
                  (python-shell-make-comint (python-shell-parse-command) proc-name nil nil))))
  
          (with-current-buffer buffer
            (let ((cumtime 0))
              (while (and (when (boundp 'python-shell--first-prompt-received)
                            (not python-shell--first-prompt-received))
                          (< cumtime 3))
                (sleep-for 0.1)
                (setq cumtime (+ cumtime 0.1)))))
  
          (setq lispy--python-middleware-file
                (if (file-name-absolute-p lispy-python-middleware-file)
                    lispy-python-middleware-file
                  (expand-file-name "lispy-python.py" lispy-site-directory)))
          (setq lispy--python-init-file lispy-python-init-file)
          (setq process (get-buffer-process buffer))
          (unless (process-live-p process)
            (pop-to-buffer buffer)
            (user-error "Could not start %s" python-binary-name))
          (with-current-buffer buffer
            (python-shell-completion-native-turn-on)
            (setq lispy-python-buf buffer)
            (lispy-python-middleware-reload)))
        process)))