|
3 | 3 | import getpass |
4 | 4 | from enum import Enum |
5 | 5 | from pathlib import Path |
6 | | -from typing import List, Optional, Union |
| 6 | +from typing import TYPE_CHECKING, List, Optional, Union |
7 | 7 | from warnings import warn |
8 | 8 |
|
9 | 9 | import yaml |
10 | 10 | from pydantic.v1 import PrivateAttr |
11 | 11 |
|
12 | | -from osw.custom_types import PossibleFilePath |
13 | 12 | from osw.defaults import paths as default_paths |
14 | 13 | from osw.model.static import OswBaseModel |
15 | 14 |
|
| 15 | +if TYPE_CHECKING: |
| 16 | + PossibleFilePath = Path |
| 17 | +else: |
| 18 | + from osw.custom_types import PossibleFilePath |
| 19 | + |
16 | 20 |
|
17 | 21 | class CredentialManager(OswBaseModel): |
18 | 22 | """Handles credentials""" |
@@ -85,6 +89,7 @@ def __init__(self, **data): |
85 | 89 | if self.cred_filepath: |
86 | 90 | if not isinstance(self.cred_filepath, list): |
87 | 91 | self.cred_filepath = [self.cred_filepath] |
| 92 | + self.cred_filepath = [Path(fp) for fp in self.cred_filepath if fp != ""] |
88 | 93 | # Make sure to at least warn the user if they pass cred_filepath instead of |
89 | 94 | # cred_filepath |
90 | 95 | attribute_names = self.__dict__.keys() |
@@ -114,41 +119,40 @@ def get_credential(self, config: CredentialConfig) -> BaseCredential: |
114 | 119 | filepaths = [filepaths] |
115 | 120 |
|
116 | 121 | 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) |
152 | 156 |
|
153 | 157 | match_iri = "" |
154 | 158 | cred = None |
|
0 commit comments