Skip to content

Commit

Permalink
refacor: change monkeypatch to use local imports
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmilsonRodrigues committed Jan 16, 2025
1 parent 633bbd4 commit 81e2eef
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 23 deletions.
4 changes: 0 additions & 4 deletions juju/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Copyright 2024 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.
"""Python Library for Juju."""

from backports.datetime_fromisoformat import MonkeyPatch

MonkeyPatch.patch_fromisoformat()
4 changes: 3 additions & 1 deletion juju/client/gocookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import json
import time

from backports.datetime_fromisoformat import datetime_fromisoformat


class GoCookieJar(cookiejar.FileCookieJar):
"""A CookieJar implementation that reads and writes cookies
Expand Down Expand Up @@ -50,7 +52,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 = datetime.datetime.fromisoformat(go_cookie["Expires"])
t = datetime_fromisoformat(go_cookie["Expires"])
expires = t.timestamp()
return cookiejar.Cookie(
version=0,
Expand Down
9 changes: 4 additions & 5 deletions juju/machine.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.

import datetime
import asyncio
import ipaddress
import logging
import typing

from backports.datetime_fromisoformat import datetime_fromisoformat

from juju.utils import block_until, juju_ssh_key_paths

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

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

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

import datetime
import logging

from backports.datetime_fromisoformat import datetime_fromisoformat

from juju.errors import JujuAPIError, JujuError

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

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

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

import datetime
import logging

from backports.datetime_fromisoformat import datetime_fromisoformat

from . import errors, tag
from .client import client

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

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

@property
def access(self):
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/test_gocookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# 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

from backports.datetime_fromisoformat import datetime_fromisoformat

from juju.client.gocookies import GoCookieJar

# cookie_content holds the JSON contents of a Go-produced
Expand Down Expand Up @@ -223,9 +224,7 @@ def test_expiry_time(self):
]"""
jar = self.load_jar(content)
got_expires = tuple(jar)[0].expires
want_expires = int(
datetime.datetime.fromisoformat("2345-11-15T18:16:08Z").timestamp()
)
want_expires = int(datetime_fromisoformat("2345-11-15T18:16:08Z").timestamp())
self.assertEqual(got_expires, want_expires)

def load_jar(self, content):
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ commands =
envdir = {toxworkdir}/py3
deps =
backports-datetime-fromisoformat
allowlist_externals =
pytest
commands =
pytest {toxinidir}/tests/unit {posargs}

Expand Down

0 comments on commit 81e2eef

Please sign in to comment.