forked from awslabs/s3-connector-for-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor UserAgent setup for extensibility
- Loading branch information
dnnanuti
committed
Feb 29, 2024
1 parent
7ac85ef
commit 01dc22a
Showing
6 changed files
with
84 additions
and
6 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
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# // SPDX-License-Identifier: BSD | ||
from typing import List | ||
|
||
from ._version import __version__ | ||
|
||
# https://www.rfc-editor.org/rfc/rfc9110#name-user-agent | ||
|
||
|
||
class UserAgent: | ||
def __init__(self, comments: List[str] = None): | ||
self._user_agent_prefix = f"{__package__}/{__version__}" | ||
self._comments = comments or [] | ||
self._comments_separator = "; " | ||
|
||
def add_comment(self, comment): | ||
self._comments.append(comment) | ||
|
||
@property | ||
def prefix(self): | ||
comments_str = f"{self._comments_separator.join(self._comments)}" or "" | ||
if comments_str != "": | ||
return f"{self._user_agent_prefix} ({comments_str})" | ||
return self._user_agent_prefix |
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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# // SPDX-License-Identifier: BSD | ||
|
||
from s3torchconnector._version import __version__ | ||
from s3torchconnector._user_agent import UserAgent | ||
|
||
|
||
def test_default_user_agent(): | ||
user_agent = UserAgent() | ||
assert user_agent.prefix == f"s3torchconnector/{__version__}" | ||
|
||
|
||
def test_user_agent_creation_with_empty_comments(): | ||
user_agent = UserAgent([]) | ||
assert user_agent.prefix == f"s3torchconnector/{__version__}" | ||
|
||
|
||
def test_user_agent_creation_with_empty_str_comments(): | ||
user_agent = UserAgent([""]) | ||
assert user_agent.prefix == f"s3torchconnector/{__version__}" | ||
|
||
|
||
def test_user_agent_creation_with_none_comments(): | ||
user_agent = UserAgent(None) | ||
assert user_agent.prefix == f"s3torchconnector/{__version__}" | ||
|
||
|
||
def test_user_agent_creation_with_comments(): | ||
user_agent = UserAgent(["component/version", "metadata"]) | ||
assert ( | ||
user_agent.prefix | ||
== f"s3torchconnector/{__version__} (component/version; metadata)" | ||
) |