Translate CLI to french & extract pre-processing

This commit is contained in:
Gabriel Augendre 2021-08-21 09:07:07 +02:00
parent a3516cabf6
commit bf1f57eb00
3 changed files with 30 additions and 16 deletions

View file

@ -40,7 +40,7 @@ type InseeData struct {
func NewInseeData(inseeNumber string) (*InseeData, error) { func NewInseeData(inseeNumber string) (*InseeData, error) {
if len(inseeNumber) != 15 { if len(inseeNumber) != 15 {
return nil, fmt.Errorf("provided insee number must contain 15 characters") return nil, fmt.Errorf("le numéro INSEE number must contain 15 characters")
} }
num := inseeNumber num := inseeNumber
departmentCode := num[5:7] departmentCode := num[5:7]

18
main.go
View file

@ -4,19 +4,27 @@ import (
"flag" "flag"
"fmt" "fmt"
"insee_number_translator/data" "insee_number_translator/data"
"os"
) )
func main() { func main() {
var preProcess bool flag.Usage = func() {
flag.BoolVar(&preProcess, "pre-process", false, "Pre-process data files.") out := flag.CommandLine.Output()
fmt.Fprintf(out, "Usage: %s [flags] [numero_insee...]\n", os.Args[0])
fmt.Fprintf(out, "\nCe programme décode les informations contenues dans votre numéro INSEE (numéro de sécurité sociale français)")
fmt.Fprintf(out, "et vous les affiche d'une manière lisible et claire.\n")
flag.PrintDefaults()
fmt.Fprintf(out, "\nLes arguments numero_insee doivent comporter 15 caractères. Il est possible d'en spécifier plusieurs séparés par un espace.\n")
}
flag.Parse() flag.Parse()
if preProcess { numbers := flag.Args()
data.PreProcessRawData("data/raw_data", "data/curated_data") if len(numbers) == 0 {
flag.Usage()
return return
} }
numbers := flag.Args()
for _, number := range numbers { for _, number := range numbers {
insee, err := data.NewInseeData(number) insee, err := data.NewInseeData(number)
if err != nil { if err != nil {

View file

@ -1,4 +1,4 @@
package data package main
import ( import (
"encoding/csv" "encoding/csv"
@ -8,27 +8,33 @@ import (
"os" "os"
) )
func main() {
fmt.Println("This is intended for tinkerers only, not for end users.")
PreProcessRawData("data/raw_data", "data/curated_data")
}
func PreProcessRawData(sourceFolder, targetFolder string) { func PreProcessRawData(sourceFolder, targetFolder string) {
err := os.MkdirAll(targetFolder, 0755) err := os.MkdirAll(targetFolder, 0755)
if err != nil { if err != nil {
fmt.Printf("Error, couldn't create target folder %s: %s", targetFolder, err) fmt.Printf("Error, couldn't create target folder %s: %s", targetFolder, err)
return return
} }
err = preProcessCities(sourceFolder+"/commune2021.csv", targetFolder+"/cities.json") err = preProcessCities(sourceFolder+"/commune.csv", targetFolder+"/cities.json")
if err != nil { if err != nil {
fmt.Println("Error during cities pre processing") fmt.Fprintln(os.Stderr, "Error during cities pre processing")
fmt.Println(err.Error()) fmt.Fprintln(os.Stderr, err.Error())
} }
err = preProcessDepartments(sourceFolder+"/departement2021.csv", targetFolder+"/departments.json") err = preProcessDepartments(sourceFolder+"/departement.csv", targetFolder+"/departments.json")
if err != nil { if err != nil {
fmt.Println("Error during departments pre processing") fmt.Fprintln(os.Stderr, "Error during departments pre processing")
fmt.Println(err.Error()) fmt.Fprintln(os.Stderr, err.Error())
} }
err = preProcessCountries(sourceFolder+"/pays2021.csv", targetFolder+"/countries.json") err = preProcessCountries(sourceFolder+"/pays.csv", targetFolder+"/countries.json")
if err != nil { if err != nil {
fmt.Println("Error during countries pre processing") fmt.Fprintln(os.Stderr, "Error during countries pre processing")
fmt.Println(err.Error()) fmt.Fprintln(os.Stderr, err.Error())
} }
fmt.Println("Finished pre-processing data.")
} }
func preProcessCities(sourceFileName, targetFileName string) error { func preProcessCities(sourceFileName, targetFileName string) error {