Skip to content

Commit

Permalink
cleanup and renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Jan 12, 2017
1 parent 58580f1 commit 8521a43
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
8 changes: 4 additions & 4 deletions challonge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
__version__ = "0.5.0"
__author__ = "fp12"

CHALLONGE_AUTO_GET_PARTICIPANTS = True
CHALLONGE_AUTO_GET_MATCHES = True
CHALLONGE_USE_FIELDS_DESCRIPTORS = True
CHALLONGE_USE_EXCEPTIONS = True
AUTO_GET_PARTICIPANTS = True
AUTO_GET_MATCHES = True
USE_FIELDS_DESCRIPTORS = True
USE_EXCEPTIONS = True


from .helpers import APIException
Expand Down
7 changes: 3 additions & 4 deletions challonge/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class APIException(Exception):

def assert_or_raise(cond, exc, *args):
if not cond:
# print(challonge.CHALLONGE_USE_EXCEPTIONS)
if challonge.CHALLONGE_USE_EXCEPTIONS:
if challonge.USE_EXCEPTIONS:
raise exc(*args)
else:
log.warning('a silent exception `{}` has been raised `{}`'.format(exc.__name__, args))
Expand Down Expand Up @@ -46,7 +45,7 @@ def _find_holder(self, local_list, e_id):

def _get_from_dict(self, data):
for a in self._fields:
name = FieldHolder.private_name.format(a) if challonge.CHALLONGE_USE_FIELDS_DESCRIPTORS else a
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):
Expand All @@ -57,7 +56,7 @@ def __init__(cls, name, bases, dct):
cls._get_from_dict = FieldHolder._get_from_dict
cls.__eq__ = lambda self, other: self._id == other._id

if challonge.CHALLONGE_USE_FIELDS_DESCRIPTORS:
if challonge.USE_FIELDS_DESCRIPTORS:
for a in cls._fields:
setattr(cls, a, FieldDescriptor(FieldHolder.private_name.format(a)))

Expand Down
18 changes: 9 additions & 9 deletions challonge/tournament.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from enum import Enum

from . import CHALLONGE_AUTO_GET_PARTICIPANTS, CHALLONGE_AUTO_GET_MATCHES
from . import AUTO_GET_PARTICIPANTS, AUTO_GET_MATCHES
from .helpers import FieldHolder, assert_or_raise
from .participant import Participant
from .match import Match
Expand Down Expand Up @@ -149,8 +149,8 @@ async def start(self):
"""
params = {
'include_participants': 1 if CHALLONGE_AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_participants': 1 if AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('POST', 'tournaments/{}/start'.format(self._id), **params)
self._refresh_from_json(res)
Expand All @@ -168,8 +168,8 @@ async def reset(self):
"""
params = {
'include_participants': 1 if CHALLONGE_AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_participants': 1 if AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('POST', 'tournaments/{}/reset'.format(self._id), **params)
self._refresh_from_json(res)
Expand All @@ -187,8 +187,8 @@ async def finalize(self):
"""
params = {
'include_participants': 1 if CHALLONGE_AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_participants': 1 if AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('POST', 'tournaments/{}/finalize'.format(self._id), **params)
self._refresh_from_json(res)
Expand Down Expand Up @@ -746,7 +746,7 @@ async def process_check_ins(self):
"""
params = {
'include_participants': 1, # forced to 1 since we need to update the Participant instances
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('POST', 'tournaments/{}/process_check_ins'.format(self._id), **params)
self._refresh_from_json(res)
Expand All @@ -770,7 +770,7 @@ async def abort_check_in(self):
"""
params = {
'include_participants': 1, # forced to 1 since we need to update the Participant instances
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('POST', 'tournaments/{}/abort_check_in'.format(self._id), **params)
self._refresh_from_json(res)
13 changes: 3 additions & 10 deletions challonge/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import CHALLONGE_AUTO_GET_PARTICIPANTS, CHALLONGE_AUTO_GET_MATCHES
from . import AUTO_GET_PARTICIPANTS, AUTO_GET_MATCHES
from .helpers import get_connection
from .tournament import Tournament, TournamentType

Expand All @@ -14,13 +14,6 @@ def __init__(self, username: str, api_key: str, **kwargs):
self.tournaments = None
self.connection = get_connection(username, api_key, **kwargs)

def _add_tournament(self, t: Tournament):
if t is not None:
if self.tournaments is None:
self.tournaments = [t]
else:
self.tournaments.append(t)

def _refresh_tournament_from_json(self, tournament_data):
if self.tournaments is None:
self.tournaments = [self._create_tournament(tournament_data)]
Expand Down Expand Up @@ -94,8 +87,8 @@ async def get_tournaments(self, force_update=False) -> list:
"""
if force_update or self.tournaments is None:
params = {
'include_participants': 1 if CHALLONGE_AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if CHALLONGE_AUTO_GET_MATCHES else 0
'include_participants': 1 if AUTO_GET_PARTICIPANTS else 0,
'include_matches': 1 if AUTO_GET_MATCHES else 0
}
res = await self.connection('GET', 'tournaments', **params)
if len(res) == 0:
Expand Down
8 changes: 6 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def test_a_raise(self):
with self.assertRaises(NameError):
yield from t.update(fake_argument=0)

challonge.CHALLONGE_USE_EXCEPTIONS = False
challonge.USE_EXCEPTIONS = False

with self.assertLogs('challonge', level='WARN'):
yield from t.update(fake_argument=0)

challonge.CHALLONGE_USE_EXCEPTIONS = True
challonge.USE_EXCEPTIONS = True

yield from self.user.destroy_tournament(t)

Expand Down Expand Up @@ -282,6 +282,8 @@ def test_e_bulk(self):
@unittest.expectedFailure
@async_test
def test_f_checkin(self):
challonge.USE_EXCEPTIONS = False

random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
p = yield from t.add_participant(username)
Expand Down Expand Up @@ -341,6 +343,8 @@ def test_f_checkin(self):

yield from self.user.destroy_tournament(t)

challonge.USE_EXCEPTIONS = True

self.fail('expected failure that sometimes work')

# @unittest.skip('')
Expand Down

0 comments on commit 8521a43

Please sign in to comment.