Skip to content

Commit

Permalink
Moving stuff around + autodoc attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Dec 31, 2016
1 parent 72abe8d commit 8ddb212
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 33 deletions.
4 changes: 2 additions & 2 deletions challonge/attachment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .helpers import FieldHolder, get_from_dict
from .helpers import FieldHolder


class Attachment(metaclass=FieldHolder):
Expand All @@ -15,4 +15,4 @@ def __init__(self, connection, json_def):

def _refresh_from_json(self, json_def):
if 'match_attachment' in json_def:
get_from_dict(self, json_def['match_attachment'], *self._fields)
self._get_from_dict(json_def['match_attachment'])
42 changes: 22 additions & 20 deletions challonge/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@


class ChallongeException(Exception):
""" If anything goes wrong during the request to the Challonge API,
this exception will be raised
"""
""" If anything goes wrong during the request to the Challonge API, this exception will be raised """
pass


class FieldDescriptor:
def __init__(self, attr):
self.attr = attr
self.__doc__ = 'readonly attribute'

def __get__(self, instance, owner):
return getattr(instance, self.attr)
return getattr(instance, self.attr) if instance else self


class FieldHolder(type):
private_name = '_{}'

def _create_holder(self, holder_class, json_def):
return holder_class(self.connection, json_def)

Expand All @@ -31,14 +32,29 @@ def _add_holder(self, local_list, x):
local_list = []
local_list.append(x)

def _find_holder(self, local_list, e_id):
if local_list is not None:
for e in local_list:
if e.id == int(e_id):
return e
return None

def _get_from_dict(self, data):
for a in self._fields:
name = FieldHolder.private_name.format(a) if CHALLONGE_USE_FIELDS_DESCRIPTORS else a
setattr(self, name, data[a] if a in data else None)

def __init__(cls, name, bases, dct):
super(FieldHolder, cls).__init__(name, bases, dct)

cls._create_holder = FieldHolder._create_holder
cls._add_holder = FieldHolder._add_holder
cls._find_holder = FieldHolder._find_holder
cls._get_from_dict = FieldHolder._get_from_dict

if CHALLONGE_USE_FIELDS_DESCRIPTORS:
for a in cls._fields:
name = '_{}'.format(a)
setattr(cls, a, FieldDescriptor(name))
setattr(cls, a, FieldDescriptor(FieldHolder.private_name.format(a)))


class Connection:
Expand Down Expand Up @@ -89,17 +105,3 @@ def val(value):

def get_connection(username, api_key, timeout=DEFAULT_TIMEOUT, loop=None):
return Connection(username, api_key, timeout, loop)


def get_from_dict(s, data, *args):
for a in args:
name = '_{}'.format(a) if CHALLONGE_USE_FIELDS_DESCRIPTORS else a
setattr(s, name, data[a] if a in data else None)


def find_local(local_list, e_id):
if local_list is not None:
for e in local_list:
if e.id == int(e_id):
return e
return None
4 changes: 2 additions & 2 deletions challonge/match.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from .helpers import FieldHolder, get_from_dict
from .helpers import FieldHolder
from .participant import Participant
from .attachment import Attachment

Expand Down Expand Up @@ -33,7 +33,7 @@ def __init__(self, connection, json_def):

def _refresh_from_json(self, json_def):
if 'match' in json_def:
get_from_dict(self, json_def['match'], *self._fields)
self._get_from_dict(json_def['match'])

async def _report(self, scores_csv, winner=None):
if not verify_score_format(scores_csv):
Expand Down
4 changes: 2 additions & 2 deletions challonge/participant.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .helpers import FieldHolder, get_from_dict
from .helpers import FieldHolder


class Participant(metaclass=FieldHolder):
Expand All @@ -19,7 +19,7 @@ def __init__(self, connection, json_def):

def _refresh_from_json(self, json_def):
if 'participant' in json_def:
get_from_dict(self, json_def['participant'], *self._fields)
self._get_from_dict(json_def['participant'])

async def _change(self, **params):
return await self.connection('PUT',
Expand Down
6 changes: 3 additions & 3 deletions challonge/tournament.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum

from . import CHALLONGE_AUTO_GET_PARTICIPANTS, CHALLONGE_AUTO_GET_MATCHES
from .helpers import FieldHolder, get_from_dict, find_local
from .helpers import FieldHolder
from .participant import Participant
from .match import Match

Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self, connection, json_def):
def _refresh_from_json(self, json_def):
if 'tournament' in json_def:
t_data = json_def['tournament']
get_from_dict(self, t_data, *self._fields)
self._get_from_dict(t_data)
if 'participants' in t_data:
self.participants = [self._create_participant(p) for p in t_data['participants']]
if 'matches' in t_data:
Expand Down Expand Up @@ -167,7 +167,7 @@ async def get_participant(self, p_id: int, force_update=False) -> Participant:
ChallongeException
"""
found_p = find_local(self.participants, p_id)
found_p = self._find_holder(self.participants, p_id)
if force_update or found_p is None:
res = await self.connection('GET',
'tournaments/{}/participants/{}'.format(self._id, p_id))
Expand Down
13 changes: 10 additions & 3 deletions challonge/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import CHALLONGE_AUTO_GET_PARTICIPANTS, CHALLONGE_AUTO_GET_MATCHES
from .helpers import get_connection, find_local
from .helpers import get_connection
from .tournament import Tournament, TournamentType


Expand All @@ -23,6 +23,13 @@ def _add_tournament(self, t: Tournament):
def _create_tournament(self, json_def) -> Tournament:
return Tournament(self.connection, json_def)

def _find_tournament(self, e_id):
if self.tournaments is not None:
for e in self.tournaments:
if e.id == int(e_id):
return e
return None

async def validate(self):
""" checks whether the current user is connected
Expand Down Expand Up @@ -50,7 +57,7 @@ async def get_tournament(self, t_id: int, force_update=False) -> Tournament:
ChallongeException
"""
found_t = find_local(self.tournaments, t_id)
found_t = self._find_tournament(t_id)
if force_update or found_t is None:
res = await self.connection('GET', 'tournaments/{}'.format(t_id))
found_t = self._create_tournament(res)
Expand Down Expand Up @@ -123,7 +130,7 @@ async def destroy_tournament(self, t: Tournament):
ChallongeException
"""
found_t = find_local(self.tournaments, t.id)
found_t = self._find_tournament(t.id)
if found_t is None:
print('Unreferenced tournament')
else:
Expand Down
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
# Usually you set "language" from the command line for these cases.
language = None

autodoc_member_order = 'bysource'


# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
Expand Down
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_b_add_participants(self):
p1_1 = yield from t.get_participant(p1.id, force_update=True)
self.assertEqual(p1.id, p1_1.id)
ps = yield from t.get_participants(force_update=True)
remaining = yield from t.remove_participant(p2)
remaining = yield from t.remove_participant(p2, get_participants=True)
self.assertEqual(len(remaining), 1)
for p in ps:
if p.id == p1.id:
Expand Down

0 comments on commit 8ddb212

Please sign in to comment.