Skip to content

Commit 71f885b

Browse files
authored
Merge pull request #1247 from EdmilsonRodrigues/backports
#1247 …() and datetime.datetime.isoformat() #### Description *I tried using backports.datetime_fromisoformat but than the tox tests broke, probably because it required an import in the unit tests. I tried than just using the usual builtin datetime.datetime.fromisoformat and it worked just well.* *<Fixes: >* #### QA Steps *<Commands / tests / steps to run to verify that the change works:>* ``` tox -e py3 -- tests/unit/... ``` ``` tox -e integration -- tests/integration/... ``` All CI tests need to pass. *<Please note that most likely an additional test will be required by the reviewers for any change that's not a one liner to land.>* #### Notes & Discussion *This is a secondary branch from the Pull Request #1243 *
2 parents 1907f7a + 2bbe89b commit 71f885b

File tree

7 files changed

+18
-16
lines changed

7 files changed

+18
-16
lines changed

juju/client/gocookies.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import time
88

9-
import pyrfc3339
9+
from backports.datetime_fromisoformat import datetime_fromisoformat
1010

1111

1212
class GoCookieJar(cookiejar.FileCookieJar):
@@ -52,7 +52,7 @@ def go_to_py_cookie(go_cookie):
5252
"""Convert a Go-style JSON-unmarshaled cookie into a Python cookie"""
5353
expires = None
5454
if go_cookie.get("Expires") is not None:
55-
t = pyrfc3339.parse(go_cookie["Expires"])
55+
t = datetime_fromisoformat(go_cookie["Expires"])
5656
expires = t.timestamp()
5757
return cookiejar.Cookie(
5858
version=0,
@@ -101,8 +101,9 @@ def py_to_go_cookie(py_cookie):
101101
if py_cookie.path_specified:
102102
go_cookie["Path"] = py_cookie.path
103103
if py_cookie.expires is not None:
104-
unix_time = datetime.datetime.fromtimestamp(py_cookie.expires)
105104
# Note: fromtimestamp bizarrely produces a time without
106105
# a time zone, so we need to use accept_naive.
107-
go_cookie["Expires"] = pyrfc3339.generate(unix_time, accept_naive=True)
106+
go_cookie["Expires"] = datetime.datetime.fromtimestamp(
107+
py_cookie.expires
108+
).isoformat()
108109
return go_cookie

juju/machine.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
import typing
88

9-
import pyrfc3339
9+
from backports.datetime_fromisoformat import datetime_fromisoformat
1010

1111
from juju.utils import block_until, juju_ssh_key_paths
1212

@@ -239,7 +239,7 @@ def agent_status(self):
239239
@property
240240
def agent_status_since(self):
241241
"""Get the time when the `agent_status` was last updated."""
242-
return pyrfc3339.parse(self.safe_data["agent-status"]["since"])
242+
return datetime_fromisoformat(self.safe_data["agent-status"]["since"])
243243

244244
@property
245245
def agent_version(self):
@@ -266,7 +266,7 @@ def status_message(self):
266266
@property
267267
def status_since(self):
268268
"""Get the time when the `status` was last updated."""
269-
return pyrfc3339.parse(self.safe_data["instance-status"]["since"])
269+
return datetime_fromisoformat(self.safe_data["instance-status"]["since"])
270270

271271
@property
272272
def dns_name(self):

juju/unit.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
import pyrfc3339
6+
from backports.datetime_fromisoformat import datetime_fromisoformat
77

88
from juju.errors import JujuAPIError, JujuError
99

@@ -27,7 +27,7 @@ def agent_status(self):
2727
@property
2828
def agent_status_since(self):
2929
"""Get the time when the `agent_status` was last updated."""
30-
return pyrfc3339.parse(self.safe_data["agent-status"]["since"])
30+
return datetime_fromisoformat(self.safe_data["agent-status"]["since"])
3131

3232
@property
3333
def is_subordinate(self):
@@ -54,7 +54,7 @@ def workload_status(self):
5454
@property
5555
def workload_status_since(self):
5656
"""Get the time when the `workload_status` was last updated."""
57-
return pyrfc3339.parse(self.safe_data["workload-status"]["since"])
57+
return datetime_fromisoformat(self.safe_data["workload-status"]["since"])
5858

5959
@property
6060
def workload_status_message(self):

juju/user.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
import pyrfc3339
6+
from backports.datetime_fromisoformat import datetime_fromisoformat
77

88
from . import errors, tag
99
from .client import client
@@ -31,7 +31,7 @@ def display_name(self):
3131

3232
@property
3333
def last_connection(self):
34-
return pyrfc3339.parse(self._user_info.last_connection)
34+
return datetime_fromisoformat(self._user_info.last_connection)
3535

3636
@property
3737
def access(self):

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ classifiers = [
2323
]
2424
dependencies = [
2525
"macaroonbakery>=1.1,<2.0",
26-
"pyRFC3339>=1.0,<2.0",
2726
"pyyaml>=5.1.2",
2827
"websockets>=13.0.1",
2928
"paramiko>=2.4.0",
@@ -35,6 +34,7 @@ dependencies = [
3534
"packaging",
3635
"typing-extensions>=4.5.0",
3736
'backports.strenum>=1.3.1; python_version < "3.11"',
37+
"backports-datetime-fromisoformat>=2.0.2",
3838
]
3939
[project.optional-dependencies]
4040
dev = [

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package_data={"juju": ["py.typed"]},
2222
install_requires=[
2323
"macaroonbakery>=1.1,<2.0",
24-
"pyRFC3339>=1.0,<2.0",
2524
"pyyaml>=5.1.2",
2625
"websockets>=13.0.1",
2726
"paramiko>=2.4.0",
@@ -33,6 +32,7 @@
3332
"packaging",
3433
"typing-extensions>=4.5.0",
3534
'backports.strenum>=1.3.1; python_version < "3.11"',
35+
"backports-datetime-fromisoformat>=2.0.2",
3636
],
3737
extras_require={
3838
"dev": [

tests/unit/test_gocookies.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import unittest
99
import urllib.request
1010

11-
import pyrfc3339
11+
from backports.datetime_fromisoformat import datetime_fromisoformat
1212

1313
from juju.client.gocookies import GoCookieJar
1414

1515
# cookie_content holds the JSON contents of a Go-produced
1616
# cookie file (reformatted so it's not all on one line but
1717
# otherwise unchanged).
18+
1819
cookie_content = """
1920
[
2021
{
@@ -223,7 +224,7 @@ def test_expiry_time(self):
223224
]"""
224225
jar = self.load_jar(content)
225226
got_expires = tuple(jar)[0].expires
226-
want_expires = int(pyrfc3339.parse("2345-11-15T18:16:08Z").timestamp())
227+
want_expires = int(datetime_fromisoformat("2345-11-15T18:16:08Z").timestamp())
227228
self.assertEqual(got_expires, want_expires)
228229

229230
def load_jar(self, content):

0 commit comments

Comments
 (0)