[pychess] push by gbtami - Handle non existing eco.db error on 2012-01-02 12:35 GMT

3 views
Skip to first unread message

pyc...@googlecode.com

unread,
Jan 2, 2012, 7:36:31 AM1/2/12
to pychess...@googlegroups.com
Revision: fcd8faaf19fa
Author: gbtami
Date: Mon Jan 2 04:34:24 2012
Log: Handle non existing eco.db error
http://code.google.com/p/pychess/source/detail?r=fcd8faaf19fa

Added:
/pgn2ecodb.py
/pgn2ecodb.sh
Deleted:
/pgn2fen.py
/pgn2fen.sh
Modified:
/lib/pychess/Utils/eco.py

=======================================
--- /dev/null
+++ /pgn2ecodb.py Mon Jan 2 04:34:24 2012
@@ -0,0 +1,80 @@
+# English eco.pgn was converted from
+#
http://www.chessville.com/downloads_files/instructional_materials/ECO_Codes_With_Names_and_Moves.zip
+# others from wikipedia
+
+import os
+import sys
+import sqlite3
+import struct
+
+from pychess.Savers.pgn import load
+from pychess.System.prefix import addDataPrefix
+from pychess.Utils.eco import hash_struct
+
+path = os.path.join(addDataPrefix("eco.db"))
+conn = sqlite3.connect(path)
+
+if __name__ == '__main__':
+ c = conn.cursor()
+
+ c.execute("drop table if exists openings")
+
+ # Unfortunately sqlite doesn't support uint64, so we have to use blob
type to store polyglot-hash values
+ c.execute("create table openings(hash blob, base integer, eco text,
lang text, opening text, variation text)")
+
+ def feed(pgnfile, lang):
+ cf = load(open(pgnfile))
+ rows = []
+ old_eco = ""
+ ply_max = 0
+ for i, game in enumerate(cf.games):
+ model = cf.loadToModel(i, quick_parse=True)
+
+ eco = cf._getTag(i, "ECO")[:3]
+
+ opening = cf._getTag(i, "Opening")
+ if opening is None:
+ opening = ""
+
+ variation = cf._getTag(i, "Variation")
+ if variation is None:
+ variation = ""
+
+ base = int(old_eco != eco)
+
+ ply = len(model.moves)
+ ply_max = max(ply_max, ply)
+ if ply == 0:
+ cu = conn.cursor()
+ cu.execute("select * from openings where eco=? and
lang='en' and base=1", (eco,))
+ res = cu.fetchone()
+ if res is not None:
+ hash = res[0]
+ else:
+ hash =
buffer(hash_struct.pack(model.boards[-1].board.hash))
+
+ if opening:
+ rows.append((hash, base, eco, lang, opening, variation))
+
+ old_eco = eco
+
+ c.executemany("insert into openings(hash, base, eco, lang,
opening, variation) values (?, ?, ?, ?, ?, ?)", rows)
+ conn.commit()
+
+ print "Max ply was %s" % ply_max
+
+ # Several eco list contains only eco+name pairs, so
+ # we will use base ECO line positions from en eco.pgn
+ print "processing en eco.pgn"
+ feed("lang/en/eco.pgn", "en")
+
+ for lang in [d for d in os.listdir("lang") if
os.path.isdir("lang/"+d)]:
+ if lang == "en":
+ continue
+
+ pgnfile = "lang/%s/eco.pgn" % lang
+ if os.path.isfile(pgnfile):
+ print "processing %s eco.pgn" % lang
+ feed(pgnfile, lang)
+
+ conn.close()
=======================================
--- /dev/null
+++ /pgn2ecodb.sh Mon Jan 2 04:34:24 2012
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+PYTHONPATH=./lib python -3 -W ignore pgn2ecodb.py
=======================================
--- /pgn2fen.py Thu Dec 15 12:13:53 2011
+++ /dev/null
@@ -1,80 +0,0 @@
-# English eco.pgn was converted from
-#
http://www.chessville.com/downloads_files/instructional_materials/ECO_Codes_With_Names_and_Moves.zip
-# others from wikipedia
-
-import os
-import sys
-import sqlite3
-import struct
-
-from pychess.Savers.pgn import load
-from pychess.System.prefix import addDataPrefix
-from pychess.Utils.eco import hash_struct
-
-path = os.path.join(addDataPrefix("eco.db"))
-conn = sqlite3.connect(path)
-
-if __name__ == '__main__':
- c = conn.cursor()
-
- c.execute("drop table if exists openings")
-
- # Unfortunately sqlite doesn't support uint64, so we have to use blob
type to store polyglot-hash values
- c.execute("create table openings(hash blob, base integer, eco text,
lang text, opening text, variation text)")
-
- def feed(pgnfile, lang):
- cf = load(open(pgnfile))
- rows = []
- old_eco = ""
- ply_max = 0
- for i, game in enumerate(cf.games):
- model = cf.loadToModel(i, quick_parse=True)
-
- eco = cf._getTag(i, "ECO")[:3]
-
- opening = cf._getTag(i, "Opening")
- if opening is None:
- opening = ""
-
- variation = cf._getTag(i, "Variation")
- if variation is None:
- variation = ""
-
- base = int(old_eco != eco)
-
- ply = len(model.moves)
- ply_max = max(ply_max, ply)
- if ply == 0:
- cu = conn.cursor()
- cu.execute("select * from openings where eco=? and
lang='en' and base=1", (eco,))
- res = cu.fetchone()
- if res is not None:
- hash = res[0]
- else:
- hash =
buffer(hash_struct.pack(model.boards[-1].board.hash))
-
- if opening:
- rows.append((hash, base, eco, lang, opening, variation))
-
- old_eco = eco
-
- c.executemany("insert into openings(hash, base, eco, lang,
opening, variation) values (?, ?, ?, ?, ?, ?)", rows)
- conn.commit()
-
- print "Max ply was %s" % ply_max
-
- # Several eco list contains only eco+name pairs, so
- # we will use base ECO line positions from en eco.pgn
- print "processing en eco.pgn"
- feed("lang/en/eco.pgn", "en")
-
- for lang in [d for d in os.listdir("lang") if
os.path.isdir("lang/"+d)]:
- if lang == "en":
- continue
-
- pgnfile = "lang/%s/eco.pgn" % lang
- if os.path.isfile(pgnfile):
- print "processing %s eco.pgn" % lang
- feed(pgnfile, lang)
-
- conn.close()
=======================================
--- /pgn2fen.sh Sun Dec 4 13:06:43 2011
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-
-PYTHONPATH=./lib python -3 -W ignore pgn2fen.py
=======================================
--- /lib/pychess/Utils/eco.py Thu Dec 15 12:13:53 2011
+++ /lib/pychess/Utils/eco.py Mon Jan 2 04:34:24 2012
@@ -6,10 +6,14 @@

from pychess.System.prefix import addDataPrefix

-path = os.path.join(addDataPrefix("eco.db"))
-conn = sqlite3.connect(path, check_same_thread = False)
-
-atexit.register(conn.close)
+db_path = os.path.join(addDataPrefix("eco.db"))
+if os.path.exists(db_path):
+ conn = sqlite3.connect(db_path, check_same_thread = False)
+ atexit.register(conn.close)
+ ECO_OK = True
+else:
+ print "Warning: eco.db not find, run pgn2ecodb.sh"
+ ECO_OK = False

mofile = gettext.find('pychess', localedir=addDataPrefix("lang"))
if mofile is None:
@@ -21,6 +25,8 @@
hash_struct = struct.Struct('>Q')

def get_eco(hash):
+ if not ECO_OK:
+ return
cur = conn.cursor()
select = "select eco, opening, variation from openings where hash=?
and lang=?"
cur.execute(select, (buffer(hash_struct.pack(hash)), lang))

Reply all
Reply to author
Forward
0 new messages