sync: Extract function that builds an exercice from importer

This commit is contained in:
nemunaire 2019-07-04 20:12:59 +02:00
parent 682598fdbb
commit 3f99771910

View File

@ -4,19 +4,19 @@ import (
"errors" "errors"
"fmt" "fmt"
"path" "path"
"strings"
"strconv" "strconv"
"strings"
"srs.epita.fr/fic-server/libfic"
"gopkg.in/russross/blackfriday.v2" "gopkg.in/russross/blackfriday.v2"
"srs.epita.fr/fic-server/libfic"
) )
func fixnbsp(s string) string { func fixnbsp(s string) string {
return strings.Replace(strings.Replace(strings.Replace(s, " ?", " ?", -1), " !", " !", -1), " :", " :", -1) return strings.Replace(strings.Replace(strings.Replace(s, " ?", " ?", -1), " !", " !", -1), " :", " :", -1)
} }
// getExercices returns all exercice directories existing in a given theme, considering the given Importer. // GetExercices returns all exercice directories existing in a given theme, considering the given Importer.
func getExercices(i Importer, theme fic.Theme) ([]string, error) { func GetExercices(i Importer, theme fic.Theme) ([]string, error) {
var exercices []string var exercices []string
if len(theme.Path) == 0 { if len(theme.Path) == 0 {
@ -36,7 +36,7 @@ func getExercices(i Importer, theme fic.Theme) ([]string, error) {
func buildDependancyMap(i Importer, theme fic.Theme) (dmap map[int64]fic.Exercice, err error) { func buildDependancyMap(i Importer, theme fic.Theme) (dmap map[int64]fic.Exercice, err error) {
var exercices []string var exercices []string
if exercices, err = getExercices(i, theme); err != nil { if exercices, err = GetExercices(i, theme); err != nil {
return return
} else { } else {
dmap = map[int64]fic.Exercice{} dmap = map[int64]fic.Exercice{}
@ -81,17 +81,18 @@ func parseExerciceDirname(edir string) (eid int, ename string, err error) {
return return
} }
// SyncExercice imports new or updates existing given exercice. // BuildExercice creates an Exercice from a given importer.
func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic.Exercice) (e fic.Exercice, eid int, errs []string) { func BuildExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic.Exercice) (e *fic.Exercice, p ExerciceParams, eid int, edir string, errs []string) {
var err error e = &fic.Exercice{}
e.Path = epath e.Path = epath
edir := path.Base(epath) edir = path.Base(epath)
var err error
eid, e.Title, err = parseExerciceDirname(edir) eid, e.Title, err = parseExerciceDirname(edir)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to parse exercice directory: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: unable to parse exercice directory: %s", edir, err))
return return nil, p, eid, edir, errs
} }
e.URLId = fic.ToURLid(e.Title) e.URLId = fic.ToURLid(e.Title)
@ -130,7 +131,7 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
} }
// Parse challenge.txt // Parse challenge.txt
p, err := parseExerciceParams(i, epath) p, err = parseExerciceParams(i, epath)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err))
return return
@ -167,7 +168,7 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
for k, _ := range *dmap { for k, _ := range *dmap {
dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k)) dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k))
} }
errs = append(errs, fmt.Sprintf("%q: Unable to find required dependancy %d (available at time of processing: %s)", edir, p.Dependencies[0].Id, strings.Join(dmap_keys, ","))) errs = append(errs, fmt.Sprintf("%q: Unable to find required exercice dependancy %d (available at time of processing: %s)", edir, p.Dependencies[0].Id, strings.Join(dmap_keys, ",")))
} }
} }
} }
@ -186,8 +187,20 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
e.VideoURI = "" e.VideoURI = ""
} }
return
}
// SyncExercice imports new or updates existing given exercice.
func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic.Exercice) (e *fic.Exercice, eid int, errs []string) {
var err error
var edir string
var p ExerciceParams
e, p, eid, edir, errs = BuildExercice(i, theme, epath, dmap)
if e != nil {
// Create or update the exercice // Create or update the exercice
err = theme.SaveNamedExercice(&e) err = theme.SaveNamedExercice(e)
if err != nil { if err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice save: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: error on exercice save: %s", edir, err))
return return
@ -203,12 +216,14 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
return return
} }
} }
}
return return
} }
// SyncExercices imports new or updates existing exercices, in a given theme. // SyncExercices imports new or updates existing exercices, in a given theme.
func SyncExercices(i Importer, theme fic.Theme) (errs []string) { func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
if exercices, err := getExercices(i, theme); err != nil { if exercices, err := GetExercices(i, theme); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
} else { } else {
emap := map[string]int{} emap := map[string]int{}
@ -217,10 +232,12 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
for _, edir := range exercices { for _, edir := range exercices {
e, eid, cur_errs := SyncExercice(i, theme, path.Join(theme.Path, edir), &dmap) e, eid, cur_errs := SyncExercice(i, theme, path.Join(theme.Path, edir), &dmap)
if e != nil {
emap[e.Title] = eid emap[e.Title] = eid
dmap[int64(eid)] = e dmap[int64(eid)] = *e
errs = append(errs, cur_errs...) errs = append(errs, cur_errs...)
} }
}
// Remove old exercices // Remove old exercices
if exercices, err := theme.GetExercices(); err == nil { if exercices, err := theme.GetExercices(); err == nil {
@ -236,5 +253,5 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
// ApiListRemoteExercices is an accessor letting foreign packages to access remote exercices list. // ApiListRemoteExercices is an accessor letting foreign packages to access remote exercices list.
func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) { func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) {
return getExercices(GlobalImporter, theme) return GetExercices(GlobalImporter, theme)
} }