From: Ant Zucaro Date: Sat, 5 Nov 2016 23:38:24 +0000 (-0400) Subject: Use mixins to avoid duplication. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=2beb927cf6675c89e885c57759238eaefc05fbb0;p=xonotic%2Fxonstat.git Use mixins to avoid duplication. --- diff --git a/xonstat/models/game.py b/xonstat/models/game.py index d221cec..18cf9e8 100644 --- a/xonstat/models/game.py +++ b/xonstat/models/game.py @@ -2,18 +2,17 @@ Models related to games. """ -from calendar import timegm +from xonstat.models.mixins import FuzzyDateMixin, EpochMixin +from xonstat.util import strip_colors, html_colors -from xonstat.util import pretty_date, strip_colors, html_colors - -class Game(object): +class Game(FuzzyDateMixin, EpochMixin): """ An individual game. """ - def __init__(self, game_id=None, start_dt=None, game_type_cd=None, - server_id=None, map_id=None, winner=None): + def __init__(self, game_id=None, start_dt=None, game_type_cd=None, server_id=None, map_id=None, + winner=None): self.game_id = game_id self.start_dt = start_dt self.game_type_cd = game_type_cd @@ -22,7 +21,7 @@ class Game(object): self.winner = winner def __repr__(self): - return ("".format(self)) + return "".format(self) def to_dict(self): return { @@ -32,12 +31,6 @@ class Game(object): 'server_id': self.server_id } - def fuzzy_date(self): - return pretty_date(self.start_dt) - - def epoch(self): - return timegm(self.start_dt.timetuple()) - class PlayerGameStat(object): """ diff --git a/xonstat/models/map.py b/xonstat/models/map.py index befa346..5549559 100644 --- a/xonstat/models/map.py +++ b/xonstat/models/map.py @@ -4,10 +4,11 @@ Models related to maps. from calendar import timegm +from xonstat.models.mixins import FuzzyDateMixin, EpochMixin from xonstat.util import pretty_date, strip_colors, html_colors -class Map(object): +class Map(FuzzyDateMixin, EpochMixin): """ A playable map. Roughly equivalent to a pk3 file, but distinguished by name instead. """ @@ -25,12 +26,6 @@ class Map(object): 'version': self.version, } - def fuzzy_date(self): - return pretty_date(self.create_dt) - - def epoch(self): - return timegm(self.create_dt.timetuple()) - # TODO: investigate if this model is truly a model, or really just a query (i.e. namedtuple) class MapCapTime(object): diff --git a/xonstat/models/mixins.py b/xonstat/models/mixins.py new file mode 100644 index 0000000..7274421 --- /dev/null +++ b/xonstat/models/mixins.py @@ -0,0 +1,29 @@ +""" +Mixins for methods used by several model classes. +""" +from calendar import timegm +from xonstat.util import pretty_date, html_colors + + +class FuzzyDateMixin(object): + """Provides a class with a "create_dt" attribute the ability to return a fuzzy date.""" + + def fuzzy_date(self): + return pretty_date(self.create_dt) + + +class EpochMixin(object): + """Provides a class with a "create_dt" attribute the ability to return the epoch time.""" + + def epoch(self): + return timegm(self.create_dt.timetuple()) + + +class NickColorsMixin(object): + """Provides a class with a "nick" attribute the ability to return the nick's HTML colors.""" + + def nick_html_colors(self, limit=None): + if self.nick is None: + return "Anonymous Player" + else: + return html_colors(self.nick, limit) diff --git a/xonstat/models/player.py b/xonstat/models/player.py index 564c18d..2564dd3 100644 --- a/xonstat/models/player.py +++ b/xonstat/models/player.py @@ -4,26 +4,22 @@ Models related to players. from calendar import timegm -from xonstat.util import html_colors, strip_colors, pretty_date, qfont_decode +from xonstat.models.mixins import FuzzyDateMixin, EpochMixin, NickColorsMixin +from xonstat.util import strip_colors, pretty_date, qfont_decode -class Player(object): +class Player(EpochMixin, NickColorsMixin): """ A player, which can represent either a human or a bot. """ - def nick_html_colors(self, limit=None): - if self.nick is None: - return "Anonymous Player" - else: - return html_colors(self.nick, limit) - def nick_strip_colors(self): if self.nick is None: return "Anonymous Player" else: return strip_colors(self.nick) + # TODO: use FuzzyDateMixin instead, but change the method calls def joined_pretty_date(self): return pretty_date(self.create_dt) @@ -40,9 +36,6 @@ class Player(object): 'stripped_nick': qfont_decode(self.stripped_nick), } - def epoch(self): - return timegm(self.create_dt.timetuple()) - class Achievement(object): """ @@ -134,17 +127,11 @@ class PlayerElo(object): } -class PlayerRank(object): +class PlayerRank(NickColorsMixin): """ A player's rank for a given game type. """ - def nick_html_colors(self, limit=None): - if self.nick is None: - return "Anonymous Player" - else: - return html_colors(self.nick, limit) - def __repr__(self): return ("" .format(self)) @@ -157,7 +144,7 @@ class PlayerRank(object): } -class PlayerCaptime(object): +class PlayerCaptime(FuzzyDateMixin, EpochMixin): """ A flag capture time for a player on a given map. """ @@ -172,12 +159,6 @@ class PlayerCaptime(object): def __repr__(self): return "".format(self) - def fuzzy_date(self): - return pretty_date(self.create_dt) - - def epoch(self): - return timegm(self.create_dt.timetuple()) - class PlayerGroups(object): """ diff --git a/xonstat/models/server.py b/xonstat/models/server.py index 20f8c11..40c4a0b 100644 --- a/xonstat/models/server.py +++ b/xonstat/models/server.py @@ -2,13 +2,12 @@ Models related to servers. """ -from calendar import timegm from datetime import datetime as dt -from xonstat.util import pretty_date +from xonstat.models.mixins import FuzzyDateMixin, EpochMixin -class Server(object): +class Server(FuzzyDateMixin, EpochMixin): """ A Xonotic server, identifiable by name and (when there's a conflict) hashkey. """ @@ -29,9 +28,3 @@ class Server(object): 'ip_addr': self.ip_addr, 'location': self.location, } - - def fuzzy_date(self): - return pretty_date(self.create_dt) - - def epoch(self): - return timegm(self.create_dt.timetuple()) \ No newline at end of file