summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-12-30 15:03:56 +0100
committeremkael <emkael@tlen.pl>2019-12-30 15:46:42 +0100
commit5e66c1684a8a2b0597fc2633078c23e212cce228 (patch)
treeaceb836619e4cde2f71cae5a71e27899f9b3a7cf
parent9e29356e95c06907ace1239b1367b134155d3167 (diff)
Auto-loading tournament result info client classes
-rw-r--r--jfr_playoff/data/info.py76
-rw-r--r--jfr_playoff/data/tournament/__init__.py10
-rw-r--r--jfr_playoff/data/tournament/jfrdb.py8
-rw-r--r--jfr_playoff/data/tournament/jfrhtml.py8
-rw-r--r--jfr_playoff/data/tournament/tcjson.py7
5 files changed, 76 insertions, 33 deletions
diff --git a/jfr_playoff/data/info.py b/jfr_playoff/data/info.py
index aec78c2..230196d 100644
--- a/jfr_playoff/data/info.py
+++ b/jfr_playoff/data/info.py
@@ -1,4 +1,7 @@
+import glob
+import inspect
import re
+from os.path import dirname, basename, isfile, join
from urlparse import urljoin
import jfr_playoff.sql as p_sql
@@ -7,12 +10,46 @@ from jfr_playoff.remote import RemoteUrl as p_remote
from jfr_playoff.logger import PlayoffLogger
+class ResultInfoClient(object):
+ def __init__(self, settings, database=None):
+ self.settings = settings
+ self.database = database
+
+ @property
+ def priority(self):
+ return 0
+
+ def is_capable(self):
+ return False
+
+ def get_exceptions(self, method):
+ pass
+
+
class ResultInfo(object):
def __init__(self, *args):
- self.clients = self.fill_client_list(*args)
-
- def fill_client_list(self, settings, database):
- return []
+ self.clients = self._fill_client_list(*args)
+
+ @property
+ def submodule_path(self):
+ raise NotImplementedError()
+
+ @property
+ def _client_classes(self):
+ module = __import__(self.submodule_path, fromlist=[''])
+ for f in glob.glob(join(dirname(module.__file__), "*.py")):
+ if isfile(f) and not f.endswith('__init__.py'):
+ submodule_name = basename(f)[:-3]
+ submodule_path = self.submodule_path + '.' + submodule_name
+ submodule = __import__(submodule_path, fromlist=[''])
+ for member in inspect.getmembers(submodule, inspect.isclass):
+ if member[1].__module__ == submodule_path:
+ yield member[1]
+
+ def _fill_client_list(self, *args):
+ all_clients = [c(*args) for c in self._client_classes]
+ clients = [c for c in all_clients if c.is_capable()]
+ return sorted(clients, key=lambda c: c.priority, reverse=True)
def call_client(self, method, default, *args):
PlayoffLogger.get('resultinfo').info(
@@ -35,34 +72,22 @@ class ResultInfo(object):
return default
-from jfr_playoff.data.tournament.jfrdb import JFRDbTournamentInfo
-from jfr_playoff.data.tournament.jfrhtml import JFRHtmlTournamentInfo
-from jfr_playoff.data.tournament.tcjson import TCJsonTournamentInfo
-
-
class TournamentInfo(ResultInfo):
def __init__(self, settings, database):
- self.settings = settings
ResultInfo.__init__(self, settings, database)
+ self.final_positions = settings.get('final_positions', [])
- def fill_client_list(self, settings, database):
- clients = []
- if (database is not None) and ('database' in settings):
- clients.append(JFRDbTournamentInfo(settings, database))
- if 'link' in settings:
- if settings['link'].endswith('leaderb.html'):
- clients.append(JFRHtmlTournamentInfo(settings))
- clients.append(TCJsonTournamentInfo(settings))
- return clients
+ @property
+ def submodule_path(self):
+ return 'jfr_playoff.data.tournament'
def get_tournament_results(self):
teams = self.call_client('get_tournament_results', [])
if self.is_finished():
- final_positions = self.settings.get('final_positions', [])
PlayoffLogger.get('tournamentinfo').info(
'setting final positions from tournament results: %s',
- final_positions)
- for position in final_positions:
+ self.final_positions)
+ for position in self.final_positions:
if len(teams) >= position:
teams[position-1] = (teams[position-1] + [None] * 4)[0:4]
teams[position-1][3] = position
@@ -80,6 +105,7 @@ class MatchInfo(ResultInfo):
matches = {}
def __init__(self, match_config, teams, database, aliases=None):
+ ResultInfo.__init__(self, match_config, database)
self.config = match_config
self.teams = teams
self.database = database
@@ -88,13 +114,13 @@ class MatchInfo(ResultInfo):
for team, team_aliases in aliases.iteritems():
for alias in team_aliases:
self.aliases[alias] = team
- ResultInfo.__init__(self, match_config, database)
self.info = Match()
self.__init_info()
self.__fetch_match_link()
- def fill_client_list(self, settings, database):
- return []
+ @property
+ def submodule_path(self):
+ return 'jfr_playoff.data.match'
def __init_info(self):
self.info.id = self.config['id']
diff --git a/jfr_playoff/data/tournament/__init__.py b/jfr_playoff/data/tournament/__init__.py
index 9ec71dd..ef16e54 100644
--- a/jfr_playoff/data/tournament/__init__.py
+++ b/jfr_playoff/data/tournament/__init__.py
@@ -1,8 +1,7 @@
-class TournamentInfoClient(object):
- def __init__(self, settings, database=None):
- self.settings = settings
- self.database = database
+from jfr_playoff.data.info import ResultInfoClient
+
+class TournamentInfoClient(ResultInfoClient):
def get_results_link(self, suffix):
pass
@@ -11,6 +10,3 @@ class TournamentInfoClient(object):
def get_tournament_results(self):
pass
-
- def get_exceptions(self, method):
- pass
diff --git a/jfr_playoff/data/tournament/jfrdb.py b/jfr_playoff/data/tournament/jfrdb.py
index 48645df..6317a79 100644
--- a/jfr_playoff/data/tournament/jfrdb.py
+++ b/jfr_playoff/data/tournament/jfrdb.py
@@ -1,5 +1,4 @@
import jfr_playoff.sql as p_sql
-
from jfr_playoff.logger import PlayoffLogger
from jfr_playoff.data.tournament import TournamentInfoClient
@@ -9,6 +8,13 @@ SWISS_TIE_WARNING = 'tie detected in swiss %s.' + \
class JFRDbTournamentInfo(TournamentInfoClient):
+ @property
+ def priority(self):
+ return 50
+
+ def is_capable(self):
+ return (self.database is not None) and ('database' in self.settings)
+
def get_exceptions(self, method):
return (IOError, TypeError, IndexError, KeyError)
diff --git a/jfr_playoff/data/tournament/jfrhtml.py b/jfr_playoff/data/tournament/jfrhtml.py
index 017ce3b..ac57d4c 100644
--- a/jfr_playoff/data/tournament/jfrhtml.py
+++ b/jfr_playoff/data/tournament/jfrhtml.py
@@ -7,6 +7,14 @@ from jfr_playoff.data.tournament import TournamentInfoClient
class JFRHtmlTournamentInfo(TournamentInfoClient):
+ @property
+ def priority(self):
+ return 30
+
+ def is_capable(self):
+ return ('link' in self.settings) \
+ and (self.settings['link'].endswith('leaderb.html'))
+
def get_exceptions(self, method):
if method == 'get_results_link':
return (KeyError, ValueError)
diff --git a/jfr_playoff/data/tournament/tcjson.py b/jfr_playoff/data/tournament/tcjson.py
index 5e375ed..8d7cd34 100644
--- a/jfr_playoff/data/tournament/tcjson.py
+++ b/jfr_playoff/data/tournament/tcjson.py
@@ -9,6 +9,13 @@ FLAG_CDN_URL = 'https://cdn.tournamentcalculator.com/flags/'
class TCJsonTournamentInfo(TournamentInfoClient):
+ @property
+ def priority(self):
+ return 20
+
+ def is_capable(self):
+ return 'link' in self.settings
+
def get_exceptions(self, method):
return (TypeError, IndexError, KeyError, IOError, ValueError)