1
0
Fork 0
mirror of https://github.com/on4kjm/FLEcli.git synced 2025-03-11 05:23:18 +01:00
FLEcli/fleprocess/load_file.go

368 lines
11 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
)
2020-07-29 08:22:13 +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
regexpLineComment := regexp.MustCompile("^[[:blank:]]*#")
regexpOnlySpaces := regexp.MustCompile("^\\s+$")
regexpSingleMultiLineComment := regexp.MustCompile("^[[:blank:]]*{.+}$")
regexpStartMultiLineComment := regexp.MustCompile("^[[:blank:]]*{")
regexpEndMultiLineComment := regexp.MustCompile("}$")
regexpHeaderMyCall := regexp.MustCompile("(?i)^mycall ")
regexpHeaderOperator := regexp.MustCompile("(?i)^operator ")
regexpHeaderMyWwff := regexp.MustCompile("(?i)^mywwff ")
regexpHeaderMySota := regexp.MustCompile("(?i)^mysota ")
regexpHeaderMyGrid := regexp.MustCompile("(?i)^mygrid ")
regexpHeaderQslMsg := regexp.MustCompile("(?i)^qslmsg ")
regexpHeaderNickname := regexp.MustCompile("(?i)^nickname ")
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 := ""
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
2020-06-08 13:49:52 +02:00
var cleanedInput []string
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 droping the unecessary lines
// ****
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) {
2020-05-30 22:12:45 +02:00
//Single-line "multi-line" coment
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]))
2020-06-08 13:49:52 +02:00
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]))
2020-06-08 13:49:52 +02:00
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]))
2020-06-08 22:13:58 +02:00
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
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]))
2020-06-08 22:13:58 +02:00
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))
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))
}
//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])
2020-06-09 12:48:38 +02:00
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.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-09-10 22:09:59 +02:00
//previousLogLine.Date = headerDate
2020-06-22 23:00:01 +02:00
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
if isInterpolateTime {
var isEndOfGap bool
if isEndOfGap, err = wrkTimeBlock.storeTimeGap(logline, len(fullLog)); err != nil {
fmt.Println("\nProcessing errors:")
for _, errorLogLine := range errorLog {
fmt.Println(errorLogLine)
}
2020-07-10 22:49:28 +02:00
log.Println("Fatal error: ", err)
os.Exit(1)
}
//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 occured it is a fatal error
fmt.Println("\nProcessing errors:")
for _, errorLogLine := range errorLog {
fmt.Println(errorLogLine)
}
2020-07-10 22:49:28 +02:00
log.Println("Fatal error: ", err)
os.Exit(1)
}
//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
2020-08-30 16:16:21 +02:00
//no error or endOfGap processing as it has already been successfully processed
2020-07-10 22:49:28 +02:00
wrkTimeBlock.storeTimeGap(logline, len(fullLog))
}
}
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 {
for _, timeBlock := range missingTimeBlockList {
for i := 0; i < timeBlock.noTimeCount; i++ {
position := timeBlock.logFilePosition + i
pLogLine := &fullLog[position]
// durationOffset := time.Second * time.Duration(timeBlock.deltatime*(i+1))
durationOffset := timeBlock.deltatime * time.Duration(i+1)
newTime := timeBlock.lastRecordedTime.Add(durationOffset)
updatedTimeString := newTime.Format("1504")
pLogLine.Time = updatedTimeString
}
}
}
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
}