| 1 |
|
|---|
| 2 |
abstractGameList::abstractGameList(int bs, PyObject* pBar) { |
|---|
| 3 |
boardsize = bs; |
|---|
| 4 |
if (boardsize==19) bsize = 0; |
|---|
| 5 |
if (boardsize==13) bsize = 1; |
|---|
| 6 |
if (boardsize==9) bsize = 2; |
|---|
| 7 |
|
|---|
| 8 |
vector<SGFDatabase> DBlist[3]; |
|---|
| 9 |
Bwins = Wwins = Owins = 0; |
|---|
| 10 |
|
|---|
| 11 |
references = new map ...; |
|---|
| 12 |
sort = None; |
|---|
| 13 |
gameIndex = new vector<pair<int, int> >; |
|---|
| 14 |
|
|---|
| 15 |
showFilename = 1; |
|---|
| 16 |
showDate = 0; |
|---|
| 17 |
|
|---|
| 18 |
perDB = 1; |
|---|
| 19 |
criterion = ""; |
|---|
| 20 |
reverse = 0; |
|---|
| 21 |
|
|---|
| 22 |
progBar = pBar; |
|---|
| 23 |
stopAddDBVar = 0; |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
void abstractGameList::readReferences(char* basepath) throw(IOError) { |
|---|
| 27 |
|
|---|
| 28 |
ref_file = open(os.path.join(basepath, 'references')); |
|---|
| 29 |
|
|---|
| 30 |
if (!ref_file) throw IOerror(); |
|---|
| 31 |
r_list = ref_file.readlines(); |
|---|
| 32 |
ref_file.close(); |
|---|
| 33 |
|
|---|
| 34 |
abbr = {} |
|---|
| 35 |
found_abbr = 0 |
|---|
| 36 |
found_data = 0 |
|---|
| 37 |
for line in r_list: |
|---|
| 38 |
|
|---|
| 39 |
if line and line[0] == '#': continue |
|---|
| 40 |
|
|---|
| 41 |
if not found_abbr: |
|---|
| 42 |
if line[:7] != '---Abbr': continue |
|---|
| 43 |
else: |
|---|
| 44 |
found_abbr = 1 |
|---|
| 45 |
continue |
|---|
| 46 |
|
|---|
| 47 |
if found_abbr and not found_data: |
|---|
| 48 |
if strip(line): |
|---|
| 49 |
s = split(line) |
|---|
| 50 |
abbr[s[0]] = join(s[1:], ' ') |
|---|
| 51 |
|
|---|
| 52 |
if found_abbr and not found_data: |
|---|
| 53 |
if line[:7] != '---Data': continue |
|---|
| 54 |
else: |
|---|
| 55 |
found_data = 1 |
|---|
| 56 |
continue |
|---|
| 57 |
|
|---|
| 58 |
if found_data and strip(line): |
|---|
| 59 |
s = split(line) |
|---|
| 60 |
if abbr.has_key(s[1]): |
|---|
| 61 |
if self.references.has_key(s[0]): |
|---|
| 62 |
self.references[s[0]] += ', ' + abbr[s[1]] + ' ' + join(s[2:], ' ') |
|---|
| 63 |
else: |
|---|
| 64 |
self.references[s[0]] = abbr[s[1]] + ' ' + join(s[2:], ' ') |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
pair<int,int> abstractGameList::getIndex(int i) { |
|---|
| 68 |
if (i < gameIndex.size()) return gameIndex[i]; |
|---|
| 69 |
else return pair<int,int>(-1, -1); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
pair<char*,int> abstractGameList::getFilename(int index1) { |
|---|
| 74 |
pair<int,int> p = getIndex(index1); |
|---|
| 75 |
index = DBlist[bsize][p.first].current[p.second]; |
|---|
| 76 |
return self.DBlist[bsize][p.first].getFilename(index); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
int abstractGameList::getMoveno(int index) { |
|---|
| 81 |
pair<int,int> p = self.getIndex(index); |
|---|
| 82 |
int moveno; |
|---|
| 83 |
|
|---|
| 84 |
if (DBlist[bsize][p.first].results.size() > p.second) { |
|---|
| 85 |
s = DBlist[bsize][p.first].results[p.second]; |
|---|
| 86 |
int i = 0; |
|---|
| 87 |
while (i<strlen(s) && s[i] in digits) i++; |
|---|
| 88 |
moveno = int(s[:i]); |
|---|
| 89 |
} |
|---|
| 90 |
else moveno = 0; |
|---|
| 91 |
return moveno; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
char* abstractGameList::getString(int index) { |
|---|
| 96 |
pair<int,int> p = self.getIndex(index); |
|---|
| 97 |
int DBindex = p.first; |
|---|
| 98 |
int indexWithinCurrent = p.second; |
|---|
| 99 |
|
|---|
| 100 |
int indexWithinDB = DBlist[bsize][DBindex].current[indexWithinCurrent]; |
|---|
| 101 |
|
|---|
| 102 |
if (DBindex == -1) return ""; |
|---|
| 103 |
|
|---|
| 104 |
l = DBlist[bsize][DBindex].namelist[indexWithinDB]; |
|---|
| 105 |
sl = char[1000]; |
|---|
| 106 |
|
|---|
| 107 |
if (references.has_key(l[4])) sl.append('* '); |
|---|
| 108 |
|
|---|
| 109 |
if (showFilename) sl.append(l[0]+': '); |
|---|
| 110 |
|
|---|
| 111 |
sl.append(l[2] + ' - ' + l[1] + ' (' + l[3] + '), '); |
|---|
| 112 |
if (self.showDate) sl.append(l[5]+', '); |
|---|
| 113 |
if (results...) sl.append(join(self.DBlist[`self.boardsize`][DBindex].results[indexWithinCurrent], '')); |
|---|
| 114 |
return sl; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
vector<char*> abstractGameList::getAllStrings() { |
|---|
| 119 |
return [self.getString(i) for i in range(self.numOfGames())]; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
char* abstractGameList::getGameInfo(int index) { |
|---|
| 124 |
pair<int,int> p = getIndex(index); |
|---|
| 125 |
int DBindex = p.first; |
|---|
| 126 |
int indexWithinCurrent = p.second; |
|---|
| 127 |
|
|---|
| 128 |
pair<char*, int> q = getFilename(index); |
|---|
| 129 |
|
|---|
| 130 |
try { |
|---|
| 131 |
f = open(q.first); |
|---|
| 132 |
sgf = f.read(); |
|---|
| 133 |
f.close(); |
|---|
| 134 |
Node node = Cursor(sgf, 1).getRootNode(q.second); |
|---|
| 135 |
} |
|---|
| 136 |
catch(...) return ""; |
|---|
| 137 |
|
|---|
| 138 |
char* t = new char[]; |
|---|
| 139 |
|
|---|
| 140 |
if (node.has_key("PW")) strcat(t, node.getstr("PW", 0)); |
|---|
| 141 |
else strcat(t, " ?"); |
|---|
| 142 |
if (node.has_key("WR")) strcat(t, node.getstr("WR", 0)); |
|---|
| 143 |
|
|---|
| 144 |
strcat(t, " - "); |
|---|
| 145 |
|
|---|
| 146 |
if (node.has_key("PB")) strcat(t, node.getstr("PB", 0)); |
|---|
| 147 |
else strcat(t, " ?"); |
|---|
| 148 |
if (node.has_key("BR")) strcat(t, node.getstr("BR", 0)); |
|---|
| 149 |
|
|---|
| 150 |
if (node.has_key("RE")) strcat(t, node.getstr("RE", 0)); |
|---|
| 151 |
if (node.has_key('KM')) { |
|---|
| 152 |
strcat(t, " (Komi "); |
|---|
| 153 |
strcat(t, node.getstr("KM", 0)); |
|---|
| 154 |
strcat(t, ")"); |
|---|
| 155 |
} |
|---|
| 156 |
if (node.has_key("HA")) { |
|---|
| 157 |
strcat(t, " (Hcp "); |
|---|
| 158 |
strcat(t, node.getstr("HA", 0)); |
|---|
| 159 |
strcat(t, ")"); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
strcat(t, "\n"); |
|---|
| 163 |
|
|---|
| 164 |
if (node.has_key("EV")) { |
|---|
| 165 |
strcat(t, node.getstr("EV", 0)); |
|---|
| 166 |
strcat(t, ", "); |
|---|
| 167 |
} |
|---|
| 168 |
if (node.has_key("RO")) { |
|---|
| 169 |
strcat(t, node.getstr("RO", 0)); |
|---|
| 170 |
strcat(t, ", "); |
|---|
| 171 |
} |
|---|
| 172 |
if (node.has_key("DT")) { |
|---|
| 173 |
strcat(t, node.getstr("DT", 0)); |
|---|
| 174 |
strcat(t, "\n"); |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
if (node.has_key("GC")) { |
|---|
| 178 |
char* s = node.getstr("GC", 0); |
|---|
| 179 |
char* gc = new char[strlen(s)+1]; |
|---|
| 180 |
int gcIndex = 0 |
|---|
| 181 |
for(int i=0; i<strlen(s); i++) |
|---|
| 182 |
if (s[i] != '\n' && s[i] != '\r') gc[gcIndex++] = s[i]; |
|---|
| 183 |
else gc[gcIndex++] = ' '; |
|---|
| 184 |
gc[gcIndex] = 0; |
|---|
| 185 |
strcat(t, gc); |
|---|
| 186 |
delete [] gc; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
char* t2 = new char[]; |
|---|
| 190 |
try { |
|---|
| 191 |
t2 = 'Commentary in ' +\ |
|---|
| 192 |
self.references[self.DBlist[`self.boardsize`][DBindex].namelist[self.DBlist[`self.boardsize`][DBindex].current[indexWithinCurrent]][4]]; |
|---|
| 193 |
} |
|---|
| 194 |
catch() else t2 = ''; |
|---|
| 195 |
|
|---|
| 196 |
strcpy ...; |
|---|
| 197 |
delete [] t; |
|---|
| 198 |
delete [] t2; |
|---|
| 199 |
return t, t2; |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
void abstractGameList::changeBoardSize(int bs) { |
|---|
| 204 |
boardsize = bs; |
|---|
| 205 |
if (boardsize==19) bsize = 0; |
|---|
| 206 |
if (boardsize==13) bsize = 1; |
|---|
| 207 |
if (boardsize==9) bsize = 2; |
|---|
| 208 |
|
|---|
| 209 |
Bwins 0; |
|---|
| 210 |
Wwins = 0; |
|---|
| 211 |
Owins = 0; |
|---|
| 212 |
|
|---|
| 213 |
references = {}; |
|---|
| 214 |
gameIndex.clear(); |
|---|
| 215 |
|
|---|
| 216 |
for(db in self.DBlist[`self.boardsize`]) { |
|---|
| 217 |
success = db.loadDB(); |
|---|
| 218 |
if (success != 1) ...; |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
updateGameIndex(); |
|---|
| 222 |
|
|---|
| 223 |
for bs in [9, 13, 19] { |
|---|
| 224 |
if (bs != self.boardsize) |
|---|
| 225 |
for(db in self.DBlist[`bs`]) |
|---|
| 226 |
db.clear(); |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
void abstractGameList::reset() { |
|---|
| 232 |
|
|---|
| 233 |
vector<>::iterator it; |
|---|
| 234 |
for(it = DBlist[bsize].begin(); it != DBlist[bsize].end(); it++) { |
|---|
| 235 |
if (!db.disabled) { |
|---|
| 236 |
db.current = array('L', range(len(db.namelist))); |
|---|
| 237 |
db.results = [''] * len(db.namelist); |
|---|
| 238 |
} |
|---|
| 239 |
else { |
|---|
| 240 |
db.current = array('L'); |
|---|
| 241 |
db.results = []; |
|---|
| 242 |
} |
|---|
| 243 |
} |
|---|
| 244 |
updateGameIndex(); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
char* abstractGameList::sortCrit(int index, int c) { |
|---|
| 249 |
pair<int,int> p = getIndex(index); |
|---|
| 250 |
return DBlist[bsize][p.first].namelist[DBlist[bsize][p.first].current[p.second]][c]; |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
void abstractGameList::updateGameIndex(int sortList) { |
|---|
| 255 |
|
|---|
| 256 |
gameIndex.clear(); |
|---|
| 257 |
|
|---|
| 258 |
int c = 0; |
|---|
| 259 |
if (criterion == "PW") c = 2; |
|---|
| 260 |
if (criterion == "PB") c = 1; |
|---|
| 261 |
if (criterion == "DT") c = 5; |
|---|
| 262 |
|
|---|
| 263 |
if ((perDB && c == 0) || !sortlist) |
|---|
| 264 |
for (i in range(len(self.DBlist[`self.boardsize`]))) { |
|---|
| 265 |
db = self.DBlist[`self.boardsize`][i]; |
|---|
| 266 |
if db.disabled: continue; |
|---|
| 267 |
self.gameIndex.extend([(i, j) for j in range(len(db.current))]); |
|---|
| 268 |
} |
|---|
| 269 |
else { |
|---|
| 270 |
if (perDB) |
|---|
| 271 |
for i in range(len(self.DBlist[`self.boardsize`])) { |
|---|
| 272 |
db = self.DBlist[`self.boardsize`][i]; |
|---|
| 273 |
if db.disabled: continue; |
|---|
| 274 |
|
|---|
| 275 |
helpList = []; |
|---|
| 276 |
helpList.extend([(db.namelist[j][c], j) for j in range(len(db.namelist))]); |
|---|
| 277 |
helpList.sort(); |
|---|
| 278 |
|
|---|
| 279 |
self.gameIndex.extend([(i, j) for dummy, j in helpList]); |
|---|
| 280 |
} |
|---|
| 281 |
else { |
|---|
| 282 |
srt = []; |
|---|
| 283 |
for i in range(len(self.DBlist[`self.boardsize`])) { |
|---|
| 284 |
db = self.DBlist[`self.boardsize`][i]; |
|---|
| 285 |
if db.disabled: continue; |
|---|
| 286 |
srt.extend([(db.namelist[j][c], i, j) for j in range(len(db.namelist))]); |
|---|
| 287 |
} |
|---|
| 288 |
srt.sort(); |
|---|
| 289 |
self.gameIndex = [(i, j) for dummy, i, j in srt]; |
|---|
| 290 |
} |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
if (reverse && sortList) self.gameIndex.reverse(); |
|---|
| 294 |
|
|---|
| 295 |
Bwins = Wwins = Owins = 0; |
|---|
| 296 |
for (db in DBlist[bsize]) { |
|---|
| 297 |
if db.disabled: continue; |
|---|
| 298 |
s = join([db.namelist[db.current[i]][3] for i in range(len(db.current))], ''); |
|---|
| 299 |
Bwins = count(s, 'B'); |
|---|
| 300 |
Wwins = count(s, 'W'); |
|---|
| 301 |
Owins = len(s) - Bwins - Wwins; |
|---|
| 302 |
} |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
int abstractGameList::numOfGames() { |
|---|
| 307 |
int num = 0; |
|---|
| 308 |
for (db in self.DBlist[`self.boardsize`]) { |
|---|
| 309 |
if (db.disabled) continue; |
|---|
| 310 |
num += db.current.size(); |
|---|
| 311 |
} |
|---|
| 312 |
return num; |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
void abstractGameList::toggleDisabled(int DBIndex, int disabled) { |
|---|
| 317 |
DBlist[bsize][DBIndex].disabled = disabled; |
|---|
| 318 |
updateGameIndex(); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
char* abstractGameList::saveData() { |
|---|
| 323 |
|
|---|
| 324 |
char result[1000]; |
|---|
| 325 |
for (bs=0; bs<3; bs++) { |
|---|
| 326 |
for (db in self.DBlist[bs]) { |
|---|
| 327 |
if (db.disabled) |
|---|
| 328 |
result.append('dD|%' + bs + '|%' + db.sgfpath + '|%' + db.datapath[0] +\ |
|---|
| 329 |
'|%' + db.datapath[1] +'\n'); |
|---|
| 330 |
else |
|---|
| 331 |
result.append('d|%' + bs + '|%' + db.sgfpath + '|%' + db.datapath[0] +\ |
|---|
| 332 |
'|%' + db.datapath[1] +'\n'); |
|---|
| 333 |
} |
|---|
| 334 |
} |
|---|
| 335 |
return strcpy(result, ''); |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
void abstractGameList::addDB(char* sgfpath, pair<char*,int> datap, PyObject* messages, int recProcess, |
|---|
| 340 |
int algos, char* filenames, int sloppySGF, int duplicateCheck, |
|---|
| 341 |
int strictDuplicateCheck) { |
|---|
| 342 |
stopAddDBVar = 0; |
|---|
| 343 |
|
|---|
| 344 |
if (!recProcess) addOneDB((messages, datap, algos, filenames, |
|---|
| 345 |
sloppySGF, duplicateCheck, strictDuplicateCheck), sgfpath, None); |
|---|
| 346 |
else os.path.walk(sgfpath, self.addOneDB, |
|---|
| 347 |
(messages, datap, algos, filenames, |
|---|
| 348 |
sloppySGF, duplicateCheck, strictDuplicateCheck)); |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
def addOneDB(self, arguments, dbpath, dummy): # dummys needed for os.path.walk |
|---|
| 352 |
if self.stopAddDBVar: return |
|---|
| 353 |
|
|---|
| 354 |
messages, datap, algos, filenames,\ |
|---|
| 355 |
sloppySGF, duplicateCheck, strictDuplicateCheck = arguments |
|---|
| 356 |
if datap[1] == '#': datap = (dbpath, '') |
|---|
| 357 |
else: |
|---|
| 358 |
try: |
|---|
| 359 |
flist = glob.glob(os.path.join(datap[0], 'namelist*.db')) |
|---|
| 360 |
i = 1 |
|---|
| 361 |
|
|---|
| 362 |
while 1: |
|---|
| 363 |
if not os.path.normpath(os.path.join(datap[0], 'namelist' + `i` + '.db')) in flist: |
|---|
| 364 |
break |
|---|
| 365 |
i += 1 |
|---|
| 366 |
except IOError: |
|---|
| 367 |
messages.insert('end', 'Error: ' + datap[0] + 'cannot be used as database path.\n') |
|---|
| 368 |
return |
|---|
| 369 |
|
|---|
| 370 |
datap = (datap[0], `i`) |
|---|
| 371 |
|
|---|
| 372 |
for db in self.DBlist[`self.boardsize`]: |
|---|
| 373 |
if dbpath == db.sgfpath: |
|---|
| 374 |
messages.insert('end', 'Error: The database ' + dbpath + ' is already in the list.\n') |
|---|
| 375 |
return |
|---|
| 376 |
|
|---|
| 377 |
newDB = SGFDatabase(self.boardsize, self.progBar) |
|---|
| 378 |
if newDB.loadDB(dbpath, datap) != 1: |
|---|
| 379 |
try: |
|---|
| 380 |
|
|---|
| 381 |
newDB.process(dbpath, datap, algos, filenames, messages, |
|---|
| 382 |
sloppySGF, duplicateCheck, strictDuplicateCheck, self) |
|---|
| 383 |
except IOError: # FIXME |
|---|
| 384 |
messages.insert('Error', |
|---|
| 385 |
'A fatal error occured when processing ' + dbpath + '. Please send a bug report.\n') |
|---|
| 386 |
return |
|---|
| 387 |
|
|---|
| 388 |
self.DBlist[`self.boardsize`].append(newDB) |
|---|
| 389 |
self.updateGameIndex() |
|---|
| 390 |
messages.insert('end', 'Added ' + dbpath + '.\n') |
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
def removeDB(self, toBeRemoved, messages): |
|---|
| 394 |
for ii in toBeRemoved: |
|---|
| 395 |
i = int(ii) |
|---|
| 396 |
datap = self.DBlist[`self.boardsize`][i].datapath |
|---|
| 397 |
dbpath = self.DBlist[`self.boardsize`][i].sgfpath |
|---|
| 398 |
filelist = [] |
|---|
| 399 |
for algo in ALL_ALGOS.keys(): |
|---|
| 400 |
if self.DBlist[`self.boardsize`][i].algos & algo: |
|---|
| 401 |
filelist.extend(ALL_ALGOS[algo].filelist) |
|---|
| 402 |
del self.DBlist[`self.boardsize`][i] |
|---|
| 403 |
|
|---|
| 404 |
deletionFailed = 0 |
|---|
| 405 |
for filename in filelist: |
|---|
| 406 |
try: |
|---|
| 407 |
os.remove(os.path.join(datap[0], filename+datap[1]+'.db')) |
|---|
| 408 |
except: |
|---|
| 409 |
deletionFailed = 1 |
|---|
| 410 |
if deletionFailed: messages.insert('end', 'I/O Error: Could not delete the database files.\n') |
|---|
| 411 |
messages.insert('end', 'Removed ' + dbpath + '\n') |
|---|
| 412 |
self.updateGameIndex() |
|---|
| 413 |
self.reset() |
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
def reprocessDB(self, toBeReprocessed, messages, algos=None, filenames='*.sgf', |
|---|
| 417 |
sloppySGF=1, duplicateCheck=1, strictDuplicateCheck=0): |
|---|
| 418 |
failed = [] |
|---|
| 419 |
if not toBeReprocessed: return |
|---|
| 420 |
|
|---|
| 421 |
for i in toBeReprocessed: |
|---|
| 422 |
if algos is None: thisAlgos = self.DBlist[`self.boardsize`][i].algos |
|---|
| 423 |
else: thisAlgos = algos |
|---|
| 424 |
|
|---|
| 425 |
sgfpath = self.DBlist[`self.boardsize`][i].sgfpath |
|---|
| 426 |
datap = self.DBlist[`self.boardsize`][i].datapath |
|---|
| 427 |
filelist = [] |
|---|
| 428 |
for algo in ALL_ALGOS.keys(): |
|---|
| 429 |
if self.DBlist[`self.boardsize`][i].algos & algo: |
|---|
| 430 |
filelist.extend(ALL_ALGOS[algo].filelist) |
|---|
| 431 |
del self.DBlist[`self.boardsize`][i] |
|---|
| 432 |
|
|---|
| 433 |
deletionFailed = 0 |
|---|
| 434 |
for filename in filelist: |
|---|
| 435 |
try: |
|---|
| 436 |
os.remove(os.path.join(datap[0], filename+datap[1]+'.db')) |
|---|
| 437 |
except: |
|---|
| 438 |
deletionFailed = 1 |
|---|
| 439 |
if deletionFailed: |
|---|
| 440 |
messages.insert('end','I/O Error: Could not delete the database files.\n') |
|---|
| 441 |
|
|---|
| 442 |
newDB = SGFDatabase(self.boardsize, self.progBar) |
|---|
| 443 |
try: |
|---|
| 444 |
newDB.process(sgfpath, datap, thisAlgos, filenames, messages, |
|---|
| 445 |
sloppySGF, duplicateCheck, strictDuplicateCheck, self) |
|---|
| 446 |
except IOError: # FIXME |
|---|
| 447 |
messages.insert('end', 'Error: A fatal error occurred when processing ' + sgfpath + '.\n') |
|---|
| 448 |
failed.append(i) |
|---|
| 449 |
continue |
|---|
| 450 |
try: |
|---|
| 451 |
file = open(os.path.join(datap[0], 'namelist'+datap[1]+'.db')) |
|---|
| 452 |
file.close() |
|---|
| 453 |
except: |
|---|
| 454 |
self.reset() |
|---|
| 455 |
failed.append(i) |
|---|
| 456 |
continue |
|---|
| 457 |
|
|---|
| 458 |
self.DBlist[`self.boardsize`][i:i] = [newDB] |
|---|
| 459 |
|
|---|
| 460 |
self.reset() |
|---|
| 461 |
return failed |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
def loadDB(self, sgfpath, datapath, boardsize, disabled=0, sortList=0, insertWhere=-1): |
|---|
| 465 |
newDB = SGFDatabase(boardsize, self.progBar) |
|---|
| 466 |
if boardsize == self.boardsize: |
|---|
| 467 |
success = newDB.loadDB(sgfpath, datapath, disabled) |
|---|
| 468 |
if success != 1: return success |
|---|
| 469 |
else: |
|---|
| 470 |
newDB.sgfpath = sgfpath |
|---|
| 471 |
newDB.datapath = datapath |
|---|
| 472 |
newDB.disabled = disabled |
|---|
| 473 |
if insertWhere == -1: self.DBlist[`boardsize`].append(newDB) |
|---|
| 474 |
else: self.DBlist[`boardsize`][insertWhere:insertWhere] = [newDB] |
|---|
| 475 |
|
|---|
| 476 |
self.updateGameIndex(sortList) |
|---|
| 477 |
return 1 |
|---|
| 478 |
|
|---|
| 479 |
# --- searching ----------------------------------------------------------------- |
|---|
| 480 |
|
|---|
| 481 |
def search(self, pattern, options={}, sortLabelsByFrequency=0, |
|---|
| 482 |
continuations={}, contLabels = None): |
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
if options.has_key('nextmove'): nextMoveVar = options['nextmove'] |
|---|
| 486 |
else: nextMoveVar = 0 |
|---|
| 487 |
if options.has_key('fixedcolor'): fixedColorVar = options['fixedcolor'] |
|---|
| 488 |
else: fixedColorVar = 0 |
|---|
| 489 |
|
|---|
| 490 |
print nextMoveVar |
|---|
| 491 |
patternList = PatternList(pattern, fixedColorVar, nextMoveVar) |
|---|
| 492 |
|
|---|
| 493 |
if contLabels is None: |
|---|
| 494 |
contLabels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L' ,'M' ,'N', |
|---|
| 495 |
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z', 'a', 'b', 'c', 'd', |
|---|
| 496 |
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', |
|---|
| 497 |
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', |
|---|
| 498 |
'7', '8', '9'] |
|---|
| 499 |
contLabelsIndex = 0 |
|---|
| 500 |
|
|---|
| 501 |
numOfHits, Bwins, Wwins, numSwitched = 0, 0, 0, 0 |
|---|
| 502 |
|
|---|
| 503 |
startTime = time.time() |
|---|
| 504 |
numOfGames = self.numOfGames() |
|---|
| 505 |
doneSoFar = 0 |
|---|
| 506 |
|
|---|
| 507 |
for db in self.DBlist[`self.boardsize`]: |
|---|
| 508 |
if db.disabled or len(db.current)==0: continue |
|---|
| 509 |
progStart = doneSoFar |
|---|
| 510 |
progSpan = len(db.current)*1.0/numOfGames |
|---|
| 511 |
progFirstInt = 0.3 |
|---|
| 512 |
doneSoFar += progSpan |
|---|
| 513 |
|
|---|
| 514 |
a0 = Algo_hash_full_corners(self.boardsize) |
|---|
| 515 |
if not a0.search(patternList, options, db, self.progBar, |
|---|
| 516 |
progStart, progStart+progFirstInt*progSpan): |
|---|
| 517 |
progFirstInt = 0.5 |
|---|
| 518 |
a1 = Algo_finalpos(self.boardsize) |
|---|
| 519 |
a1.search(patternList, options, db, self.progBar, |
|---|
| 520 |
progStart, progStart+progFirstInt*progSpan) |
|---|
| 521 |
|
|---|
| 522 |
intervals = 0 |
|---|
| 523 |
|
|---|
| 524 |
# if numOfStones in pattern is large (in proportion to pattern size), use intervals |
|---|
| 525 |
# this is probably in particular useful for central patterns |
|---|
| 526 |
# final tuning will have to be done with the C++ implementations |
|---|
| 527 |
|
|---|
| 528 |
if intervals: # FIXME: decide which algo to use for second stage here |
|---|
| 529 |
a2 = Algo_intervals(self.boardsize) |
|---|
| 530 |
nh, bw, ww, sw = a2.search(patternList, options, db, |
|---|
| 531 |
continuations, contLabelsIndex, contLabels, |
|---|
| 532 |
self.progBar, progStart+progFirstInt*progSpan, |
|---|
| 533 |
progStart+progSpan) |
|---|
| 534 |
|
|---|
| 535 |
else: |
|---|
| 536 |
a2 = Algo_movelist(self.boardsize) |
|---|
| 537 |
nh, bw, ww, sw = a2.search(patternList, options, db, |
|---|
| 538 |
continuations, contLabelsIndex, contLabels, |
|---|
| 539 |
self.progBar, progStart+progFirstInt*progSpan, progStart+progSpan) |
|---|
| 540 |
|
|---|
| 541 |
numOfHits += nh |
|---|
| 542 |
Bwins += bw |
|---|
| 543 |
Wwins += ww |
|---|
| 544 |
numSwitched += sw |
|---|
| 545 |
|
|---|
| 546 |
if sortLabelsByFrequency: |
|---|
| 547 |
contList = [ (continuations[k]['B']+continuations[k]['W'], k) for k in continuations.keys() |
|---|
| 548 |
if continuations[k]['N'] != '?' ] |
|---|
| 549 |
contList.sort() |
|---|
| 550 |
contList.reverse() |
|---|
| 551 |
contLabelsIndex = 0 |
|---|
| 552 |
t = [] |
|---|
| 553 |
for dummy, k in contList: |
|---|
| 554 |
t.append(continuations[k]['N']) |
|---|
| 555 |
continuations[k]['N'] = contLabels[contLabelsIndex] |
|---|
| 556 |
contLabelsIndex += 1 |
|---|
| 557 |
trans = maketrans(join(t, ''), join(contLabels[:contLabelsIndex], '')) |
|---|
| 558 |
for db in self.DBlist[`self.boardsize`]: |
|---|
| 559 |
s1 = join(db.results, '|%') |
|---|
| 560 |
s2 = translate(s1, trans) |
|---|
| 561 |
db.results = split(s2, '|%') |
|---|
| 562 |
|
|---|
| 563 |
self.updateGameIndex() |
|---|
| 564 |
if self.progBar: self.progBar.redraw(1.0) |
|---|
| 565 |
if self.progBar: self.progBar.write('%1.1f seconds' % (time.time() - startTime)) |
|---|
| 566 |
|
|---|
| 567 |
return numOfHits, bw, ww, sw, continuations |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
def doGISearch(self, pbVar, pwVar, pVar, evVar, frVar, toVar, awVar, refVar, |
|---|
| 571 |
caseInsensitive): |
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
if not self.numOfGames(): return |
|---|
| 575 |
|
|---|
| 576 |
if caseInsensitive: |
|---|
| 577 |
pbVar = lower(pbVar) |
|---|
| 578 |
pwVar = lower(pwVar) |
|---|
| 579 |
pVar = lower(pVar) |
|---|
| 580 |
evVar = lower(evVar) |
|---|
| 581 |
awVar = lower(awVar) |
|---|
| 582 |
|
|---|
| 583 |
if frVar: |
|---|
| 584 |
if not (re.match('\d\d\d\d-\d\d-\d\d', frVar) or re.match('\d\d\d\d-\d\d', frVar) \ |
|---|
| 585 |
or re.match('\d\d\d\d', frVar)): |
|---|
| 586 |
frVar = '' |
|---|
| 587 |
|
|---|
| 588 |
if toVar: |
|---|
| 589 |
if re.match('\d\d\d\d-\d\d-\d\d', toVar) or re.match('\d\d\d\d-\d\d', toVar) or re.match('\d\d\d\d', toVar): |
|---|
| 590 |
toVar += 'Z' |
|---|
| 591 |
else: |
|---|
| 592 |
toVar = '' |
|---|
| 593 |
|
|---|
| 594 |
if not (pbVar or pwVar or pVar or evVar or frVar or toVar or awVar or refVar): return |
|---|
| 595 |
|
|---|
| 596 |
ctr = 0 |
|---|
| 597 |
currentTime = time.time() |
|---|
| 598 |
|
|---|
| 599 |
oldfilename = '' |
|---|
| 600 |
|
|---|
| 601 |
for DBIndex in range(len(self.DBlist[`self.boardsize`])): |
|---|
| 602 |
db = self.DBlist[`self.boardsize`][DBIndex] |
|---|
| 603 |
|
|---|
| 604 |
index = 'start' # we can't do index = db.next() at the end of the loop, so we need this |
|---|
| 605 |
|
|---|
| 606 |
while 1: |
|---|
| 607 |
if index != 'start': index = db.next() |
|---|
| 608 |
else: index = db.start() |
|---|
| 609 |
if index is None: break |
|---|
| 610 |
|
|---|
| 611 |
g = db.namelist[index] |
|---|
| 612 |
|
|---|
| 613 |
ctr = ctr + 1 |
|---|
| 614 |
if ctr % 10 == 0: |
|---|
| 615 |
if self.progBar: self.progBar.redraw(ctr*1.0/self.numOfGames()) |
|---|
| 616 |
|
|---|
| 617 |
if caseInsensitive: |
|---|
| 618 |
if pwVar and find(lower(g[2]), pwVar) == -1: continue |
|---|
| 619 |
if pbVar and find(lower(g[1]), pbVar) == -1: continue |
|---|
| 620 |
if pVar and find(lower(g[2]), pVar) == -1 \ |
|---|
| 621 |
and find(lower(g[1]), pVar) == -1: continue |
|---|
| 622 |
|
|---|
| 623 |
if frVar or toVar: |
|---|
| 624 |
date = g[5] |
|---|
| 625 |
|
|---|
| 626 |
if frVar and not date >= frVar: continue |
|---|
| 627 |
if toVar and not date <= toVar: continue |
|---|
| 628 |
|
|---|
| 629 |
if refVar and not self.references.has_key(g[4]): continue |
|---|
| 630 |
|
|---|
| 631 |
if evVar or awVar: |
|---|
| 632 |
filename, gameNumber = db.getFilename(index) |
|---|
| 633 |
|
|---|
| 634 |
if not oldfilename == filename: |
|---|
| 635 |
oldfilename = filename |
|---|
| 636 |
try: |
|---|
| 637 |
file = open(filename) |
|---|
| 638 |
sgf = file.read() |
|---|
| 639 |
file.close() |
|---|
| 640 |
except IOError: |
|---|
| 641 |
showwarning('IO Error', 'File not found: ' + filename) |
|---|
| 642 |
continue |
|---|
| 643 |
newfile = 1 |
|---|
| 644 |
else: newfile = 0 |
|---|
| 645 |
|
|---|
| 646 |
if gameNumber == -1: # no collection, so we get away without parsing the SGF file |
|---|
| 647 |
sgf = lower(sgf) |
|---|
| 648 |
if awVar and find(sgf, awVar) == -1: continue |
|---|
| 649 |
|
|---|
| 650 |
ok = 1 |
|---|
| 651 |
for var,tag in [(evVar,'ev[')]: # note that we have already lower'ed sgf! |
|---|
| 652 |
if var: |
|---|
| 653 |
v_begin = find(sgf, tag) |
|---|
| 654 |
if v_begin == -1: ok = 0 |
|---|
| 655 |
else: |
|---|
| 656 |
v_text = sgf[v_begin: find(sgf, ']', v_begin)] |
|---|
| 657 |
if find(v_text, var) == -1: ok = 0 |
|---|
| 658 |
if not ok: continue |
|---|
| 659 |
|
|---|
| 660 |
else: |
|---|
| 661 |
if newfile: |
|---|
| 662 |
try: |
|---|
| 663 |
c = Cursor(sgf, 1) |
|---|
| 664 |
except: |
|---|
| 665 |
showwarning('Error', 'SGF error in file ' + filename) |
|---|
| 666 |
continue |
|---|
| 667 |
node = c.getRootNode(gameNumber) |
|---|
| 668 |
|
|---|
| 669 |
ok = 1 |
|---|
| 670 |
for var,tag in [(evVar,'EV')]: |
|---|
| 671 |
if var and not (node.has_key(tag) and find(lower(node[tag][0]), var) != -1): |
|---|
| 672 |
ok = 0 |
|---|
| 673 |
if not ok: continue |
|---|
| 674 |
|
|---|
| 675 |
if awVar: |
|---|
| 676 |
c.game(gameNumber) |
|---|
| 677 |
s = lower(c.outputVar(c.currentN)) |
|---|
| 678 |
if find(s, awVar) == -1: continue |
|---|
| 679 |
else: |
|---|
| 680 |
if pwVar and find(g[2], pwVar) == -1: continue |
|---|
| 681 |
if pbVar and find(g[1], pbVar) == -1: continue |
|---|
| 682 |
if pVar and find(g[2], pVar) == -1 \ |
|---|
| 683 |
and find(g[1], pVar) == -1: continue |
|---|
| 684 |
|
|---|
| 685 |
if frVar or toVar: |
|---|
| 686 |
date = g[5] |
|---|
| 687 |
|
|---|
| 688 |
if frVar and not date >= frVar: continue |
|---|
| 689 |
if toVar and not date <= toVar: continue |
|---|
| 690 |
|
|---|
| 691 |
if refVar and not self.references.has_key(g[4]): continue |
|---|
| 692 |
|
|---|
| 693 |
if evVar or awVar: |
|---|
| 694 |
filename, gameNumber = db.getFilename(index) |
|---|
| 695 |
|
|---|
| 696 |
if not oldfilename == filename: |
|---|
| 697 |
oldfilename = filename |
|---|
| 698 |
try: |
|---|
| 699 |
file = open(filename) |
|---|
| 700 |
sgf = file.read() |
|---|
| 701 |
file.close() |
|---|
| 702 |
except IOError: |
|---|
| 703 |
showwarning('IO Error', 'File not found: ' + filename) |
|---|
| 704 |
continue |
|---|
| 705 |
newfile = 1 |
|---|
| 706 |
else: |
|---|
| 707 |
newfile = 0 |
|---|
| 708 |
|
|---|
| 709 |
if gameNumber == -1: |
|---|
| 710 |
|
|---|
| 711 |
if awVar and find(sgf, awVar) == -1: continue |
|---|
| 712 |
|
|---|
| 713 |
ok = 1 |
|---|
| 714 |
for var,tag in [(evVar,'EV[')]: |
|---|
| 715 |
if var: |
|---|
| 716 |
v_begin = find(sgf, tag) |
|---|
| 717 |
if v_begin == -1: ok = 0 |
|---|
| 718 |
else: |
|---|
| 719 |
v_text = sgf[v_begin: find(sgf, ']', v_begin)] |
|---|
| 720 |
if find(v_text, var) == -1: ok = 0 |
|---|
| 721 |
if not ok: continue |
|---|
| 722 |
|
|---|
| 723 |
else: |
|---|
| 724 |
if newfile: |
|---|
| 725 |
try: c = Cursor(sgf, 1) |
|---|
| 726 |
except: |
|---|
| 727 |
showwarning('Error', 'SGF error in file ' + filename) |
|---|
| 728 |
continue |
|---|
| 729 |
|
|---|
| 730 |
node = c.getRootNode(gameNumber) |
|---|
| 731 |
|
|---|
| 732 |
ok = 1 |
|---|
| 733 |
for var,tag in [(evVar,'EV')]: |
|---|
| 734 |
if var and not (node.has_key(tag) and find(node[tag][0], var) != -1): |
|---|
| 735 |
ok = 0 |
|---|
| 736 |
if not ok: continue |
|---|
| 737 |
|
|---|
| 738 |
if awVar: |
|---|
| 739 |
c.game(gameNumber) |
|---|
| 740 |
s = c.outputVar(c.currentN) |
|---|
| 741 |
if find(s, awVar) == -1: continue |
|---|
| 742 |
|
|---|
| 743 |
db.makeCurrentHit('') |
|---|
| 744 |
|
|---|
| 745 |
self.updateGameIndex() |
|---|
| 746 |
|
|---|
| 747 |
if self.progBar: |
|---|
| 748 |
self.progBar.redraw(1) |
|---|
| 749 |
self.progBar.write('%1.1f seconds' % (time.time() - currentTime)) |
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
def doSigSearch(self, l): |
|---|
| 753 |
|
|---|
| 754 |
if not self.numOfGames(): return |
|---|
| 755 |
|
|---|
| 756 |
sig_list = [] |
|---|
| 757 |
for f in Pattern.flips: # we cannot use symmetrizeSig here because of the wildcards |
|---|
| 758 |
k1 = [ f(x[0], x[1]) for x in l] |
|---|
| 759 |
k2 = [] |
|---|
| 760 |
for i in range(6): |
|---|
| 761 |
if chr(l[i][0]+ord('a')) == '.': k2.append('..') |
|---|
| 762 |
else: |
|---|
| 763 |
k2.append(chr(k1[i][0]+ord('a'))) |
|---|
| 764 |
k2.append(chr(k1[i][1]+ord('a'))) |
|---|
| 765 |
|
|---|
| 766 |
s = join(k2, '') |
|---|
| 767 |
if not s in sig_list: sig_list.append(s) |
|---|
| 768 |
|
|---|
| 769 |
sp_list = [re.compile(s) for s in sig_list] |
|---|
| 770 |
|
|---|
| 771 |
ctr = 0 |
|---|
| 772 |
if self.progBar: self.progBar.clear() |
|---|
| 773 |
currentTime = time.time() |
|---|
| 774 |
|
|---|
| 775 |
for db in self.DBlist[`self.boardsize`]: |
|---|
| 776 |
index = db.start() |
|---|
| 777 |
while not index is None: |
|---|
| 778 |
ctr = ctr + 1 |
|---|
| 779 |
if ctr % 100 == 0: |
|---|
| 780 |
if self.progBar: self.progBar.redraw(ctr*1.0/self.numOfGames()) |
|---|
| 781 |
|
|---|
| 782 |
for sp in sp_list: |
|---|
| 783 |
if sp.match(db.namelist[index][4]): |
|---|
| 784 |
db.makeCurrentHit('') |
|---|
| 785 |
break |
|---|
| 786 |
index = db.next() |
|---|
| 787 |
|
|---|
| 788 |
self.updateGameIndex() |
|---|
| 789 |
if self.progBar: |
|---|
| 790 |
self.progBar.redraw(1) |
|---|
| 791 |
self.progBar.write('%1.1f seconds' % (time.time() - currentTime)) |
|---|
| 792 |
|
|---|