summaryrefslogtreecommitdiff
path: root/pybcdd.py
blob: 019f1c9d728747e017f23cb93421498bc043fdf6 (plain)
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
import os
import sys

from bcdd.DDTable import DDTable
from bcdd.ParScore import ParScore
from bcdd.Exceptions import DllNotFoundException, FieldNotFoundException
from bcdd.PBNFile import PBNFile

def get_files(args):
    jfr_only = False
    if '--jfr' in args:
        jfr_only = True
        args.remove('--jfr')
    filenames = [name for name in args
                 if os.path.exists(name)
                 and (os.path.realpath(name) != os.path.realpath(__file__))]
    if len(filenames) == 0:
        raise FileNotFoundError('No valid filepaths provided!')
    return filenames, jfr_only

def main():
    files, jfr_only = get_files(sys.argv)
    errors = []
    for filename in files:
        try:
            print('Analyzing %s' % (filename))
            pbn_file = PBNFile(filename)
            for board in pbn_file.boards:
                table = DDTable(board)
                board_no = ''
                try:
                    board_no = board.get_number()
                except FieldNotFoundException:
                    board_no = '?'
                try:
                    dd_table = table.get_dd_table()
                    if dd_table is not None:
                        print('Board ' + board_no)
                        table.print_table(dd_table)
                        par = ParScore(board)
                        contract = par.get_par_contract(dd_table)
                        print(contract)
                        print('')
                        board.save_dd_table(dd_table, jfr_only)
                        board.save_par_contract(contract, jfr_only)
                        pbn_file.write_board(board)
                    else:
                        error = 'unable to determine DD table for board %s' \
                                % (board_no)
                        errors.append('[%s] %s' % (filename, error))
                        print('ERROR: ' + error)
                except DllNotFoundException:
                    raise
                except Exception as ex:
                    errors.append('[%s:%s] %s' % (filename, board_no, str(ex)))
                    print('ERROR: ' + str(ex))
                    pbn_file.write_board(board)
            pbn_file.save()
        except DllNotFoundException as ex:
            errors.append("libbcalcdds library could not be loaded - make sure it's present in application directory!");
            print('ERROR: ' + str(ex))
            break
        except Exception as ex:
            errors.append(str(ex))
            print('ERROR: ' + str(ex));
            raise
    if len(errors) > 0:
        print('Following ERRORs occured:')
        for error in errors:
            print(error)

if __name__ == '__main__':
    main()