1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-22 08:55:08 +00:00

refactor w/ black

This commit is contained in:
NextChai 2022-04-30 02:27:16 -04:00
parent cc3cde89c8
commit b474492637
8 changed files with 187 additions and 165 deletions

View file

@ -1,4 +1,4 @@
#== objects.py ==#
# == objects.py ==#
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union, Dict
@ -23,23 +23,22 @@ __all__ = (
'Organization',
)
def dt_formatter(time_str: str) -> Optional[datetime]:
if time_str is not None:
return datetime.strptime(time_str, r"%Y-%m-%dT%H:%M:%SZ")
return None
def repr_dt(_datetime: datetime) -> str:
return _datetime.strftime(r'%d-%m-%Y, %H:%M:%S')
class APIObject:
__slots__: Tuple[str, ...] = (
'_response',
'_http'
)
__slots__: Tuple[str, ...] = ('_response', '_http')
def __init__(self, response: Dict[str, Any] , _http: http) -> None:
def __init__(self, response: Dict[str, Any], _http: http) -> None:
self._http = _http
self._response = response
@ -47,13 +46,15 @@ class APIObject:
return f'<{self.__class__.__name__}>'
#=== User stuff ===#
# === User stuff ===#
class _BaseUser(APIObject):
__slots__ = (
'login',
'id',
)
)
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
self._http = _http
@ -64,19 +65,19 @@ class _BaseUser(APIObject):
return f'<{self.__class__.__name__} id = {self.id}, login = {self.login!r}>'
async def repos(self) -> list[Repository]:
results = await self._http.get_user_repos(self) # type: ignore
results = await self._http.get_user_repos(self) # type: ignore
return [Repository(data, self._http) for data in results]
async def gists(self) -> list[Gist]:
results = await self._http.get_user_gists(self) # type: ignore
results = await self._http.get_user_gists(self) # type: ignore
return [Gist(data, self._http) for data in results]
async def orgs(self) -> list[Organization]:
results = await self._http.get_user_orgs(self) # type: ignore
results = await self._http.get_user_orgs(self) # type: ignore
return [Organization(data, self._http) for data in results]
class User(_BaseUser):
class User(_BaseUser):
__slots__ = (
'login',
'id',
@ -87,11 +88,12 @@ class User(_BaseUser):
'followers',
'following',
'created_at',
)
)
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
tmp = self.__slots__ + _BaseUser.__slots__
keys = {key: value for key,value in self._response.items() if key in tmp}
keys = {key: value for key, value in self._response.items() if key in tmp}
for key, value in keys.items():
if '_at' in key and value is not None:
setattr(self, key, dt_formatter(value))
@ -109,7 +111,7 @@ class PartialUser(_BaseUser):
'site_admin',
'html_url',
'avatar_url',
) + _BaseUser.__slots__
) + _BaseUser.__slots__
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
@ -121,25 +123,25 @@ class PartialUser(_BaseUser):
return f'<{self.__class__.__name__} login: {self.login!r}, id: {self.id}, site_admin: {self.site_admin}, html_url: {self.html_url}>'
async def _get_user(self) -> User:
"""Upgrades the PartialUser to a User object."""
"""Upgrades the PartialUser to a User object."""
response = await self._http.get_user(self.login)
return User(response, self._http)
#=== Repository stuff ===#
# === Repository stuff ===#
class Repository(APIObject):
if TYPE_CHECKING:
id: int
name: str
owner: str
__slots__ = (
'id',
'name',
'owner',
'size'
'created_at',
'size' 'created_at',
'url',
'html_url',
'archived',
@ -151,11 +153,12 @@ class Repository(APIObject):
'stargazers_count',
'watchers_count',
'license',
)
)
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
tmp = self.__slots__ + APIObject.__slots__
keys = {key: value for key,value in self._response.items() if key in tmp}
keys = {key: value for key, value in self._response.items() if key in tmp}
for key, value in keys.items():
if key == 'owner':
setattr(self, key, PartialUser(value, self._http))
@ -198,6 +201,7 @@ class Repository(APIObject):
def forks(self) -> int:
return self._response.get('forks')
class Issue(APIObject):
__slots__ = (
'id',
@ -212,7 +216,7 @@ class Issue(APIObject):
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
tmp = self.__slots__ + APIObject.__slots__
keys = {key: value for key,value in self._response.items() if key in tmp}
keys = {key: value for key, value in self._response.items() if key in tmp}
for key, value in keys.items():
if key == 'user':
setattr(self, key, PartialUser(value, self._http))
@ -241,7 +245,9 @@ class Issue(APIObject):
def html_url(self) -> str:
return self._response.get('html_url')
#=== Gist stuff ===#
# === Gist stuff ===#
class File:
def __init__(self, fp: Union[str, io.StringIO], filename: str = 'DefaultFilename.txt') -> None:
@ -253,17 +259,18 @@ class File:
if os.path.exists(self.fp):
with open(self.fp) as fp:
data = fp.read()
return data
return self.fp
elif isinstance(self.fp, io.BytesIO):
return self.fp.read()
elif isinstance(self.fp, io.StringIO): # type: ignore
elif isinstance(self.fp, io.StringIO): # type: ignore
return self.fp.getvalue()
raise TypeError(f'Expected str, io.StringIO, or io.BytesIO, got {type(self.fp)}')
class Gist(APIObject):
__slots__ = (
'id',
@ -274,11 +281,12 @@ class Gist(APIObject):
'owner',
'created_at',
'truncated',
)
)
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
tmp = self.__slots__ + APIObject.__slots__
keys = {key: value for key,value in self._response.items() if key in tmp}
keys = {key: value for key, value in self._response.items() if key in tmp}
for key, value in keys.items():
if key == 'owner':
setattr(self, key, PartialUser(value, self._http))
@ -309,25 +317,26 @@ class Gist(APIObject):
return self._response
#=== Organization stuff ===#
# === Organization stuff ===#
class Organization(APIObject):
__slots__ = (
'login',
'id',
'is_verified',
'public_repos',
'public_gists',
'followers',
'following',
'created_at',
'avatar_url',
'login',
'id',
'is_verified',
'public_repos',
'public_gists',
'followers',
'following',
'created_at',
'avatar_url',
)
def __init__(self, response: Dict[str, Any], _http: http) -> None:
super().__init__(response, _http)
tmp = self.__slots__ + APIObject.__slots__
keys = {key: value for key,value in self._response.items() if key in tmp}
keys = {key: value for key, value in self._response.items() if key in tmp}
for key, value in keys.items():
if key == 'login':
setattr(self, key, value)
@ -349,4 +358,4 @@ class Organization(APIObject):
@property
def html_url(self):
return self._response.get('html_url')
return self._response.get('html_url')