Skip to content

Commit

Permalink
linter errs correction
Browse files Browse the repository at this point in the history
  • Loading branch information
zrvku2000 committed Jan 28, 2025
1 parent 74891d3 commit ffba946
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
16 changes: 8 additions & 8 deletions aiohttp_devtools/runserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ def __init__(self, *,

self.bind_address = bind_address
if main_port is None:
main_port = 8000 if ssl_context_factory_name == None else 8443
main_port = 8000 if ssl_context_factory_name is None else 8443
self.main_port = main_port
self.aux_port = aux_port or (main_port + 1)
self.browser_cache = browser_cache
self.ssl_context_factory_name = ssl_context_factory_name
logger.debug('config loaded:\n%s', self)

@property
def protocol(self) -> Literal["http", "https"]:
return "http" if self.ssl_context_factory_name is None else "https"
Expand Down Expand Up @@ -145,7 +145,7 @@ def _resolve_path(self, _path: str, check: str, arg_name: str) -> Path:
if not path.is_dir():
raise AdevConfigError('{} is not a directory'.format(path))
return path

def import_module(self) -> ModuleType:
"""Import and return python module.
Expand Down Expand Up @@ -199,22 +199,22 @@ def get_app_factory(self, module: ModuleType) -> AppFactory:
self.py_file.name, self.app_factory_name))

return attr # type: ignore[no-any-return]

def get_ssl_context(self, module: ModuleType) -> Union[SSLContext, None]:
if self.ssl_context_factory_name is None:
return None
else:
else:
try:
attr = getattr(module, self.ssl_context_factory_name)
except AttributeError:
raise AdevConfigError("Module '{}' does not define a '{}' attribute/class".format(
self.py_file.name, self.ssl_context_factory_name))
self.py_file.name, self.ssl_context_factory_name))
ssl_context = attr()
if isinstance(ssl_context, SSLContext):
return ssl_context
else:
raise AdevConfigError("ssl-context-factory '{}' in module '{}' didn't return valid SSLContext".format(
self.ssl_context_factory_name, self.py_file.name))
raise AdevConfigError("ssl-context-factory '{}' in module '{}' didn't return valid SSLContext".format(
self.ssl_context_factory_name, self.py_file.name))

async def load_app(self, app_factory: AppFactory) -> web.Application:
if isinstance(app_factory, web.Application):
Expand Down
1 change: 0 additions & 1 deletion aiohttp_devtools/runserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class RunServer(TypedDict):
ssl_context: Union[SSLContext, None]



def runserver(**config_kwargs: Any) -> RunServer:
"""Prepare app ready to run development server.
Expand Down
3 changes: 2 additions & 1 deletion aiohttp_devtools/runserver/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def shutdown() -> NoReturn:

path = config.path_prefix + "/shutdown"
app.router.add_route("GET", path, do_shutdown, name="_devtools.shutdown")
dft_logger.debug("Created shutdown endpoint at {}://{}:{}{}".format(config.protocol, config.host, config.main_port, path))
dft_logger.debug("Created shutdown endpoint at {}://{}:{}{}".format(
config.protocol, config.host, config.main_port, path))

if config.static_path is not None:
static_url = '{}://{}:{}/{}'.format(config.protocol, config.host, config.aux_port, static_path)
Expand Down
3 changes: 2 additions & 1 deletion aiohttp_devtools/runserver/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ async def _src_reload_when_live(self, checks: int) -> None:

def _start_dev_server(self) -> None:
act = 'Start' if self._reloads == 0 else 'Restart'
logger.info('%sing dev server at %s://%s:%s ●', act, self._config.protocol, self._config.host, self._config.main_port)
logger.info('%sing dev server at %s://%s:%s ●',
act, self._config.protocol, self._config.host, self._config.main_port)

try:
tty_path = os.ttyname(sys.stdin.fileno())
Expand Down
11 changes: 7 additions & 4 deletions tests/test_runserver_with_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async def test_load_invalid_app(tmpworkdir):
with pytest.raises(AiohttpDevConfigError):
Config(app_path='invalid')


async def check_server_running(check_callback, sslcontext):
port_open = False
async with aiohttp.ClientSession(timeout=ClientTimeout(total=1)) as session:
Expand All @@ -37,9 +38,10 @@ async def check_server_running(check_callback, sslcontext):
await check_callback(session, sslcontext)
await asyncio.sleep(.25) # TODO(aiohttp 4): Remove this hack


@pytest.mark.filterwarnings(r"ignore:unclosed:ResourceWarning")
@forked
@pytest.mark.datafiles('tests/test_certs', keep_top_dir = True)
@pytest.mark.datafiles('tests/test_certs', keep_top_dir=True)
def test_start_runserver_ssl(datafiles, tmpworkdir, smart_caplog):
mktree(tmpworkdir, {
'app.py': """\
Expand All @@ -66,7 +68,8 @@ def get_ssl_context():
})
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
args = runserver(app_path="app.py", static_path="static_dir", bind_address="0.0.0.0", ssl_context_factory_name='get_ssl_context')
args = runserver(app_path="app.py", static_path="static_dir",
bind_address="0.0.0.0", ssl_context_factory_name='get_ssl_context')
aux_app = args["app"]
aux_port = args["port"]
runapp_host = args["host"]
Expand All @@ -75,7 +78,9 @@ def get_ssl_context():
assert runapp_host == "0.0.0.0"
for startup in aux_app.on_startup:
loop.run_until_complete(startup(aux_app))

sslcontext = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)

async def check_callback(session, sslcontext):
print(session, sslcontext)
async with session.get('https://localhost:8443/', ssl=sslcontext) as r:
Expand All @@ -102,5 +107,3 @@ async def check_callback(session, sslcontext):
'adev.server.dft INFO: Starting dev server at https://localhost:8443 ●\n'
) in smart_caplog
loop.run_until_complete(asyncio.sleep(.25)) # TODO(aiohttp 4): Remove this hack


0 comments on commit ffba946

Please sign in to comment.