Merge pull request 'fix bug in combination with laguage installation' (#5) from fixed_bug_language_install into main
Reviewed-on: http://192.168.178.135:3000/Fabelous/Go_server_w_argos_translate/pulls/5
This commit is contained in:
commit
d89b123ab0
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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
|
|
@ -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()
|
||||
|
Loading…
Reference in New Issue