rm .csv reading and switched to direct package reading
This commit is contained in:
parent
2f8c114f9f
commit
ad09a4baec
69
main.go
69
main.go
|
@ -2,8 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"encoding/csv"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -11,6 +9,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ type StreamResponse struct {
|
||||||
Response string `json:"response"`
|
Response string `json:"response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const env = "/home/translator/env/bin/getPythonCommand()"
|
const linux = "python3"
|
||||||
|
|
||||||
func createStreamResponse(fromLanguage, toLanguage, message string) string {
|
func createStreamResponse(fromLanguage, toLanguage, message string) string {
|
||||||
response := StreamResponse{
|
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
|
// Use a goroutine for streaming responses so we can proceed with the download
|
||||||
go streamResponse(w, fromLanguage, toLanguage, messages)
|
go streamResponse(w, fromLanguage, toLanguage, messages)
|
||||||
|
|
||||||
// Start the getPythonCommand() command in the background
|
// Start the python command in the background
|
||||||
cmd := exec.Command(env, "translator/download.py", fromLanguage, toLanguage)
|
cmd := exec.Command(linux, "translator/download.py", fromLanguage, toLanguage)
|
||||||
output, err := cmd.StdoutPipe()
|
output, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
messages <- fmt.Sprintf("Error starting Download: %s", err.Error())
|
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)
|
scanner := bufio.NewScanner(output)
|
||||||
firstOutputReceived := false
|
firstOutputReceived := false
|
||||||
for scanner.Scan() {
|
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())
|
messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {
|
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
|
// A goroutine will manage sending streamed responses
|
||||||
go streamResponse(w, fromLanguage, toLanguage, messages)
|
go streamResponse(w, fromLanguage, toLanguage, messages)
|
||||||
|
|
||||||
// Start the getPythonCommand() command in the background
|
// Start the python command in the background
|
||||||
cmd := exec.Command(env, "translator/translate.py", message, fromLanguage, toLanguage)
|
cmd := exec.Command(linux, "translator/translate.py", message, fromLanguage, toLanguage)
|
||||||
output, err := cmd.StdoutPipe()
|
output, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
messages <- fmt.Sprintf("Error starting Translation: %s", err.Error())
|
messages <- fmt.Sprintf("Error starting Translation: %s", err.Error())
|
||||||
|
@ -140,7 +138,7 @@ func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage
|
||||||
return
|
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)
|
scanner := bufio.NewScanner(output)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
messages <- scanner.Text() // Sends each line of the output to the stream
|
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) {
|
func CheckLanguagesInstalled(fromCode, toCode string) (bool, error) {
|
||||||
// Open the CSV file
|
// Construct the target directory path
|
||||||
file, err := os.Open("data/installed_packages.csv")
|
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 err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return false, fmt.Errorf("file not found")
|
return false, nil // Directory does not exist, indicating the language pair is not installed
|
||||||
}
|
}
|
||||||
return false, err
|
return false, fmt.Errorf("error checking for language directory: %w", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Create a new CSV reader
|
// The directory exists, indicating the language pair is installed
|
||||||
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 true, nil
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
// Read the request body
|
// Read the request body
|
||||||
|
@ -244,7 +216,6 @@ 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)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import argostranslate.translate
|
import argostranslate.translate
|
||||||
import argostranslate.package
|
import argostranslate.package
|
||||||
from note_all_packages import write_installed_packages_to_csv
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def install_language():
|
def install_language():
|
||||||
|
@ -24,8 +23,7 @@ def install_language():
|
||||||
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
|
# After successful installation, append package details to the CSV file
|
||||||
write_installed_packages_to_csv()
|
print(f"Finished Download, now you can translate from {from_lang} to {to_lang}")
|
||||||
print("Finished Download")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return e
|
return e
|
||||||
|
|
|
@ -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()
|
|
||||||
|
|
Loading…
Reference in New Issue