1#
2# db_import.py -- batch import of multiple sgf files into relational database
3#
4# by Jon Kinsey <Jon_Kinsey@hotmail.com>, 2004
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id: db_import.py,v 1.6 2014/08/08 10:55:51 mdpetch Exp $
20
21"""
22 db_import.py -- batch import of multiple sgf files into relational database
23
24 by Jon Kinsey <Jon_Kinsey@hotmail.com>, 2004
25\n"""
26
27import os
28
29if sys.version_info >= (3, 0):
30    def python3_raw_input(prompt):
31        return input(prompt)
32
33    raw_input = python3_raw_input
34
35def GetFiles(dir):
36    "Look for gnubg import files in dir"
37    try:
38        files = os.listdir(dir)
39    except:
40        print ("  ** Directory not found **")
41        return 0
42
43    fileList = []
44    foundAnyFile = False
45    foundBGFile = False
46    # Check each file in dir
47    for file in files:
48        # Check it's a file (not a directory)
49        if os.path.isfile(dir + file):
50            foundAnyFile = True
51            # Check has supported extension
52            dot = file.rfind('.')
53            if dot != -1:
54                ext = file[dot + 1:].lower()
55                if ext == "sgf":
56                    foundBGFile = True
57                    fileList.append(file)
58
59    if foundBGFile:
60        return fileList
61    else:
62        if not foundAnyFile:
63            print ("  ** No files in directory **")
64        else:
65            print ("  ** No sgf files found in directory **")
66        return 0
67
68
69def ImportFile(prompt, file, dir):
70    "Run commands to import stats into gnubg"
71    print (prompt + " Importing " + file)
72    gnubg.command('load match "' + dir + file + '"')
73    gnubg.command('relational add match')
74
75
76def GetYN(prompt):
77    confirm = ''
78    while len(confirm) == 0 or (confirm[0] != 'y' and confirm[0] != 'n'):
79        confirm = raw_input(prompt + " (y/n): ").lower()
80    return confirm
81
82
83def GetDir(prompt):
84    dir = raw_input(prompt)
85    if dir:
86        # Make sure dir ends in a slash
87        if (dir[-1] != '\\' and dir[-1] != '/'):
88            dir = dir + '/'
89    return dir
90
91
92def BatchImport():
93    "Import stats for all sgf files in a directory"
94
95    inFiles = []
96    while not inFiles:
97        # Get directory with original files in
98        dir = GetDir("Directory containing files to import (enter-exit): ")
99        if not dir:
100            return
101
102        # Look for some files
103        inFiles = GetFiles(dir)
104
105    # Display files that will be analyzed
106    for file in inFiles:
107        print ("    " + file)
108
109    print ("\n", len(inFiles), "files found\n")
110
111    # Check user wants to continue
112    if GetYN("Continue") == 'n':
113        return
114
115    # Get stats for each file
116    num = 0
117    for file in inFiles:
118        num = num + 1
119        prompt = "(%d/%d)" % (num, len(inFiles))
120        ImportFile(prompt, file, dir)
121
122    print ("\n** Finished **")
123    return
124
125# Run batchimport on load
126try:
127    print (__doc__)
128    BatchImport()
129except Exception:
130    e = sys.exc_info()[1].args[0]
131    print ("Error: " + e)
132