From 4845ca5f07fb585c06415e19dfa052fce9719c70 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Wed, 13 Mar 2024 21:40:34 +0100 Subject: [PATCH] fix bug in combination with laguage installation --- scripts/main.go | 18 ++++++++++++++ scripts/python/create_response.py | 3 ++- scripts/python/main.py | 3 +-- .../package_mangement/check_packages.py | 14 ++--------- .../package_mangement/downlaod_languages.py | 24 +++++++++++++++---- .../package_mangement/note_all_packages.py | 22 +++++++++++++++++ 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 scripts/python/package_mangement/note_all_packages.py diff --git a/scripts/main.go b/scripts/main.go index 7a25b0c..d439773 100644 --- a/scripts/main.go +++ b/scripts/main.go @@ -2,9 +2,11 @@ package main import ( "bufio" + "bytes" "encoding/json" "fmt" "io/ioutil" + "log" "net/http" "os" "os/exec" @@ -16,6 +18,21 @@ type Message struct { To string `json:"to"` } +func getAllPackages() { + TurnOn := exec.Command("python", "scripts/python/package_mangement/note_all_packages.py") + // Create a buffer to capture the standard output. + var out bytes.Buffer + TurnOn.Stdout = &out + + // Execute the command. + err := TurnOn.Run() + if err != nil { + log.Fatal(err) + } + + // Retrieve and print the standard output from the command. + fmt.Println("Output:", out.String()) +} func handleRequest(w http.ResponseWriter, r *http.Request) { // Read the request body body, err := ioutil.ReadAll(r.Body) @@ -67,6 +84,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { } func main() { + getAllPackages() // Define the HTTP handler function http.HandleFunc("/api", handleRequest) diff --git a/scripts/python/create_response.py b/scripts/python/create_response.py index ccd5495..de3b2ff 100644 --- a/scripts/python/create_response.py +++ b/scripts/python/create_response.py @@ -2,7 +2,7 @@ import json from datetime import datetime, timezone def create_respone(translated, from_code, to_code, error): -# Creating a dictionary that matches the structure of your JSON + # Creating a dictionary that matches the structure of your JSON data = { "from": from_code, "to": to_code, @@ -15,3 +15,4 @@ def create_respone(translated, from_code, to_code, error): # Printing the JSON string to the console print(json_string) + diff --git a/scripts/python/main.py b/scripts/python/main.py index 414da83..356226b 100644 --- a/scripts/python/main.py +++ b/scripts/python/main.py @@ -11,13 +11,12 @@ def main(): # Check if languages are installed is_installed = check_languages_installed(from_lang, to_lang) - if not is_installed: # Attempt to install the languages installed = install_language(from_lang, to_lang) if installed != 0: # Installation failed - error = "Error: Installation failed" + error = f"No available package for translating from {from_lang} to {to_lang}" create_respone("None", from_lang, to_lang, error) return # Exit the function early diff --git a/scripts/python/package_mangement/check_packages.py b/scripts/python/package_mangement/check_packages.py index 6ad9515..79557bb 100644 --- a/scripts/python/package_mangement/check_packages.py +++ b/scripts/python/package_mangement/check_packages.py @@ -1,28 +1,18 @@ import csv def check_languages_installed(from_code, to_code): - """ - Check if the language codes match the given criteria in a predefined .csv file. - - Parameters: - - from_code (str): The source language code to check. - - to_code (str): The target language code to check. - - Outputs: - - Prints True if a matching row is found, else prints False. - """ try: # Assuming the CSV file is named 'languages.csv' and located in the same directory - with open('installed_packages_info.csv', mode='r') as csv_file: + with open('data/installed_packages.csv', mode='r') as csv_file: csv_reader = csv.reader(csv_file) for row in csv_reader: # Check if the current row matches the criteria if len(row) >= 2 and row[0] == from_code and row[1] == to_code: return True - # If the loop completes without finding a match, print False return False except FileNotFoundError: return 1 except Exception: return 1 + \ No newline at end of file diff --git a/scripts/python/package_mangement/downlaod_languages.py b/scripts/python/package_mangement/downlaod_languages.py index 93a8bea..9400f9e 100644 --- a/scripts/python/package_mangement/downlaod_languages.py +++ b/scripts/python/package_mangement/downlaod_languages.py @@ -1,18 +1,32 @@ import argostranslate.translate import argostranslate.package +from package_mangement.note_all_packages import write_installed_packages_to_csv def install_language(from_code, to_code): - try: + try: argostranslate.package.update_package_index() available_packages = argostranslate.package.get_available_packages() - available_package = list( + # Convert the filter result into a list + filtered_packages = list( filter( lambda x: x.from_code == from_code and x.to_code == to_code, available_packages ) - )[0] + ) + # Check if the filtered list is empty + if not filtered_packages: + print(f"No available package for translating from {from_code} to {to_code}") + return 1 + + # If we have at least one package, proceed to download and install + available_package = filtered_packages[0] 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() + return 0 - except Exception: - return 1 + except Exception as e: + print(f"An error occurred: {e}") + return e \ No newline at end of file diff --git a/scripts/python/package_mangement/note_all_packages.py b/scripts/python/package_mangement/note_all_packages.py new file mode 100644 index 0000000..5f6a22e --- /dev/null +++ b/scripts/python/package_mangement/note_all_packages.py @@ -0,0 +1,22 @@ +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