8 changed files with 311 additions and 128 deletions
@ -1,8 +1,5 @@ |
|||||
from a2s.exceptions import BrokenMessageError, BufferExhaustedError |
from a2s.exceptions import BrokenMessageError, BufferExhaustedError |
||||
from a2s.defaults import \ |
|
||||
set_default_timeout, get_default_timeout, \ |
|
||||
set_default_encoding, get_default_encoding |
|
||||
|
|
||||
from a2s.info import info, InfoResponse |
from a2s.info import info, SourceInfo, GoldSrcInfo |
||||
from a2s.players import players, PlayersResponse, PlayerEntry |
from a2s.players import players, Player |
||||
from a2s.rules import rules, RulesResponse |
from a2s.rules import rules |
||||
|
@ -0,0 +1,47 @@ |
|||||
|
""" |
||||
|
Cheap dataclasses module backport |
||||
|
|
||||
|
Check out the official documentation to see what this is trying to |
||||
|
achieve: |
||||
|
https://docs.python.org/3/library/dataclasses.html |
||||
|
""" |
||||
|
|
||||
|
import collections |
||||
|
import copy |
||||
|
|
||||
|
class DataclsBase: |
||||
|
def __init__(self, **kwargs): |
||||
|
for name, value in self._defaults.items(): |
||||
|
if name in kwargs: |
||||
|
value = kwargs[name] |
||||
|
setattr(self, name, copy.copy(value)) |
||||
|
|
||||
|
def __iter__(self): |
||||
|
for name in self.__annotations__: |
||||
|
yield (name, getattr(self, name)) |
||||
|
|
||||
|
def __repr__(self): |
||||
|
return "{}({})".format( |
||||
|
self.__class__.__name__, |
||||
|
", ".join(name + "=" + repr(value) for name, value in self)) |
||||
|
|
||||
|
class DataclsMeta(type): |
||||
|
def __new__(cls, name, bases, prop): |
||||
|
values = collections.OrderedDict() |
||||
|
for member_name in prop["__annotations__"].keys(): |
||||
|
# Check if member has a default value set as class variable |
||||
|
if member_name in prop: |
||||
|
# Store default value and remove the class variable |
||||
|
values[member_name] = prop[member_name] |
||||
|
del prop[member_name] |
||||
|
else: |
||||
|
# Set None as the default value |
||||
|
values[member_name] = None |
||||
|
|
||||
|
prop["__slots__"] = list(values.keys()) |
||||
|
prop["_defaults"] = values |
||||
|
bases = (DataclsBase, *bases) |
||||
|
return super().__new__(cls, name, bases, prop) |
||||
|
|
||||
|
def __prepare__(self, *args, **kwargs): |
||||
|
return collections.OrderedDict() |
@ -1,16 +1,2 @@ |
|||||
default_timeout = 3.0 |
DEFAULT_TIMEOUT = 3.0 |
||||
default_encoding = "utf-8" |
DEFAULT_ENCODING = "utf-8" |
||||
|
|
||||
def set_default_timeout(timeout): |
|
||||
"""Set module-wide default timeout in seconds""" |
|
||||
default_timeout = timeout |
|
||||
|
|
||||
def get_default_timeout(): |
|
||||
"""Get module-wide default timeout in seconds""" |
|
||||
return default_timeout |
|
||||
|
|
||||
def set_default_encoding(enc): |
|
||||
default_encoding = enc |
|
||||
|
|
||||
def get_default_encoding(): |
|
||||
return default_encoding |
|
||||
|
Loading…
Reference in new issue