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"
"fmt"
"path"
"strings"
"strconv"
"strings"
"srs.epita.fr/fic-server/libfic"
"gopkg.in/russross/blackfriday.v2"
"srs.epita.fr/fic-server/libfic"
)
func fixnbsp(s string) string {
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.
func getExercices(i Importer, theme fic.Theme) ([]string, error) {
// GetExercices returns all exercice directories existing in a given theme, considering the given Importer.
func GetExercices(i Importer, theme fic.Theme) ([]string, error) {
var exercices []string
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) {
var exercices []string
if exercices, err = getExercices(i, theme); err != nil {
if exercices, err = GetExercices(i, theme); err != nil {
return
} else {
dmap = map[int64]fic.Exercice{}
@ -81,17 +81,18 @@ func parseExerciceDirname(edir string) (eid int, ename string, err error) {
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
// BuildExercice creates an Exercice from a given importer.
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) {
e = &fic.Exercice{}
e.Path = epath
edir := path.Base(epath)
edir = path.Base(epath)
var err error
eid, e.Title, err = parseExerciceDirname(edir)
if err != nil {
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)
@ -130,7 +131,7 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
}
// Parse challenge.txt
p, err := parseExerciceParams(i, epath)
p, err = parseExerciceParams(i, epath)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err))
return
@ -167,7 +168,7 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
for k, _ := range *dmap {
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,29 +187,43 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
e.VideoURI = ""
}
// Create or update the exercice
err = theme.SaveNamedExercice(&e)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice save: %s", edir, err))
return
}
return
}
// Import eercice tags
if _, err := e.WipeTags(); err != nil {
errs = append(errs, fmt.Sprintf("%q: Unable to wipe tags: %s", edir, err))
}
for _, tag := range p.Tags {
if _, err := e.AddTag(tag); err != nil {
errs = append(errs, fmt.Sprintf("%q: Unable to add tag: %s", edir, err))
// 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
err = theme.SaveNamedExercice(e)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice save: %s", edir, err))
return
}
// Import eercice tags
if _, err := e.WipeTags(); err != nil {
errs = append(errs, fmt.Sprintf("%q: Unable to wipe tags: %s", edir, err))
}
for _, tag := range p.Tags {
if _, err := e.AddTag(tag); err != nil {
errs = append(errs, fmt.Sprintf("%q: Unable to add tag: %s", edir, err))
return
}
}
}
return
}
// SyncExercices imports new or updates existing exercices, in a given theme.
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())
} else {
emap := map[string]int{}
@ -217,9 +232,11 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
for _, edir := range exercices {
e, eid, cur_errs := SyncExercice(i, theme, path.Join(theme.Path, edir), &dmap)
emap[e.Title] = eid
dmap[int64(eid)] = e
errs = append(errs, cur_errs...)
if e != nil {
emap[e.Title] = eid
dmap[int64(eid)] = *e
errs = append(errs, cur_errs...)
}
}
// Remove old exercices
@ -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.
func ApiListRemoteExercices(theme fic.Theme, _ []byte) (interface{}, error) {
return getExercices(GlobalImporter, theme)
return GetExercices(GlobalImporter, theme)
}