Merge pull request 'rm .csv reading and switched to direct package reading' (#13) from code_improvement into main
Reviewed-on: http://192.168.178.135:3000/Fabelous/GO-Translator/pulls/13
This commit is contained in:
commit
7e91e39dbe
69
main.go
69
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, 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
|
||||
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 {
|
||||
// The directory exists, indicating the language pair is installed
|
||||
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) {
|
||||
// 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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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