From be3daa2aa5b757daf4396e02cd583d68d4b0aaf1 Mon Sep 17 00:00:00 2001 From: hubisan Date: Tue, 15 Aug 2023 02:58:41 +0200 Subject: [PATCH] this is working it seems --- woerterbuch.el | 35 +++++++++++++++-------------------- init.py => woerterbuch.py | 6 +++--- 2 files changed, 18 insertions(+), 23 deletions(-) rename init.py => woerterbuch.py (98%) diff --git a/woerterbuch.el b/woerterbuch.el index 391b847..82d8ade 100644 --- a/woerterbuch.el +++ b/woerterbuch.el @@ -200,7 +200,7 @@ Returns a cons cell with the car being the word and cdr the bounds." ;; "Number of seconds program waits for the definition of the python functions.") (defvar woerterbuch--process-python-init-path - (expand-file-name "init.py" woerterbuch--package-directory) + (expand-file-name "woerterbuch.py" woerterbuch--package-directory) "Path to the file that holds the python init code.") (defvar woerterbuch--process-output nil @@ -216,28 +216,23 @@ It stores the output in `woerterbuch--process-output'." Loads the modules needed and defines the functions and variables. If RESTART is non-nil then kill the process and start it again." (when (and restart (process-live-p woerterbuch--process)) - (kill-process woerterbuch--process)) + (when-let* ((buffer-name woerterbuch--process-buffer-name) + (buffer (get-buffer buffer-name))) + (kill-buffer buffer)) + ;; Hope this will never result in an endless loop + (kill-process woerterbuch--process) + (while (process-live-p woerterbuch--process))) (if (process-live-p woerterbuch--process) woerterbuch--process (let* ((process-connection-type nil) ; use a pipe (coding-system-for-write 'utf-8-auto) (coding-system-for-read 'utf-8-auto) + (path woerterbuch--process-python-init-path) (process-buffer-name woerterbuch--process-buffer-name) (process-buffer (get-buffer-create process-buffer-name)) (process (start-process "woerterbuch python" process-buffer woerterbuch-process-python-programm - ;; "-i" path - ))) - ;; (set-process-filter process t) - ;; ;; Send the string to the process to load the modules and define the - ;; ;; functions. - ;; (unless (accept-process-output - ;; (process-send-string process - ;; (woerterbuch--read-file-contents - ;; woerterbuch--process-python-init-path)) - - ;; 10) - ;; (error "Was not able to start the process. Timeout reached (30 s)")) + "-u" path))) (setq woerterbuch--process process)))) (defun woerterbuch--process-capture-output (code) @@ -254,12 +249,12 @@ If RESTART is non-nil then kill the process and start it again." woerterbuch-process-timeout) (error "Timeout reached before output was received")) (when woerterbuch--process-output - woerterbuch--process-output - ;; (json-parse-string woerterbuch--process-output - ;; :object-type 'plist) - )) - ;; (set-process-filter process t) - ))) + (pcase woerterbuch--process-output + ("None\n" nil) + ("Invalid command\n" + (error "%s (%s)" "Python code is invalid" code)) + (output (json-parse-string output :object-type 'plist))))) + (set-process-filter process t)))) ;;; German Definitions diff --git a/init.py b/woerterbuch.py similarity index 98% rename from init.py rename to woerterbuch.py index 5ba4607..8b122d6 100644 --- a/init.py +++ b/woerterbuch.py @@ -170,8 +170,8 @@ def woerterbuch_definitions_and_synonyms(word: str, part_of_speech: str = None, # different to the param and the defintions with the synonyms for each # definition. output = { - "word param": word, - "word lemma": word_lemma, + "word-param": word, + "word-lemma": word_lemma, "definitions": definitions_and_synonyms, } return json.dumps(output, ensure_ascii=False) @@ -217,4 +217,4 @@ def process_command(command: str, args: str) -> Optional[str]: result = process_command(command, args) # Print the result of the executed command or an error message. - print(result) \ No newline at end of file + print(result)