-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgnparser.go
276 lines (226 loc) · 9.42 KB
/
pgnparser.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
pgnparser.go
Description: PGN parser
-----------------------------------------------------------------------------
Started on <Sun May 3 23:44:57 2015 Carlos Linares Lopez>
Last update <martes, 29 marzo 2016 21:06:50 Carlos Linares Lopez (clinares)>
-----------------------------------------------------------------------------
$Id:: $
$Date:: $
$Revision:: $
-----------------------------------------------------------------------------
Made by Carlos Linares Lopez
Login <clinares@atlas>
*/
package main
// imports
// ----------------------------------------------------------------------------
import (
"flag" // arg parsing
"fmt" // printing msgs
"log" // logging services
"os" // operating system services
"time"
// also use several tools for handling games in pgn format
"github.com/clinaresl/pgnparser/pgntools"
)
// global variables
// ----------------------------------------------------------------------------
const VERSION string = "0.1.0" // current version
const AUTHOR string = "Carlos Linares López"
const EMAIL string = "[email protected]"
const TABLE_TEMPLATE = "templates/table/simple.tpl"
var EXIT_SUCCESS int = 0 // exit with success
var EXIT_FAILURE int = 1 // exit with failure
// Options
var filename string // base directory
var list bool // whether games should be listed or not
var play int = 0 // number of moves between boards
var filter string // select query to filter games
var histogram string // histogram descriptor
var sort string // sorting descriptor
var output string // name of the file that stores results
var tableTemplate string // file with the table template
var latexTemplate string // file with the latex template
var verbose bool // has verbose output been requested?
var version bool // has version info been requested?
// functions
// ----------------------------------------------------------------------------
// initializes the command-line parser
func init() {
// Flag to store the pgn file to parse
flag.StringVar(&filename, "file", "", "pgn file to parse. While this utility is expected to be generic, it specifically adheres to the format of ficsgames.org as used in lichess.org")
// Flag to store the number of moves between boards
flag.BoolVar(&list, "list", false, "if given, a table with general information about all games found in the PGN file is shown")
// Flag to store the number of moves between boards
flag.IntVar(&play, "play", 0, "if given, each game in the PGN file is played, and the chess board is shown between the number of consecutive plies given. The board is not shown by default")
// Flag to request filtering games by some criteria
flag.StringVar(&filter, "filter", "", "generates a new pgn file with those games satisfying the given filtering criteria. For information about the filtering criteria see the documentation.")
// Flag to request sorting games by some criteria
flag.StringVar(&sort, "sort", "", "generates a new pgn file with games sorted according to the given criteria. For information about the sorting criteria see the documentation.")
// Flag to request generating histograms
flag.StringVar(&histogram, "histogram", "", "generates a table with a summary about the given variables. For information about the histogram variables see the documentation.")
// Flag to store the output filename
flag.StringVar(&output, "output", "output.pgn", "name of the file where the result of any manipulations is stored. It is used only in case any of the directives --filter or --sort is given. By default, 'output.pgn'")
// Flag to store the template to use to generate the ascii table
flag.StringVar(&tableTemplate, "table", "", "file with an ASCII template that can be used to override the output shown by default. For more information on how to create and use these templates see the documentation")
// Flag to store the file with the LaTeX template
flag.StringVar(&latexTemplate, "latex", "", "file with a LaTeX template to use. If given, a file with the same name used in 'file' and extension '.tex' is automatically generated in the same directory where the pgn file resides. For more information on how to create and use LaTeX templates see the documentation")
// other optional parameters are verbose and version
flag.BoolVar(&verbose, "verbose", false, "provides verbose output")
flag.BoolVar(&version, "version", false, "shows version info and exists")
}
// shows version info and exists with the specified signal
func showVersion(signal int) {
fmt.Printf("\n %v", os.Args[0])
fmt.Printf("\n Version: %v\n", VERSION)
fmt.Printf("\n %v", AUTHOR)
fmt.Printf("\n %v\n\n", EMAIL)
os.Exit(signal)
}
// parse the flags and verifies that proper values were given. If not, a fatal
// error is logged
func verify() {
// first, parse the flags ---in case help was given, it is automatically
// handled by the flags package
flag.Parse()
// if version information was requested show it now and exit
if version {
showVersion(EXIT_SUCCESS)
}
// verify that a pgn file to examine was given. Note that this argument is
// mandatory
if len(filename) == 0 {
log.Fatalf(" Error: a PGN file must be given with --file")
}
}
// Main body
func main() {
// verify the values parsed
verify()
// PgnFile
// ------------------------------------------------------------------------
// Create a new PgnFile
start := time.Now()
pgnfile, err := pgntools.NewPgnFile(filename)
if err != nil {
log.Fatalf(" Error: %v\n", err)
}
// Show information of the PgnFile provided by the user
fmt.Println()
fmt.Println(pgnfile)
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
// Obtain all games in this file as a collection of PgnGames
start = time.Now()
games, err := pgnfile.Games()
if err != nil {
log.Fatalln(err)
} else {
fmt.Printf(" %v games found\n", games.Len())
}
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
// List games
// ------------------------------------------------------------------------
// show a table with information of the games been processed. For this,
// a template is used: tableTemplate contains the location of a default
// template to use; others can be defined with --table
if list || len(tableTemplate) > 0 {
// In case a list was requested but no template is given to produce it,
// use the default one
if len(tableTemplate) == 0 {
tableTemplate = TABLE_TEMPLATE
}
games.GamesToWriterFromTemplate(os.Stdout, tableTemplate)
}
// Play/verify games
// ------------------------------------------------------------------------
// Play all games unconditionally. This is necessary to verify that the
// transcription of all games is correct. In case a strictly positive value
// is given then the board is shown on the standard output
start = time.Now()
if err := games.Play(play, os.Stdout); err != nil {
log.Fatalln(err)
}
fmt.Printf(" Games verified!\n")
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
// Filter games
// ------------------------------------------------------------------------
// In case it has been requested to filter games, do so
if filter != "" {
start = time.Now()
if filtered, err := games.Filter(filter); err != nil {
log.Fatalln(err)
} else {
fmt.Printf(" %v games filtered\n", filtered.Len())
// and make the filtered collection the current one
games = filtered
}
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
}
// Sort games
// ------------------------------------------------------------------------
if sort != "" {
start = time.Now()
if sorted, err := games.Sort(sort); err != nil {
log.Fatalln(err)
} else {
fmt.Printf(" %v games sorted\n", sorted.Len())
// and make the sorted collection the current one
games = sorted
}
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
}
// In case either sorting and/or filter has been requested, write the result
// in the output file
if sort != "" || filter != "" {
// Check first whether there are some games to write
if games.Len() == 0 {
fmt.Println(" No games to store!")
} else {
// In case there are effectively some games to store, do! Create the
// file in write mode and then write the contents of the entire
// collection
stream, err := os.Create(output)
defer stream.Close()
if err != nil {
log.Fatalln(err)
} else {
games.GetPGN(stream)
}
}
}
// Histogram
// ------------------------------------------------------------------------
if histogram != "" {
start = time.Now()
if pgnhistogram, err := games.GetHistogram(histogram); err != nil {
log.Fatalln(err)
} else {
fmt.Println(*pgnhistogram)
}
fmt.Printf(" [%v]\n", time.Since(start))
fmt.Println()
}
// LaTeX
// ------------------------------------------------------------------------
// in case a LaTeX template has been given, then generate a LaTeX file
// with the same name than the pgn file (and in the same location) with
// extension '.tex' from the contents given in the specified template
if latexTemplate != "" {
// Create a LaTeX file to write the output
if latexStream, err := os.Create(output + ".tex"); err != nil {
log.Fatalln(err)
} else {
games.GamesToWriterFromTemplate(latexStream, latexTemplate)
}
}
}
/* Local Variables: */
/* mode:go */
/* fill-column:80 */
/* End: */