master
Raw Download raw file
 1// Questioner contains the data necessary for retrieving questions
 2package main
 3
 4import (
 5	"bufio"
 6	"fmt"
 7	"log"
 8	"os"
 9	"strings"
10)
11
12// assignCategory figures out what category
13// this question is a member of
14func assignCategory(file *os.File) string {
15	tagPath := strings.Replace(file.Name(), serverConfig.QUESTIONS, "", 1)
16	tags := strings.Split(tagPath, string(os.PathSeparator))
17	tags[len(tags)-1] = strings.Replace(tags[len(tags)-1], ".csv", "", -1)
18	if tags[0] == "" {
19		tags = tags[1:]
20	}
21	return strings.Join(tags, CATEGORY_SEPARATOR)
22}
23
24// parseLine parses a csv line and returns a single record
25func parseLine(line string) (Record, error) {
26	var record Record
27	if strings.HasPrefix(line, "#") {
28		return record, nil
29	} else if strings.Compare(strings.TrimSpace(line), "") == 0 {
30		return record, nil
31	}
32	tokens := strings.Split(line, ",")
33	if len(tokens) < 3 {
34		return record, fmt.Errorf("Unable to parse line for %s", line)
35	}
36	record.Question = strings.TrimSpace(tokens[0])
37	record.Answer = strings.TrimSpace(tokens[1])
38	if len(tokens) >= 4 {
39		record.Reference = strings.TrimSpace(tokens[3])
40	}
41	return record, nil
42}
43
44// loadFile reads the lines of the Answer directory and returns an array of Records
45// The questions are expected in the form of a csv in the order
46// question, Category, category
47// Lines starting with '#' are ignored
48func LoadFile(file *os.File) error {
49	log.Printf("Loading file: %+v", file.Name())
50	if !strings.HasSuffix(file.Name(), ".csv") {
51		return nil
52	}
53	catPath := assignCategory(file)
54	scanner := bufio.NewScanner(file)
55	for scanner.Scan() {
56		if record, err := parseLine(scanner.Text()); err != nil {
57			log.Printf("Error while parsing line: %+v.", err)
58		} else {
59			if record.Answer != "" {
60				record.Path = catPath
61				if err := addRecordToDB(&record); err != nil {
62					log.Printf("Error loading record into DB: %+v.", err)
63				}
64			}
65		}
66	}
67	return nil
68}
69
70// loadRecords loads the records into memory according to
71// the supplied configuration for the given file.
72func loadRecords(path string, fileInfo os.FileInfo, err error) error {
73	file, err := os.Open(path)
74	if err != nil {
75		return err
76	}
77	defer file.Close()
78	fileStats, err := file.Stat()
79	if err != nil {
80		return err
81	}
82	if fileStats.Mode().IsRegular() {
83		err = LoadFile(file)
84		if err != nil {
85			return err
86		}
87	}
88	return nil
89}