summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2015-08-28 14:44:24 +0200
committeremkael <emkael@tlen.pl>2015-08-28 14:44:24 +0200
commitb4d8d5b814e7b240339ac15947f22f8e63696691 (patch)
tree6c10e65cc9ac307b4fa3b7d28fdef7e14d8a2a57
parent0a572efea5955ddf12ceb3c41efac394b9383a03 (diff)
* modularization
* argument parsing
-rw-r--r--.gitignore1
-rw-r--r--TODO.md1
-rw-r--r--bidding_data.py173
3 files changed, 71 insertions, 104 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0d20b64
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.pyc
diff --git a/TODO.md b/TODO.md
index c78d3f3..2dd9e19 100644
--- a/TODO.md
+++ b/TODO.md
@@ -3,4 +3,3 @@
* niedodawanie linków tam, gdzie nie ma licytacji
* czytać bezpośrednio z BWS (albo chociaż obsłużyć Excelowy format CSV)
* pobieranie nazwisk z BWS(?)
- * argparse i walidacja parametrów
diff --git a/bidding_data.py b/bidding_data.py
index 7dd1a58..3107e2f 100644
--- a/bidding_data.py
+++ b/bidding_data.py
@@ -30,12 +30,9 @@ class JFRBidding:
table_no = sitting[0] + '_' + sitting[1]
if round_no not in round_lineups:
round_lineups[round_no] = {}
- round_lineups[round_no][table_no] = sorted(
- [
- int(sitting[3]),
- int(sitting[4])
- ]
- )
+ round_lineups[round_no][table_no] = sorted([
+ int(sitting[3]),
+ int(sitting[4])])
return round_lineups
# converts CSV bidding to
@@ -60,16 +57,12 @@ class JFRBidding:
if bids[board_no][table_no][round_no][bid_counter][
'direction'] == bid[6]:
bids[board_no][table_no][round_no].pop(
- bid_counter,
- None
- )
+ bid_counter, None)
if len(bids[board_no][table_no][round_no]) == 0:
bids[board_no][table_no].pop(round_no, None)
else:
bids[board_no][table_no][round_no][bid_counter] = {
- 'direction': bid[6],
- 'bid': bid[7]
- }
+ 'direction': bid[6], 'bid': bid[7]}
return bids
# converts bidding data into HTML table
@@ -91,9 +84,7 @@ class JFRBidding:
if call_match:
bid_cell.append(call_match.group(1))
bid_icon = html_output.new_tag(
- 'img',
- src='images/' + call_match.group(2) + '.gif'
- )
+ 'img', src='images/' + call_match.group(2) + '.gif')
bid_cell.append(bid_icon)
else:
bid_cell.append(bid)
@@ -108,32 +99,25 @@ class JFRBidding:
table_no=None,
pair_numbers=None):
return '{0}_bidding_{1:03}_{2}.txt'.format(
- self.__tournament_prefix,
- board_no,
- '_'.join(
- map(str,
- self.__round_lineups[round_no][table_no]
- if pair_numbers is None # read pair numbers from lineup
- else pair_numbers) # or use provided pair numbers
- )
- )
+ self.__tournament_prefix, board_no,
+ '_'.join(map(str,
+ self.__round_lineups[round_no][table_no]
+ if pair_numbers is None # read numbers from lineup
+ else pair_numbers))) # or use provided pair numbers
def __map_board_numbers(self, custom_mapping=None):
self.__tournament_files = [
f for f
in glob.glob(self.__tournament_prefix + '*.html')
- if re.search(self.__tournament_files_match, f)
- ]
- if custom_mapping is not None:
+ if re.search(self.__tournament_files_match, f)]
+ if custom_mapping is not None and len(custom_mapping) >= 3:
custom_files = []
for jfr_number in range(custom_mapping[0], custom_mapping[1]+1):
# only include these board numbers from custom mapping
# which actually exist in JFP output
- board_files = [
- f for f
- in self.__tournament_files
- if f.endswith('{0:03}.html'.format(jfr_number))
- ]
+ board_files = [f for f
+ in self.__tournament_files
+ if f.endswith('{0:03}.html'.format(jfr_number))]
if len(board_files):
self.__board_number_mapping[
jfr_number - custom_mapping[0] + custom_mapping[2]
@@ -146,16 +130,13 @@ class JFRBidding:
# and read actual board numbers from HTML headers
file_number = re.match(
self.__tournament_files_match,
- tournament_file
- ).group(1)
+ tournament_file).group(1)
with file(tournament_file, 'r+') as board_html:
board_content = bs4(board_html, from_encoding='utf-8')
# first found <h4> element should be actual board number
board_number = re.sub(
- '[^0-9]',
- '',
- board_content.select('h4')[0].contents[0].strip()
- )
+ '[^0-9]', '',
+ board_content.select('h4')[0].contents[0].strip())
self.__board_number_mapping[
int(board_number, 10)
] = int(file_number, 10)
@@ -177,17 +158,13 @@ class JFRBidding:
def __init__(self, bidding_file, lineup_file, file_prefix, board_mapping):
self.__round_lineups = self.__parse_lineup_data(
- self.__csv_to_list(lineup_file)
- )
+ self.__csv_to_list(lineup_file))
self.__bids = self.__parse_bidding_data(
- self.__csv_to_list(bidding_file)
- )
+ self.__csv_to_list(bidding_file))
self.__tournament_prefix = path.splitext(
- path.realpath(file_prefix + '.html')
- )[0]
+ path.realpath(file_prefix + '.html'))[0]
self.__tournament_files_match = re.compile(
- re.escape(self.__tournament_prefix) + '([0-9]{3})\.html'
- )
+ re.escape(self.__tournament_prefix) + '([0-9]{3})\.html')
self.__map_board_numbers(board_mapping)
def write_bidding_tables(self):
@@ -213,21 +190,17 @@ class JFRBidding:
# (so that bidding table is a proper matrix)
for pos in range(
self.__directions.index(last_bidder),
- len(self.__directions)
- ):
+ len(self.__directions)):
bidding_table[pos].append('')
# transpose the bidding table
# aligning it row-by-row (bidding round-by-round)
bidding_table = map(list, zip(*bidding_table))
bidding_fpath = self.__get_bidding_file_output_path(
self.__board_number_mapping[board_no],
- round_no,
- table_no
- )
+ round_no, table_no)
with file(bidding_fpath, 'w') as bidding_file:
bidding_file.write(
- self.__format_bidding(bidding_table)
- )
+ self.__format_bidding(bidding_table))
def write_bidding_scripts(self):
for tournament_file in self.__tournament_files:
@@ -235,96 +208,90 @@ class JFRBidding:
board_content = bs4(board_html, from_encoding='utf-8')
header_scripts = board_content.select('head script')
# check for jQuery, append if necessary
- jquery_scripts = [
- script for script
- in header_scripts
- if script['src'] == 'javas/jquery.js'
- ]
+ jquery_scripts = [script for script in header_scripts
+ if script['src'] == 'javas/jquery.js']
if not len(jquery_scripts):
jquery = board_content.new_tag(
- 'script',
- src='javas/jquery.js',
- type='text/javascript'
- )
+ 'script', src='javas/jquery.js',
+ type='text/javascript')
jquery_scripts.append(jquery)
board_content.head.append(jquery)
# check for bidding.js
bidding_scripts = [
- script for script
- in header_scripts
- if script['src'] == 'javas/bidding.js'
- ]
+ script for script in header_scripts
+ if script['src'] == 'javas/bidding.js']
# and make sure bidding.js is appended after jQuery
for script in bidding_scripts:
script.extract()
bidding_script = board_content.new_tag(
- 'script',
- src='javas/bidding.js',
- type='text/javascript'
- )
+ 'script', src='javas/bidding.js',
+ type='text/javascript')
jquery_scripts[0].insert_after(bidding_script)
board_html.seek(0)
board_html.write(board_content.prettify(
- 'utf-8',
- formatter='html'
- ))
+ 'utf-8', formatter='html'))
board_html.truncate()
def write_bidding_links(self):
for tournament_file in self.__tournament_files:
file_number = re.match(
self.__tournament_files_match,
- tournament_file
- ).group(1)
+ tournament_file).group(1)
board_text_path = path.splitext(tournament_file)[0] + '.txt'
with file(board_text_path, 'r+') as board_text:
board_text_content = bs4(
- board_text,
- from_encoding='iso-8859-2'
- )
+ board_text, from_encoding='iso-8859-2')
for row in board_text_content.select('tr'):
cells = row.select('td')
# traveller table rows for specific score entries
# should have 11 cells
if len(cells) == 11:
try:
- pair_numbers = sorted(
- [
- int(cells[1].contents[0]),
- int(cells[2].contents[0])
- ]
- )
+ pair_numbers = sorted([
+ int(cells[1].contents[0]),
+ int(cells[2].contents[0])])
except ValueError:
continue
bidding_link = board_text_content.new_tag(
- 'a',
- href='#',
- **{'class': 'biddingLink'}
- )
+ 'a', href='#', **{'class': 'biddingLink'})
bidding_link.string = '[lic]'
bidding_link['data-bidding-link'] = path.basename(
self.__get_bidding_file_output_path(
int(file_number, 10),
- pair_numbers=pair_numbers
- )
- )
+ pair_numbers=pair_numbers))
# fourth cell is the contract
for link in cells[3].select('a.biddingLink'):
link.extract()
cells[3].append(bidding_link)
board_text.seek(0)
board_text.write(board_text_content.table.prettify(
- 'iso-8859-2',
- formatter='html'
- ))
+ 'iso-8859-2', formatter='html'))
board_text.truncate()
-bidding_parser = JFRBidding(
- bidding_file=sys.argv[1],
- lineup_file=sys.argv[2],
- file_prefix=sys.argv[3],
- board_mapping=map(int, sys.argv[4:]) if len(sys.argv) > 4 else None
-)
-bidding_parser.write_bidding_tables()
-bidding_parser.write_bidding_scripts()
-bidding_parser.write_bidding_links()
+if __name__ == '__main__':
+ import argparse
+
+ argument_parser = argparse.ArgumentParser(
+ description='Display bidding data from BWS files on JFR Pary pages')
+ argument_parser.add_argument('bidding_file', metavar='BIDDING_FILE',
+ help='CSV containing bidding data from BWS')
+ argument_parser.add_argument('lineup_file', metavar='LINEUP_FILE',
+ help='CSV containing lineup data from BWS')
+ argument_parser.add_argument('path', metavar='PATH',
+ help='tournament path with JFR prefix')
+ argument_parser.add_argument('board_mapping', metavar='BOARD_MAPPING',
+ default=[], nargs='*', type=int,
+ help='board number mapping ' +
+ '(JFR_FROM JFR_TO BWS_FROM)')
+
+ arguments = argument_parser.parse_args()
+
+ bidding_parser = JFRBidding(
+ bidding_file=arguments.bidding_file,
+ lineup_file=arguments.lineup_file,
+ file_prefix=arguments.path,
+ board_mapping=arguments.board_mapping
+ )
+ bidding_parser.write_bidding_tables()
+ bidding_parser.write_bidding_scripts()
+ bidding_parser.write_bidding_links()