~ubuntu-branches/debian/sid/gnubg/sid

« back to all changes in this revision

Viewing changes to scripts/db_import.py

  • Committer: Bazaar Package Importer
  • Author(s): Russ Allbery
  • Date: 2006-12-28 10:45:05 UTC
  • mfrom: (2.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061228104505-4p6sxxdosrlvhgpr
Tags: 0.14.3+20060923-4
* Translation updates:
  - French, thanks Thomas Huriaux.  (Closes: #404254)
  - Spanish, thanks Javier Ruano.  (Closes: #404613)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 version 2 of the GNU General Public License as
 
8
# published by the Free Software Foundation.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
# $Id: db_import.py,v 1.1 2004/11/08 12:24:41 Superfly_Jon 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
 
 
27
import os
 
28
 
 
29
def GetFiles(dir):
 
30
    "Look for gnubg import files in dir"
 
31
    try:
 
32
        files = os.listdir(dir)
 
33
    except:
 
34
        print "  ** Directory not found **"
 
35
        return 0
 
36
 
 
37
    fileList = []
 
38
    foundAnyFile = False
 
39
    foundBGFile = False
 
40
    # Check each file in dir
 
41
    for file in files:
 
42
        # Check it's a file (not a directory)
 
43
        if os.path.isfile(dir + file):
 
44
            foundAnyFile = True
 
45
            # Check has supported extension
 
46
            dot = file.rfind('.')
 
47
            if dot != -1:
 
48
                ext = file[dot + 1 : ].lower()
 
49
                if ext == "sgf":
 
50
                    foundBGFile = True
 
51
                    fileList.append(file)
 
52
 
 
53
    if foundBGFile:
 
54
        return fileList
 
55
    else:
 
56
        if not foundAnyFile:
 
57
            print "  ** No files in directory **"
 
58
        else:
 
59
            print "  ** No sgf files found in directory **"
 
60
        return 0
 
61
 
 
62
def ImportFile(prompt, file, dir):
 
63
    "Run commands to import stats into gnubg"
 
64
    print prompt + " Importing " + file
 
65
    gnubg.command('load match "' + dir + file + '"')
 
66
    gnubg.command('relational add match')
 
67
 
 
68
def GetYN(prompt):
 
69
    confirm = '';
 
70
    while len(confirm) == 0 or (confirm[0] != 'y' and confirm[0] != 'n'):
 
71
        confirm = raw_input(prompt + " (y/n): ").lower()
 
72
    return confirm
 
73
 
 
74
def GetDir(prompt):
 
75
    dir = raw_input(prompt)
 
76
    if dir:
 
77
        # Make sure dir ends in a slash
 
78
        if (dir[-1] != '\\' and dir[-1] != '/'):
 
79
            dir = dir + '/'
 
80
    return dir
 
81
 
 
82
def BatchImport():
 
83
    "Import stats for all sgf files in a directory"
 
84
 
 
85
    inFiles = []
 
86
    while not inFiles:
 
87
        # Get directory with original files in
 
88
        dir = GetDir("Directory containing files to import (enter-exit): ")
 
89
        if not dir:
 
90
            return
 
91
 
 
92
        # Look for some files
 
93
        inFiles = GetFiles(dir)
 
94
 
 
95
    # Display files that will be analyzed
 
96
    for file in inFiles:
 
97
        print "    " + file
 
98
 
 
99
    print "\n", len(inFiles), "files found\n"
 
100
 
 
101
    # Check user wants to continue
 
102
    if GetYN("Continue") == 'n':
 
103
        return
 
104
    
 
105
    # Get stats for each file
 
106
    num = 0
 
107
    for file in inFiles:
 
108
        num = num + 1
 
109
        prompt = "(%d/%d)" % (num, len(inFiles))
 
110
        ImportFile(prompt, file, dir)
 
111
 
 
112
    print "\n** Finished **"
 
113
    return
 
114
 
 
115
# Run batchimport on load
 
116
try:
 
117
    print __doc__
 
118
    BatchImport()
 
119
except Exception, (e):
 
120
    print "Error:", e