From d5ba5bc58dc8dec1b0ab45bd4e9525c664af3d78 Mon Sep 17 00:00:00 2001 From: sudosnok Date: Sun, 27 Mar 2022 13:04:00 +0100 Subject: [PATCH] Moved objects into their own place in a folder for readability --- Github/http.py | 1 - Github/main.py | 4 +- Github/objects/__init__.py | 5 ++ Github/objects/objects.py | 29 ++++++++++ Github/objects/repo.py | 49 ++++++++++++++++ Github/{objects.py => objects/user.py} | 78 +++----------------------- 6 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 Github/objects/__init__.py create mode 100644 Github/objects/objects.py create mode 100644 Github/objects/repo.py rename Github/{objects.py => objects/user.py} (54%) diff --git a/Github/http.py b/Github/http.py index baadd8b..7e4b953 100644 --- a/Github/http.py +++ b/Github/http.py @@ -9,7 +9,6 @@ import re from .exceptions import * from .objects import * -from .objects import APIOBJECT from .urls import * LINK_PARSING_RE = re.compile(r"<(\S+(\S))>; rel=\"(\S+)\"") diff --git a/Github/main.py b/Github/main.py index 6aa144d..48227e8 100644 --- a/Github/main.py +++ b/Github/main.py @@ -9,7 +9,7 @@ import aiohttp from . import http from .exceptions import AlreadyStarted, NotStarted -from .objects import User +from .objects import User, Repository class Github: _auth = None @@ -61,7 +61,7 @@ class Github: """Fetch a Github user from their username.""" return User(await http.get_user(self.session, username), self.session) - async def get_repo(self, repo_name: str) -> 'Repo': + async def get_repo(self, repo_name: str) -> Repository: """Fetch a Github repository from it's name.""" pass diff --git a/Github/objects/__init__.py b/Github/objects/__init__.py new file mode 100644 index 0000000..d771569 --- /dev/null +++ b/Github/objects/__init__.py @@ -0,0 +1,5 @@ +#== __init__.py ==# + +from .objects import * +from .user import * +from .repo import * \ No newline at end of file diff --git a/Github/objects/objects.py b/Github/objects/objects.py new file mode 100644 index 0000000..9118bd2 --- /dev/null +++ b/Github/objects/objects.py @@ -0,0 +1,29 @@ +#== objects.py ==# + +from datetime import datetime +import aiohttp + +__all__ = ( + 'APIOBJECT', +) + +def dt_formatter(time_str): + if time_str is not None: + return datetime.strptime(time_str, r"%Y-%m-%dT%H:%M:%SZ") + return None + +def repr_dt(time_str): + return time_str.strftime(r'%d-%m-%Y, %H:%M:%S') + +class APIOBJECT: + __slots__ = ( + '_response', + 'session' + ) + + def __init__(self, response: dict[str, str | int], session: aiohttp.ClientSession) -> None: + self._response = response + self.session = session + + def __repr__(self) -> str: + return f'<{self.__class__.__name__}>' \ No newline at end of file diff --git a/Github/objects/repo.py b/Github/objects/repo.py new file mode 100644 index 0000000..d709920 --- /dev/null +++ b/Github/objects/repo.py @@ -0,0 +1,49 @@ +#== repo.py ==# + +import aiohttp + +from .objects import APIOBJECT, dt_formatter +from . import PartialUser +from .. import http + +__all__ = ( + 'Repository', +) + +class Repository(APIOBJECT): + __slots__ = ( + 'id', + 'name', + 'owner', + 'size' + 'created_at', + 'url', + 'html_url', + 'archived', + 'disabled', + 'updated_at', + 'open_issues_count', + 'default_branch', + 'clone_url', + 'stargazers_count', + 'watchers_count', + 'forks', + 'license' + ) + def __init__(self, response: dict, session: aiohttp.ClientSession) -> None: + super().__init__(response, session) + tmp = self.__slots__ + APIOBJECT.__slots__ + keys = {key: value for key,value in self.response.items() if key in tmp} + for key, value in key.items(): + if key == 'owner': + self.owner = PartialUser(value, self.session) + return + + if '_at' in key and value is not None: + setattr(self, key, dt_formatter(value)) + return + + setattr(self, key, value) + + def __repr__(self) -> str: + return f'' diff --git a/Github/objects.py b/Github/objects/user.py similarity index 54% rename from Github/objects.py rename to Github/objects/user.py index ee0f49e..091ccc8 100644 --- a/Github/objects.py +++ b/Github/objects/user.py @@ -1,37 +1,15 @@ -#== objects.py ==# - -from datetime import datetime +#== user.py ==# import aiohttp -from . import http +from .objects import APIOBJECT, dt_formatter +from .. import http __all__ = ( - 'User' + 'User', + 'PartialUser' ) -def dt_formatter(time_str): - if time_str is not None: - return datetime.strptime(time_str, r"%Y-%m-%dT%H:%M:%SZ") - return None - -def repr_dt(time_str): - return time_str.strftime(r'%d-%m-%Y, %H:%M:%S') - -class APIOBJECT: - __slots__ = ( - '_response', - 'session' - ) - - def __init__(self, response: dict[str, str | int], session: aiohttp.ClientSession) -> None: - self._response = response - self.session = session - - - def __repr__(self) -> str: - return f'<{self.__class__.__name__}>' - class _BaseUser(APIOBJECT): __slots__ = ( 'login', @@ -72,7 +50,7 @@ class User(_BaseUser): setattr(self, key, value) def __repr__(self): - return f'' + return f'' @classmethod async def get_user(cls, session: aiohttp.ClientSession, username: str) -> 'User': @@ -95,49 +73,9 @@ class PartialUser(_BaseUser): def __repr__(self): - return f'' + return f'' async def _get_user(self): """Upgrades the PartialUser to a User object.""" response = await http.get_user(self.session, self.login) - return User(response, self.session) - - -class Repository(APIOBJECT): - __slots__ = ( - 'id', - 'name', - 'owner', - 'size' - 'created_at', - 'url', - 'html_url', - 'archived', - 'disabled', - 'updated_at', - 'open_issues_count', - 'default_branch', - 'clone_url', - 'stargazers_count', - 'watchers_count', - 'forks', - 'license' - ) - def __init__(self, response: dict, session: aiohttp.ClientSession) -> None: - super().__init__(response, session) - tmp = self.__slots__ - keys = {key: value for key,value in self.response.items() if key in tmp} - for key, value in key.items(): - if key == 'owner': - self.owner = PartialUser(value, self.session) - return - - if '_at' in key and value is not None: - setattr(self, key, dt_formatter(value)) - return - - setattr(self, key, value) - - def __repr__(self) -> str: - return f'' - + return User(response, self.session) \ No newline at end of file