Divide API into modules

This commit is contained in:
2026-05-04 20:01:43 +02:00
parent 10a25b82af
commit 044ccba66a
5 changed files with 324 additions and 139 deletions
+112
View File
@@ -0,0 +1,112 @@
# Copyright 2026 jCloud
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import jcloud_config_parser
import ipaddress
import pathlib
import logging
import argparse
__all__ = [
'load_config',
'process_host_and_port'
]
def load_config(config_directory: pathlib.Path) -> tuple[
jcloud_config_parser.ini.INIConfiguration,
jcloud_config_parser.ini.INIConfiguration
]:
'''
Loads the configuration and the default configuration.
:param config_directory: The configuration directory.
:type config_directory: pathlib.Path
:return: The configuration.
:rtype: tuple[jcloud_config_parser.ini.INIConfiguration, jcloud_config_parser.ini.INIConfiguration]
'''
# load default configuration
with open(os.path.dirname(__file__) + '/../../../default_configuration.conf') as f:
default_configuration = jcloud_config_parser.ini.INIConfiguration.from_string(f.read())
f.close()
# load configuration
configuration = jcloud_config_parser.ini.INIConfiguration.from_string(
(config_directory / 'server.conf').read_text(),
default=default_configuration,
ignore_errors=True
)
return configuration, default_configuration
def _validate_host(host: str) -> bool:
'''
Checks whether the address is a valid address (``ip:port``), e. g. ``0.0.0.0:3000``.
:param addr: The address
:type addr: str
:return: ``True`` if the address is valid, otherwise ``False``.
:rtype: bool
'''
try:
ipaddress.ip_address(host)
return True
except Exception:
return False
def process_host_and_port(
configuration: jcloud_config_parser.ini.INIConfiguration,
default_configuration: jcloud_config_parser.ini.INIConfiguration,
args: argparse.Namespace,
logger: logging.Logger
) -> tuple[str, int]:
'''
Processes the host and port in the configuration.
Uses default values and the configuration data passed via
command-line arguments as a fallback.
:param configuration: The configuration.
:type configuration: jcloud_config_parser.ini.INIConfiguration
:param default_configuration: The default configuration.
:type default_configuration: jcloud_config_parser.ini.INIConfiguration
:param args: The arguments.
:type args: argparse.Namespace
:return: The host and the port
:rtype: tuple[str, int]
'''
# host
host = args.host or configuration.server.host
if not _validate_host(host):
logger.error(f'Error in configuration: [server]host: \'{host}\' is not a valid host, using default value.')
host = default_configuration.server.host
# port
port = args.port or configuration.server.port
if not port.isdigit():
logger.error(f'Error in configuration: [server]port: \'{port}\' is not an integer, using default value.')
port = default_configuration.server.port
else:
if not (0 <= int(port) <= 65535):
logger.error(f'Error in configuration: [server]port: {port} is not between 0 and 65535, using default value.')
port = default_configuration.server.port
return host, int(port)