summaryrefslogtreecommitdiff
path: root/src/bidding_data.py
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2015-09-13 01:08:21 +0200
committeremkael <emkael@tlen.pl>2015-09-13 01:08:21 +0200
commitad104e7d8edcc77274f37014650e0d19ec50ddef (patch)
tree3f3ba5d1d04f7c83a3e8036197f387509024ad30 /src/bidding_data.py
parente9dee61993bc002116241d1135ce25ed269c243e (diff)
* logging debug information
Diffstat (limited to 'src/bidding_data.py')
-rw-r--r--src/bidding_data.py127
1 files changed, 113 insertions, 14 deletions
diff --git a/src/bidding_data.py b/src/bidding_data.py
index a5550c3..3c7d8f7 100644
--- a/src/bidding_data.py
+++ b/src/bidding_data.py
@@ -20,12 +20,15 @@ class JFRBidding:
def __parse_lineup_data(self, sitting_data):
round_lineups = {}
for sitting in sitting_data:
- round_no = sitting[2]
+ log.getLogger('lineup').debug(sitting)
+ round_no = sitting[2] if sitting[2] is not None else 0
table_no = str(sitting[0]) + '_' + str(sitting[1])
+ lineup = sorted([sitting[3], sitting[4]])
if round_no not in round_lineups:
round_lineups[round_no] = {}
- round_lineups[round_no][table_no] = sorted([
- sitting[3], sitting[4]])
+ round_lineups[round_no][table_no] = lineup
+ log.getLogger('lineup').debug('round %d, table %s: %s',
+ round_no, table_no, lineup)
return round_lineups
# converts BWS bidding to the structure:
@@ -34,6 +37,7 @@ class JFRBidding:
def __parse_bidding_data(self, bidding_data):
bids = {}
for bid in bidding_data:
+ log.getLogger('bidding').debug(bid)
round_no = bid[3]
table_no = str(bid[1]) + '_' + str(bid[2])
board_no = str(bid[4]) + '_' + str(round_no) + '_' + table_no
@@ -51,15 +55,30 @@ class JFRBidding:
'direction'] == bid[6]:
bids[board_no][table_no][round_no].pop(
bid_counter, None)
+ log.getLogger('bidding').debug(
+ 'erased bid %d from board %s, ' +
+ 'round %s, table %s-%s',
+ bid_counter, *board_no.split('_'))
if len(bids[board_no][table_no][round_no]) == 0:
bids[board_no][table_no].pop(round_no, None)
+ log.getLogger('bidding').debug(
+ 'bidding on board %s, round %s, ' +
+ 'table %s-%s empty, removing',
+ *board_no.split('_'))
+ else:
+ log.getLogger('bidding').debug(
+ 'bid does not match, not removing')
else:
bids[board_no][table_no][round_no][bid_counter] = {
'direction': bid[6], 'bid': bid[7]}
+ log.getLogger('bidding').debug(
+ 'board %s, round %s, table %s-%s, bid %d: %s by %s',
+ *(board_no.split('_') + [bid_counter, bid[7], bid[6]]))
return bids
# converts bidding data into HTML table
def __format_bidding(self, bidding):
+ log.getLogger('b_format').debug('formatting bidding: %s', bidding)
bid_match = re.compile('(\d)([SHDCN])')
html_output = bs4('<table>', 'lxml')
header_row = html_output.new_tag('tr')
@@ -82,6 +101,7 @@ class JFRBidding:
else:
bid_cell.append(bid)
bidding_row.append(bid_cell)
+ log.getLogger('b_format').debug('%5s' * 4, *bid_round)
return html_output.table.prettify()
# returns file path for bidding HTML output
@@ -103,7 +123,10 @@ class JFRBidding:
f for f
in glob.glob(self.__tournament_prefix + '*.html')
if re.search(self.__tournament_files_match, f)]
+ log.getLogger('b_map').debug('found %d possible board files to map',
+ len(self.__tournament_files))
for round_data in self.__lineup_data:
+ log.getLogger('b_map').debug('round data: %s', round_data)
# 13th column has JFR number for the first board
if len(round_data) > 12:
jfr_number = round_data[12]
@@ -119,9 +142,13 @@ class JFRBidding:
str(round_no),
str(sector_no),
str(table_no)])
+ board_no = jfr_number + board_number - round_data[5]
self.__board_number_mapping[
board_string
- ] = jfr_number + board_number - round_data[5]
+ ] = board_no
+ log.getLogger('b_map').debug('mapping %s -> %d',
+ board_string,
+ board_no)
# only include these board numbers from mapping
# which actually exist in JFR output
custom_files = []
@@ -134,6 +161,9 @@ class JFRBidding:
custom_files = custom_files + board_files
else:
self.__board_number_mapping[b_number] = None
+ log.getLogger('b_map').debug(
+ 'board %s -> %d not in board files, ignoring',
+ b_number, jfr_number)
self.__tournament_files = list(set(custom_files))
# sitting read from BWS
@@ -155,17 +185,27 @@ class JFRBidding:
__bidding_files = []
def __init__(self, bws_file, file_prefix):
+ log.getLogger('init').debug('reading BWS file: %s', bws_file)
with pypyodbc.win_connect_mdb(bws_file) as connection:
cursor = connection.cursor()
self.__lineup_data = cursor.execute(
'SELECT * FROM RoundData').fetchall()
bid_data = cursor.execute('SELECT * FROM BiddingData').fetchall()
+ log.getLogger('init').debug('parsing lineup data (%d entries)',
+ len(self.__lineup_data))
self.__round_lineups = self.__parse_lineup_data(self.__lineup_data)
+ log.getLogger('init').debug('parsing bidding data (%d entries)',
+ len(bid_data))
self.__bids = self.__parse_bidding_data(bid_data)
+ log.getLogger('init').debug('parsing prefix, filename = %s',
+ file_prefix)
self.__tournament_prefix = path.splitext(
path.realpath(file_prefix))[0]
+ log.getLogger('init').debug('prefix = %s', self.__tournament_prefix)
self.__tournament_files_match = re.compile(
re.escape(self.__tournament_prefix) + '([0-9]{3})\.html')
+ log.getLogger('init').debug('tournament files pattern: %s',
+ self.__tournament_files_match.pattern)
self.__map_board_numbers()
def write_bidding_tables(self):
@@ -177,6 +217,9 @@ class JFRBidding:
if table_no in self.__round_lineups[round_no]:
bidding = sorted(round_data)
dealer = round_data[bidding[0]]['direction']
+ log.getLogger('tables').debug(
+ 'board %s: %d bids, dealer %s',
+ board_no, len(bidding), dealer)
bidding_table = [[], [], [], []]
# compile bidding player-by-player
for bid_index in bidding:
@@ -200,6 +243,9 @@ class JFRBidding:
# transpose the bidding table
# aligning it row-by-row (bid round-by-round)
bidding_table = map(list, zip(*bidding_table))
+ log.getLogger('tables').debug(
+ 'compiled into %d rounds of bidding',
+ len(bidding_table))
bidding_fpath = \
self.__get_bidding_file_output_path(
self.__board_number_mapping[board_no],
@@ -208,9 +254,26 @@ class JFRBidding:
with file(bidding_fpath, 'w') as bidding_file:
bidding_file.write(
self.__format_bidding(bidding_table))
+ log.getLogger('tables').info(
+ 'written bidding table to %s',
+ bidding_fpath)
+ else:
+ log.getLogger('tables').info(
+ 'lineup for table %s, round %s not found',
+ table_no, round_no)
+ print self.__round_lineups[round_no]
+ else:
+ log.getLogger('tables').info(
+ 'lineup for round %s not found',
+ round_no)
+ else:
+ log.getLogger('tables').info('mapping for board %s not found',
+ board_no)
def write_bidding_scripts(self):
for tournament_file in self.__tournament_files:
+ log.getLogger('scripts').info('writing scripts into: %s',
+ tournament_file)
with file(tournament_file, 'r+') as board_html:
board_content = bs4(board_html, 'lxml', from_encoding='utf-8')
header_scripts = board_content.select('head script')
@@ -223,10 +286,13 @@ class JFRBidding:
type='text/javascript')
jquery_scripts.append(jquery)
board_content.head.append(jquery)
+ log.getLogger('scripts').debug('jQuery not found, adding')
# check for bidding.js
bidding_scripts = [
script for script in header_scripts
if script['src'] == 'javas/bidding.js']
+ log.getLogger('scripts').debug('found %d bidding.js scripts',
+ len(bidding_scripts))
# and make sure bidding.js is appended after jQuery
for script in bidding_scripts:
script.extract()
@@ -245,11 +311,19 @@ class JFRBidding:
self.__tournament_files_match,
tournament_file).group(1)
board_text_path = path.splitext(tournament_file)[0] + '.txt'
+ log.getLogger('links').info('writing traveller for board %s: %s',
+ file_number, board_text_path)
with file(board_text_path, 'r+') as board_text:
board_text_content = bs4(
board_text, 'lxml')
for row in board_text_content.select('tr'):
cells = row.select('td')
+ debug_string = ' '.join(map(
+ lambda c: ''.join(filter(
+ lambda cc: isinstance(cc, basestring),
+ c.contents)).strip(),
+ cells))
+ log.getLogger('links').debug('row: %s', debug_string)
# traveller table rows for specific score entries
# should have 11 cells
if len(cells) == 11:
@@ -257,7 +331,11 @@ class JFRBidding:
pair_numbers = sorted([
int(cells[1].contents[0]),
int(cells[2].contents[0])])
+ log.getLogger('links').debug(
+ 'pairs: %s', pair_numbers)
except ValueError:
+ log.getLogger('links').debug(
+ 'invalid pair numbers, skipping')
continue
bidding_link = board_text_content.new_tag(
'a', href='#', **{'class': 'biddingLink'})
@@ -267,22 +345,35 @@ class JFRBidding:
pair_numbers=pair_numbers)
bidding_link['data-bidding-link'] = path.basename(
bidding_path)
- if bidding_path in self.__bidding_files:
- del self.__bidding_files[
- self.__bidding_files.index(bidding_path)]
# only append link if we've got bidding data
if path.isfile(path.join(
path.dirname(self.__tournament_prefix),
bidding_link['data-bidding-link'])):
+ if bidding_path in self.__bidding_files:
+ del self.__bidding_files[
+ self.__bidding_files.index(bidding_path)]
+ log.getLogger('links').info(
+ 'linking: %s',
+ bidding_link['data-bidding-link'])
# fourth cell is the contract
for link in cells[3].select('a.biddingLink'):
+ log.getLogger('links').debug(
+ 'removing existing link')
link.extract()
cells[3].append(bidding_link)
+ else:
+ log.getLogger('links').warning(
+ 'bidding for file path %s not found',
+ bidding_link['data-bidding-link'])
+ else:
+ log.getLogger('links').debug('skipping row')
board_text.seek(0)
board_text.write(board_text_content.table.prettify(
'utf-8', formatter='html'))
board_text.truncate()
for unused_file in self.__bidding_files:
+ log.getLogger('links').warning(
+ 'bidding file %s not used, deleting', unused_file)
remove(unused_file)
if __name__ == '__main__':
@@ -343,10 +434,18 @@ if __name__ == '__main__':
'%(levelname)-8s %(name)-8s: %(message)s'))
log.getLogger().addHandler(console_log)
- bidding_parser = JFRBidding(
- bws_file=arguments.bws_file,
- file_prefix=arguments.path,
- )
- bidding_parser.write_bidding_tables()
- bidding_parser.write_bidding_scripts()
- bidding_parser.write_bidding_links()
+ log.info('-------- program started --------')
+ log.debug('parsed arguments: %s', arguments)
+
+ try:
+ bidding_parser = JFRBidding(
+ bws_file=arguments.bws_file,
+ file_prefix=arguments.path,
+ )
+ bidding_parser.write_bidding_tables()
+ bidding_parser.write_bidding_scripts()
+ bidding_parser.write_bidding_links()
+ except Exception as e:
+ log.getLogger('root').error(e.strerror)
+
+ log.info('--------- program ended ---------')