Skip to content

Commit d79029d

Browse files
committed
chore: fix model; restore jasyncio re-export
1 parent 05dfde5 commit d79029d

11 files changed

Lines changed: 150 additions & 115 deletions

File tree

juju/client/facade.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,12 @@ def serialize(self):
713713
def to_json(self):
714714
return json.dumps(self.serialize(), cls=TypeEncoder, sort_keys=True)
715715

716+
def __contains__(self, key):
717+
return key in self._toPy
718+
719+
def get(self, key, default=None):
720+
return self[key] if key in self else default
721+
716722
# treat subscript gets as JSON representation
717723
def __getitem__(self, key):
718724
attr = self._toPy[key]

juju/controller.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,10 @@ async def get_cloud(self):
565565
cloud_facade = client.CloudFacade.from_connection(self.connection())
566566

567567
result = await cloud_facade.Clouds()
568-
cloud = list(result.clouds.keys())[0] # only lives on one cloud
568+
cloud = next(iter(result.clouds))
569569
return tag.untag("cloud-", cloud)
570570

571-
async def get_models(self, all=False, username=None):
571+
async def get_models(self, all=False, username=None): # noqa: A002
572572
""".. deprecated:: 0.7.0
573573
Use :meth:`.list_models` instead.
574574
"""
@@ -630,10 +630,7 @@ async def get_model(self, model):
630630
:returns Model: Connected Model instance.
631631
"""
632632
uuids = await self.model_uuids()
633-
if model in uuids:
634-
uuid = uuids[model]
635-
else:
636-
uuid = model
633+
uuid = uuids.get(model) or model
637634

638635
from juju.model import Model
639636

juju/delta.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def get_id(self):
1717
return self.data["id"]
1818

1919
@classmethod
20-
def get_entity_class(self):
20+
def get_entity_class(cls):
2121
return None
2222

2323

2424
class ActionDelta(EntityDelta):
2525
@classmethod
26-
def get_entity_class(self):
26+
def get_entity_class(cls):
2727
from .action import Action
2828

2929
return Action
@@ -34,7 +34,7 @@ def get_id(self):
3434
return self.data["name"]
3535

3636
@classmethod
37-
def get_entity_class(self):
37+
def get_entity_class(cls):
3838
from .application import Application
3939

4040
return Application
@@ -45,7 +45,7 @@ def get_id(self):
4545
return self.data["tag"]
4646

4747
@classmethod
48-
def get_entity_class(self):
48+
def get_entity_class(cls):
4949
from .annotation import Annotation
5050

5151
return Annotation
@@ -56,15 +56,15 @@ def get_id(self):
5656
return self.data["model-uuid"]
5757

5858
@classmethod
59-
def get_entity_class(self):
59+
def get_entity_class(cls):
6060
from .model import ModelInfo
6161

6262
return ModelInfo
6363

6464

6565
class MachineDelta(EntityDelta):
6666
@classmethod
67-
def get_entity_class(self):
67+
def get_entity_class(cls):
6868
from .machine import Machine
6969

7070
return Machine
@@ -75,15 +75,15 @@ def get_id(self):
7575
return self.data["name"]
7676

7777
@classmethod
78-
def get_entity_class(self):
78+
def get_entity_class(cls):
7979
from .unit import Unit
8080

8181
return Unit
8282

8383

8484
class RelationDelta(EntityDelta):
8585
@classmethod
86-
def get_entity_class(self):
86+
def get_entity_class(cls):
8787
from .relation import Relation
8888

8989
return Relation
@@ -94,7 +94,7 @@ def get_id(self):
9494
return self.data["name"]
9595

9696
@classmethod
97-
def get_entity_class(self):
97+
def get_entity_class(cls):
9898
from .remoteapplication import RemoteApplication
9999

100100
return RemoteApplication
@@ -105,7 +105,7 @@ def get_id(self):
105105
return self.data["charm-url"]
106106

107107
@classmethod
108-
def get_entity_class(self):
108+
def get_entity_class(cls):
109109
from .charm import Charm
110110

111111
return Charm
@@ -116,7 +116,7 @@ def get_id(self):
116116
return self.data["application-name"]
117117

118118
@classmethod
119-
def get_entity_class(self):
119+
def get_entity_class(cls):
120120
from .remoteapplication import ApplicationOffer
121121

122122
return ApplicationOffer

juju/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, result):
2424
self.error_code = result.get("error-code")
2525
self.response = result["response"]
2626
self.request_id = result["request-id"]
27-
self.error_info = result["error-info"] if "error-info" in result else None
27+
self.error_info = result.get("error-info")
2828
super().__init__(self.message)
2929

3030

juju/jasyncio.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,38 @@
1313
import functools
1414
import logging
1515
import signal
16-
17-
import websockets
18-
19-
ROOT_LOGGER = logging.getLogger()
20-
2116
from asyncio import (
2217
CancelledError,
2318
create_task,
2419
wait,
20+
Event as Event, TimeoutError as TimeoutError, Queue as Queue, ensure_future as ensure_future,
21+
gather as gather,
22+
sleep as sleep,
23+
wait_for as wait_for,
24+
create_subprocess_exec as create_subprocess_exec,
25+
subprocess as subprocess,
26+
27+
FIRST_COMPLETED as FIRST_COMPLETED,
28+
Lock as Lock,
29+
as_completed as as_completed,
30+
new_event_loop as new_event_loop,
31+
32+
get_event_loop_policy as get_event_loop_policy,
33+
get_running_loop as
34+
get_running_loop,
35+
ALL_COMPLETED as
36+
ALL_COMPLETED,
37+
all_tasks as
38+
all_tasks,
39+
current_task as
40+
current_task,
41+
shield as shield,
2542
)
2643

44+
import websockets
45+
46+
ROOT_LOGGER = logging.getLogger()
47+
2748

2849
def create_task_with_handler(coro, task_name, logger=ROOT_LOGGER):
2950
"""Wrapper around "asyncio.create_task" to make sure the task
@@ -68,7 +89,7 @@ class SingletonEventLoop:
6889

6990
def __new__(cls):
7091
if not hasattr(cls, "instance"):
71-
cls.instance = super(SingletonEventLoop, cls).__new__(cls)
92+
cls.instance = super().__new__(cls)
7293
cls.instance.loop = asyncio.new_event_loop()
7394

7495
return cls.instance

juju/loop.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
import warnings
77

8-
warnings.warn("juju.loop module is being deprecated by 3.0, use juju.jasyncio instead")
8+
warnings.warn(
9+
"juju.loop module is being deprecated by 3.0, use juju.jasyncio instead",
10+
DeprecationWarning,
11+
stacklevel=1,
12+
)

juju/machine.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,14 @@ def dns_name(self):
274274
275275
May return None if no suitable address is found.
276276
"""
277-
ordered_addresses = []
278277
ordered_scopes = ["public", "local-cloud", "local-fan"]
279-
for scope in ordered_scopes:
280-
for address in self.addresses:
281-
if scope == address["scope"]:
282-
ordered_addresses.append(address)
278+
ordered_addresses = [
279+
address
280+
for scope in ordered_scopes
281+
for address in self.addresses
282+
if scope == address["scope"]
283+
]
284+
283285
for address in ordered_addresses:
284286
scope = address["scope"]
285287
for check_scope in ordered_scopes:

0 commit comments

Comments
 (0)