From ad09a4baecea9b8e1938b3dd76f830d006a73cc7 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Sat, 16 Mar 2024 11:21:40 +0100 Subject: [PATCH] rm .csv reading and switched to direct package reading --- data/installed_packages.csv | 0 main.go | 71 ++++++++++----------------------- translator/download.py | 4 +- translator/note_all_packages.py | 23 ----------- 4 files changed, 22 insertions(+), 76 deletions(-) delete mode 100644 data/installed_packages.csv delete mode 100644 translator/note_all_packages.py diff --git a/data/installed_packages.csv b/data/installed_packages.csv deleted file mode 100644 index e69de29..0000000 diff --git a/main.go b/main.go index dbab51e..6dc6f9b 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,6 @@ package main import ( "bufio" - "bytes" - "encoding/csv" "encoding/json" "fmt" "io" @@ -11,6 +9,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "time" ) @@ -28,7 +27,7 @@ type StreamResponse struct { Response string `json:"response"` } -const env = "/home/translator/env/bin/getPythonCommand()" +const linux = "python3" func createStreamResponse(fromLanguage, toLanguage, message string) string { response := StreamResponse{ @@ -72,8 +71,8 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { // Use a goroutine for streaming responses so we can proceed with the download go streamResponse(w, fromLanguage, toLanguage, messages) - // Start the getPythonCommand() command in the background - cmd := exec.Command(env, "translator/download.py", fromLanguage, toLanguage) + // Start the python command in the background + cmd := exec.Command(linux, "translator/download.py", fromLanguage, toLanguage) output, err := cmd.StdoutPipe() if err != nil { messages <- fmt.Sprintf("Error starting Download: %s", err.Error()) @@ -97,7 +96,7 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { } }() - // Read the output of the getPythonCommand() command and send it to the channel + // Read the output of the python command and send it to the channel scanner := bufio.NewScanner(output) firstOutputReceived := false for scanner.Scan() { @@ -115,7 +114,6 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error()) return } - return } func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) { @@ -126,8 +124,8 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage // A goroutine will manage sending streamed responses go streamResponse(w, fromLanguage, toLanguage, messages) - // Start the getPythonCommand() command in the background - cmd := exec.Command(env, "translator/translate.py", message, fromLanguage, toLanguage) + // Start the python command in the background + cmd := exec.Command(linux, "translator/translate.py", message, fromLanguage, toLanguage) output, err := cmd.StdoutPipe() if err != nil { messages <- fmt.Sprintf("Error starting Translation: %s", err.Error()) @@ -140,7 +138,7 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage return } - // Read the output of the getPythonCommand() command and send it through the channel + // Read the output of the python command and send it through the channel scanner := bufio.NewScanner(output) for scanner.Scan() { messages <- scanner.Text() // Sends each line of the output to the stream @@ -155,50 +153,24 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage } func CheckLanguagesInstalled(fromCode, toCode string) (bool, error) { - // Open the CSV file - file, err := os.Open("data/installed_packages.csv") + // Construct the target directory path + homeDir, err := os.UserHomeDir() + if err != nil { + return false, fmt.Errorf("unable to determine the user home directory: %w", err) + } + targetDir := filepath.Join(homeDir, ".local", "share", "argos-translate", "packages", fromCode+"_"+toCode) + + // Check if the directory exists + _, err = os.Stat(targetDir) if err != nil { if os.IsNotExist(err) { - return false, fmt.Errorf("file not found") - } - return false, err - } - defer file.Close() - - // Create a new CSV reader - reader := csv.NewReader(file) - - // Iterate through the records - for { - record, err := reader.Read() - if err != nil { - if err == io.EOF { - break - } - return false, err - } - if len(record) >= 2 && record[0] == fromCode && record[1] == toCode { - return true, nil + return false, nil // Directory does not exist, indicating the language pair is not installed } + return false, fmt.Errorf("error checking for language directory: %w", err) } - return false, nil -} -func getAllPackages() error { - cmd := exec.Command(env, "translator/note_all_packages.py") - // Create a buffer to capture the standard output. - var out bytes.Buffer - cmd.Stdout = &out - - // Execute the command. - err := cmd.Run() - if err != nil { - return err - } - - // Log the captured output. - fmt.Println("Output:", out.String()) - return nil + // The directory exists, indicating the language pair is installed + return true, nil } func handleRequest(w http.ResponseWriter, r *http.Request) { // Read the request body @@ -244,7 +216,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } func main() { - getAllPackages() // Define the HTTP handler function http.HandleFunc("/api", handleRequest) diff --git a/translator/download.py b/translator/download.py index d48c1d9..c6c20d2 100644 --- a/translator/download.py +++ b/translator/download.py @@ -1,6 +1,5 @@ import argostranslate.translate import argostranslate.package -from note_all_packages import write_installed_packages_to_csv import sys def install_language(): @@ -24,8 +23,7 @@ def install_language(): download_path = available_package.download() argostranslate.package.install_from_path(download_path) # After successful installation, append package details to the CSV file - write_installed_packages_to_csv() - print("Finished Download") + print(f"Finished Download, now you can translate from {from_lang} to {to_lang}") except Exception as e: return e diff --git a/translator/note_all_packages.py b/translator/note_all_packages.py deleted file mode 100644 index 6c5ee54..0000000 --- a/translator/note_all_packages.py +++ /dev/null @@ -1,23 +0,0 @@ -import argostranslate.package -import csv - - -# Function to write installed package details into a CSV file -def write_installed_packages_to_csv(filename='data/installed_packages.csv'): - # Fetch all installed packages - installed_packages = argostranslate.package.get_installed_packages() - # Open/Create a CSV file to write into - with open(filename, mode='w', newline='', encoding='utf-8') as file: - writer = csv.writer(file) - - # Write package details row by row - for package in installed_packages: - from_language = package.from_code - to_language = package.to_code - package_name = package.from_name - - writer.writerow([from_language, to_language, package_name]) - -if __name__ == "__main__": - write_installed_packages_to_csv() - \ No newline at end of file