Browse Source

Complete declaration table functionality

master
Riyyi 2 months ago
parent
commit
9340a2d10e
  1. 5
      go.mod
  2. 2
      go.sum
  3. 41
      main.go
  4. 99
      src/declaration.go
  5. 27
      src/file.go
  6. 64
      src/process.go
  7. 38
      src/util.go

5
go.mod

@ -2,6 +2,9 @@ module worklog
go 1.22.5
require github.com/alexflint/go-arg v1.5.1
require (
github.com/alexflint/go-arg v1.5.1
github.com/atotto/clipboard v0.1.4
)
require github.com/alexflint/go-scalar v1.2.0 // indirect

2
go.sum

@ -2,6 +2,8 @@ github.com/alexflint/go-arg v1.5.1 h1:nBuWUCpuRy0snAG+uIJ6N0UvYxpxA0/ghA/AaHxlT8
github.com/alexflint/go-arg v1.5.1/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

41
main.go

@ -5,12 +5,16 @@
package main
import "errors"
import "os"
import (
"errors"
"fmt"
"os"
"strconv"
import "github.com/alexflint/go-arg"
"github.com/alexflint/go-arg"
import "worklog/src"
"worklog/src"
)
type Args struct {
Decl string `arg:"-d,--decl" help:"Generate travel declaration table" placeholder:"MONTH"`
@ -19,27 +23,44 @@ type Args struct {
}
func (Args) Description() string {
return "\nworklog - process a worklog file\n"
return "worklog - process a worklog file\n"
}
func main() {
var args Args
parser := arg.MustParse(&args)
// File validation
_, err := os.Stat(args.File);
if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) {
parser.Fail("file was not readable: " + args.File)
}
// Month validation
var month int
if args.Decl != "" {
month, err = strconv.Atoi(args.Decl)
if err != nil || month < 1 || month > 12 {
parser.Fail("decl is not a valid month")
}
}
// Execute
if args.Process {
var api src.Api = src.MakeApi()
var process src.Process = src.MakeProcess()
var job = func(line string, line_number int) string {
return api.Process(line, line_number)
return process.Process(line, line_number)
}
src.Parse(args.File, job)
src.File.Parse(args.File, job, true)
}
if args.Decl != "" {
// TODO: generate declaration table..
if month > 0 {
var decl src.Declaration = src.MakeDeclaration(month)
var job = func(line string, line_number int) string {
return decl.Generate(line, line_number)
}
src.File.Parse(args.File, job, false)
fmt.Println(decl.Result())
}
}

99
src/declaration.go

@ -0,0 +1,99 @@
package src
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/atotto/clipboard"
)
type Location int
const (
NONE Location = iota // 0
HOME
OFFICE
VISIT
)
type Declaration struct {
month int
clock_in *regexp.Regexp
dates [31]Location
result string
}
// Constructor
func MakeDeclaration(month int) Declaration {
return Declaration{
month: month,
clock_in: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+IN\s+\|\s+[0-9]{2}:[0-9]{2}\s+\|\s+[a-zA-Z]+\s+\|`),
}
}
func (self *Declaration) Generate(line string, line_number int) string {
var err error
if self.clock_in.MatchString(line) {
err = self.parseLocation(line, line_number)
}
assert(err)
return line
}
func (self *Declaration) Result() string {
var result string
for _, date := range self.dates {
if date == HOME {
result += "x\t\t"
} else if date == OFFICE {
result +="\tx\t"
} else if date == VISIT {
result += "\t\tx"
}
result += "\n"
}
clipboard.WriteAll(result)
result = "-home--office--visit-" + result
result = result + "---------------------"
return result
}
// -----------------------------------------
func (self *Declaration) parseLocation(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 4)
if err != nil { return err }
var month_string string = data[0][5:7]
month, err := strconv.Atoi(month_string)
if err != nil || month < 1 || month > 12 {
return fmt.Errorf("invalid month '%s' on line %d\n%s", month_string, line_number, line)
}
var day_string string = data[0][8:]
day, err := strconv.Atoi(day_string)
if err != nil || day < 1 || day > 31 {
return fmt.Errorf("invalid day '%s' on line %d\n%s", day_string, line_number, line)
}
if month == self.month {
var data_month = strings.ToLower(data[3])
if data_month == strings.ToLower("Home") {
self.dates[day - 1] = HOME
} else if data_month == strings.ToLower("Office") {
self.dates[day - 1] = OFFICE
} else if data_month == strings.ToLower("Visit") {
self.dates[day - 1] = VISIT
} else {
return fmt.Errorf("invalid location '%s' on line %d\n%s", data[3], line_number, line)
}
}
return nil
}
// | 2024-07-06 | IN | 08:30 | Office |

27
src/file.go

@ -3,7 +3,11 @@ package src
import "bufio"
import "os"
func Parse(path string, job func(line string, line_number int) string) {
var File file
type file struct {}
func (file) Parse(path string, job func(line string, line_number int) string, overwrite bool) {
// Input file
file, err := os.Open(path)
assert(err)
@ -11,11 +15,14 @@ func Parse(path string, job func(line string, line_number int) string) {
var scanner *bufio.Scanner = bufio.NewScanner(file)
// Output file
output_file, err := os.Create(path + ".tmp")
assert(err)
defer output_file.Close()
var writer *bufio.Writer = bufio.NewWriter(output_file)
defer writer.Flush()
var writer *bufio.Writer
if overwrite {
output_file, err := os.Create(path + ".tmp")
assert(err)
defer output_file.Close()
writer = bufio.NewWriter(output_file)
defer writer.Flush()
}
var line string
var line_number int = 1
@ -25,8 +32,10 @@ func Parse(path string, job func(line string, line_number int) string) {
line_number++
// Write line to output_file
_, err := writer.WriteString(line + "\n")
assert(err)
if writer != nil {
_, err := writer.WriteString(line + "\n")
assert(err)
}
}
// Detect table if it was at the end of the file
@ -35,7 +44,7 @@ func Parse(path string, job func(line string, line_number int) string) {
err = scanner.Err()
assert(err)
// move file
// TODO: move file
}
// - [v] while looping, start writing a new file, .tmp, Q: write per line or per chunk? how big are the chunks?

64
src/api.go → src/process.go

@ -1,12 +1,8 @@
package src
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
import "errors"
import "fmt"
import "regexp"
type Date struct {
clock_in string
@ -18,7 +14,7 @@ type Date struct {
last_description string
}
type Api struct {
type Process struct {
clock_in *regexp.Regexp
task_line *regexp.Regexp
clock_out *regexp.Regexp
@ -26,16 +22,16 @@ type Api struct {
}
// Constructor
func MakeApi() Api {
return Api{
clock_in: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \|\s+IN\s+\|`),
task_line: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \| [0-9]{2}:[0-9]{2} \|`),
clock_out: compileRegex(`^\s*\| [0-9]{4}-[0-9]{2}-[0-9]{2} \|\s+OUT\s+\|`),
func MakeProcess() Process {
return Process{
clock_in: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+IN\s+\|`),
task_line: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+[0-9]{2}:[0-9]{2}\s+\|`),
clock_out: Util.CompileRegex(`^\s*\|\s+[0-9]{4}-[0-9]{2}-[0-9]{2}\s+\|\s+OUT\s+\|`),
dates: make(map[string]Date),
}
}
func (self *Api) Process(line string, line_number int) string {
func (self *Process) Process(line string, line_number int) string {
var err error
if self.clock_in.MatchString(line) {
err = self.parseClockIn(line, line_number)
@ -52,34 +48,8 @@ func (self *Api) Process(line string, line_number int) string {
// -----------------------------------------
func compileRegex(pattern string) *regexp.Regexp {
regex, err := regexp.Compile(pattern)
assert(err)
return regex
}
func (self *Api) parseLine(line string, line_number int, size int) ([]string, error) {
var data []string = strings.Split(line, "|")
if len(data) != size + 2 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
data = data[1:size + 1]
for i, value := range data {
data[i] = strings.TrimSpace(value)
if len(data[i]) == 0 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
}
return data, nil
}
func (self *Api) parseClockIn(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 4)
func (self *Process) parseClockIn(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 4)
if err != nil { return err }
// Set clock_in, location
@ -91,8 +61,8 @@ func (self *Api) parseClockIn(line string, line_number int) error {
return nil
}
func (self *Api) parseTask(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 5)
func (self *Process) parseTask(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 5)
if err != nil { return err }
var date Date = self.dates[data[0]]
@ -123,8 +93,8 @@ func (self *Api) parseTask(line string, line_number int) error {
return nil
}
func (self *Api) parseClockOut(line string, line_number int) error {
data, err := self.parseLine(line, line_number, 3)
func (self *Process) parseClockOut(line string, line_number int) error {
data, err := Util.ParseLine(line, line_number, 3)
if err != nil { return err }
// Set clock_out
@ -142,7 +112,7 @@ func (self *Api) parseClockOut(line string, line_number int) error {
return nil
}
func (self *Api) callApi(date string, from_time string, to_time string, item_id string, description string) error {
func (self *Process) callApi(date string, from_time string, to_time string, item_id string, description string) error {
fmt.Println("API |" + date + "|" + from_time + "|" + to_time + "|" + item_id + "|" + description)
// parse line

38
src/util.go

@ -0,0 +1,38 @@
package src
import (
"errors"
"regexp"
"strconv"
"strings"
)
var Util util
type util struct {}
func (util) CompileRegex(pattern string) *regexp.Regexp {
regex, err := regexp.Compile(pattern)
assert(err)
return regex
}
func (util) ParseLine(line string, line_number int, size int) ([]string, error) {
var data []string = strings.Split(line, "|")
if len(data) != size + 2 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
data = data[1:size + 1]
for i, value := range data {
data[i] = strings.TrimSpace(value)
if len(data[i]) == 0 {
return nil, errors.New("malformed line " + strconv.Itoa(line_number) + "\n" + line)
}
}
return data, nil
}
Loading…
Cancel
Save