Skip to content

Commit

Permalink
Merge pull request #389 from Backblaze/remove_arrow_dependency
Browse files Browse the repository at this point in the history
Remove arrow dependency
  • Loading branch information
ppolewicz committed Apr 17, 2023
2 parents afa22c3 + dffe06b commit 967db6c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* Add support for custom upload timestamp

### Infrastructure
* Remove dependency from `arrow`

## [1.20.0] - 2023-03-23

### Added
Expand Down
9 changes: 0 additions & 9 deletions b2sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ def filter(self, record):
import b2sdk.version
__version__ = b2sdk.version.VERSION
assert __version__ # PEP-0396

# https://github.com/crsmithdev/arrow/issues/612 - To get rid of the ArrowParseWarning messages in 0.14.3 onward.
try:
from arrow.factory import ArrowParseWarning
except ImportError:
pass
else:
import warnings
warnings.simplefilter("ignore", ArrowParseWarning)
27 changes: 21 additions & 6 deletions b2sdk/b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
######################################################################

from random import random
from contextlib import contextmanager
import datetime
import io
import json
import locale
import logging
import socket
import threading

import arrow
import requests
from requests.adapters import HTTPAdapter
import time
Expand All @@ -30,6 +33,7 @@
from .requests import NotDecompressingResponse
from .version import USER_AGENT

LOCALE_LOCK = threading.Lock()
logger = logging.getLogger(__name__)


Expand All @@ -47,6 +51,16 @@ def _print_exception(e, indent=''):
_print_exception(a, indent + ' ')


@contextmanager
def setlocale(name):
with LOCALE_LOCK:
saved = locale.setlocale(locale.LC_ALL)
try:
yield locale.setlocale(locale.LC_ALL, name)
finally:
locale.setlocale(locale.LC_ALL, saved)


class ResponseContextManager:
"""
A context manager that closes a requests.Response when done.
Expand Down Expand Up @@ -114,15 +128,16 @@ def post_request(self, method, url, headers, response):

# Convert the server time to a datetime object
try:
server_time = arrow.get(
server_date_str, 'ddd, DD MMM YYYY HH:mm:ss ZZZ'
) # this, unlike datetime.datetime.strptime, always uses English locale
except arrow.parser.ParserError:
with setlocale("C"):
server_time = datetime.datetime.strptime(
server_date_str, '%a, %d %b %Y %H:%M:%S %Z'
)
except ValueError:
logger.exception('server returned date in an inappropriate format')
raise BadDateFormat(server_date_str)

# Get the local time
local_time = arrow.utcnow()
local_time = datetime.datetime.utcnow()

# Check the difference.
max_allowed = 10 * 60 # ten minutes, in seconds
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
arrow>=1.0.2,<2.0.0
importlib-metadata>=3.3.0; python_version < '3.8'
logfury>=1.0.1,<2.0.0
requests>=2.9.1,<3.0.0
Expand Down
19 changes: 19 additions & 0 deletions test/unit/b2http/test_b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import datetime
import requests
import socket
import locale

from ..test_base import TestBase

Expand All @@ -20,6 +21,7 @@
from apiver_deps import B2Http
from apiver_deps import B2HttpApiConfig
from apiver_deps import ClockSkewHook
from b2sdk.b2http import setlocale

from unittest.mock import call, MagicMock, patch

Expand Down Expand Up @@ -294,6 +296,23 @@ class TestB2HttpUserAgentAppend(TestB2Http):
}


class TestSetLocaleContextManager(TestBase):
def test_set_locale_context_manager(self):
test_locale = locale.normalize(
'C.utf8'
) # C.UTF-8 on Ubuntu 18.04 Bionic, C.utf8 on Ubuntu 22.04 Jammy
other_locale = 'C'

saved = locale.setlocale(locale.LC_ALL)
if saved == test_locale:
test_locale, other_locale = other_locale, test_locale

locale.setlocale(locale.LC_ALL, other_locale)
with setlocale(test_locale):
assert locale.setlocale(category=locale.LC_ALL) == test_locale
locale.setlocale(locale.LC_ALL, saved)


class TestClockSkewHook(TestBase):
def test_bad_format(self):
response = MagicMock()
Expand Down

0 comments on commit 967db6c

Please sign in to comment.