diff --git a/HISTORY.rst b/HISTORY.rst index c6411e2..411e90b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,13 @@ History ======= +1.1.1 (2024-10-09) +------------------- + +* Removes hardcodings +* Adds Configuration Interface +* Updates the docs + 1.1.0 (2024-10-04) ------------------- diff --git a/MANIFEST.in b/MANIFEST.in index e59b177..7f80a41 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -3,6 +3,7 @@ include HISTORY.rst include LICENSE include README.rst +include conf/k2hr3client.ini recursive-include k2hr3client * recursive-exclude * __pycache__ diff --git a/conf/k2hr3client.ini b/conf/k2hr3client.ini new file mode 100644 index 0000000..00b639f --- /dev/null +++ b/conf/k2hr3client.ini @@ -0,0 +1,19 @@ +[DEFAULT] +debug = False +iaas_url = "http://172.24.4.1" +iaas_project = "demo" +iaas_user = "demo" +iaas_password = "password" +log_file = sys.stdout +log_dir = logs +log_level = logging.INFO + +[k2hr3] +api_url = "http://127.0.0.1:18080" +api_version = "v1" + +[http] +timeout_seconds = 30 +retry_interval_seconds = 60 +max_retries = 3 +allow_self_signed_cert = True diff --git a/docs/locale/ja/LC_MESSAGES/history.po b/docs/locale/ja/LC_MESSAGES/history.po index f8e97f4..0f9686e 100644 --- a/docs/locale/ja/LC_MESSAGES/history.po +++ b/docs/locale/ja/LC_MESSAGES/history.po @@ -10,7 +10,7 @@ msgid "" msgstr "" "Project-Id-Version: k2hr3client \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-10-04 20:13+0900\n" +"POT-Creation-Date: 2024-10-09 15:10+0900\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: ja\n" @@ -26,62 +26,74 @@ msgid "History" msgstr "歴史" #: ../../../HISTORY.rst:6 -msgid "1.1.0 (2024-10-04)" +msgid "1.1.1 (2024-10-09)" msgstr "" #: ../../../HISTORY.rst:8 -msgid "Removes the deprecated Interfaces" -msgstr "非推奨のインタフェースを削除" +msgid "Removes hardcodings" +msgstr "ハードコーディングを削除" #: ../../../HISTORY.rst:9 -msgid "Removes the examples" -msgstr "例示プログラムの削除" +msgid "Adds Configuration Interface" +msgstr "設定用インターフェースを追加" -#: ../../../HISTORY.rst:10 +#: ../../../HISTORY.rst:10 ../../../HISTORY.rst:17 msgid "Updates the docs" msgstr "ドキュメントの更新" #: ../../../HISTORY.rst:13 -msgid "1.0.3 (2024-10-03)" +msgid "1.1.0 (2024-10-04)" msgstr "" #: ../../../HISTORY.rst:15 +msgid "Removes the deprecated Interfaces" +msgstr "非推奨のインタフェースを削除" + +#: ../../../HISTORY.rst:16 +msgid "Removes the examples" +msgstr "例示プログラムの削除" + +#: ../../../HISTORY.rst:20 +msgid "1.0.3 (2024-10-03)" +msgstr "" + +#: ../../../HISTORY.rst:22 msgid "Fixes the K2hr3 API version" msgstr "K2hr3 APIのバージョンを固定化" -#: ../../../HISTORY.rst:16 +#: ../../../HISTORY.rst:23 msgid "Deprecates K2hr3Resource.create_conf_resource params" msgstr "K2hr3Resource.create_conf_resourceのパラメータを一部非推奨化" -#: ../../../HISTORY.rst:19 +#: ../../../HISTORY.rst:26 msgid "1.0.2 (2024-09-26)" msgstr "" -#: ../../../HISTORY.rst:21 +#: ../../../HISTORY.rst:28 msgid "Fixes lint errors" msgstr "静的解析ツールのエラーを修正" -#: ../../../HISTORY.rst:24 +#: ../../../HISTORY.rst:31 msgid "1.0.1 (2024-09-25)" msgstr "" -#: ../../../HISTORY.rst:26 +#: ../../../HISTORY.rst:33 msgid "Fixes resource API bugs" msgstr "Resource APIの不具合を修正" -#: ../../../HISTORY.rst:29 +#: ../../../HISTORY.rst:36 msgid "1.0.0 (2024-08-28)" msgstr "" -#: ../../../HISTORY.rst:31 +#: ../../../HISTORY.rst:38 msgid "Supports the other APIs" msgstr "他のAPIをサポート" -#: ../../../HISTORY.rst:34 +#: ../../../HISTORY.rst:41 msgid "0.0.1 (2020-08-28)" msgstr "" -#: ../../../HISTORY.rst:36 +#: ../../../HISTORY.rst:43 msgid "Supports Token, Resource, Policy and Role API" msgstr "トークン、リソース、ポリシーおよびロールAPIをサポート" diff --git a/src/k2hr3client/__init__.py b/src/k2hr3client/__init__.py index 7e0144a..5bac0ad 100644 --- a/src/k2hr3client/__init__.py +++ b/src/k2hr3client/__init__.py @@ -22,8 +22,10 @@ """K2HR3 Python Client of Token API.""" __author__ = 'Hirotaka Wakabayashi ' -__version__ = '1.1.0' +__version__ = '1.1.1' +import configparser +from pathlib import Path import sys if sys.platform.startswith('win'): @@ -39,6 +41,65 @@ def get_version() -> str: return __version__ +# 1. Defines the default config as a package level variable. +CONFIG = configparser.ConfigParser() +# [DEFAULT] +# debug = False +# iaas_url = http://172.24.4.1 +# iaas_project = demo +# iaas_user = demo +# iaas_password = password +# log_file = sys.stdout +# log_dir = logs +# log_format = %(asctime)-15s %(levelname)s %(name)s %(message)s +# log_level = logging.INFO +default_section = CONFIG['DEFAULT'] +default_section['debug'] = "False" +default_section['iaas_url'] = "http://172.24.4.1" +default_section['iaas_project'] = "demo" +default_section['iaas_user'] = "demo" +default_section['iaas_password'] = "password" +default_section['log_file'] = "sys.stderr" +default_section['log_dir'] = "logs" +default_section['log_level'] = "logging.INFO" + +# [k2hr3] +# api_url = "http://127.0.0.1:18080" +# api_version = "v1" +CONFIG['k2hr3'] = {} +k2hr3_section = CONFIG['k2hr3'] +k2hr3_section['api_url'] = "http://127.0.0.1:18080" +k2hr3_section['api_version'] = "v1" + +# [http] +# timeout_seconds = 30 +# retry_interval_seconds = 60 +# max_retries = 3 +# allow_self_signed_cert = True +CONFIG['http'] = {} +http_section = CONFIG['http'] +http_section['timeout_seconds'] = "30" +http_section['retry_interval_seconds'] = "60" +http_section['max_retries'] = "3" +http_section['allow_self_signed_cert'] = "True" + +# 2. Overrides the default config by the config file. +# Find the config using precedence of the location: +# ./k2hr3client.ini +# ~/.k2hr3client.ini +# /etc/antpickax/k2hr3client.ini +# NOTE: +# Using the configuration value may occur KeyError. +# All values are string. Use getboolean or something. +config_path = [Path("k2hr3client.ini"), + Path(Path.home() / '.k2hr3client.ini'), + Path('/etc/antpickax/k2hr3client.ini')] +for my_config in config_path: + if my_config.is_file(): + # Override the value if the key is defined. + CONFIG.read(my_config.absolute()) + break + # # Local variables: # tab-width: 4 diff --git a/src/k2hr3client/http.py b/src/k2hr3client/http.py index cfbaf86..0bab18a 100644 --- a/src/k2hr3client/http.py +++ b/src/k2hr3client/http.py @@ -51,6 +51,7 @@ from k2hr3client.api import K2hr3HTTPMethod, K2hr3Api from k2hr3client.exception import K2hr3Exception +from k2hr3client import CONFIG LOG = logging.getLogger(__name__) @@ -75,12 +76,12 @@ class K2hr3Http(): # pylint: disable=too-many-instance-attributes def __init__(self, baseurl: str) -> None: """Init the members.""" self._set_baseurl(baseurl) - self._timeout_seconds = 30 + self._timeout_seconds = CONFIG['http'].getint('timeout_seconds') self._url = None # type: Optional[str] self._urlparams = None # type: Optional[str] - self._retry_interval_seconds = 60 # type: int - self._retries = 3 # type: int - self._allow_self_signed_cert = True # type: bool + self._retry_interval_seconds = CONFIG['http'].getint('retry_interval_seconds') # noqa + self._retries = CONFIG['http'].getint('retries') + self._allow_self_signed_cert = CONFIG['http'].getboolean('allow_self_signed_cert') # noqa def __repr__(self) -> str: """Represent the members."""