From 8e470272fea69a26c7d860c20433c9a0007f0b2e Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 Aug 2017 02:27:54 +0200 Subject: DTOs moved to separate submodule --- jfr_playoff/dto.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 jfr_playoff/dto.py (limited to 'jfr_playoff/dto.py') diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py new file mode 100644 index 0000000..d34b01d --- /dev/null +++ b/jfr_playoff/dto.py @@ -0,0 +1,14 @@ +class Team(object): + name = '' + score = 0.0 + +class Match(object): + teams = None + running = 0 + link = None + winner = None + loser = None + winner_matches = None + loser_matches = None + +__all__ = ['Team', 'Match'] -- cgit v1.2.3 From 904f476b1cb17f83a369e1b03440b00809535fc1 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 Aug 2017 02:35:02 +0200 Subject: Playoff phase is now also stored as DTO, arbitrary setting reads factored out of content generator --- jfr_playoff/data.py | 9 ++++++--- jfr_playoff/dto.py | 7 ++++++- jfr_playoff/generator.py | 14 +++++++------- 3 files changed, 19 insertions(+), 11 deletions(-) (limited to 'jfr_playoff/dto.py') diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index 6c3f0ed..2a965d1 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -19,15 +19,18 @@ class PlayoffData(object): phase_count = len(phase['matches']) if 'dummies' in phase: phase_count += len(phase['dummies']) - phase_grid = [None] * phase_count + phase_object = Phase() + phase_object.title = phase['title'] + phase_object.link = phase['link'] + phase_object.matches = [None] * phase_count phase_pos = 0 for match in phase['matches']: if 'dummies' in phase: while phase_pos in phase['dummies']: phase_pos += 1 - phase_grid[phase_pos] = match['id'] + phase_object.matches[phase_pos] = match['id'] phase_pos += 1 - grid.append(phase_grid) + grid.append(phase_object) return grid def fill_match_info(self): diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py index d34b01d..9a72a12 100644 --- a/jfr_playoff/dto.py +++ b/jfr_playoff/dto.py @@ -11,4 +11,9 @@ class Match(object): winner_matches = None loser_matches = None -__all__ = ['Team', 'Match'] +class Phase(object): + title = None + link = None + matches = [] + +__all__ = ['Team', 'Match', 'Phase'] diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index 80405cd..81ca6c9 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -55,21 +55,21 @@ class PlayoffGenerator(object): ) - self.page['margin'] grid_boxes = '' col_no = 0 - for column in grid: + for phase in grid: grid_x = col_no * (self.page['width'] + self.page['margin']) grid_header = p_temp.MATCH_GRID_PHASE_RUNNING if len([ - match for match in column if match is not None and matches[match].running > 0 + match for match in phase.matches if match is not None and matches[match].running > 0 ]) > 0 else p_temp.MATCH_GRID_PHASE grid_boxes += grid_header % ( - self.settings.get('phases', col_no, 'link'), + phase.link, self.page['width'], grid_x, - self.settings.get('phases', col_no, 'title') + phase.title ) row_no = 0 - column_height = grid_height / len(column) - for match in column: - grid_y = int(row_no * column_height + 0.5 * (column_height - self.page['height'])) + match_height = grid_height / len(phase.matches) + for match in phase.matches: + grid_y = int(row_no * match_height + 0.5 * (match_height - self.page['height'])) if match is not None: grid_boxes += p_temp.MATCH_BOX % ( grid_x, grid_y, -- cgit v1.2.3 From ca21319b59b3188582b70a78965729b9131dd409 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 Aug 2017 13:41:27 +0200 Subject: Additional properties in DTOs --- jfr_playoff/data.py | 12 +++++++++--- jfr_playoff/dto.py | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'jfr_playoff/dto.py') diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index 3952c64..997b038 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -10,11 +10,12 @@ class PlayoffData(object): self.database = PlayoffDB(self.settings.get('database')) self.phases = self.settings.get('phases') self.teams = self.settings.get('teams') + self.grid = [] self.match_info = {} self.leaderboard = [] def generate_phases(self): - grid = [] + self.grid = [] for phase in self.phases: phase_count = len(phase['matches']) if 'dummies' in phase: @@ -30,14 +31,18 @@ class PlayoffData(object): phase_pos += 1 phase_object.matches[phase_pos] = match['id'] phase_pos += 1 - grid.append(phase_object) - return grid + self.grid.append(phase_object) + return self.grid def fill_match_info(self): self.match_info = {} for phase in self.phases: for match in phase['matches']: self.match_info[match['id']] = self.get_match_info(match) + if self.match_info[match['id']].running > 0: + for phase_obj in self.grid: + if match['id'] in phase_obj.matches: + phase_obj.running = True if self.match_info[match['id']].link is None: self.match_info[match['id']].link = phase['link'] else: @@ -107,6 +112,7 @@ class PlayoffData(object): def get_match_info(self, match): info = Match() + info.id = match['id'] info.winner_matches = [] info.loser_matches = [] for i in range(0, 2): diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py index 9a72a12..bf3e16a 100644 --- a/jfr_playoff/dto.py +++ b/jfr_playoff/dto.py @@ -3,6 +3,7 @@ class Team(object): score = 0.0 class Match(object): + id = None teams = None running = 0 link = None @@ -15,5 +16,6 @@ class Phase(object): title = None link = None matches = [] + running = False __all__ = ['Team', 'Match', 'Phase'] -- cgit v1.2.3 From ca6c87f457f49cf33f357984d7642b1c83a0eefb Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 Aug 2017 14:06:25 +0200 Subject: Code formatting --- jfr_playoff/data.py | 38 ++++++++++++++++---------------------- jfr_playoff/db.py | 3 +-- jfr_playoff/dto.py | 4 +++- jfr_playoff/filemanager.py | 11 ++++++----- jfr_playoff/generator.py | 36 +++++++++++++----------------------- jfr_playoff/settings.py | 1 + jfr_playoff/sql.py | 6 ++++-- jfr_playoff/template.py | 2 +- 8 files changed, 45 insertions(+), 56 deletions(-) (limited to 'jfr_playoff/dto.py') diff --git a/jfr_playoff/data.py b/jfr_playoff/data.py index db65393..6fc3c4b 100644 --- a/jfr_playoff/data.py +++ b/jfr_playoff/data.py @@ -49,8 +49,7 @@ class PlayoffData(object): self.match_info[match['id']].link = phase['link'] else: self.match_info[match['id']].link = urljoin( - phase['link'], self.match_info[match['id']].link - ) + phase['link'], self.match_info[match['id']].link) return self.match_info def get_match_link(self, match): @@ -67,8 +66,7 @@ class PlayoffData(object): teams = [Team(), Team()] row = self.database.fetch( match['database'], p_sql.MATCH_RESULTS, - (match['table'], match['round']) - ) + (match['table'], match['round'])) teams[0].name = row[0] teams[1].name = row[1] teams[0].score = row[3] + row[5] @@ -91,27 +89,24 @@ class PlayoffData(object): if 'winner' in match['teams'][i]: match_teams += [ self.match_info[winner_match].winner - for winner_match in match['teams'][i]['winner'] - ] + for winner_match in match['teams'][i]['winner']] if 'loser' in match['teams'][i]: match_teams += [ self.match_info[loser_match].loser - for loser_match in match['teams'][i]['loser'] - ] + for loser_match in match['teams'][i]['loser']] if 'place' in match['teams'][i]: match_teams += [ self.teams[place-1][0] - for place in match['teams'][i]['place'] - ] + for place in match['teams'][i]['place']] known_teams = [team for team in match_teams if team is not None] if len(known_teams) > 0: teams[i].name = '
'.join([ - team if team is not None else '??' for team in match_teams]) + team if team is not None + else '??' for team in match_teams]) else: teams[i].name = '' return teams - def get_match_info(self, match): info = Match() info.id = match['id'] @@ -132,14 +127,12 @@ class PlayoffData(object): try: towels = self.database.fetch( match['database'], p_sql.TOWEL_COUNT, - (match['table'], match['round']) - ) + (match['table'], match['round'])) row = [0 if r is None else r for r in self.database.fetch( match['database'], p_sql.BOARD_COUNT, - (match['table'], match['round']) - )] + (match['table'], match['round']))] if row[1] > 0: info.running = int(row[1]) if row[1] >= row[0] - towels[0]: @@ -170,15 +163,18 @@ class PlayoffData(object): winner_key = tuple(match['winner']) if winner_key not in leaderboard_teams: leaderboard_teams[winner_key] = [] - leaderboard_teams[winner_key].append(self.match_info[match['id']].winner) + leaderboard_teams[winner_key].append( + self.match_info[match['id']].winner) if 'loser' in match: loser_key = tuple(match['loser']) if loser_key not in leaderboard_teams: leaderboard_teams[loser_key] = [] - leaderboard_teams[loser_key].append(self.match_info[match['id']].loser) + leaderboard_teams[loser_key].append( + self.match_info[match['id']].loser) for positions, position_teams in leaderboard_teams.iteritems(): positions = list(positions) - if len(positions) == len([team for team in position_teams if team is not None]): + if len(positions) == len([ + team for team in position_teams if team is not None]): for table_team in self.teams: if table_team[0] in position_teams: position = positions.pop(0) @@ -192,9 +188,7 @@ class PlayoffData(object): len(phase['matches']) + len(phase['dummies']) if 'dummies' in phase else len(phase['matches']) - for phase in self.phases - ]) - ) + for phase in self.phases])) def get_shortname(self, fullname): for team in self.teams: diff --git a/jfr_playoff/db.py b/jfr_playoff/db.py index 19ec2fb..b94f3d5 100644 --- a/jfr_playoff/db.py +++ b/jfr_playoff/db.py @@ -10,8 +10,7 @@ class PlayoffDB(object): user=settings['user'], password=settings['pass'], host=settings['host'], - port=settings['port'] - ) + port=settings['port']) self.db_cursor = self.database.cursor(buffered=True) def get_cursor(self): diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py index bf3e16a..f5e08ef 100644 --- a/jfr_playoff/dto.py +++ b/jfr_playoff/dto.py @@ -2,6 +2,7 @@ class Team(object): name = '' score = 0.0 + class Match(object): id = None teams = None @@ -12,10 +13,11 @@ class Match(object): winner_matches = None loser_matches = None + class Phase(object): title = None link = None matches = [] running = False -__all__ = ['Team', 'Match', 'Phase'] +__all__ = ('Team', 'Match', 'Phase') diff --git a/jfr_playoff/filemanager.py b/jfr_playoff/filemanager.py index 03dbaf7..5cd2a80 100644 --- a/jfr_playoff/filemanager.py +++ b/jfr_playoff/filemanager.py @@ -22,7 +22,6 @@ class PlayoffFileManager(object): if path.startswith(self.output_path): self.files.add(path.replace(self.output_path, '')) - def write_content(self, content): output = open(self.output_file, 'w') output.write(content.encode('utf8')) @@ -33,16 +32,18 @@ class PlayoffFileManager(object): def copy_scripts(self, script_path='sklady/playoff.js'): script_output_path = os.path.join(self.output_path, script_path) shutil.copy( - unicode(os.path.join(os.path.dirname(__main__.__file__), 'playoff.js')), - unicode(script_output_path) - ) + unicode(os.path.join( + os.path.dirname(__main__.__file__), 'playoff.js')), + unicode(script_output_path)) self.register_file(script_output_path) return script_output_path def send_files(self): if self.goniec['enabled']: try: - content_lines = [self.output_path] + list(self.files) + ['bye', ''] + content_lines = [self.output_path] + \ + list(self.files) + \ + ['bye', ''] print '\n'.join(content_lines) goniec = socket.socket() goniec.connect((self.goniec['host'], self.goniec['port'])) diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index bc2a388..5868750 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -16,23 +16,18 @@ class PlayoffGenerator(object): return p_temp.PAGE % ( p_temp.PAGE_HEAD % ( p_temp.PAGE_HEAD_REFRESH % ( - self.page['refresh'] - ) if self.page['refresh'] > 0 else '', - self.page['title'] - ), + self.page['refresh']) + if self.page['refresh'] > 0 else '', + self.page['title']), p_temp.PAGE_BODY % ( self.page['logoh'], self.get_match_grid( self.data.get_dimensions(), self.data.generate_phases(), - self.data.fill_match_info() - ), + self.data.fill_match_info()), self.get_leaderboard_table(self.data.fill_leaderboard()), p_temp.PAGE_BODY_FOOTER.decode('utf8') % ( - datetime.now().strftime('%Y-%m-%d o %H:%M') - ) - ) - ) + datetime.now().strftime('%Y-%m-%d o %H:%M')))) def get_match_table(self, match): rows = '' @@ -46,16 +41,13 @@ class PlayoffGenerator(object): team.name, ' / '.join([ self.data.get_shortname(name) for name in - team.name.split('
') - ]), + team.name.split('
')]), match.link, - team.score - ) + team.score) html = p_temp.MATCH_TABLE.decode('utf8') % ( int(self.page['width'] * 0.75), int(self.page['width'] * 0.25), - rows - ) + rows) if match.running > 0: html += p_temp.MATCH_RUNNING % (match.link, match.running) return html @@ -69,8 +61,7 @@ class PlayoffGenerator(object): phase.link, self.page['width'], position, - phase.title - ) + phase.title) def get_match_box(self, match, position): if match is not None: @@ -83,8 +74,7 @@ class PlayoffGenerator(object): ' '.join([ str(m) for m in match.loser_matches ]) if match.loser_matches is not None else '', - self.get_match_table(match) - ) + self.get_match_table(match)) return '' def get_match_grid(self, dimensions, grid, matches): @@ -94,8 +84,7 @@ class PlayoffGenerator(object): ) - self.page['margin'], dimensions[1] * ( self.page['height'] + self.page['margin'] - ) - self.page['margin'] - ) + ) - self.page['margin']) grid_boxes = '' col_no = 0 for phase in grid: @@ -126,7 +115,8 @@ class PlayoffGenerator(object): position = 1 rows = '' for team in leaderboard: - rows += p_temp.LEADERBOARD_ROW % (position, self.get_flag(team), team or '') + rows += p_temp.LEADERBOARD_ROW % ( + position, self.get_flag(team), team or '') position += 1 html = p_temp.LEADERBOARD.decode('utf8') % (rows) return html diff --git a/jfr_playoff/settings.py b/jfr_playoff/settings.py index 3b765bc..da92458 100644 --- a/jfr_playoff/settings.py +++ b/jfr_playoff/settings.py @@ -7,6 +7,7 @@ import sys def complete_filename(text, state): return (glob.glob(text+'*')+[None])[state] + class PlayoffSettings(object): def __init__(self): diff --git a/jfr_playoff/sql.py b/jfr_playoff/sql.py index 76ea728..b01bd08 100644 --- a/jfr_playoff/sql.py +++ b/jfr_playoff/sql.py @@ -1,5 +1,6 @@ MATCH_RESULTS = ''' -SELECT t1.fullname, t2.fullname, matches.carry, matches.vph, matches.vpv, matches.corrh, matches.corrv +SELECT t1.fullname, t2.fullname, matches.carry, + matches.vph, matches.vpv, matches.corrh, matches.corrv FROM #db#.matches matches JOIN #db#.teams t1 ON t1.id = #db#.matches.homet @@ -9,7 +10,8 @@ WHERE matches.tabl = %s AND matches.rnd = %s ''' BOARD_COUNT = ''' -SELECT segmentsperround*boardspersegment, SUM(sc1.contract IS NOT NULL AND sc2.contract IS NOT NULL) +SELECT segmentsperround*boardspersegment, + SUM(sc1.contract IS NOT NULL AND sc2.contract IS NOT NULL) FROM #db#.scores sc1 JOIN #db#.scores sc2 ON sc1.rnd = sc2.rnd diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index 8713ab9..99e6225 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -1,4 +1,4 @@ -#encoding=utf-8 +# -*- coding: utf-8 -*- MATCH_TABLE = ''' -- cgit v1.2.3