Skip to content

Commit

Permalink
Merge pull request #5 from rams3sh/poetry
Browse files Browse the repository at this point in the history
Changed github action runner and migrated to poetry for package management
  • Loading branch information
rams3sh committed Mar 13, 2024
2 parents 5aede7a + 365f04a commit a4e6fac
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 48 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/publish-to-test-pypi-and-pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@ on:
jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to TestPyPI and PyPI
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Set up Python 3.7
uses: actions/setup-python@v1
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.10

- name: Install pypa/build
run: >-
python -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
- name: Publish distribution 📦 to Test PyPI
# Any release with tags need not be published to Test PYPI , as it would have already been published and may lead to error.
if: startsWith(github.ref, 'refs/tags') != true
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/

- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

68 changes: 42 additions & 26 deletions botocache/botocache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,69 @@
logger = logging.getLogger(__name__)


def botocache_context(cache=None, action_regex_to_cache=["List.*", "Get.*", "Describe.*"],
call_log=False,
supress_warning_message=False):
def botocache_context(
cache=None,
action_regex_to_cache=["List.*", "Get.*", "Describe.*"],
call_log=False,
supress_warning_message=False,
):

if not (isinstance(action_regex_to_cache, list)):
action_regex_to_cache = [action_regex_to_cache]

class BotoCache(BaseClient):

def return_cache_key(self, operation_name, api_params):
cache_key = \
"{access_key}_{service}_{action}_{region}_{api_params}".format(
# Access Key to identify the Principal
access_key=self._request_signer._credentials.access_key,
# Service for identifying which service is being queried
service=self._service_model.service_name,
# Action of the service
action=operation_name,
# Region where the call is being made
region=self.meta.region_name,
# Api Parameters. This takes care of pagination token, marker and other params.
# The API Params dictionary is sorted before hashing
api_params=str(OrderedDict(sorted(api_params.items()))))
cache_key = "{access_key}_{service}_{action}_{region}_{api_params}".format(
# Access Key to identify the Principal
access_key=self._request_signer._credentials.access_key,
# Service for identifying which service is being queried
service=self._service_model.service_name,
# Action of the service
action=operation_name,
# Region where the call is being made
region=self.meta.region_name,
# Api Parameters. This takes care of pagination token, marker and other params.
# The API Params dictionary is sorted before hashing
api_params=str(OrderedDict(sorted(api_params.items()))),
)
hash_gen = hashlib.sha256()
hash_gen.update(cache_key.encode("utf-8"))
return hash_gen.hexdigest()

def _make_api_call(self, operation_name, api_params):
if call_log:
logger.info("API Call Logger: Region - {region}, "
"Service - {service}, "
"Action - {action}, "
"API Params - {api_params}".format(region=self.meta.region_name,
service=self._service_model.service_name,
action=operation_name, api_params=str(api_params)))
if any([bool(re.match(regex, operation_name)) for regex in action_regex_to_cache]):
logger.info(
"API Call Logger: Region - {region}, "
"Service - {service}, "
"Action - {action}, "
"API Params - {api_params}".format(
region=self.meta.region_name,
service=self._service_model.service_name,
action=operation_name,
api_params=str(api_params),
)
)
if any(
[
bool(re.match(regex, operation_name))
for regex in action_regex_to_cache
]
):
try:
return self._make_cached_api_call(operation_name, api_params)
except Exception as e:
# In case of any errors with caching , normal make api will be called
if not supress_warning_message:
logger.error("Error encountered : {}. Retrying the same call without cached context.".format(e))
logger.error(
"Error encountered : {}. Retrying the same call without cached context.".format(
e
)
)
return super()._make_api_call(operation_name, api_params)

@cached(cache=cache, key=return_cache_key)
def _make_cached_api_call(self, operation_name, api_params):
return super()._make_api_call(operation_name, api_params)

return patch('botocore.client.BaseClient', new=BotoCache)

return patch("botocore.client.BaseClient", new=BotoCache)
115 changes: 115 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "botocache"
version = "0.0.6"
description = "Caching for Boto and Boto3 SDK"
authors = ["rams3sh"]
readme = "README.md"
include=['botocache']

[tool.poetry.dependencies]
python = ">=3.8"
botocore = "^1.34.61"
cachetools = "5.3.3"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

14 changes: 0 additions & 14 deletions setup.py

This file was deleted.

0 comments on commit a4e6fac

Please sign in to comment.