1
0
Fork 0
mirror of https://github.com/on4kjm/FLEcli.git synced 2025-02-16 20:12:35 +01:00
FLEcli/fleprocess/load_file.go

402 lines
13 KiB
Go
Raw Normal View History

2020-07-29 08:22:13 +02:00
package fleprocess
2020-06-25 21:05:54 +02:00
2020-05-27 22:29:59 +02:00
/*
2020-05-28 12:40:53 +02:00
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
2020-05-27 22:29:59 +02:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
2020-06-25 21:05:54 +02:00
"bufio"
2020-05-27 22:29:59 +02:00
"fmt"
2020-05-29 22:39:23 +02:00
"log"
"os"
2020-05-30 21:07:55 +02:00
"regexp"
2020-08-09 16:15:40 +02:00
"strings"
2020-07-10 22:49:28 +02:00
"time"
2020-05-27 22:29:59 +02:00
)
// LoadFile FIXME:
// returns nill if failure to process
2020-07-29 08:22:13 +02:00
func LoadFile(inputFilename string, isInterpolateTime bool) (filleFullLog []LogLine, isProcessedOK bool) {
2020-05-29 22:39:23 +02:00
file, err := os.Open(inputFilename)
2020-06-25 21:05:54 +02:00
2020-05-29 22:39:23 +02:00
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
2020-06-25 21:05:54 +02:00
2020-05-29 22:39:23 +02:00
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var txtlines []string
2020-06-25 21:05:54 +02:00
2020-05-29 22:39:23 +02:00
for scanner.Scan() {
txtlines = append(txtlines, scanner.Text())
}
2020-06-25 21:05:54 +02:00
2020-06-27 22:53:23 +02:00
if error := scanner.Err(); error != nil {
log.Fatal(error)
}
2020-05-29 22:39:23 +02:00
file.Close()
2020-05-30 21:07:55 +02:00
2020-09-25 23:13:36 +02:00
//isInferTimeFatalError is set to true is something bad happened while storing time gaps.
isInferTimeFatalError := false
regexpLineComment := regexp.MustCompile(`^[[:blank:]]*#`)
regexpOnlySpaces := regexp.MustCompile(`^\s+$`)
regexpSingleMultiLineComment := regexp.MustCompile(`^[[:blank:]]*{.+}$`)
regexpStartMultiLineComment := regexp.MustCompile(`^[[:blank:]]*{`)
regexpEndMultiLineComment := regexp.MustCompile(`}$`)
//FIXME: fields can delimited with space or TAB ("(?i)^mywwff\s+")
regexpHeaderMyCall := regexp.MustCompile(`(?i)^mycall\s+`)
regexpHeaderOperator := regexp.MustCompile(`(?i)^operator\s+`)
regexpHeaderMyWwff := regexp.MustCompile(`(?i)^mywwff\s+`)
regexpHeaderMySota := regexp.MustCompile(`(?i)^mysota\s+`)
regexpHeaderMyPota := regexp.MustCompile(`(?i)^mypota\s+`)
regexpHeaderMyGrid := regexp.MustCompile(`(?i)^mygrid\s+`)
regexpHeaderQslMsg := regexp.MustCompile(`(?i)^qslmsg\s+`)
regexpHeaderNickname := regexp.MustCompile(`(?i)^nickname\s+`)
2020-05-30 21:07:55 +02:00
2020-06-07 22:08:27 +02:00
headerMyCall := ""
headerOperator := ""
2020-06-08 22:13:58 +02:00
headerMyWWFF := ""
headerMySOTA := ""
headerMyPOTA := ""
2020-09-04 21:42:15 +02:00
headerMyGrid := ""
2020-06-09 12:48:38 +02:00
headerQslMsg := ""
headerNickname := ""
2020-09-10 22:09:59 +02:00
//headerDate := ""
2020-06-08 13:49:52 +02:00
lineCount := 0
2020-06-07 22:08:27 +02:00
2020-07-10 22:49:28 +02:00
wrkTimeBlock := InferTimeBlock{}
missingTimeBlockList := []InferTimeBlock{}
2020-06-25 21:05:54 +02:00
var isInMultiLine = false
// var cleanedInput []string
2020-06-08 13:49:52 +02:00
var errorLog []string
2020-06-22 23:00:01 +02:00
var previousLogLine LogLine
fullLog := []LogLine{}
2020-06-25 21:05:54 +02:00
2020-05-30 22:12:45 +02:00
//Loop through all the stored lined
2020-05-29 22:39:23 +02:00
for _, eachline := range txtlines {
2020-06-08 13:49:52 +02:00
lineCount++
2020-05-30 22:12:45 +02:00
// ****
// ** Lets do some house keeping first by dropping the unnecessary lines
2020-05-30 22:12:45 +02:00
// ****
2020-05-30 21:07:55 +02:00
//Skip the line if it starts with "#"
2020-06-25 21:05:54 +02:00
if regexpLineComment.MatchString(eachline) {
2020-05-30 21:07:55 +02:00
continue
}
//Skip if line is empty or blank
2020-06-25 21:05:54 +02:00
if (len(eachline) == 0) || (regexpOnlySpaces.MatchString(eachline)) {
2020-05-30 21:07:55 +02:00
continue
}
2020-05-30 22:12:45 +02:00
// Process multi-line comments
2020-06-25 21:05:54 +02:00
if regexpStartMultiLineComment.MatchString(eachline) {
//Single-line "multi-line" comment
2020-06-25 21:05:54 +02:00
if regexpSingleMultiLineComment.MatchString(eachline) {
continue
2020-05-30 22:12:45 +02:00
}
isInMultiLine = true
continue
}
2020-06-25 21:05:54 +02:00
if isInMultiLine {
if regexpEndMultiLineComment.MatchString(eachline) {
2020-05-30 22:12:45 +02:00
isInMultiLine = false
}
continue
}
// ****
2020-06-01 11:06:30 +02:00
// ** Process the Header block
2020-05-30 22:12:45 +02:00
// ****
2020-06-08 22:13:58 +02:00
//My Call
2020-06-25 21:05:54 +02:00
if regexpHeaderMyCall.MatchString(eachline) {
//Attempt to redefine value
if headerMyCall != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine MyCall at line %d", lineCount))
continue
}
2020-06-07 22:08:27 +02:00
errorMsg := ""
2020-06-25 21:05:54 +02:00
myCallList := regexpHeaderMyCall.Split(eachline, -1)
2020-08-09 16:15:40 +02:00
if len(strings.TrimSpace(myCallList[1])) > 0 {
headerMyCall, errorMsg = ValidateCall(strings.TrimSpace(myCallList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("My call: %s", headerMyCall))
2020-06-25 21:05:54 +02:00
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid myCall at line %d: %s (%s)", lineCount, myCallList[1], errorMsg))
}
2020-06-01 11:06:30 +02:00
}
2020-06-25 21:05:54 +02:00
//If there is no data after the marker, we just skip the data.
2020-06-01 10:22:56 +02:00
continue
}
2020-06-08 22:13:58 +02:00
//Operator
2020-06-25 21:05:54 +02:00
if regexpHeaderOperator.MatchString(eachline) {
//Attempt to redefine value
if headerOperator != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine Operator at line %d", lineCount))
continue
}
2020-06-07 22:08:27 +02:00
errorMsg := ""
2020-06-25 21:05:54 +02:00
myOperatorList := regexpHeaderOperator.Split(eachline, -1)
2020-08-09 16:15:40 +02:00
if len(strings.TrimSpace(myOperatorList[1])) > 0 {
headerOperator, errorMsg = ValidateCall(strings.TrimSpace(myOperatorList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("Operator: %s", headerOperator))
2020-06-25 21:05:54 +02:00
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid Operator at line %d: %s (%s)", lineCount, myOperatorList[1], errorMsg))
}
2020-06-07 22:08:27 +02:00
}
2020-06-08 22:13:58 +02:00
//If there is no data after the marker, we just skip the data.
2020-06-07 22:08:27 +02:00
continue
}
2020-06-08 22:13:58 +02:00
// My WWFF
2020-06-25 21:05:54 +02:00
if regexpHeaderMyWwff.MatchString(eachline) {
//Attempt to redefine value
if headerMyWWFF != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine MyWWFF at line %d", lineCount))
continue
}
2020-06-08 22:13:58 +02:00
errorMsg := ""
2020-06-25 21:05:54 +02:00
myWwffList := regexpHeaderMyWwff.Split(eachline, -1)
2020-08-09 16:15:40 +02:00
if len(strings.TrimSpace(myWwffList[1])) > 0 {
headerMyWWFF, errorMsg = ValidateWwff(strings.TrimSpace(myWwffList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("My WWFF: %s", headerMyWWFF))
2020-06-25 21:05:54 +02:00
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid \"My WWFF\" at line %d: %s (%s)", lineCount, myWwffList[1], errorMsg))
2020-06-08 22:13:58 +02:00
}
2020-06-25 21:05:54 +02:00
}
2020-06-08 22:13:58 +02:00
//If there is no data after the marker, we just skip the data.
continue
}
2020-06-25 21:05:54 +02:00
//My Pota
if regexpHeaderMyPota.MatchString(eachline) {
//Attempt to redefine value
if headerMyPOTA != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine MyPOTA at line %d", lineCount))
continue
}
errorMsg := ""
myPotaList := regexpHeaderMyPota.Split(eachline, -1)
if len(strings.TrimSpace(myPotaList[1])) > 0 {
headerMyPOTA, errorMsg = ValidatePota(strings.TrimSpace(myPotaList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("My Pota: %s", headerMyPOTA))
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid \"My POTA\" at line %d: %s (%s)", lineCount, myPotaList[1], errorMsg))
}
}
//If there is no data after the marker, we just skip the data.
continue
}
2020-06-08 22:13:58 +02:00
//My Sota
2020-06-25 21:05:54 +02:00
if regexpHeaderMySota.MatchString(eachline) {
//Attempt to redefine value
if headerMySOTA != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine MySOTA at line %d", lineCount))
continue
}
2020-06-08 22:13:58 +02:00
errorMsg := ""
2020-06-25 21:05:54 +02:00
mySotaList := regexpHeaderMySota.Split(eachline, -1)
2020-08-09 16:15:40 +02:00
if len(strings.TrimSpace(mySotaList[1])) > 0 {
headerMySOTA, errorMsg = ValidateSota(strings.TrimSpace(mySotaList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("My Sota: %s", headerMySOTA))
2020-06-25 21:05:54 +02:00
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid \"My SOTA\" at line %d: %s (%s)", lineCount, mySotaList[1], errorMsg))
2020-06-08 22:13:58 +02:00
}
}
//If there is no data after the marker, we just skip the data.
continue
}
2020-09-04 21:42:15 +02:00
//My Grid
if regexpHeaderMyGrid.MatchString(eachline) {
//Attempt to redefine value
if headerMyGrid != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine MyGrid at line %d", lineCount))
continue
}
2020-09-04 21:42:15 +02:00
errorMsg := ""
myGridList := regexpHeaderMyGrid.Split(eachline, -1)
if len(strings.TrimSpace(myGridList[1])) > 0 {
headerMyGrid, errorMsg = ValidateGridLocator(strings.TrimSpace(myGridList[1]))
// cleanedInput = append(cleanedInput, fmt.Sprintf("My Grid: %s", headerMyGrid))
2020-09-04 21:42:15 +02:00
if len(errorMsg) != 0 {
errorLog = append(errorLog, fmt.Sprintf("Invalid \"My Grid\" at line %d: %s (%s)", lineCount, myGridList[1], errorMsg))
}
}
//If there is no data after the marker, we just skip the data.
continue
}
2020-06-08 22:13:58 +02:00
//QSL Message
2020-06-25 21:05:54 +02:00
if regexpHeaderQslMsg.MatchString(eachline) {
myQslMsgList := regexpHeaderQslMsg.Split(eachline, -1)
if len(myQslMsgList[1]) > 0 {
2020-06-09 12:48:38 +02:00
headerQslMsg = myQslMsgList[1]
// cleanedInput = append(cleanedInput, fmt.Sprintf("QSL Message: %s", headerQslMsg))
2020-06-09 12:48:38 +02:00
}
//If there is no data after the marker, we just skip the data.
continue
}
2020-06-09 13:35:34 +02:00
//Nickname
2020-06-25 21:05:54 +02:00
if regexpHeaderNickname.MatchString(eachline) {
//Attempt to redefine value
if headerNickname != "" {
errorLog = append(errorLog, fmt.Sprintf("Attempt to redefine eQSL Nickname at line %d", lineCount))
continue
}
2020-06-25 21:05:54 +02:00
myNicknameList := regexpHeaderNickname.Split(eachline, -1)
2020-08-09 16:15:40 +02:00
if len(strings.TrimSpace(myNicknameList[1])) > 0 {
headerNickname = strings.TrimSpace(myNicknameList[1])
// cleanedInput = append(cleanedInput, fmt.Sprintf("eQSL Nickmane: %s", headerNickname))
2020-06-08 22:13:58 +02:00
}
//If there is no data after the marker, we just skip the data.
continue
}
2020-06-01 11:06:30 +02:00
// ****
// ** Process the data block
// ****
2020-06-25 21:05:54 +02:00
2020-06-22 23:00:01 +02:00
// Load the header values in the previousLogLine
previousLogLine.MyCall = headerMyCall
previousLogLine.Operator = headerOperator
previousLogLine.MyWWFF = headerMyWWFF
previousLogLine.MyPOTA = headerMyPOTA
2020-06-22 23:00:01 +02:00
previousLogLine.MySOTA = headerMySOTA
2020-09-04 21:42:15 +02:00
previousLogLine.MyGrid = headerMyGrid
2020-06-22 23:00:01 +02:00
previousLogLine.QSLmsg = headerQslMsg //previousLogLine.QslMsg is redundant
previousLogLine.Nickname = headerNickname
2020-07-10 22:49:28 +02:00
//parse a line
2020-06-22 23:00:01 +02:00
logline, errorLine := ParseLine(eachline, previousLogLine)
2020-07-10 22:49:28 +02:00
//we have a valid line (contains a call)
2020-06-22 23:00:01 +02:00
if logline.Call != "" {
fullLog = append(fullLog, logline)
2020-07-10 22:49:28 +02:00
//store time inference data
2020-09-25 23:13:36 +02:00
if isInterpolateTime && !isInferTimeFatalError {
2020-07-10 22:49:28 +02:00
var isEndOfGap bool
if isEndOfGap, err = wrkTimeBlock.storeTimeGap(logline, len(fullLog)); err != nil {
2020-09-25 23:13:36 +02:00
errorLog = append(errorLog, fmt.Sprintf("Fatal error at line %d: %s", lineCount, err))
isInferTimeFatalError = true
2020-07-10 22:49:28 +02:00
}
//If we reached the end of the time gap, we make the necessary checks and make our gap calculation
if isEndOfGap {
if err := wrkTimeBlock.finalizeTimeGap(); err != nil {
//If an error occurred it is a fatal error
2020-09-25 23:13:36 +02:00
errorLog = append(errorLog, fmt.Sprintf("Fatal error at line %d: %s", lineCount, err))
isInferTimeFatalError = true
}
if isInferTimeFatalError {
break
2020-07-10 22:49:28 +02:00
}
//add it to the gap collection
missingTimeBlockList = append(missingTimeBlockList, wrkTimeBlock)
//create a new block
wrkTimeBlock = InferTimeBlock{}
//Store this record in the new block as a new gap might be following
_, err := wrkTimeBlock.storeTimeGap(logline, len(fullLog))
//no real error or endOfGap processing as it has already been successfully processed
if err != nil {
errorLog = append(errorLog, fmt.Sprintf("Fatal error at line %d: %s", lineCount, err))
isInferTimeFatalError = true
}
2020-07-10 22:49:28 +02:00
}
}
2020-06-22 23:00:01 +02:00
}
2020-07-10 22:49:28 +02:00
//Store append the accumulated soft parsing errors into the global parsing error log file
2020-06-22 23:00:01 +02:00
if errorLine != "" {
2020-06-25 21:05:54 +02:00
errorLog = append(errorLog, fmt.Sprintf("Parsing error at line %d: %s ", lineCount, errorLine))
2020-06-22 23:00:01 +02:00
}
2020-07-10 22:49:28 +02:00
//store the current logline so that it can be used as a model when parsing the next line
2020-06-22 23:00:01 +02:00
previousLogLine = logline
2020-07-10 22:49:28 +02:00
//We go back to the top to process the next loaded log line (Continue not necessary here)
}
//***
//*** We have done processing the log file, so let's post process it
//***
//if asked to infer the date, lets update the loaded logfile accordingly
if isInterpolateTime {
2020-09-25 23:13:36 +02:00
//Do we have an open timeBlok that has not been closed.
if (wrkTimeBlock.noTimeCount > 0) && (wrkTimeBlock.nextValidTime.IsZero()) {
errorLog = append(errorLog, "Fatal error: missing new time to infer time")
2020-09-25 23:13:36 +02:00
} else {
for _, timeBlock := range missingTimeBlockList {
if err := timeBlock.validateTimeGap(); err != nil {
errorLog = append(errorLog, fmt.Sprintf("Fatal error: %s", err))
break
}
for i := 0; i < timeBlock.noTimeCount; i++ {
position := timeBlock.logFilePosition + i
pLogLine := &fullLog[position]
durationOffset := timeBlock.deltatime * time.Duration(i+1)
newTime := timeBlock.lastRecordedTime.Add(durationOffset)
updatedTimeString := newTime.Format("1504")
pLogLine.Time = updatedTimeString
}
2020-07-10 22:49:28 +02:00
}
}
}
displayLogSimple(fullLog)
2020-06-01 11:06:30 +02:00
//Display parsing errors, if any
if len(errorLog) != 0 {
fmt.Println("\nProcessing errors:")
for _, errorLogLine := range errorLog {
fmt.Println(errorLogLine)
}
2020-06-29 13:55:11 +02:00
isProcessedOK = false
2020-06-30 22:20:56 +02:00
} else {
2020-08-30 16:16:21 +02:00
fmt.Println("\nSuccessfully parsed ", lineCount, " lines.")
2020-06-29 13:55:11 +02:00
isProcessedOK = true
}
2020-06-29 13:55:11 +02:00
return fullLog, isProcessedOK
}
// displayLogSimple will print to stdout a simplified dump of a full log
func displayLogSimple(fullLog []LogLine) {
2020-06-22 23:00:01 +02:00
firstLine := true
for _, filledLogLine := range fullLog {
if firstLine {
fmt.Println(SprintHeaderValues(filledLogLine))
2020-08-09 16:15:40 +02:00
fmt.Print(SprintColumnTitles())
2020-06-22 23:00:01 +02:00
firstLine = false
}
fmt.Print(SprintLogInColumn(filledLogLine))
}
2020-08-09 16:15:40 +02:00
}