Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove pyrfc3339 and change to datetime.datetime.fromisoformat… #1247

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions juju/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Copyright 2024 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.
"""Python Library for Juju."""

try:
from backports.datetime_fromisoformat import MonkeyPatch

MonkeyPatch.patch_fromisoformat()
except ImportError:
pass
9 changes: 4 additions & 5 deletions juju/client/gocookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import json
import time

import pyrfc3339


class GoCookieJar(cookiejar.FileCookieJar):
"""A CookieJar implementation that reads and writes cookies
Expand Down Expand Up @@ -52,7 +50,7 @@ def go_to_py_cookie(go_cookie):
"""Convert a Go-style JSON-unmarshaled cookie into a Python cookie"""
expires = None
if go_cookie.get("Expires") is not None:
t = pyrfc3339.parse(go_cookie["Expires"])
t = datetime.datetime.fromisoformat(go_cookie["Expires"])
expires = t.timestamp()
return cookiejar.Cookie(
version=0,
Expand Down Expand Up @@ -101,8 +99,9 @@ def py_to_go_cookie(py_cookie):
if py_cookie.path_specified:
go_cookie["Path"] = py_cookie.path
if py_cookie.expires is not None:
unix_time = datetime.datetime.fromtimestamp(py_cookie.expires)
# Note: fromtimestamp bizarrely produces a time without
# a time zone, so we need to use accept_naive.
go_cookie["Expires"] = pyrfc3339.generate(unix_time, accept_naive=True)
go_cookie["Expires"] = datetime.datetime.fromtimestamp(
py_cookie.expires
).isoformat()
return go_cookie
9 changes: 5 additions & 4 deletions juju/machine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.

import datetime
import asyncio
import ipaddress
import logging
import typing

import pyrfc3339

from juju.utils import block_until, juju_ssh_key_paths

from . import model, tag
Expand Down Expand Up @@ -239,7 +238,7 @@ def agent_status(self):
@property
def agent_status_since(self):
"""Get the time when the `agent_status` was last updated."""
return pyrfc3339.parse(self.safe_data["agent-status"]["since"])
return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"])

@property
def agent_version(self):
Expand All @@ -266,7 +265,9 @@ def status_message(self):
@property
def status_since(self):
"""Get the time when the `status` was last updated."""
return pyrfc3339.parse(self.safe_data["instance-status"]["since"])
return datetime.datetime.fromisoformat(
self.safe_data["instance-status"]["since"]
)

@property
def dns_name(self):
Expand Down
9 changes: 5 additions & 4 deletions juju/unit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.

import datetime
import logging

import pyrfc3339

from juju.errors import JujuAPIError, JujuError

from . import model, tag
Expand All @@ -25,7 +24,7 @@ def agent_status(self):
@property
def agent_status_since(self):
"""Get the time when the `agent_status` was last updated."""
return pyrfc3339.parse(self.safe_data["agent-status"]["since"])
return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"])

@property
def is_subordinate(self):
Expand All @@ -52,7 +51,9 @@ def workload_status(self):
@property
def workload_status_since(self):
"""Get the time when the `workload_status` was last updated."""
return pyrfc3339.parse(self.safe_data["workload-status"]["since"])
return datetime.datetime.fromisoformat(
self.safe_data["workload-status"]["since"]
)

@property
def workload_status_message(self):
Expand Down
5 changes: 2 additions & 3 deletions juju/user.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.

import datetime
import logging

import pyrfc3339

from . import errors, tag
from .client import client

Expand All @@ -31,7 +30,7 @@ def display_name(self):

@property
def last_connection(self):
return pyrfc3339.parse(self._user_info.last_connection)
return datetime.datetime.fromisoformat(self._user_info.last_connection)

@property
def access(self):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ classifiers = [
]
dependencies = [
"macaroonbakery>=1.1,<2.0",
"pyRFC3339>=1.0,<2.0",
"pyyaml>=5.1.2",
"websockets>=13.0.1",
"paramiko>=2.4.0",
Expand All @@ -35,6 +34,7 @@ dependencies = [
"packaging",
"typing-extensions>=4.5.0",
'backports.strenum>=1.3.1; python_version < "3.11"',
"backports-datetime-fromisoformat>=2.0.2",
]
[project.optional-dependencies]
dev = [
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package_data={"juju": ["py.typed"]},
install_requires=[
"macaroonbakery>=1.1,<2.0",
"pyRFC3339>=1.0,<2.0",
"pyyaml>=5.1.2",
"websockets>=13.0.1",
"paramiko>=2.4.0",
Expand All @@ -33,6 +32,7 @@
"packaging",
"typing-extensions>=4.5.0",
'backports.strenum>=1.3.1; python_version < "3.11"',
"backports-datetime-fromisoformat>=2.0.2",
],
extras_require={
"dev": [
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/test_gocookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# Licensed under the Apache V2, see LICENCE file for details.
"""Tests for the gocookies code."""

import datetime
import os
import shutil
import tempfile
import unittest
import urllib.request

import pyrfc3339

from juju.client.gocookies import GoCookieJar

# cookie_content holds the JSON contents of a Go-produced
# cookie file (reformatted so it's not all on one line but
# otherwise unchanged).

cookie_content = """
[
{
Expand Down Expand Up @@ -223,7 +223,9 @@ def test_expiry_time(self):
]"""
jar = self.load_jar(content)
got_expires = tuple(jar)[0].expires
want_expires = int(pyrfc3339.parse("2345-11-15T18:16:08Z").timestamp())
want_expires = int(
datetime.datetime.fromisoformat("2345-11-15T18:16:08Z").timestamp()
)
self.assertEqual(got_expires, want_expires)

def load_jar(self, content):
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ commands =

[testenv:unit]
envdir = {toxworkdir}/py3
deps =
backports-datetime-fromisoformat
allowlist_externals =
pytest
commands =
pytest {toxinidir}/tests/unit {posargs}

Expand Down