Skip to content

Commit

Permalink
fix: preserve hyperlinks with hyphens (#1140)
Browse files Browse the repository at this point in the history
The default behaviour of textwrap.wrap is to break text on hyphens. This PR sets the break_on_hyphens parameter of textwrap.wrap to False in order to preserve hyperlinks with hyphens.

Fixes #1131
  • Loading branch information
parthea committed Jan 25, 2022
1 parent fe57eb2 commit b091bfc
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 20 deletions.
6 changes: 6 additions & 0 deletions gapic/utils/lines.py
Expand Up @@ -78,9 +78,12 @@ def wrap(text: str, width: int, *, offset: int = None, indent: int = 0) -> str:
# Break off the first line of the string to address non-zero offsets.
first = text.split('\n')[0] + '\n'
if len(first) > width - offset:
# Ensure `break_on_hyphens` is set to `False` when using
# `textwrap.wrap` to avoid breaking hyperlinks with hyphens.
initial = textwrap.wrap(first,
break_long_words=False,
width=width - offset,
break_on_hyphens=False,
)
# Strip the first \n from the text so it is not misidentified as an
# intentionally short line below.
Expand All @@ -107,11 +110,14 @@ def wrap(text: str, width: int, *, offset: int = None, indent: int = 0) -> str:
# Wrap the remainder of the string at the desired width.
return '{first}{text}'.format(
first=first,
# Ensure `break_on_hyphens` is set to `False` when using
# `textwrap.fill` to avoid breaking hyperlinks with hyphens.
text='\n'.join([textwrap.fill(
break_long_words=False,
initial_indent=' ' * indent,
subsequent_indent=' ' * indent,
text=token,
width=width,
break_on_hyphens=False,
) for token in tokens]),
).rstrip('\n')
Expand Up @@ -48,8 +48,8 @@ class IAMCredentialsAsyncClient:
Service account credentials are used to temporarily assume the
identity of the service account. Supported credential types
include OAuth 2.0 access tokens, OpenID Connect ID tokens, self-
signed JSON Web Tokens (JWTs), and more.
include OAuth 2.0 access tokens, OpenID Connect ID tokens,
self-signed JSON Web Tokens (JWTs), and more.
"""

_client: IAMCredentialsClient
Expand Down
Expand Up @@ -83,8 +83,8 @@ class IAMCredentialsClient(metaclass=IAMCredentialsClientMeta):
Service account credentials are used to temporarily assume the
identity of the service account. Supported credential types
include OAuth 2.0 access tokens, OpenID Connect ID tokens, self-
signed JSON Web Tokens (JWTs), and more.
include OAuth 2.0 access tokens, OpenID Connect ID tokens,
self-signed JSON Web Tokens (JWTs), and more.
"""

@staticmethod
Expand Down
Expand Up @@ -39,8 +39,8 @@ class IAMCredentialsGrpcTransport(IAMCredentialsTransport):
Service account credentials are used to temporarily assume the
identity of the service account. Supported credential types
include OAuth 2.0 access tokens, OpenID Connect ID tokens, self-
signed JSON Web Tokens (JWTs), and more.
include OAuth 2.0 access tokens, OpenID Connect ID tokens,
self-signed JSON Web Tokens (JWTs), and more.
This class defines the same methods as the primary client, so the
primary client can load the underlying transport implementation
Expand Down
Expand Up @@ -40,8 +40,8 @@ class IAMCredentialsGrpcAsyncIOTransport(IAMCredentialsTransport):
Service account credentials are used to temporarily assume the
identity of the service account. Supported credential types
include OAuth 2.0 access tokens, OpenID Connect ID tokens, self-
signed JSON Web Tokens (JWTs), and more.
include OAuth 2.0 access tokens, OpenID Connect ID tokens,
self-signed JSON Web Tokens (JWTs), and more.
This class defines the same methods as the primary client, so the
primary client can load the underlying transport implementation
Expand Down
Expand Up @@ -2418,8 +2418,8 @@ def sample_get_cmek_settings():
The request object. The parameters to
[GetCmekSettings][google.logging.v2.ConfigServiceV2.GetCmekSettings].
See [Enabling CMEK for Logs
Router](https://cloud.google.com/logging/docs/routing/managed-
encryption) for more information.
Router](https://cloud.google.com/logging/docs/routing/managed-encryption)
for more information.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand Down Expand Up @@ -2520,8 +2520,8 @@ def sample_update_cmek_settings():
The request object. The parameters to
[UpdateCmekSettings][google.logging.v2.ConfigServiceV2.UpdateCmekSettings].
See [Enabling CMEK for Logs
Router](https://cloud.google.com/logging/docs/routing/managed-
encryption) for more information.
Router](https://cloud.google.com/logging/docs/routing/managed-encryption)
for more information.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand Down
Expand Up @@ -2616,8 +2616,8 @@ def sample_get_cmek_settings():
The request object. The parameters to
[GetCmekSettings][google.logging.v2.ConfigServiceV2.GetCmekSettings].
See [Enabling CMEK for Logs
Router](https://cloud.google.com/logging/docs/routing/managed-
encryption) for more information.
Router](https://cloud.google.com/logging/docs/routing/managed-encryption)
for more information.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand Down Expand Up @@ -2720,8 +2720,8 @@ def sample_update_cmek_settings():
The request object. The parameters to
[UpdateCmekSettings][google.logging.v2.ConfigServiceV2.UpdateCmekSettings].
See [Enabling CMEK for Logs
Router](https://cloud.google.com/logging/docs/routing/managed-
encryption) for more information.
Router](https://cloud.google.com/logging/docs/routing/managed-encryption)
for more information.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
Expand Down
Expand Up @@ -144,8 +144,7 @@ class LogView(proto.Message):
name (str):
The resource name of the view.
For example
"projects/my-project-id/locations/my-
location/buckets/my-bucket-id/views/my-view
"projects/my-project-id/locations/my-location/buckets/my-bucket-id/views/my-view
description (str):
Describes this view.
create_time (google.protobuf.timestamp_pb2.Timestamp):
Expand Down
Expand Up @@ -95,8 +95,8 @@ class Instance(proto.Message):
addresses that are reserved for this instance.
If not provided, the service will choose an
unused /29 block, for example, 10.0.0.0/29 or
192.168.0.0/29. Ranges must be unique and non-
overlapping with existing subnets in an
192.168.0.0/29. Ranges must be unique and
non-overlapping with existing subnets in an
authorized network.
host (str):
Output only. Hostname or IP address of the
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/utils/test_lines.py
Expand Up @@ -87,3 +87,7 @@ def test_wrap_indent_short():

def test_wrap_short_line_preserved():
assert lines.wrap('foo\nbar\nbaz', width=80) == 'foo\nbar\nbaz'


def test_wrap_does_not_break_hyphenated_word():
assert lines.wrap('do-not-break', width=5) == 'do-not-break'

0 comments on commit b091bfc

Please sign in to comment.