fix bug in combination with laguage installation
This commit is contained in:
parent
da8141544b
commit
4845ca5f07
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -16,6 +18,21 @@ type Message struct {
|
||||||
To string `json:"to"`
|
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) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
// Read the request body
|
// Read the request body
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
@ -67,6 +84,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
getAllPackages()
|
||||||
// Define the HTTP handler function
|
// Define the HTTP handler function
|
||||||
http.HandleFunc("/api", handleRequest)
|
http.HandleFunc("/api", handleRequest)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import json
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
def create_respone(translated, from_code, to_code, error):
|
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 = {
|
data = {
|
||||||
"from": from_code,
|
"from": from_code,
|
||||||
"to": to_code,
|
"to": to_code,
|
||||||
|
@ -15,3 +15,4 @@ def create_respone(translated, from_code, to_code, error):
|
||||||
# Printing the JSON string to the console
|
# Printing the JSON string to the console
|
||||||
print(json_string)
|
print(json_string)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,13 +11,12 @@ def main():
|
||||||
|
|
||||||
# Check if languages are installed
|
# Check if languages are installed
|
||||||
is_installed = check_languages_installed(from_lang, to_lang)
|
is_installed = check_languages_installed(from_lang, to_lang)
|
||||||
|
|
||||||
if not is_installed:
|
if not is_installed:
|
||||||
# Attempt to install the languages
|
# Attempt to install the languages
|
||||||
installed = install_language(from_lang, to_lang)
|
installed = install_language(from_lang, to_lang)
|
||||||
if installed != 0:
|
if installed != 0:
|
||||||
# Installation failed
|
# 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)
|
create_respone("None", from_lang, to_lang, error)
|
||||||
return # Exit the function early
|
return # Exit the function early
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,18 @@
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
def check_languages_installed(from_code, to_code):
|
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:
|
try:
|
||||||
# Assuming the CSV file is named 'languages.csv' and located in the same directory
|
# 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)
|
csv_reader = csv.reader(csv_file)
|
||||||
for row in csv_reader:
|
for row in csv_reader:
|
||||||
# Check if the current row matches the criteria
|
# Check if the current row matches the criteria
|
||||||
if len(row) >= 2 and row[0] == from_code and row[1] == to_code:
|
if len(row) >= 2 and row[0] == from_code and row[1] == to_code:
|
||||||
return True
|
return True
|
||||||
# If the loop completes without finding a match, print False
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return 1
|
return 1
|
||||||
except Exception:
|
except Exception:
|
||||||
return 1
|
return 1
|
||||||
|
|
|
@ -1,18 +1,32 @@
|
||||||
import argostranslate.translate
|
import argostranslate.translate
|
||||||
import argostranslate.package
|
import argostranslate.package
|
||||||
|
from package_mangement.note_all_packages import write_installed_packages_to_csv
|
||||||
|
|
||||||
|
|
||||||
def install_language(from_code, to_code):
|
def install_language(from_code, to_code):
|
||||||
try:
|
try:
|
||||||
argostranslate.package.update_package_index()
|
argostranslate.package.update_package_index()
|
||||||
available_packages = argostranslate.package.get_available_packages()
|
available_packages = argostranslate.package.get_available_packages()
|
||||||
available_package = list(
|
# Convert the filter result into a list
|
||||||
|
filtered_packages = list(
|
||||||
filter(
|
filter(
|
||||||
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
|
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()
|
download_path = available_package.download()
|
||||||
argostranslate.package.install_from_path(download_path)
|
argostranslate.package.install_from_path(download_path)
|
||||||
|
|
||||||
|
# After successful installation, append package details to the CSV file
|
||||||
|
write_installed_packages_to_csv()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
return 1
|
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