-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdundocs.go
158 lines (143 loc) · 4.19 KB
/
dundocs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dundocs
import (
"fmt"
"log"
"path/filepath"
"strings"
"github.com/Sinar/go-dundocs/internal/hansard"
)
type DUNDocs struct {
// DUN Session Label
DUNSession string
Conf Configuration
Options *ExtractPDFOptions
}
// CommandMode specifies the operation being executed.
type CommandMode int
// The available commands.
const (
PLAN CommandMode = iota
SPLIT
RESET
)
type ExtractPDFOptions struct {
StartPage int
NumPages int
}
type Configuration struct {
// Source PDF can be anywhere; maybe make it a Reader to be read direct from S3?
SourcePDFPath string
// ./raw + ./data folders are assumed to be relative to this dir
WorkingDir string
// Data directory name; can be relative or absolute?
DataDir string
}
func NewDUNDocs() *DUNDocs {
dunDocs := DUNDocs{}
return &dunDocs
}
func (dd *DUNDocs) Plan() {
log.Println("In Plan ..")
//pdfPath := dd.Conf.SourcePDFPath
//conf := Configuration{}
//splitPlan := hansard.NewSplitHansardDocumentPlan(
// dd.Conf.SourcePDFPath, dd.Conf.WorkingDir,
// dd.Conf.DataDir, dd.DUNSession,
// dd.Options.StartPage, dd.Options.NumPages)
//// Perissit it
//splitPlan.SavePlan()
var options *hansard.ExtractPDFOptions
// Fill in the options if needed ..
if dd.Options != nil {
options = &hansard.ExtractPDFOptions{
StartPage: dd.Options.StartPage,
NumPages: dd.Options.NumPages,
}
}
// Prepare config
conf := hansard.Configuration{
DUNSession: dd.DUNSession,
WorkingDir: dd.Conf.WorkingDir,
DataDir: dd.Conf.DataDir,
SourcePDFPath: dd.Conf.SourcePDFPath,
Options: options,
}
// DEBUG
//spew.Dump(c)
//err := hansard.PlanAndSave(conf)
//if err != nil {
// panic(err)
//}
// Above no need; as equivalent to below; can be r efectored out I guess ..
splitPlan := hansard.NewSplitHansardDocumentPlan(conf.SourcePDFPath, conf.WorkingDir, conf.DataDir, conf.DUNSession, conf.Options)
// Try to persist the plan
sperr := splitPlan.SavePlan()
if sperr != nil {
panic(sperr)
}
// if see flag; then call the following; not executed by default ..
//hansard.LoadAndSplit()
}
func (dd *DUNDocs) Split() {
log.Println("In Split ..")
// Setup; possibly into a helper function? Seen it repeated a few places
workingDir := "."
if dd.Conf.WorkingDir != "" {
workingDir = dd.Conf.WorkingDir
}
dataDir := ""
if dd.Conf.DataDir != "" {
dataDir = dd.Conf.DataDir
}
// Needs absolute Path for Source .. absoluteSrcPDF, sperr := filepath.Abs(tt.fields.srcPDFPath)
sourcePDFPath, sperr := filepath.Abs(dd.Conf.SourcePDFPath)
if sperr != nil {
panic(sperr)
}
absoluteDataDir := hansard.GetAbsoluteDataDir(workingDir, dataDir)
// Extract out filename as folder for split.yml plan
// https://stackoverflow.com/questions/13027912/trim-strings-suffix-or-extension
basePDFPath := filepath.Base(sourcePDFPath)
// Plan file name is hardcoded split.yml
absolutePlanFile := absoluteDataDir + fmt.Sprintf("/%s/split.yml", strings.TrimSuffix(basePDFPath, filepath.Ext(basePDFPath)))
// Maybe above to be moved into NewEmptySplitHansardDocumentPlan?
splitPlan := hansard.NewEmptySplitHansardDocumentPlan(
absoluteDataDir, absolutePlanFile, dd.DUNSession)
// Load plan
lderr := splitPlan.LoadPlan()
if lderr != nil {
panic(lderr)
}
// DEBUG
//spew.Dump(splitPlan.HansardDocument)
// Setup + execute; use the default splitOutDir
absoluteSplitOutput := hansard.GetAbsoluteSplitOutDir(workingDir, "")
exerr := splitPlan.ExecuteSplit(sourcePDFPath, absoluteSplitOutput)
if exerr != nil {
panic(exerr)
}
// Below also looks like a recurring pattern; restructure it somehow? Not needed?
//var options *hansard.ExtractPDFOptions
//// Fill in the options if needed ..
//if dd.Options != nil {
// options = &hansard.ExtractPDFOptions{
// StartPage: dd.Options.StartPage,
// NumPages: dd.Options.NumPages,
// }
//}
//// Prepare config
//conf := hansard.Configuration{
// DUNSession: dd.DUNSession,
// WorkingDir: dd.Conf.WorkingDir,
// DataDir: dd.Conf.DataDir,
// SourcePDFPath: dd.Conf.SourcePDFPath,
// Options: options,
//}
//hansard.LoadAndSplit(conf)
}
func (dd *DUNDocs) Reset() {
log.Println("In Reset ...")
// Clean up plan
// Clean up split pages folder
// Clean up merged pages location
}