Move logging logic to singleton

- Renamed module and class because "conductor" didn't make a lot of sense
- Used singleton approach (there's only one stdout after all)
- Redesigned progress bars (now with download speed!)
This commit is contained in:
Joscha
2021-05-18 22:43:46 +02:00
parent 1525aa15a6
commit 4b68fa771f
12 changed files with 195 additions and 193 deletions

View File

@ -2,7 +2,6 @@ from configparser import SectionProxy
from typing import Callable, Dict
from ..authenticator import Authenticator, AuthSection
from ..conductor import TerminalConductor
from ..config import Config
from .simple import SimpleAuthenticator, SimpleAuthSection
from .tfa import TfaAuthenticator
@ -11,12 +10,11 @@ AuthConstructor = Callable[[
str, # Name (without the "auth:" prefix)
SectionProxy, # Authenticator's section of global config
Config, # Global config
TerminalConductor, # Global conductor instance
], Authenticator]
AUTHENTICATORS: Dict[str, AuthConstructor] = {
"simple": lambda n, s, c, t:
SimpleAuthenticator(n, SimpleAuthSection(s), c, t),
"tfa": lambda n, s, c, t:
TfaAuthenticator(n, AuthSection(s), c, t),
"simple": lambda n, s, c:
SimpleAuthenticator(n, SimpleAuthSection(s), c),
"tfa": lambda n, s, c:
TfaAuthenticator(n, AuthSection(s), c),
}

View File

@ -1,8 +1,8 @@
from typing import Optional, Tuple
from ..authenticator import Authenticator, AuthException, AuthSection
from ..conductor import TerminalConductor
from ..config import Config
from ..logging import log
from ..utils import agetpass, ainput
@ -20,9 +20,8 @@ class SimpleAuthenticator(Authenticator):
name: str,
section: SimpleAuthSection,
config: Config,
conductor: TerminalConductor,
) -> None:
super().__init__(name, section, config, conductor)
super().__init__(name, section, config)
self._username = section.username()
self._password = section.password()
@ -34,7 +33,7 @@ class SimpleAuthenticator(Authenticator):
if self._username is not None and self._password is not None:
return self._username, self._password
async with self.conductor.exclusive_output():
async with log.exclusive_output():
if self._username is None:
self._username = await ainput("Username: ")
else:

View File

@ -1,8 +1,8 @@
from typing import Tuple
from ..authenticator import Authenticator, AuthException, AuthSection
from ..conductor import TerminalConductor
from ..config import Config
from ..logging import log
from ..utils import ainput
@ -12,15 +12,14 @@ class TfaAuthenticator(Authenticator):
name: str,
section: AuthSection,
config: Config,
conductor: TerminalConductor,
) -> None:
super().__init__(name, section, config, conductor)
super().__init__(name, section, config)
async def username(self) -> str:
raise AuthException("TFA authenticator does not support usernames")
async def password(self) -> str:
async with self.conductor.exclusive_output():
async with log.exclusive_output():
code = await ainput("TFA code: ")
return code