Skip to content

Commit

Permalink
chore: introduce Black code formatter (#150)
Browse files Browse the repository at this point in the history
This commit adds the [Black](https://github.com/psf/black) code formatter to the project
and updates the `Makefile` to use it. There are 2 targets that run the `black` tool:
- `make lint`: this will check the code with both `pylint` and `black`
- `make lint-fix`: this will reformat the code using `black`.

Initial code formatting is also included.

Signed-off-by: Norbert Biczo <[email protected]>
  • Loading branch information
pyrooka authored Nov 17, 2022
1 parent b0dfd8e commit a86cdab
Show file tree
Hide file tree
Showing 39 changed files with 779 additions and 876 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ If you want to contribute to the repository, here's a quick guide:
* Check for unnecessary whitespace with `git diff --check` before committing.
* Make sure your code supports Python 3.7, 3.8, 3.9 and 3.10. You can use `pyenv` and `tox` for this
1. Make the test pass
* Linting errors can be fixed by running `make lint-fix` in most cases
1. Check code coverage. Add tests for all new functionality and ensure overall coverage does not decrease.
1. Commit your changes
* Commits should follow the [Angular commit message guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines). This is because our release tool uses this format for determining release versions and generating changelogs. To make this easier, we recommend using the [Commitizen CLI](https://github.com/commitizen/cz-cli) with the `cz-conventional-changelog` adapter.
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ test-unit:
python -m pytest --cov=ibm_cloud_sdk_core test

lint:
./pylint.sh
./pylint.sh && black --check .

lint-fix:
black .
2 changes: 1 addition & 1 deletion ibm_cloud_sdk_core/api_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __str__(self) -> str:
msg = 'Error: ' + str(self.message) + ', Code: ' + str(self.code)
if self.global_transaction_id is not None:
msg += ' , X-global-transaction-id: ' + str(self.global_transaction_id)
return msg
return msg

@staticmethod
def _get_error_message(response: Response) -> str:
Expand Down
9 changes: 4 additions & 5 deletions ibm_cloud_sdk_core/authenticators/basic_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ def validate(self) -> None:
if self.username is None or self.password is None:
raise ValueError('The username and password shouldn\'t be None.')

if has_bad_first_or_last_char(
self.username) or has_bad_first_or_last_char(self.password):
if has_bad_first_or_last_char(self.username) or has_bad_first_or_last_char(self.password):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')
'Please remove any surrounding {, }, or \" characters.'
)

def __construct_basic_auth_header(self) -> str:
authstring = "{0}:{1}".format(self.username, self.password)
base64_authorization = base64.b64encode(
authstring.encode('utf-8')).decode('utf-8')
base64_authorization = base64.b64encode(authstring.encode('utf-8')).decode('utf-8')
return 'Basic {0}'.format(base64_authorization)

def authenticate(self, req: Request) -> None:
Expand Down
41 changes: 25 additions & 16 deletions ibm_cloud_sdk_core/authenticators/container_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,35 @@ class ContainerAuthenticator(IAMRequestBasedAuthenticator):
or client_id, and/or client_secret are not valid for IAM token requests.
"""

def __init__(self,
cr_token_filename: Optional[str] = None,
iam_profile_name: Optional[str] = None,
iam_profile_id: Optional[str] = None,
url: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
disable_ssl_verification: bool = False,
scope: Optional[str] = None,
proxies: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None) -> None:
def __init__(
self,
cr_token_filename: Optional[str] = None,
iam_profile_name: Optional[str] = None,
iam_profile_id: Optional[str] = None,
url: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
disable_ssl_verification: bool = False,
scope: Optional[str] = None,
proxies: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
) -> None:
# Check the type of `disable_ssl_verification`. Must be a bool.
if not isinstance(disable_ssl_verification, bool):
raise TypeError('disable_ssl_verification must be a bool')

self.token_manager = ContainerTokenManager(
cr_token_filename=cr_token_filename, iam_profile_name=iam_profile_name, iam_profile_id=iam_profile_id,
url=url, client_id=client_id, client_secret=client_secret,
disable_ssl_verification=disable_ssl_verification, scope=scope, proxies=proxies, headers=headers)
cr_token_filename=cr_token_filename,
iam_profile_name=iam_profile_name,
iam_profile_id=iam_profile_id,
url=url,
client_id=client_id,
client_secret=client_secret,
disable_ssl_verification=disable_ssl_verification,
scope=scope,
proxies=proxies,
headers=headers,
)

self.validate()

Expand All @@ -103,8 +113,7 @@ def validate(self) -> None:
super().validate()

if not self.token_manager.iam_profile_name and not self.token_manager.iam_profile_id:
raise ValueError(
'At least one of iam_profile_name or iam_profile_id must be specified.')
raise ValueError('At least one of iam_profile_name or iam_profile_id must be specified.')

def set_cr_token_filename(self, cr_token_filename: str) -> None:
"""Set the location of the compute resource token on the local filesystem.
Expand Down
52 changes: 32 additions & 20 deletions ibm_cloud_sdk_core/authenticators/cp4d_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,32 @@ class CloudPakForDataAuthenticator(Authenticator):
ValueError: The username, password/apikey, and/or url are not valid for CP4D token requests.
"""

def __init__(self,
username: str = None,
password: str = None,
url: str = None,
*,
apikey: str = None,
disable_ssl_verification: bool = False,
headers: Optional[Dict[str, str]] = None,
proxies: Optional[Dict[str, str]] = None,
verify: Optional[str] = None) -> None:
def __init__(
self,
username: str = None,
password: str = None,
url: str = None,
*,
apikey: str = None,
disable_ssl_verification: bool = False,
headers: Optional[Dict[str, str]] = None,
proxies: Optional[Dict[str, str]] = None,
verify: Optional[str] = None
) -> None:
# Check the type of `disable_ssl_verification`. Must be a bool.
if not isinstance(disable_ssl_verification, bool):
raise TypeError('disable_ssl_verification must be a bool')

self.token_manager = CP4DTokenManager(
username=username, password=password, apikey=apikey, url=url,
disable_ssl_verification=disable_ssl_verification, headers=headers, proxies=proxies, verify=verify)
username=username,
password=password,
apikey=apikey,
url=url,
disable_ssl_verification=disable_ssl_verification,
headers=headers,
proxies=proxies,
verify=verify,
)

self.validate()

Expand All @@ -88,24 +97,27 @@ def validate(self) -> None:
if self.token_manager.username is None:
raise ValueError('The username shouldn\'t be None.')

if ((self.token_manager.password is None and self.token_manager.apikey is None)
or (self.token_manager.password is not None and self.token_manager.apikey is not None)):
raise ValueError(
'Exactly one of `apikey` or `password` must be specified.')
if (self.token_manager.password is None and self.token_manager.apikey is None) or (
self.token_manager.password is not None and self.token_manager.apikey is not None
):
raise ValueError('Exactly one of `apikey` or `password` must be specified.')

if self.token_manager.url is None:
raise ValueError('The url shouldn\'t be None.')

if has_bad_first_or_last_char(
self.token_manager.username) or has_bad_first_or_last_char(self.token_manager.password):
if has_bad_first_or_last_char(self.token_manager.username) or has_bad_first_or_last_char(
self.token_manager.password
):
raise ValueError(
'The username and password shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')
'Please remove any surrounding {, }, or \" characters.'
)

if has_bad_first_or_last_char(self.token_manager.url):
raise ValueError(
'The url shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')
'Please remove any surrounding {, }, or \" characters.'
)

def authenticate(self, req: Request) -> None:
"""Adds CP4D authentication information to the request.
Expand Down
35 changes: 22 additions & 13 deletions ibm_cloud_sdk_core/authenticators/iam_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,32 @@ class IAMAuthenticator(IAMRequestBasedAuthenticator):
ValueError: The apikey, client_id, and/or client_secret are not valid for IAM token requests.
"""

def __init__(self,
apikey: str,
*,
url: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
disable_ssl_verification: bool = False,
headers: Optional[Dict[str, str]] = None,
proxies: Optional[Dict[str, str]] = None,
scope: Optional[str] = None) -> None:
def __init__(
self,
apikey: str,
*,
url: Optional[str] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
disable_ssl_verification: bool = False,
headers: Optional[Dict[str, str]] = None,
proxies: Optional[Dict[str, str]] = None,
scope: Optional[str] = None
) -> None:
# Check the type of `disable_ssl_verification`. Must be a bool.
if not isinstance(disable_ssl_verification, bool):
raise TypeError('disable_ssl_verification must be a bool')

self.token_manager = IAMTokenManager(
apikey, url=url, client_id=client_id, client_secret=client_secret,
apikey,
url=url,
client_id=client_id,
client_secret=client_secret,
disable_ssl_verification=disable_ssl_verification,
headers=headers, proxies=proxies, scope=scope)
headers=headers,
proxies=proxies,
scope=scope,
)

self.validate()

Expand All @@ -98,4 +106,5 @@ def validate(self) -> None:
if has_bad_first_or_last_char(self.token_manager.apikey):
raise ValueError(
'The apikey shouldn\'t start or end with curly brackets or quotes. '
'Please remove any surrounding {, }, or \" characters.')
'Please remove any surrounding {, }, or \" characters.'
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ def validate(self) -> None:
Raises:
ValueError: The client_id, and/or client_secret are not valid for IAM token requests.
"""
if (self.token_manager.client_id and
not self.token_manager.client_secret) or (
not self.token_manager.client_id and
self.token_manager.client_secret):
raise ValueError(
'Both client_id and client_secret should be initialized.')
if (self.token_manager.client_id and not self.token_manager.client_secret) or (
not self.token_manager.client_id and self.token_manager.client_secret
):
raise ValueError('Both client_id and client_secret should be initialized.')

def authenticate(self, req: Request) -> None:
"""Adds IAM authentication information to the request.
Expand Down
14 changes: 6 additions & 8 deletions ibm_cloud_sdk_core/authenticators/vpc_instance_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ class VPCInstanceAuthenticator(Authenticator):

DEFAULT_IMS_ENDPOINT = 'http://169.254.169.254'

def __init__(self,
iam_profile_crn: Optional[str] = None,
iam_profile_id: Optional[str] = None,
url: Optional[str] = None) -> None:
def __init__(
self, iam_profile_crn: Optional[str] = None, iam_profile_id: Optional[str] = None, url: Optional[str] = None
) -> None:

if not url:
url = self.DEFAULT_IMS_ENDPOINT

self.token_manager = VPCInstanceTokenManager(
url=url, iam_profile_crn=iam_profile_crn, iam_profile_id=iam_profile_id)
url=url, iam_profile_crn=iam_profile_crn, iam_profile_id=iam_profile_id
)

self.validate()

Expand All @@ -74,8 +74,7 @@ def validate(self) -> None:
super().validate()

if self.token_manager.iam_profile_crn and self.token_manager.iam_profile_id:
raise ValueError(
'At most one of "iam_profile_id" or "iam_profile_crn" may be specified.')
raise ValueError('At most one of "iam_profile_id" or "iam_profile_crn" may be specified.')

def authenticate(self, req: Request) -> None:
"""Adds IAM authentication information to the request.
Expand All @@ -92,7 +91,6 @@ def authenticate(self, req: Request) -> None:
bearer_token = self.token_manager.get_token()
headers['Authorization'] = 'Bearer {0}'.format(bearer_token)


def set_iam_profile_crn(self, iam_profile_crn: str) -> None:
"""Sets CRN of the IAM profile.
Expand Down
Loading

0 comments on commit a86cdab

Please sign in to comment.