File: db_import.py

package info (click to toggle)
gnubg 1.08.003-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,308 kB
  • sloc: ansic: 104,162; xml: 15,451; sh: 5,292; pascal: 820; yacc: 700; makefile: 586; python: 538; lex: 286; sql: 236; awk: 26
file content (134 lines) | stat: -rw-r--r-- 3,543 bytes parent folder | download
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
# Copyright (C) 2004 Jon Kinsey <Jon_Kinsey@hotmail.com>
# Copyright (C) 2014 Michael Petch <mpetch@gnubg.org>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

#
# $Id: db_import.py,v 1.8 2022/06/29 21:09:18 plm Exp $
#

"""
 db_import.py -- batch import of multiple sgf files into relational database

 by Jon Kinsey <Jon_Kinsey@hotmail.com>, 2004
\n"""

import os
import sys

if sys.version_info >= (3, 0):
    def python3_raw_input(prompt):
        return input(prompt)

    raw_input = python3_raw_input


def GetFiles(dir):
    "Look for gnubg import files in dir"
    try:
        files = os.listdir(dir)
    except OSError:
        print("  ** Directory not found **")
        return 0

    fileList = []
    foundAnyFile = False
    foundBGFile = False
    # Check each file in dir
    for file in files:
        # Check it's a file (not a directory)
        if os.path.isfile(dir + file):
            foundAnyFile = True
            # Check has supported extension
            dot = file.rfind('.')
            if dot != -1:
                ext = file[dot + 1:].lower()
                if ext == "sgf":
                    foundBGFile = True
                    fileList.append(file)

    if foundBGFile:
        return fileList
    else:
        if not foundAnyFile:
            print("  ** No files in directory **")
        else:
            print("  ** No sgf files found in directory **")
        return 0


def ImportFile(prompt, file, dir):
    "Run commands to import stats into gnubg"
    print(prompt + " Importing " + file)
    gnubg.command('load match "' + dir + file + '"')
    gnubg.command('relational add match')


def GetYN(prompt):
    confirm = ''
    while len(confirm) == 0 or (confirm[0] != 'y' and confirm[0] != 'n'):
        confirm = raw_input(prompt + " (y/n): ").lower()
    return confirm


def GetDir(prompt):
    dir = raw_input(prompt)
    if dir:
        # Make sure dir ends in a slash
        if (dir[-1] != '\\' and dir[-1] != '/'):
            dir = dir + '/'
    return dir


def BatchImport():
    "Import stats for all sgf files in a directory"

    inFiles = []
    while not inFiles:
        # Get directory with original files in
        dir = GetDir("Directory containing files to import (enter-exit): ")
        if not dir:
            return

        # Look for some files
        inFiles = GetFiles(dir)

    # Display files that will be analyzed
    for file in inFiles:
        print("    " + file)

    print("\n", len(inFiles), "files found\n")

    # Check user wants to continue
    if GetYN("Continue") == 'n':
        return

    # Get stats for each file
    num = 0
    for file in inFiles:
        num = num + 1
        prompt = "(%d/%d)" % (num, len(inFiles))
        ImportFile(prompt, file, dir)

    print("\n** Finished **")
    return


# Run batchimport on load
try:
    print(__doc__)
    BatchImport()
except Exception:
    e = sys.exc_info()[1].args[0]
    print("Error: " + e)