From 28218c054785e3cb77b6e36182bce97c8010319e Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 11:53:23 +0200 Subject: Refactoring template "engine" to provide a uniform interface --- jfr_playoff/generator.py | 90 +++++++----- jfr_playoff/template.py | 364 ++++++++++++++++++++++++----------------------- 2 files changed, 241 insertions(+), 213 deletions(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index 2b5aaf0..e5d29a8 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -1,6 +1,6 @@ from datetime import datetime -import jfr_playoff.template as p_temp +from jfr_playoff.template import Template as p_temp from jfr_playoff.data import PlayoffData @@ -21,67 +21,82 @@ class PlayoffGenerator(object): self.data.generate_phases(), self.data.fill_match_info()) leaderboard_table = self.get_leaderboard_table() - return p_temp.PAGE % ( - p_temp.PAGE_HEAD % ( - p_temp.PAGE_HEAD_REFRESH % ( - self.page['refresh']) + return p_temp.get( + 'PAGE', + p_temp.get( + 'PAGE_HEAD', + p_temp.get( + 'PAGE_HEAD_REFRESH', + self.page['refresh']) \ if self.page['refresh'] > 0 else '', self.page['title']), - p_temp.PAGE_BODY % ( + p_temp.get( + 'PAGE_BODY', self.page['logoh'], match_grid, self.get_swiss_links(), leaderboard_table, self.get_leaderboard_caption_table() if leaderboard_table else '', - p_temp.PAGE_BODY_FOOTER.decode('utf8') % ( + p_temp.get( + 'PAGE_BODY_FOOTER', datetime.now().strftime('%Y-%m-%d o %H:%M:%S')))) def get_match_table(self, match): rows = '' for team in match.teams: - score_html = p_temp.MATCH_SCORE % (team.score) + score_html = p_temp.get('MATCH_SCORE', team.score) team_label = ' / '.join([ self.data.get_shortname(name) for name in team.name.split('
')]) - team_html = p_temp.MATCH_TEAM_LINK % ( - match.link, team.name, team_label) if match.link is not None \ - else p_temp.MATCH_TEAM_NON_LINK % ( - team.name, team_label) - rows += p_temp.MATCH_TEAM_ROW % ( + team_html = p_temp.get( + 'MATCH_TEAM_LINK', + match.link, team.name, team_label) \ + if match.link is not None \ + else p_temp.get( + 'MATCH_TEAM_NON_LINK', + team.name, team_label) + rows += p_temp.get( + 'MATCH_TEAM_ROW', ' '.join([ 'winner' if team.name == match.winner else '', 'loser' if team.name == match.loser else '' ]).strip(), team_html, - p_temp.MATCH_LINK % (match.link, score_html) if match.link is not None else score_html) - html = p_temp.MATCH_TABLE.decode('utf8') % ( + p_temp.get( + 'MATCH_LINK', + match.link, score_html) \ + if match.link is not None else score_html) + html = p_temp.get( + 'MATCH_TABLE', int(self.page['width'] * 0.75), int(self.page['width'] * 0.25), rows) if match.running > 0: - running_html = p_temp.MATCH_RUNNING % (match.running) - html += p_temp.MATCH_LINK % (match.link, running_html) if match.link is not None else running_html + running_html = p_temp.get('MATCH_RUNNING', match.running) + html += p_temp.get('MATCH_LINK', match.link, running_html) if match.link is not None else running_html return html def get_phase_header(self, phase, position): - if phase.running: - grid_header = p_temp.MATCH_GRID_PHASE_RUNNING - else: - grid_header = p_temp.MATCH_GRID_PHASE - grid_header = grid_header % (phase.title) + grid_header = p_temp.get( + 'MATCH_GRID_PHASE_RUNNING' if phase.running \ + else 'MATCH_GRID_PHASE', + phase.title) if phase.link is not None: - return p_temp.MATCH_GRID_PHASE_LINK % ( + return p_temp.get( + 'MATCH_GRID_PHASE_LINK', phase.link, self.page['width'], position, grid_header) else: - return p_temp.MATCH_GRID_PHASE_NON_LINK % ( + return p_temp.get( + 'MATCH_GRID_PHASE_NON_LINK', self.page['width'], position, grid_header) def get_match_box(self, match, position): if match is not None: - return p_temp.MATCH_BOX % ( + return p_temp.get( + 'MATCH_BOX', position[0], position[1], match.id, ' '.join([ @@ -116,7 +131,8 @@ class PlayoffGenerator(object): (grid_x, grid_y)) row_no += 1 col_no += 1 - return p_temp.MATCH_GRID % ( + return p_temp.get( + 'MATCH_GRID', canvas_size[0], canvas_size[1], canvas_size[0], canvas_size[1], ' '.join(['data-%s="%s"' % ( @@ -136,9 +152,10 @@ class PlayoffGenerator(object): rows = '' for style in self.leaderboard_classes: if 'caption' in style: - rows += p_temp.LEADERBOARD_CAPTION_TABLE_ROW % ( + rows += p_temp.get( + 'LEADERBOARD_CAPTION_TABLE_ROW', style['class'], style['caption']) - return p_temp.LEADERBOARD_CAPTION_TABLE % (rows) if rows else '' + return p_temp.get('LEADERBOARD_CAPTION_TABLE', rows) if rows else '' def get_leaderboard_table(self): leaderboard = self.data.fill_leaderboard() @@ -147,24 +164,27 @@ class PlayoffGenerator(object): position = 1 rows = '' for team in leaderboard: - rows += p_temp.LEADERBOARD_ROW % ( + rows += p_temp.get( + 'LEADERBOARD_ROW', self.get_leaderboard_row_class(position), position, self.get_flag(team), team or '') position += 1 - html = p_temp.LEADERBOARD.decode('utf8') % (rows) + html = p_temp.get('LEADERBOARD', rows) return html def get_swiss_links(self): info = [] for event in self.data.get_swiss_info(): - event_label = p_temp.SWISS_DEFAULT_LABEL % (event['position']) + event_label = p_temp.get('SWISS_DEFAULT_LABEL', event['position']) if 'label' in event and event['label'] is not None: event_label = event['label'] - info.append((p_temp.SWISS_LINK if event['finished'] else p_temp.SWISS_RUNNING_LINK) % ( - event['link'], event_label - )) + info.append((p_temp.get('SWISS_LINK') \ + if event['finished'] \ + else p_temp.get( + 'SWISS_RUNNING_LINK', + event['link'], event_label))) return '\n'.join(info) def get_flag(self, team): flag = self.data.get_team_image(team) - return '' if flag is None else p_temp.LEADERBOARD_ROW_FLAG % (flag) + return '' if flag is None else p_temp.get('LEADERBOARD_ROW_FLAG', flag) diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index b99c7c7..0699c1e 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -1,180 +1,188 @@ # -*- coding: utf-8 -*- -MATCH_TABLE = ''' - - - - - -%s -
  wynik 
-''' - -MATCH_LINK = ''' - -%s - -''' - -MATCH_SCORE = ''' - %.1f  -''' - -MATCH_TEAM_LINK = ''' -%s -''' - -MATCH_TEAM_NON_LINK = ''' -%s -''' - -MATCH_TEAM_ROW = ''' - - %s  - -%s - - -''' - -MATCH_RUNNING = ''' - -%d - -''' - -MATCH_GRID = ''' -
- -%s - -
-''' - -MATCH_GRID_PHASE_LINK = ''' - -%s - -''' - -MATCH_GRID_PHASE_NON_LINK = ''' - -

%s

-
-''' - -MATCH_GRID_PHASE = ''' -%s -''' - -MATCH_GRID_PHASE_RUNNING = ''' - -%s - -''' - -MATCH_BOX = ''' -
-%s -
-''' - -LEADERBOARD = ''' - - - - - - - - - - - -%s -
 KLASYFIKACJA KOŃCOWA 
 
 miejsce  drużyna 
-''' - -LEADERBOARD_ROW = ''' - -%d - - %s  %s  - - -''' - -LEADERBOARD_ROW_FLAG = ''' - -''' - -LEADERBOARD_CAPTION_TABLE = ''' - - - -%s -
 
 LEGENDA 
-''' - -LEADERBOARD_CAPTION_TABLE_ROW = ''' - - - %s  - - -''' - -PAGE_HEAD = ''' - - - - - - -%s -%s - - -''' - -PAGE_HEAD_REFRESH = ''' - -''' - -PAGE_BODY = ''' - -%s -%s -

-%s -

-%s -%s -%s -''' - -PAGE_BODY_FOOTER = ''' -

 Admin ©Jan Romański'2005, PlayOff ©Michał Klichowicz'2017-2018, strona wygenerowana %s

-''' - -PAGE = ''' - - - -%s - - -%s - - -''' - -SWISS_LINK = ''' -[ %s ]

-''' - -SWISS_RUNNING_LINK = ''' -[  %s  ]

-''' - -SWISS_DEFAULT_LABEL = 'Turniej o %d. miejsce' +class TemplateStrings(object): + + MATCH_TABLE = ''' + + + + + + %s +
  wynik 
+ ''' + + MATCH_LINK = ''' + + %s + + ''' + + MATCH_SCORE = ''' +  %.1f  + ''' + + MATCH_TEAM_LINK = ''' + %s + ''' + + MATCH_TEAM_NON_LINK = ''' + %s + ''' + + MATCH_TEAM_ROW = ''' + +  %s  + + %s + + + ''' + + MATCH_RUNNING = ''' + + %d + + ''' + + MATCH_GRID = ''' +
+ + %s + +
+ ''' + + MATCH_GRID_PHASE_LINK = ''' + + %s + + ''' + + MATCH_GRID_PHASE_NON_LINK = ''' + +

%s

+
+ ''' + + MATCH_GRID_PHASE = ''' + %s + ''' + + MATCH_GRID_PHASE_RUNNING = ''' + + %s + + ''' + + MATCH_BOX = ''' +
+ %s +
+ ''' + + LEADERBOARD = ''' + + + + + + + + + + + + %s +
 KLASYFIKACJA KOŃCOWA 
 
 miejsce  drużyna 
+ ''' + + LEADERBOARD_ROW = ''' + + %d + +  %s  %s  + + + ''' + + LEADERBOARD_ROW_FLAG = ''' + + ''' + + LEADERBOARD_CAPTION_TABLE = ''' + + + + %s +
 
 LEGENDA 
+ ''' + + LEADERBOARD_CAPTION_TABLE_ROW = ''' + + +  %s  + + + ''' + + PAGE_HEAD = ''' + + + + + + + %s + %s + + + ''' + + PAGE_HEAD_REFRESH = ''' + + ''' + + PAGE_BODY = ''' + + %s + %s +

+ %s +

+ %s + %s + %s + ''' + + PAGE_BODY_FOOTER = ''' +

 Admin ©Jan Romański'2005, PlayOff ©Michał Klichowicz'2017-2018, strona wygenerowana %s

+ ''' + + PAGE = ''' + + + + %s + + + %s + + + ''' + + SWISS_LINK = ''' + [ %s ]

+ ''' + + SWISS_RUNNING_LINK = ''' + [  %s  ]

+ ''' + + SWISS_DEFAULT_LABEL = 'Turniej o %d. miejsce' + +class Template(object): + + @staticmethod + def get(string, *params): + return getattr(TemplateStrings, string).decode('utf8') % params -- cgit v1.2.3 From 0bb79de94e9bf78deb76dadb4a02133942665c6b Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:01:32 +0200 Subject: Class names specified --- jfr_playoff/generator.py | 2 +- jfr_playoff/template.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index e5d29a8..ce3a6f8 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -1,6 +1,6 @@ from datetime import datetime -from jfr_playoff.template import Template as p_temp +from jfr_playoff.template import PlayoffTemplate as p_temp from jfr_playoff.data import PlayoffData diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index 0699c1e..64bc0e7 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -class TemplateStrings(object): +class PlayoffTemplateStrings(object): MATCH_TABLE = ''' @@ -181,8 +181,8 @@ class TemplateStrings(object): SWISS_DEFAULT_LABEL = 'Turniej o %d. miejsce' -class Template(object): +class PlayoffTemplate(object): @staticmethod def get(string, *params): - return getattr(TemplateStrings, string).decode('utf8') % params + return getattr(PlayoffTemplateStrings, string).decode('utf8') % params -- cgit v1.2.3 From 72c30e7b12a75d43b64e31094bf3f9d3a899fe65 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:07:00 +0200 Subject: Instantation of template "engine" with i18n config --- jfr_playoff/generator.py | 56 +++++++++++++++++++++++++----------------------- jfr_playoff/template.py | 6 ++++-- 2 files changed, 33 insertions(+), 29 deletions(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index ce3a6f8..34d12e7 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -1,6 +1,6 @@ from datetime import datetime -from jfr_playoff.template import PlayoffTemplate as p_temp +from jfr_playoff.template import PlayoffTemplate from jfr_playoff.data import PlayoffData @@ -14,6 +14,8 @@ class PlayoffGenerator(object): self.leaderboard_classes = {} if settings.has_section('position_styles'): self.leaderboard_classes = settings.get('position_styles') + self.p_temp = PlayoffTemplate( + settings.get('i18n') if settings.has_section('i18n') else {}) def generate_content(self): match_grid = self.get_match_grid( @@ -21,81 +23,81 @@ class PlayoffGenerator(object): self.data.generate_phases(), self.data.fill_match_info()) leaderboard_table = self.get_leaderboard_table() - return p_temp.get( + return self.p_temp.get( 'PAGE', - p_temp.get( + self.p_temp.get( 'PAGE_HEAD', - p_temp.get( + self.p_temp.get( 'PAGE_HEAD_REFRESH', self.page['refresh']) \ if self.page['refresh'] > 0 else '', self.page['title']), - p_temp.get( + self.p_temp.get( 'PAGE_BODY', self.page['logoh'], match_grid, self.get_swiss_links(), leaderboard_table, self.get_leaderboard_caption_table() if leaderboard_table else '', - p_temp.get( + self.p_temp.get( 'PAGE_BODY_FOOTER', datetime.now().strftime('%Y-%m-%d o %H:%M:%S')))) def get_match_table(self, match): rows = '' for team in match.teams: - score_html = p_temp.get('MATCH_SCORE', team.score) + score_html = self.p_temp.get('MATCH_SCORE', team.score) team_label = ' / '.join([ self.data.get_shortname(name) for name in team.name.split('
')]) - team_html = p_temp.get( + team_html = self.p_temp.get( 'MATCH_TEAM_LINK', match.link, team.name, team_label) \ if match.link is not None \ - else p_temp.get( + else self.p_temp.get( 'MATCH_TEAM_NON_LINK', team.name, team_label) - rows += p_temp.get( + rows += self.p_temp.get( 'MATCH_TEAM_ROW', ' '.join([ 'winner' if team.name == match.winner else '', 'loser' if team.name == match.loser else '' ]).strip(), team_html, - p_temp.get( + self.p_temp.get( 'MATCH_LINK', match.link, score_html) \ if match.link is not None else score_html) - html = p_temp.get( + html = self.p_temp.get( 'MATCH_TABLE', int(self.page['width'] * 0.75), int(self.page['width'] * 0.25), rows) if match.running > 0: - running_html = p_temp.get('MATCH_RUNNING', match.running) - html += p_temp.get('MATCH_LINK', match.link, running_html) if match.link is not None else running_html + running_html = self.p_temp.get('MATCH_RUNNING', match.running) + html += self.p_temp.get('MATCH_LINK', match.link, running_html) if match.link is not None else running_html return html def get_phase_header(self, phase, position): - grid_header = p_temp.get( + grid_header = self.p_temp.get( 'MATCH_GRID_PHASE_RUNNING' if phase.running \ else 'MATCH_GRID_PHASE', phase.title) if phase.link is not None: - return p_temp.get( + return self.p_temp.get( 'MATCH_GRID_PHASE_LINK', phase.link, self.page['width'], position, grid_header) else: - return p_temp.get( + return self.p_temp.get( 'MATCH_GRID_PHASE_NON_LINK', self.page['width'], position, grid_header) def get_match_box(self, match, position): if match is not None: - return p_temp.get( + return self.p_temp.get( 'MATCH_BOX', position[0], position[1], match.id, @@ -131,7 +133,7 @@ class PlayoffGenerator(object): (grid_x, grid_y)) row_no += 1 col_no += 1 - return p_temp.get( + return self.p_temp.get( 'MATCH_GRID', canvas_size[0], canvas_size[1], canvas_size[0], canvas_size[1], @@ -152,10 +154,10 @@ class PlayoffGenerator(object): rows = '' for style in self.leaderboard_classes: if 'caption' in style: - rows += p_temp.get( + rows += self.p_temp.get( 'LEADERBOARD_CAPTION_TABLE_ROW', style['class'], style['caption']) - return p_temp.get('LEADERBOARD_CAPTION_TABLE', rows) if rows else '' + return self.p_temp.get('LEADERBOARD_CAPTION_TABLE', rows) if rows else '' def get_leaderboard_table(self): leaderboard = self.data.fill_leaderboard() @@ -164,27 +166,27 @@ class PlayoffGenerator(object): position = 1 rows = '' for team in leaderboard: - rows += p_temp.get( + rows += self.p_temp.get( 'LEADERBOARD_ROW', self.get_leaderboard_row_class(position), position, self.get_flag(team), team or '') position += 1 - html = p_temp.get('LEADERBOARD', rows) + html = self.p_temp.get('LEADERBOARD', rows) return html def get_swiss_links(self): info = [] for event in self.data.get_swiss_info(): - event_label = p_temp.get('SWISS_DEFAULT_LABEL', event['position']) + event_label = self.p_temp.get('SWISS_DEFAULT_LABEL', event['position']) if 'label' in event and event['label'] is not None: event_label = event['label'] - info.append((p_temp.get('SWISS_LINK') \ + info.append((self.p_temp.get('SWISS_LINK') \ if event['finished'] \ - else p_temp.get( + else self.p_temp.get( 'SWISS_RUNNING_LINK', event['link'], event_label))) return '\n'.join(info) def get_flag(self, team): flag = self.data.get_team_image(team) - return '' if flag is None else p_temp.get('LEADERBOARD_ROW_FLAG', flag) + return '' if flag is None else self.p_temp.get('LEADERBOARD_ROW_FLAG', flag) diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index 64bc0e7..5fd0c45 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -183,6 +183,8 @@ class PlayoffTemplateStrings(object): class PlayoffTemplate(object): - @staticmethod - def get(string, *params): + def __init__(self, settings): + self.settings = settings + + def get(self, string, *params): return getattr(PlayoffTemplateStrings, string).decode('utf8') % params -- cgit v1.2.3 From 24a151f45b5104471a1a07e2a5aa72066cba3be9 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:44:35 +0200 Subject: Internationalization and localization of strings --- jfr_playoff/i18n.py | 31 +++++++++++++++++++++++++++++++ jfr_playoff/template.py | 21 ++++++++++++--------- 2 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 jfr_playoff/i18n.py (limited to 'jfr_playoff') diff --git a/jfr_playoff/i18n.py b/jfr_playoff/i18n.py new file mode 100644 index 0000000..0caa107 --- /dev/null +++ b/jfr_playoff/i18n.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- + +import re + +PLAYOFF_I18N_DEFAULTS = { + 'SCORE': 'wynik', + 'FINAL_STANDINGS': 'klasyfikacja końcowa', + 'STANDINGS_PLACE': 'miejsce', + 'STANDINGS_TEAM': 'drużyna', + 'STANDINGS_CAPTIONS': 'legenda', + 'FOOTER_GENERATED': 'strona wygenerowana', + 'SWISS_DEFAULT_LABEL': 'Turniej o %d. miejsce' +} + +class PlayoffI18N(object): + + def __init__(self, settings): + self.settings = settings + self.string_match = re.compile(r'{{(.*)}}') + + def localize(self, string): + return re.sub( + self.string_match, + lambda x: self.__get_translation(x.group(1)), + string) + + def __get_translation(self, string): + for dictionary in [self.settings, PLAYOFF_I18N_DEFAULTS]: + if string in dictionary: + return dictionary[string].decode('utf8') + return string diff --git a/jfr_playoff/template.py b/jfr_playoff/template.py index 5fd0c45..70c179b 100644 --- a/jfr_playoff/template.py +++ b/jfr_playoff/template.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- +from jfr_playoff.i18n import PlayoffI18N + class PlayoffTemplateStrings(object): MATCH_TABLE = '''
- + %s
  wynik  {{SCORE}} 
@@ -84,14 +86,14 @@ class PlayoffTemplateStrings(object): LEADERBOARD = ''' - + - - + + %s
 KLASYFIKACJA KOŃCOWA  {{FINAL_STANDINGS}} 
 
 miejsce  drużyna  {{STANDINGS_PLACE}}  {{STANDINGS_TEAM}} 
@@ -113,7 +115,7 @@ class PlayoffTemplateStrings(object): LEADERBOARD_CAPTION_TABLE = ''' - + %s
 
 LEGENDA 
 {{STANDINGS_CAPTIONS}} 
''' @@ -156,7 +158,7 @@ class PlayoffTemplateStrings(object): ''' PAGE_BODY_FOOTER = ''' -

 Admin ©Jan Romański'2005, PlayOff ©Michał Klichowicz'2017-2018, strona wygenerowana %s

+

 Admin ©Jan Romański'2005, PlayOff ©Michał Klichowicz'2017-2018, {{FOOTER_GENERATED}} %s

''' PAGE = ''' @@ -179,12 +181,13 @@ class PlayoffTemplateStrings(object): [  %s  ]

''' - SWISS_DEFAULT_LABEL = 'Turniej o %d. miejsce' + SWISS_DEFAULT_LABEL = '{{SWISS_DEFAULT_LABEL}}' class PlayoffTemplate(object): def __init__(self, settings): - self.settings = settings + self.i18n = PlayoffI18N(settings) def get(self, string, *params): - return getattr(PlayoffTemplateStrings, string).decode('utf8') % params + return self.i18n.localize( + getattr(PlayoffTemplateStrings, string).decode('utf8')) % params -- cgit v1.2.3 From eaa2912f4dc571cb92fb68a6b0f53cc5fbff2670 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:55:11 +0200 Subject: If i18n string is not found, pass-thru the original braces --- jfr_playoff/i18n.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'jfr_playoff') diff --git a/jfr_playoff/i18n.py b/jfr_playoff/i18n.py index 0caa107..e22a563 100644 --- a/jfr_playoff/i18n.py +++ b/jfr_playoff/i18n.py @@ -28,4 +28,4 @@ class PlayoffI18N(object): for dictionary in [self.settings, PLAYOFF_I18N_DEFAULTS]: if string in dictionary: return dictionary[string].decode('utf8') - return string + return '{{%s}}' % (string) -- cgit v1.2.3