summaryrefslogtreecommitdiff
path: root/virtual_table.py
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2015-08-24 17:04:49 +0200
committeremkael <emkael@tlen.pl>2015-08-24 17:04:49 +0200
commit51ac67aec71423af2a7e1dd77d4f5d59dee75b68 (patch)
treea6e311e088db7496cc0916c72cc5e3386417e569 /virtual_table.py
* initial commit
Diffstat (limited to 'virtual_table.py')
-rw-r--r--virtual_table.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/virtual_table.py b/virtual_table.py
new file mode 100644
index 0000000..7af97a9
--- /dev/null
+++ b/virtual_table.py
@@ -0,0 +1,67 @@
+import sys, glob, re, math
+from os import path
+from bs4 import BeautifulSoup as bs4
+
+tournament_path_prefix = sys.argv[1] + '.html'
+
+output_path = path.dirname(tournament_path_prefix)
+tournament_prefix = path.splitext(path.realpath(tournament_path_prefix))[0]
+
+tournament_files_match = re.compile(tournament_prefix + '([0-9]{3})\.txt')
+tournament_files = [f for f in glob.glob(tournament_prefix + '*.txt') if re.search(tournament_files_match, f)]
+
+virtual_pairs = []
+
+for tournament_file in tournament_files:
+ with file(tournament_file, 'r+') as board_text:
+ board_text_content = bs4(board_text, from_encoding='iso-8859-2')
+ if not len(board_text_content.select('tr.virtualTable')):
+ rows = [row for row in board_text_content.select('tr') if len(row.select('td')) == 11]
+ virtual_count = int(math.ceil(len(rows) / 2.0))
+ header_added = False
+ for row in rows[virtual_count-1:]:
+ for cell in row.select('td')[1:3]:
+ pair_no = int(cell.contents[0])
+ virtual_pairs.append(pair_no)
+ if header_added:
+ row.extract()
+ else:
+ row.insert_before(bs4('<tr class="virtualTable"><td class="n"></td><td class="noc" colspan="10">Wirtualny stolik:</td></tr>'))
+ for cell in row.select('td')[1:3]:
+ cell.contents = ''
+ header_added = True
+ board_text.seek(0)
+ board_text.write(board_text_content.body.table.prettify('iso-8859-2'))
+ board_text.truncate()
+
+with file(tournament_prefix + 'WYN.txt', 'r+') as results_file:
+ results_text_content = bs4(results_file, from_encoding='iso-8859-2')
+ rows = results_text_content.select('tr')
+ for row in rows:
+ cells = row.select('td')
+ if len(cells) == 6:
+ try:
+ if int(cells[2].contents[0]) in virtual_pairs:
+ row.extract()
+ except ValueError:
+ pass
+ results_file.seek(0)
+ results_file.write(results_text_content.body.table.prettify('iso-8859-2'))
+ results_file.truncate()
+
+with file(tournament_prefix + 'zbior.html', 'r+') as results_file:
+ results_content = bs4(results_file)
+ rows = results_content.select('tr')
+ for row in rows:
+ cells = row.select('td')
+ if len(cells) == 7:
+ try:
+ if int(cells[1].contents[0]) in virtual_pairs:
+ row.extract()
+ except ValueError:
+ pass
+ if len(cells) == 1 and cells[0]['colspan'] == '7' and cells[0].contents[0] == '&nbsp':
+ row.extract()
+ results_file.seek(0)
+ results_file.write(results_content.prettify('utf-8'))
+ results_file.truncate()