Skip to content

Commit 43df7e8

Browse files
committed
Catching non-existing credential files
1 parent afcd3a5 commit 43df7e8

1 file changed

Lines changed: 41 additions & 37 deletions

File tree

‎src/osw/auth.py‎

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import getpass
44
from enum import Enum
55
from pathlib import Path
6-
from typing import List, Optional, Union
6+
from typing import TYPE_CHECKING, List, Optional, Union
77
from warnings import warn
88

99
import yaml
1010
from pydantic.v1 import PrivateAttr
1111

12-
from osw.custom_types import PossibleFilePath
1312
from osw.defaults import paths as default_paths
1413
from osw.model.static import OswBaseModel
1514

15+
if TYPE_CHECKING:
16+
PossibleFilePath = Path
17+
else:
18+
from osw.custom_types import PossibleFilePath
19+
1620

1721
class CredentialManager(OswBaseModel):
1822
"""Handles credentials"""
@@ -85,6 +89,7 @@ def __init__(self, **data):
8589
if self.cred_filepath:
8690
if not isinstance(self.cred_filepath, list):
8791
self.cred_filepath = [self.cred_filepath]
92+
self.cred_filepath = [Path(fp) for fp in self.cred_filepath if fp != ""]
8893
# Make sure to at least warn the user if they pass cred_filepath instead of
8994
# cred_filepath
9095
attribute_names = self.__dict__.keys()
@@ -114,41 +119,40 @@ def get_credential(self, config: CredentialConfig) -> BaseCredential:
114119
filepaths = [filepaths]
115120

116121
for filepath in filepaths:
117-
if filepath != "":
118-
with open(filepath, "r", encoding="utf-8") as stream:
119-
try:
120-
accounts = yaml.safe_load(stream)
121-
if accounts is None: # Catch empty file
122-
continue
123-
for iri in accounts.keys():
124-
if (
125-
"username" in accounts[iri]
126-
and "password" in accounts[iri]
127-
):
128-
cred = CredentialManager.UserPwdCredential(
129-
username=accounts[iri]["username"],
130-
password=accounts[iri]["password"],
131-
iri=iri,
132-
)
133-
_file_credentials.append(cred)
134-
if (
135-
"consumer_token" in accounts[iri]
136-
and "consumer_secret" in accounts[iri]
137-
and "access_token" in accounts[iri]
138-
and "access_secret" in accounts[iri]
139-
):
140-
cred = CredentialManager.OAuth1Credential(
141-
consumer_token=accounts[iri]["consumer_token"],
142-
consumer_secret=accounts[iri][
143-
"consumer_secret"
144-
],
145-
access_token=accounts[iri]["access_token"],
146-
access_secret=accounts[iri]["access_secret"],
147-
iri=iri,
148-
)
149-
_file_credentials.append(cred)
150-
except yaml.YAMLError as exc:
151-
print(exc)
122+
if not filepath.exists():
123+
continue
124+
with open(filepath, "r", encoding="utf-8") as stream:
125+
try:
126+
accounts = yaml.safe_load(stream)
127+
if accounts is None: # Catch empty file
128+
continue
129+
for iri in accounts.keys():
130+
if (
131+
"username" in accounts[iri]
132+
and "password" in accounts[iri]
133+
):
134+
cred = CredentialManager.UserPwdCredential(
135+
username=accounts[iri]["username"],
136+
password=accounts[iri]["password"],
137+
iri=iri,
138+
)
139+
_file_credentials.append(cred)
140+
if (
141+
"consumer_token" in accounts[iri]
142+
and "consumer_secret" in accounts[iri]
143+
and "access_token" in accounts[iri]
144+
and "access_secret" in accounts[iri]
145+
):
146+
cred = CredentialManager.OAuth1Credential(
147+
consumer_token=accounts[iri]["consumer_token"],
148+
consumer_secret=accounts[iri]["consumer_secret"],
149+
access_token=accounts[iri]["access_token"],
150+
access_secret=accounts[iri]["access_secret"],
151+
iri=iri,
152+
)
153+
_file_credentials.append(cred)
154+
except yaml.YAMLError as exc:
155+
print(exc)
152156

153157
match_iri = ""
154158
cred = None

0 commit comments

Comments
 (0)