-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from rlatn1234/auth-impl
계정정보 입력기능 추가
- Loading branch information
Showing
2 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ soupsieve==2.5 | |
tomli==2.0.1 | ||
urllib3==1.26.18 | ||
webencodings==0.5.1 | ||
tomli-w==1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,66 @@ | ||
import os | ||
import logging | ||
import tomli | ||
import tomli_w | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_credentials(profile_name): | ||
try: | ||
credentials = _read_credentials_file(profile_name) | ||
except FileNotFoundError: | ||
print("❌ ~/.dhapi/credentials 파일을 찾을 수 없습니다. 파일을 생성하고 프로필을 추가하시겠습니까? [Y/n] ", end="") | ||
answer = input().strip().lower() | ||
if answer in ["y", "yes", ""]: | ||
print("📝 입력된 프로필 이름을 사용하시겠습니까? [Y/n]", end="") | ||
answer = input().strip().lower() | ||
if answer in ["y", "yes", ""]: | ||
_add_credentials(profile_name) | ||
else: | ||
print("📝 프로필 이름을 입력하세요: ", end="") | ||
profile_name = input().strip() | ||
_add_credentials(profile_name) | ||
else: | ||
raise FileNotFoundError("~/.dhapi/credentials 파일을 찾을 수 없습니다.") | ||
|
||
credentials = _read_credentials_file(profile_name) | ||
|
||
if credentials is None: | ||
print(f"❌'{profile_name}' 프로필을 찾지 못했습니다. 추가하시겠습니까? [Y/n] ", end="") | ||
answer = input().strip().lower() | ||
if answer in ["y", "yes", ""]: | ||
_add_credentials(profile_name) | ||
credentials = _read_credentials_file(profile_name) | ||
return credentials | ||
raise ValueError(f"~/.dhapi/credentials 파일에서 '{profile_name}' 프로필을 찾지 못했습니다.") | ||
|
||
return credentials | ||
|
||
|
||
def _read_credentials_file(profile_name): | ||
with open(os.path.expanduser("~/.dhapi/credentials"), "r", encoding="UTF-8") as f: | ||
file = f.read() | ||
config = tomli.loads(file) | ||
credentials = config.get(profile_name) | ||
return credentials | ||
|
||
|
||
def _add_credentials(profile_name): | ||
print("📝 사용자 ID를 입력하세요: ", end="") | ||
user_id = input().strip() | ||
print("📝 사용자 비밀번호를 입력하세요: ", end="") | ||
user_pw = input().strip() | ||
|
||
doc = {profile_name: {"username": user_id, "password": user_pw}} | ||
|
||
with open(os.path.expanduser("~/.dhapi/credentials"), "r", encoding="UTF-8") as f: | ||
file = f.read() | ||
config = tomli.loads(file) | ||
|
||
credentials = config.get(profile_name) | ||
if credentials is None: | ||
raise ValueError(f"~/.dhapi/credentials 파일에서 '{profile_name}' 프로필을 찾지 못했습니다.") | ||
return config.get(profile_name) | ||
doc.update(config) | ||
|
||
with open(os.path.expanduser("~/.dhapi/credentials"), "wb") as f: | ||
tomli_w.dump(doc, f) | ||
f.close() |