apsniper0673-configreader 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- apsniper0673_configreader-0.1.0/LICENSE +21 -0
- apsniper0673_configreader-0.1.0/PKG-INFO +8 -0
- apsniper0673_configreader-0.1.0/README.md +1 -0
- apsniper0673_configreader-0.1.0/pyproject.toml +19 -0
- apsniper0673_configreader-0.1.0/setup.cfg +4 -0
- apsniper0673_configreader-0.1.0/src/apsniper0673_configreader.egg-info/PKG-INFO +8 -0
- apsniper0673_configreader-0.1.0/src/apsniper0673_configreader.egg-info/SOURCES.txt +10 -0
- apsniper0673_configreader-0.1.0/src/apsniper0673_configreader.egg-info/dependency_links.txt +1 -0
- apsniper0673_configreader-0.1.0/src/apsniper0673_configreader.egg-info/top_level.txt +1 -0
- apsniper0673_configreader-0.1.0/src/myconfigreader/__init__.py +1 -0
- apsniper0673_configreader-0.1.0/src/myconfigreader/config_reader.py +100 -0
- apsniper0673_configreader-0.1.0/tests/test.py +9 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 HyuntaMansei
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "apsniper0673_configreader"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "My config reader package"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "sniper0673" }
|
|
7
|
+
]
|
|
8
|
+
requires-python = ">=3.8"
|
|
9
|
+
dependencies = [
|
|
10
|
+
# "pandas>=1.5.0",
|
|
11
|
+
# "numpy>=1.23.0",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["setuptools>=64", "wheel"]
|
|
16
|
+
build-backend = "setuptools.build_meta"
|
|
17
|
+
|
|
18
|
+
[tool.setuptools.packages.find]
|
|
19
|
+
where = ["src"]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/apsniper0673_configreader.egg-info/PKG-INFO
|
|
5
|
+
src/apsniper0673_configreader.egg-info/SOURCES.txt
|
|
6
|
+
src/apsniper0673_configreader.egg-info/dependency_links.txt
|
|
7
|
+
src/apsniper0673_configreader.egg-info/top_level.txt
|
|
8
|
+
src/myconfigreader/__init__.py
|
|
9
|
+
src/myconfigreader/config_reader.py
|
|
10
|
+
tests/test.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
myconfigreader
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .config_reader import ConfigReader
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# ConfigReader 클래스 정의는 동일하게 유지
|
|
2
|
+
import configparser
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import List, Dict
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import inspect
|
|
7
|
+
|
|
8
|
+
def get_config_path(config_file_name: str, caller_path=None) -> Path:
|
|
9
|
+
"""
|
|
10
|
+
현재 파일 기준으로 상위 디렉토리를 탐색하여 'credentials' 폴더를 찾고,
|
|
11
|
+
해당 폴더 아래의 config 파일 경로를 반환합니다.
|
|
12
|
+
|
|
13
|
+
Raises:
|
|
14
|
+
FileNotFoundError: credentials 폴더를 찾지 못한 경우
|
|
15
|
+
"""
|
|
16
|
+
# 호출자 파일의 경로를 기준으로 시작
|
|
17
|
+
if caller_path is None:
|
|
18
|
+
current = Path.cwd() # 현재 작업 디렉토리
|
|
19
|
+
else:
|
|
20
|
+
current = caller_path
|
|
21
|
+
|
|
22
|
+
while current != current.parent: # 루트까지 도달하지 않은 동안
|
|
23
|
+
cred_path = current / "credentials"
|
|
24
|
+
if cred_path.exists() and cred_path.is_dir():
|
|
25
|
+
return cred_path / config_file_name
|
|
26
|
+
current = current.parent
|
|
27
|
+
|
|
28
|
+
raise FileNotFoundError("상위 디렉토리에 'credentials' 폴더를 찾을 수 없습니다.")
|
|
29
|
+
|
|
30
|
+
class _ConfigReader:
|
|
31
|
+
"""
|
|
32
|
+
ConfigReader 클래스는 설정 파일에서 값을 읽어오는 기능을 제공합니다.
|
|
33
|
+
항상 credentials/config.ini 파일을 참조합니다.
|
|
34
|
+
기본 섹션은 'DEFAULT'입니다.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, config_file_name: str = 'config.ini', caller_path: Path = None) -> None:
|
|
38
|
+
self.config = configparser.ConfigParser()
|
|
39
|
+
# config_path = Path(__file__).resolve().parent.parent / f"credentials/{config_file_name}"
|
|
40
|
+
config_path = get_config_path(config_file_name=config_file_name, caller_path=caller_path)
|
|
41
|
+
# print(f"설정 파일 경로: {config_path}") # 경로 확인용
|
|
42
|
+
if not config_path.exists():
|
|
43
|
+
raise FileNotFoundError(f"설정 파일 '{config_path}'을(를) 찾을 수 없습니다.")
|
|
44
|
+
self.config.read(config_path, encoding='utf-8')
|
|
45
|
+
self._current_section_name: str = "DEFAULT" # 현재 섹션 이름을 저장
|
|
46
|
+
self.set_section('DEFAULT') # 초기화 시 DEFAULT 섹션으로 설정
|
|
47
|
+
|
|
48
|
+
def set_section(self, section_name: str = "DEFAULT") -> None:
|
|
49
|
+
"""
|
|
50
|
+
섹션을 설정합니다.
|
|
51
|
+
"""
|
|
52
|
+
if section_name not in self.config:
|
|
53
|
+
raise KeyError(f"'{section_name}' 섹션이 config.ini 파일에 없습니다.")
|
|
54
|
+
self._current_section_name = section_name # 현재 섹션 이름 업데이트
|
|
55
|
+
self.section = self.config[section_name]
|
|
56
|
+
|
|
57
|
+
def get_section(self, section_name: str = None) -> Dict[str, str]: # 반환 타입을 dict로 변경
|
|
58
|
+
"""
|
|
59
|
+
섹션을 반환합니다.
|
|
60
|
+
지정된 섹션이 없으면 현재 설정된 섹션을 반환합니다.
|
|
61
|
+
"""
|
|
62
|
+
if section_name is None:
|
|
63
|
+
return self.section # 이미 set_section에서 설정된 self.section 반환
|
|
64
|
+
try:
|
|
65
|
+
return self.config[section_name]
|
|
66
|
+
except KeyError:
|
|
67
|
+
raise KeyError(f"'{section_name}' 섹션이 config.ini 파일에 없습니다.")
|
|
68
|
+
|
|
69
|
+
def get_value(self, key: str, default_value=None) -> str:
|
|
70
|
+
"""
|
|
71
|
+
지정된 키에 대한 값을 반환합니다.
|
|
72
|
+
"""
|
|
73
|
+
if not self.section: # set_section이 호출되지 않았을 경우를 대비
|
|
74
|
+
self.set_section('DEFAULT')
|
|
75
|
+
try:
|
|
76
|
+
return self.section[key]
|
|
77
|
+
except KeyError:
|
|
78
|
+
# 더 명확한 에러 메시지
|
|
79
|
+
print(f"'{key}' 키가 '{self._current_section_name}' 섹션에 없습니다.")
|
|
80
|
+
return default_value
|
|
81
|
+
class ConfigReader_manipulation(_ConfigReader):
|
|
82
|
+
def items(self) -> List[tuple]:
|
|
83
|
+
"""
|
|
84
|
+
현재 섹션의 모든 키-값 쌍을 반환합니다.
|
|
85
|
+
"""
|
|
86
|
+
if not self.section: # set_section이 호출되지 않았을 경우를 대비
|
|
87
|
+
self.set_section('DEFAULT')
|
|
88
|
+
return list(self.section.items())
|
|
89
|
+
|
|
90
|
+
class ConfigReader(ConfigReader_manipulation):
|
|
91
|
+
"""
|
|
92
|
+
ConfigReader 클래스는 _ConfigReader 클래스를 상속받아
|
|
93
|
+
설정 파일에서 값을 읽어오는 기능을 제공합니다.
|
|
94
|
+
기본 섹션은 'DEFAULT'입니다.
|
|
95
|
+
"""
|
|
96
|
+
def __init__(self, config_file_name: str = 'config.ini'):
|
|
97
|
+
caller_frame = inspect.stack()[1]
|
|
98
|
+
caller_path = Path(caller_frame.filename).resolve().parent
|
|
99
|
+
super().__init__(config_file_name=config_file_name, caller_path=caller_path)
|
|
100
|
+
# 추가적인 초기화가 필요할 경우 여기에 작성
|